[BioPython] SeqIO.write() Multiple Calls for fasta

Peter biopython at maubp.freeserve.co.uk
Sun Dec 14 13:05:09 UTC 2008


Matthew wrote:
> It is written that SeqIO.write() should "probably" perform fine with
> multiple calls, but with my experience it actually does overwrite
> the whole file, even when the file is opened and closed
> immediately before and after the write.

You seem to have misunderstood the documentation - are you already
familiar with working with file handles in python?  Perhaps this could
be clarified.

Using FASTA format, this is safe:

out_handle = open("example.fasta","w")
SeqIO.write(records, out_handle, "fasta")
SeqIO.write(more_records, out_handle, "fasta")
SeqIO.write(even_records, out_handle, "fasta")
out_handle.close()

You could also have written:

out_handle = open("example.fasta","w")
SeqIO.write(records+more_records+even_more_records, out_handle, "fasta")
out_handle.close()

I suspect what you are doing is instead is akin to this:

out_handle = open("example.fasta","w")
SeqIO.write(records, out_handle, "fasta")
out_handle.close()

out_handle = open("example.fasta","w")
SeqIO.write(more_records, out_handle, "fasta")
out_handle.close()

out_handle = open("example.fasta","w")
SeqIO.write(even_records, out_handle, "fasta")
out_handle.close()

This code will write the file once, then replace it, and again replace
it.  The final file contains only the third set of records.  This is
probably not what you intended.

Your example code seems to be trying to create one file per sequence.
Perhaps you have some duplicate filenames being generated as Michiel
suggested.

Peter



More information about the Biopython mailing list