Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly write meta lines with dictionary value #84

Merged
merged 2 commits into from Jan 14, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions vcf/parser.py
Expand Up @@ -158,7 +158,7 @@ def read_meta_hash(self, meta_string):
# Removing initial hash marks and final equal sign
key = items[0][2:-1]
hashItems = items[1].split(',')
val = dict(item.split("=") for item in hashItems)
val = OrderedDict(item.split("=") for item in hashItems)
return key, val

def read_meta(self, meta_string):
Expand Down Expand Up @@ -549,14 +549,15 @@ def __init__(self, stream, template, lineterminator="\r\n"):

two = '##{key}=<ID={0},Description="{1}">\n'
four = '##{key}=<ID={0},Number={num},Type={2},Description="{3}">\n'
contig_format = '##contig=<ID={ID},length={length},assembly={assembly}>\n'
_num = self._fix_field_count
for (key, vals) in template.metadata.iteritems():
if key in SINGULAR_METADATA:
vals = [vals]
for val in vals:
if key == "contig":
stream.write(contig_format.format(**val))
if isinstance(val, dict):
values = ','.join('{0}={1}'.format(key, value)
for key, value in val.items())
stream.write('##{0}=<{1}>\n'.format(key, values))
else:
stream.write('##{0}={1}\n'.format(key, val))
for line in template.infos.itervalues():
Expand Down
24 changes: 23 additions & 1 deletion vcf/test/test_vcf.py
Expand Up @@ -221,7 +221,7 @@ def testWrite(self):
out_str = out.getvalue()
for line in out_str.split("\n"):
if line.startswith("##contig"):
assert "<ID=" in line, "Found dictionary in contig line: {0}".format(line)
assert line.startswith('##contig=<'), "Found dictionary in contig line: {0}".format(line)
print (out_str)
reader2 = vcf.Reader(out)

Expand Down Expand Up @@ -257,6 +257,27 @@ def testWrite(self):
self.assertEquals(l.samples, r.samples)


class TestWriterDictionaryMeta(unittest.TestCase):

def testWrite(self):

reader = vcf.Reader(fh('example-4.1-bnd.vcf'))
out = StringIO()
writer = vcf.Writer(out, reader)

records = list(reader)

for record in records:
writer.write_record(record)
out.seek(0)
out_str = out.getvalue()
for line in out_str.split("\n"):
if line.startswith("##PEDIGREE"):
self.assertEquals(line, '##PEDIGREE=<Derived="Tumor",Original="Germline">')
if line.startswith("##SAMPLE"):
assert line.startswith('##SAMPLE=<'), "Found dictionary in meta line: {0}".format(line)


class TestRecord(unittest.TestCase):

def test_num_calls(self):
Expand Down Expand Up @@ -789,6 +810,7 @@ def test_trim(self):
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestBcfToolsOutput))
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestGatkOutputWriter))
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestBcfToolsOutputWriter))
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestWriterDictionaryMeta))
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestTabix))
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestOpenMethods))
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestFilter))
Expand Down