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

Modifications to have Writer output match Reader input #55

Merged
merged 1 commit into from Jun 26, 2012
Merged
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
21 changes: 14 additions & 7 deletions vcf/parser.py
@@ -1,4 +1,3 @@

import collections
import re
import csv
Expand Down Expand Up @@ -1012,8 +1011,8 @@ class Writer(object):
# Reverse keys and values in header field count dictionary
counts = dict((v,k) for k,v in field_counts.iteritems())

def __init__(self, stream, template):
self.writer = csv.writer(stream, delimiter="\t")
def __init__(self, stream, template, lineterminator="\r\n"):
self.writer = csv.writer(stream, delimiter="\t", lineterminator=lineterminator)
self.template = template

two = '##{key}=<ID={0},Description="{1}">\n'
Expand Down Expand Up @@ -1042,7 +1041,7 @@ def _write_header(self):
def write_record(self, record):
""" write a record to the file """
ffs = self._map(str, [record.CHROM, record.POS, record.ID, record.REF]) \
+ [self._format_alt(record.ALT), record.QUAL or '.', record.FILTER or '.',
+ [self._format_alt(record.ALT), record.QUAL or '.', self._format_filter(record.FILTER),
self._format_info(record.INFO), record.FORMAT]

samples = [self._format_sample(record.FORMAT, sample)
Expand All @@ -1058,22 +1057,30 @@ def _fix_field_count(self, num_str):

def _format_alt(self, alt):
return ','.join(self._map(str, alt))

def _format_filter(self, flt):
return self._stringify(flt, none='PASS', delim=';')

def _format_info(self, info):
if not info:
return '.'
return ';'.join(["%s=%s" % (x, self._stringify(y)) for x, y in info.iteritems()])
return ';'.join([self._stringify_pair(x,y)) for x, y in info.iteritems()])

def _format_sample(self, fmt, sample):
if sample.data["GT"] is None:
return "./."
return ':'.join(self._stringify(sample.data[f]) for f in fmt.split(':'))

def _stringify(self, x, none='.'):
def _stringify(self, x, none='.', delim=','):
if type(x) == type([]):
return ','.join(self._map(str, x, none))
return delim.join(self._map(str, x, none))
return str(x) if x is not None else none

def _stringify_pair(self, x, y, none='.', delim=','):
if isinstance(y, bool):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A manually constructed object with, for example, DB=False, would have unexpected output. Adding "if y" would avoid that.

return str(x)
return "%s=%s" % (str(x), self._stringify(y, none=none, delim=delim))

def _map(self, func, iterable, none='.'):
'''``map``, but make None values none.'''
return [func(x) if x is not None else none
Expand Down