biocore/scikit-bio — pull request #266 by wasade (2014-04-12 22:48:45+00:00)
Title: Sequence iterators and a factory method

This replaces #106
This depends on #265 and #248
This resolves some of the concerns raised in #259

The sequence iterators let you walk cleanly over multiple different sequence file formats, including compressed formats, without having to care about those details. Below is the example of the factory from the doc module:

```python
>>> import os
>>> import gzip
>>> out = open('test_seqs.fna', 'w')
>>> out.write(">s1\nATGC\n>s2\nATGGC\n")
>>> out.close()
>>> outgz = gzip.open('test_seqs.fq.gz', 'w')
>>> _ = outgz.write("@s3\nAATTGG\n+\nghghgh\n@s4\nAAA\n+\nfgh\n")
>>> outgz.close()

Now, lets see what the factory can do:

>>> from skbio.factory.sequence import factory
>>> it = factory(['test_seqs.fna', 'test_seqs.fq.gz'])
>>> for rec in it:
... print rec['SequenceID']
... print rec['Sequence']
... print rec['Qual']
s1
ATGC
None
s2
ATGGC
None
s3
AATTGG
[39 40 39 40 39 40]
s4
AAA
[38 39 40]
```

The iterators additionally support transform methods, so all sequences can be reverse complemented or what have you very easily. (the factory will pass ``kwargs`` to the actual iterators). Below is an example of applying a transform:

```python
>>> seqs1 = StringIO("@seq1\n"
... "ATGC\n"
... "+\n"
... "hhhh\n")
>>> seqs2 = StringIO("@seq2\n"
... "AATTGGCC\n"
... ">seq2\n"
... "abcdefgh\n")
>>> def rev_f(st):
... st['Sequence'] = st['Sequence'][::-1]
... st['Qual'] = st['Qual'][::-1] if st['Qual'] is not None else None
>>> it = FastqIterator(seq=[seqs1, seqs2], transform=rev_f)
>>> for record in it:
... print record['Sequence']
... print record['Qual']
CGTA
[40 40 40 40]
CCGGTTAA
[40 39 38 37 36 35 34 33]
```