[Biopython] Generator expression for SeqIO

Peter Cock p.j.a.cock at googlemail.com
Wed Dec 7 08:26:08 UTC 2011


On Wed, Dec 7, 2011 at 4:41 AM, Mic <mictadlo at gmail.com> wrote:
> No worries is was perfect.
>
> I have the following code and I do not know how to combine the *header* and
> *seq* variables from the '*with*' statement with generator expression?
>
> from Bio import SeqIO
> from Bio.SeqRecord import SeqRecord
> from Bio.Seq import Seq
> from pprint import pprint
>
> if __name__ == '__main__':
>
>    *with* open('input.txt') as f:
>        for line in f:
>            try:
>                splited_line = line.split('\t')
>
>                *header* = splited_line[0] +'_'+ splited_line[2]
>                *seq* = splited_line[3]
>            except IndexError:
>                continue
>
>    fasta_file = open('output.fasta', 'w')
>    records = (SeqRecord(???), id=????, description="") for i in ???)
>
>    SeqIO.write(records, fasta_file, "fasta")
>
> Thank you in advance.

Are you trying to parse a tabular file, with three columns
(ID, sequence, description)?

I suggest you learn about generator functions in Python.

Peter




More information about the Biopython mailing list