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

Reader compatible with files opened in bytes mode #305

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion vcf/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ def __init__(self, fsock=None, filename=None, compressed=None, prepend_chr=False
self._column_headers = []
self._tabix = None
self._prepend_chr = prepend_chr
self.encoding = encoding
self._parse_metainfo()
self._format_cache = {}
self.encoding = encoding

def __iter__(self):
return self
Expand All @@ -315,6 +315,12 @@ def _parse_metainfo(self):
parser = _vcf_metadata_parser()

line = next(self.reader)

try:
line = line.decode(self.encoding)
except AttributeError:
pass

while line.startswith('##'):
self._header_lines.append(line)

Expand Down Expand Up @@ -348,6 +354,10 @@ def _parse_metainfo(self):
self.metadata[key].append(val)

line = next(self.reader)
try:
line = line.decode(self.encoding)
except AttributeError:
pass

fields = self._row_pattern.split(line[1:])
self._column_headers = fields[:9]
Expand Down Expand Up @@ -551,6 +561,12 @@ def _parse_alt(self, str):
def next(self):
'''Return the next record in the file.'''
line = next(self.reader)

try:
line = line.decode(self.encoding)
except AttributeError:
pass

row = self._row_pattern.split(line.rstrip())
chrom = row[0]
if self._prepend_chr:
Expand Down