[Biopython] Writting fasta file
Peter
biopython at maubp.freeserve.co.uk
Wed Aug 25 13:08:41 UTC 2010
On Wed, Aug 25, 2010 at 12:22 PM, xyz <mitlox at op.pl> wrote:
> Hello,
> the following code saves a multiple fasta file in a list and after reverse
> complement it writes the content back in a fasta file.
>
> in_handle = open(opts.inputFasta, "r")
> records = list(SeqIO.parse(in_handle, "fasta"))
>
> for record in records:
> record.seq = record.seq.reverse_complement()
>
> out_handle = open(opts.outputFasta, "w")
> SeqIO.write(records, out_handle, "fasta")
>
> How is it possible to do it without a list?
>
> Thank you in advance.
Hi "xyz",
Did you try looking in the tutorial? i.e.
http://biopython.org/DIST/docs/tutorial/Tutorial.html#sec:SeqIO-reverse-complement
You could modify your code to use a generator expression like this:
def make_rc(record):
"""Modifies a SeqRecord in place, and returns it."""
record.seq = record.seq.reverse_complement()
return record
records = (make_rc(r) for r in SeqIO.parse(in_opts.inputFasta, "fasta"))
SeqIO.write(records, out_opts.outputFasta, "fasta")
I also changed it to pass filenames directly to SeqIO - its shorter ;)
See also this thread about adding a reverse complement
method to the SeqRecord which would make this easier:
http://lists.open-bio.org/pipermail/biopython-dev/2010-June/007850.html
Peter
More information about the Biopython
mailing list