This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to deal with the spaces in the sequence names with biopython?

I have a fasta file formatted as follows:

>UPF0471 protein C1orf63 homolog

some sequence

>WD repeat-containing protein 43

some sequence

>transmembrane protein 41A

some sequence

When I print out record.id or make dictionaries, biopython cannot handle the spaces in the sequence names. What should I do to let biopython recognize the name as whole rather than just taking the first word of the name?

sequence space name biopython

Replace the spaces with "_" or "-"?

You'll find most tools will take the same attitude to spaces and FASTA identifiers, so good idea!

3 answers

You can get the whole header by using record.description

Answering your second question, how to make a dictionary using SeqIO.to_dict with the full descriptions with spaces as keys - you would need to use the key_function as help(to_dict) tries to explain, e.g.

my_dict = to_dict(sequences, key_function=lambda rec: rec.description)

Then how do I make dictionaries with SeqIO.to_dict?

This isn't an answer - it is a new question, or an addendum to your old question?

Log in to answer this question.