Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Minor patch to Flag output; add ability to explicitly flush/close Writer stream #61

Merged
merged 2 commits into from

2 participants

@cmclean

Modified the Flag output for False entries to print an empty string, rather than "FLAG=False".

Added the stream object as an attribute of Writer to enable the ability to explicitly flush and close the stream. For programs that write a VCF, then perform manipulations upon it (e.g. bgzip + tabix), it is imperative that the buffer is flushed prior to beginning any further manipulations on the written VCF. Currently, to achieve this behavior you have to either keep a reference to the file object itself or delete the Writer object and hope that the Python implementation used closes files upon deletion.

http://stackoverflow.com/questions/3976711/csvwriter-not-saving-data-to-file-why
http://python.6.n6.nabble.com/CSV-writer-question-td1421639.html

cmclean added some commits
@cmclean cmclean Modified logic on Flag fields to print only if True,
but print empty line if False.

Added stream object to Writer so flush and close are
available without needing to keep a reference to the
file elsewhere.
0b64db9
@cmclean cmclean Error checking added to flush and close fe9a138
@jamescasbon jamescasbon merged commit 939e5ce into jamescasbon:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 27, 2012
  1. @cmclean

    Modified logic on Flag fields to print only if True,

    cmclean authored
    but print empty line if False.
    
    Added stream object to Writer so flush and close are
    available without needing to keep a reference to the
    file elsewhere.
  2. @cmclean
This page is out of date. Refresh to see the latest.
Showing with 17 additions and 2 deletions.
  1. +17 −2 vcf/parser.py
View
19 vcf/parser.py
@@ -1030,6 +1030,7 @@ class Writer(object):
def __init__(self, stream, template, lineterminator="\r\n"):
self.writer = csv.writer(stream, delimiter="\t", lineterminator=lineterminator)
self.template = template
+ self.stream = stream
two = '##{key}=<ID={0},Description="{1}">\n'
four = '##{key}=<ID={0},Number={num},Type={2},Description="{3}">\n'
@@ -1064,6 +1065,20 @@ def write_record(self, record):
for sample in record.samples]
self.writer.writerow(ffs + samples)
+ def flush(self):
+ """Flush the writer"""
+ try:
+ self.stream.flush()
+ except AttributeError:
+ pass
+
+ def close(self):
+ """Close the writer"""
+ try:
+ self.stream.close()
+ except AttributeError:
+ pass
+
def _fix_field_count(self, num_str):
"""Restore header number to original state"""
if num_str not in self.counts:
@@ -1093,8 +1108,8 @@ def _stringify(self, x, none='.', delim=','):
return str(x) if x is not None else none
def _stringify_pair(self, x, y, none='.', delim=','):
- if y and isinstance(y, bool):
- return str(x)
+ if isinstance(y, bool):
+ return str(x) if y else ""
return "%s=%s" % (str(x), self._stringify(y, none=none, delim=delim))
def _map(self, func, iterable, none='.'):
Something went wrong with that request. Please try again.