[Biopython] sort fasta file

xyz mitlox at op.pl
Wed Mar 17 12:01:35 UTC 2010


On Wed, 17 Mar 2010 10:22:48 +0000
Peter <biopython at maubp.freeserve.co.uk> wrote:
> For example,
> 
> handle = open("example.fasta", "rU")
> records = list(SeqIO.parse(handle, "fasta"))
> handle.close()
> records.sort(cmp=lambda x,y: cmp(len(x), len(y)))
> #records.sort(cmp=reverse=True)
> out_handle = open("sorted.fasta", "w")
> SeqIO.write(records, out_handle, "fasta")
> out_handle.close()
> 
> Peter

Thank you for the code. I only changed this and it works.

records.sort(cmp=lambda x,y: len(y.seq) - len(x.seq))

If I could not hold all the records in memory at once what could I do?




More information about the Biopython mailing list