Skip to content

Commit

Permalink
JQ-325 (cgates): Fix sporadically failing (py3) vcf test
Browse files Browse the repository at this point in the history
  • Loading branch information
cgates committed Jul 11, 2019
1 parent b66d216 commit 322633b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
17 changes: 12 additions & 5 deletions jacquard/utils/vcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,18 @@ def _sample_field(self, tag_names, sample):
sample_tag_values = self.sample_tag_values[sample]
missing_tag_names = set(sample_tag_values) - set(tag_names)
if missing_tag_names:
msg = ('sample format tags are not consistent: '
'{}:{}:{}:{}').format(self.chrom,
self.pos,
self.ref,
self.alt)
msg = ('{}:{}:{}:{}|sample format tags are not consistent: '
'requested tags [{}] but sample {} has has tags [{}] '
'leaving behind [{}]')\
.format(self.chrom,
self.pos,
self.ref,
self.alt,
', '.join(tag_names),
sample,
', '.join(['{}={}'.format(k,v) for k,v in sample_tag_values.items()]),
', '.join(missing_tag_names)
)
raise ValueError(msg)
tag_values = [sample_tag_values.get(t, '.') for t in tag_names]
if tag_values:
Expand Down
21 changes: 17 additions & 4 deletions test/utils/vcf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,13 +525,26 @@ def test_asTextWhenEmptyFormatField(self):
self.assertEquals(expected, record.text())

def test_asTextExpandsEmptyTrailingFormatField(self):
sampleA = OrderedDict({'a':'1', 'b':'2'})
sampleB = OrderedDict({'a':'10'})
sample_tag_values = OrderedDict({"SampleA":sampleA, "SampleB":sampleB})
sampleA = OrderedDict([('a','1'), ('b','2')])
sampleB = OrderedDict([('a','10')])
sample_tag_values = OrderedDict([("SampleA", sampleA), ("SampleB", sampleB)])
record = VcfRecord("CHROM", "POS", "REF", "ALT", "ID", "QUAL", "FILTER", "INFO", sample_tag_values)
expected = self.entab("CHROM|POS|ID|REF|ALT|QUAL|FILTER|INFO|a:b|1:2|10:.\n")
self.assertEquals(expected, record.text())

def test_sample_field_whenInconsistentTags(self):
# FYI this should never happen in the wild, but I wanted to test the exception formatting.
sampleA = OrderedDict([('a','1'), ('b','2')])
sampleB = OrderedDict([('a','10')])
sample_tag_values = OrderedDict([("SampleA", sampleA), ("SampleB", sampleB)])
record = VcfRecord("CHROM", "POS", "REF", "ALT", "ID", "QUAL", "FILTER", "INFO", sample_tag_values)

self.assertRaisesRegexp(ValueError,
r'CHROM:POS:REF:ALT|sample format tags are not consistent: requested tags \[a\] but sample has has tags \[a=1, b=2\] leaving behind \[b\]',
record._sample_field,
['a'],
'SampleA')


def test_equals(self):
sample_names = ["sampleA"]
Expand All @@ -550,7 +563,7 @@ def test_equals(self):
def testHash(self):
sample_names = ["sampleA"]
base = VcfRecord.parse_record(self.entab("A|B|ID|C|D|QUAL|FILTER|INFO|F|S\n"), sample_names)
base_equivalent = VcfRecord.parse_record(self.entab("A|B|ID|C|D|QUAL|FILTER||F|S\n"), sample_names)
base_equivalent = VcfRecord.parse_record(self.entab("A|B|ID|C|D|QUAL|FILTER||foo|S\n"), sample_names)
self.assertEquals(base.__hash__(), base_equivalent.__hash__())
record_set = set()
record_set.add(base)
Expand Down

0 comments on commit 322633b

Please sign in to comment.