This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Can Biopython Properly Import Fasta Headers With Spaces In Them?

When I use BioPython to create a sequence iterator, I find that any characters after the first space (" ") in the header are ignored. For instance:

If file.fasta is:

>A Header With Spaces
ATCGATCGATGC

The following code:

for sequence in SeqIO.parse(open("file.fasta"), "fasta"):
     print sequence.id

Will print:

A

Is there a way to get the full sequence id while still taking advantage of BioPython's utility?

python biopython fasta

The ID in FASTA is defined as everything that comes before the first space, so this behaviour is exactly right.

1 answer

sequence.description should give you the entire header

That did it. I can't tell you how long I've been working around this quirk when there's such a simple solution. Thanks!

A good way of seeing what methods/properties are in a python object without looking up the API is to just use the dir() function. Try printing dir(sequence) and it should list all it's props/methods.

Log in to answer this question.