Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

2to3 plus some manual fixes

  • Loading branch information...
commit 1074de743d8da5dd4197b0b26124a0730d54ecb6 1 parent 115ffed
@casbon casbon authored
Showing with 35 additions and 33 deletions.
  1. +16 −14 test/test_vcf.py
  2. +4 −4 vcf/__init__.py
  3. +13 −13 vcf/parser.py
  4. +2 −2 vcf/utils.py
View
30 test/test_vcf.py
@@ -1,8 +1,10 @@
+from __future__ import print_function
+
import unittest
import doctest
import os
-import commands
-from StringIO import StringIO
+import subprocess
+from io import StringIO
import vcf
from vcf import utils
@@ -11,7 +13,7 @@
def fh(fname):
- return file(os.path.join(os.path.dirname(__file__), fname))
+ return open(os.path.join(os.path.dirname(__file__), fname))
class TestVcfSpecs(unittest.TestCase):
@@ -65,9 +67,9 @@ def test_vcf_4_1_sv(self):
# test we can walk the file at least
for r in reader:
- print r
+ print(r)
for c in r:
- print c
+ print(c)
assert c
# asserting False while I work out what to check
@@ -136,7 +138,7 @@ def testParse(self):
- print reader.samples
+ print(reader.samples)
self.assertEqual(len(reader.samples), 7)
n = 0
for r in reader:
@@ -174,7 +176,7 @@ def testWrite(self):
records = list(reader)
- map(writer.write_record, records)
+ list(map(writer.write_record, records))
out.seek(0)
reader2 = vcf.Reader(out)
@@ -275,7 +277,7 @@ def test_gt_types(self):
reader = vcf.Reader(fh('example-4.0.vcf'))
for var in reader:
for s in var:
- print s.data
+ print(s.data)
gt_types = [s.gt_type for s in var.samples]
if var.POS == 14370:
self.assertEqual([0,1,2], gt_types)
@@ -343,21 +345,21 @@ class TestFilter(unittest.TestCase):
def testApplyFilter(self):
- s, out = commands.getstatusoutput('python scripts/vcf_filter.py --site-quality 30 test/example-4.0.vcf sq')
+ s, out = subprocess.getstatusoutput('python scripts/vcf_filter.py --site-quality 30 test/example-4.0.vcf sq')
#print out
assert s == 0
buf = StringIO()
buf.write(out)
buf.seek(0)
- print buf.getvalue()
+ print(buf.getvalue())
reader = vcf.Reader(buf)
# check filter got into output file
assert 'sq30' in reader.filters
- print reader.filters
+ print(reader.filters)
# check sites were filtered
n = 0
@@ -371,7 +373,7 @@ def testApplyFilter(self):
def testApplyMultipleFilters(self):
- s, out = commands.getstatusoutput('python scripts/vcf_filter.py --site-quality 30 '
+ s, out = subprocess.getstatusoutput('python scripts/vcf_filter.py --site-quality 30 '
'--genotype-quality 50 test/example-4.0.vcf sq mgq')
assert s == 0
#print out
@@ -380,7 +382,7 @@ def testApplyMultipleFilters(self):
buf.seek(0)
reader = vcf.Reader(buf)
- print reader.filters
+ print(reader.filters)
assert 'mgq50' in reader.filters
assert 'sq30' in reader.filters
@@ -389,7 +391,7 @@ class TestRegression(unittest.TestCase):
def test_issue_16(self):
reader = vcf.Reader(fh('issue-16.vcf'))
- assert reader.next().QUAL == None
+ assert next(reader).QUAL == None
View
8 vcf/__init__.py
@@ -152,9 +152,9 @@
see :doc:`FILTERS` for full description.
'''
-from parser import Reader, Writer
-from parser import VCFReader, VCFWriter
-from filters import Base as Filter
-from parser import RESERVED_INFO, RESERVED_FORMAT
+from .parser import Reader, Writer
+from .parser import VCFReader, VCFWriter
+from .filters import Base as Filter
+from .parser import RESERVED_INFO, RESERVED_FORMAT
VERSION = '0.4.0pre'
View
26 vcf/parser.py
@@ -373,7 +373,7 @@ def __init__(self, fsock=None, filename=None, compressed=False, prepend_chr=Fals
if filename:
self.filename = filename
if fsock is None:
- self.reader = file(filename)
+ self.reader = open(filename)
if fsock:
self.reader = fsock
@@ -413,7 +413,7 @@ def _parse_metainfo(self):
parser = _vcf_metadata_parser()
- line = self.reader.next()
+ line = next(self.reader)
while line.startswith('##'):
self._header_lines.append(line)
line = line.strip()
@@ -434,7 +434,7 @@ def _parse_metainfo(self):
key, val = parser.read_meta(line.strip())
self.metadata[key] = val
- line = self.reader.next()
+ line = next(self.reader)
fields = line.split()
self.samples = fields[9:]
@@ -509,7 +509,7 @@ def _parse_samples(self, samples, samp_fmt, site):
samp_fmt_types.append(entry_type)
samp_fmt_nums.append(entry_num)
- for name, sample in itertools.izip(self.samples, samples):
+ for name, sample in zip(self.samples, samples):
sampdict = self._parse_sample(sample, samp_fmt, samp_fmt_types, samp_fmt_nums)
samp_data.append(_Call(site, name, sampdict))
@@ -518,7 +518,7 @@ def _parse_samples(self, samples, samp_fmt, site):
def _parse_sample(self, sample, samp_fmt, samp_fmt_types, samp_fmt_nums):
sampdict = dict([(x, None) for x in samp_fmt])
- for fmt, entry_type, entry_num, vals in itertools.izip(
+ for fmt, entry_type, entry_num, vals in zip(
samp_fmt, samp_fmt_types, samp_fmt_nums, sample.split(':')):
# short circuit the most common
@@ -555,9 +555,9 @@ def _parse_sample(self, sample, samp_fmt, samp_fmt_types, samp_fmt_nums):
return sampdict
- def next(self):
+ def __next__(self):
'''Return the next record in the file.'''
- row = self.reader.next().split()
+ row = next(self.reader).split()
chrom = row[0]
if self._prepend_chr:
chrom = 'chr' + chrom
@@ -616,7 +616,7 @@ def fetch(self, chrom, start, end=None):
if end is None:
self.reader = self._tabix.fetch(chrom, start, start+1)
try:
- return self.next()
+ return next(self)
except StopIteration:
return None
@@ -633,13 +633,13 @@ def __init__(self, stream, template):
self.writer = csv.writer(stream, delimiter="\t")
self.template = template
- for line in template.metadata.items():
+ for line in list(template.metadata.items()):
stream.write('##%s=%s\n' % line)
- for line in template.infos.values():
+ for line in list(template.infos.values()):
stream.write('##INFO=<ID=%s,Number=%s,Type=%s,Description="%s">\n' % tuple(self._map(str, line)))
- for line in template.formats.values():
+ for line in list(template.formats.values()):
stream.write('##FORMAT=<ID=%s,Number=%s,Type=%s,Description="%s">\n' % tuple(self._map(str, line)))
- for line in template.filters.values():
+ for line in list(template.filters.values()):
stream.write('##FILTER=<ID=%s,Description="%s">\n' % tuple(self._map(str, line)))
self._write_header()
@@ -662,7 +662,7 @@ def _format_alt(self, alt):
return ','.join([x or '.' for x in alt])
def _format_info(self, info):
- return ';'.join(["%s=%s" % (x, self._stringify(y)) for x, y in info.items()])
+ return ';'.join(["%s=%s" % (x, self._stringify(y)) for x, y in list(info.items())])
def _format_sample(self, fmt, sample):
if sample.data["GT"] is None:
View
4 vcf/utils.py
@@ -6,7 +6,7 @@ def walk_together(*readers):
inputs are sorted in the same way and use the same reference
otherwise behaviour is undefined.
"""
- nexts = [reader.next() for reader in readers]
+ nexts = [next(reader) for reader in readers]
while True:
min_next = min([x for x in nexts if x is not None])
@@ -21,7 +21,7 @@ def walk_together(*readers):
if n is not None and n == min_next:
try:
- nexts[i] = readers[i].next()
+ nexts[i] = next(readers[i])
except StopIteration:
nexts[i] = None
Please sign in to comment.
Something went wrong with that request. Please try again.