[Biopython] Writting fasta file
Peter
biopython at maubp.freeserve.co.uk
Thu Aug 26 07:59:56 EDT 2010
On Thu, Aug 26, 2010 at 12:27 PM, xyz <mitlox at op.pl> wrote:
> On 25/08/10 23:08, Peter wrote:
>>
>> 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 ;)
>
> Thank you for your code it looks much better than mine.
>
> In order to save memory is it possible to write each record ie. one by one
> with SeqIO.write instead of all records at once?
The code is already doing it one by one.
It uses a generator expression (rather than a list or list comprehension
which would put all the records into memory at once) to make the
reverse complemented records:
For writing FASTA files (and most other formats), SeqIO.write() only
needs to look at one record at a time, and won't load them all into
memory.
Peter
More information about the Biopython
mailing list