Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use ordered dict for samples, fixes #2

  • Loading branch information...
commit bc8c85ed68a4f89feda77365c1f12f66959eb73c 1 parent dcbb72c
James Casbon authored
Showing with 23 additions and 11 deletions.
  1. +10 −1 setup.py
  2. +3 −0  test/test_vcf.py
  3. +10 −10 vcf.py
View
11 setup.py
@@ -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',
@@ -7,5 +15,6 @@
author='James Casbon',
author_email='casbon@gmail.com',
description='Variant Call Format (VCF) parser for python',
- test_suite='test'
+ test_suite='test',
+ requires=requires
)
View
3  test/test_vcf.py
@@ -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)
View
20 vcf.py
@@ -93,6 +93,12 @@
import re
import csv
+try:
+ from ordereddict import OrderedDict
+except ImportError:
+ from collections import OrderedDict
+
+
# Metadata parsers/constants
RESERVED_INFO = {
@@ -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(',')
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.