Skip to content

Commit

Permalink
Merge pull request #31 from mdshw5/FastaRecord.__iter__
Browse files Browse the repository at this point in the history
Closes #31. Closes #32.
  • Loading branch information
mdshw5 committed Nov 20, 2014
2 parents 322a22f + 428a4c1 commit f4b73ed
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ language: python
python:
- '3.4'
- '3.3'
- '3.2'
- '2.7'
- '2.6'
- pypy
- pypy3
install:
- pip install biopython
- pip install biopython || true
- pip install -e .
script: nosetests
deploy:
Expand Down
16 changes: 14 additions & 2 deletions pyfaidx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ class FastaRecord(object):
def __init__(self, name, fa):
self.name = name
self._fa = fa
self._len = self._fa.faidx.index[self.name].rlen

def __getitem__(self, n):
"""Return sequence from region [start, end)
Expand All @@ -416,12 +417,23 @@ def __getitem__(self, n):
except FetchError:
raise

def __iter__(self):
line_len = self._fa.faidx.index[self.name].lenc
start = 0
while True:
end = start + line_len
if end < len(self):
yield self[start:end]
else:
yield self[start:]
raise StopIteration
start += line_len

def __repr__(self):
return 'FastaRecord("%s")' % (self.name)

def __len__(self):
""" Return length of chromosome """
return self._fa.faidx.index[self.name].rlen
return self._len

def __str__(self):
return str(self[:])
Expand Down
19 changes: 19 additions & 0 deletions tests/test_FastaRecord_iter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os
from pyfaidx import Fasta

path = os.path.dirname(__file__)
os.chdir(path)

class TestFastaRecordIter:
def __init__(self):
self.genes = os.path.join(path, 'data/genes.fasta')
self.fasta = Fasta(self.genes)

def test_fetch_whole_fasta(self):
expect = open(self.genes).read()
result = ''.join(['>' + record.name + '\n' + ''.join([line.seq + '\n' for line in record]) for record in self.fasta]).rstrip()
assert expect == result

def test_line_len(self):
for record in self.fasta:
assert len(next(iter(record))) == self.fasta.faidx.index[record.name].lenc
29 changes: 22 additions & 7 deletions tests/test_bio_seqio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import os
from pyfaidx import Fasta, FetchError
from Bio import SeqIO
from nose.plugins.skip import Skip, SkipTest
try:
from Bio import SeqIO
test_bio = True
except ImportError:
test_bio = False

path = os.path.dirname(__file__)
os.chdir(path)
Expand All @@ -9,15 +14,25 @@ class TestBioSeqIO:
def __init__(self):
self.genes = os.path.join(path, 'data/genes.fasta')
self.fasta = Fasta(self.genes)
with open(self.genes, "rU") as fh:
self.seqio = SeqIO.to_dict(SeqIO.parse(fh, "fasta"))
if test_bio:
with open(self.genes, "rU") as fh:
self.seqio = SeqIO.to_dict(SeqIO.parse(fh, "fasta"))

def test_fetch_whole_entry(self):
assert str(self.fasta['KF435150.1']) == str(self.seqio['KF435150.1'].seq)
assert self.fasta['KF435150.1'].name == str(self.seqio['KF435150.1'].name)
if test_bio:
assert str(self.fasta['KF435150.1']) == str(self.seqio['KF435150.1'].seq)
assert self.fasta['KF435150.1'].name == str(self.seqio['KF435150.1'].name)
else:
raise SkipTest

def test_slice_whole_entry(self):
assert str(self.fasta['KF435150.1'][::3]) == str(self.seqio['KF435150.1'].seq[::3])
if test_bio:
assert str(self.fasta['KF435150.1'][::3]) == str(self.seqio['KF435150.1'].seq[::3])
else:
raise SkipTest

def test_revcomp_whole_entry(self):
assert str(self.fasta['KF435150.1'][:].reverse.complement) == str(self.seqio['KF435150.1'].reverse_complement().seq)
if test_bio:
assert str(self.fasta['KF435150.1'][:].reverse.complement) == str(self.seqio['KF435150.1'].reverse_complement().seq)
else:
raise SkipTest

0 comments on commit f4b73ed

Please sign in to comment.