Permalink
Browse files

introduce a nocmp list class

  • Loading branch information...
1 parent 115ffed commit 1753479eaed21f5bce62fad4c171d7d4a18acc77 James Casbon committed Feb 17, 2012
Showing with 23 additions and 6 deletions.
  1. +10 −2 test/test_vcf.py
  2. +13 −4 vcf/parser.py
View
12 test/test_vcf.py
@@ -5,6 +5,7 @@
from StringIO import StringIO
import vcf
+from vcf.parser import NoCmpList
from vcf import utils
suite = doctest.DocTestSuite(vcf.parser)
@@ -29,7 +30,7 @@ def test_vcf_4_0(self):
assert not r.is_monomorphic
if 'AF' in r.INFO:
- self.assertEqual(type(r.INFO['AF']), type([]))
+ self.assertEqual(isinstance(r.INFO['AF'], []))
for c in r:
assert c
@@ -38,7 +39,7 @@ def test_vcf_4_0(self):
if c.called:
self.assertEqual(type(c.data['GQ']), type(1))
if 'HQ' in c.data and c.data['HQ'] is not None:
- self.assertEqual(type(c.data['HQ']), type([]))
+ self.assertEqual(type(c.data['HQ']), type(NoCmpList()))
@@ -426,7 +427,14 @@ def test_walk(self):
assert recs[0] is not None
assert recs[1] is not None
+ def test_nocmp_list(self):
+ nc = NoCmpList([1,2,3])
+
+ assert nc > [0,1]
+
+ with self.assertRaises(Exception):
+ assert nc > 1
View
17 vcf/parser.py
@@ -108,6 +108,14 @@ def read_meta(self, meta_string):
return match.group('key'), match.group('val')
+class NoCmpList(list):
+
+ def __cmp__(self, other):
+ if type(other) != type(self):
+ raise Exception('Cannot compare vcf attribute lists to type %s' % type(other))
+ return list.__cmp__(self, other)
+
+
class _Call(object):
""" A genotype call, a cell entry in a VCF file"""
@@ -442,8 +450,8 @@ def _parse_metainfo(self):
def _map(self, func, iterable, bad='.'):
'''``map``, but make bad values None.'''
- return [func(x) if x != bad else None
- for x in iterable]
+ return NoCmpList([func(x) if x != bad else None
+ for x in iterable])
def _parse_info(self, info_str):
'''Parse the INFO field of a VCF entry into a dictionary of Python
@@ -537,7 +545,8 @@ def _parse_sample(self, sample, samp_fmt, samp_fmt_types, samp_fmt_nums):
sampdict[fmt] = vals
if entry_num != 1:
- sampdict[fmt] = (sampdict[fmt])
+ sampdict[fmt] = NoCmpList([sampdict[fmt]])
+
continue
@@ -670,7 +679,7 @@ def _format_sample(self, fmt, sample):
return ':'.join(self._stringify(sample.data[f]) for f in fmt.split(':'))
def _stringify(self, x, none='.'):
- if type(x) == type([]):
+ if isinstance(x, list):
return ','.join(self._map(str, x, none))
return str(x) if x is not None else none

0 comments on commit 1753479

Please sign in to comment.