Skip to content

Commit

Permalink
use ordered dict for samples, fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
James Casbon committed Jan 16, 2012
1 parent dcbb72c commit bc8c85e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
11 changes: 10 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
from setuptools import setup
import sys

requires = []

# install ordereddict for python < 2.7
py_version = sys.version_info
if py_version.major == 2 and py_version.minor < 7:
requires.append('ordereddict')

setup(
name='PyVCF',
Expand All @@ -7,5 +15,6 @@
author='James Casbon',
author_email='[email protected]',
description='Variant Call Format (VCF) parser for python',
test_suite='test'
test_suite='test',
requires=requires
)
3 changes: 3 additions & 0 deletions test/test_vcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def testCalls(self):
n += 1
self.assertEqual(len(site.samples), len(self.samples))

# check sample ordering is preserved:
self.assertEqual([x['name'] for x in site.samples.values()], self.samples)

self.assertEqual(n, self.n_calls)


Expand Down
20 changes: 10 additions & 10 deletions vcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@
import re
import csv

try:
from ordereddict import OrderedDict
except ImportError:
from collections import OrderedDict



# Metadata parsers/constants
RESERVED_INFO = {
Expand Down Expand Up @@ -387,9 +393,9 @@ def _parse_info(self, info_str):
def _parse_samples(self, samples, samp_fmt):
'''Parse a sample entry according to the format specified in the FORMAT
column.'''
samp_data = []
samp_data = OrderedDict()
samp_fmt = samp_fmt.split(':')
for sample in samples:
for name, sample in zip(self.samples, samples):
sampdict = dict(zip(samp_fmt, sample.split(':')))
for fmt in sampdict:
vals = sampdict[fmt].split(',')
Expand All @@ -408,14 +414,8 @@ def _parse_samples(self, samples, samp_fmt):
elif sampdict[fmt] == './.' and self.aggro:
sampdict[fmt] = None

samp_data.append(sampdict)

for name, data in zip(self.samples, samp_data):
data['name'] = name
try:
data['_GT'] = int(data['GT'][0]), int(data['GT'][-1])
except ValueError:
pass
sampdict['name'] = name
samp_data[name] = sampdict

return samp_data

Expand Down

0 comments on commit bc8c85e

Please sign in to comment.