This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to change name of species in FASTA file with BioPython

Hi I am trying to figure out a basic script to edit the names of the species in my FASTA file. I need the final product of the names to look like this:

>Homo_sapiens (FTO_NP_001073901) 
>Pan_troglodytes (FTO_XP_510968) 
>Nomascus_leucogenys (FTO_XP_003263101) 
>Pongo_abelii (FTO_NP_001126250)
fasta python biopython

Hello and welcome to biostars Daphnia3000 ,

your post is missing important information. How does your input looks like? What have tried and where did you get stuck?

fin swimmer

As finswimmer mentioned, the minimum we need is an example of your input so we know what manipulations are needed to create your desired output.

1 answer

Like others also said, we need more info to give the exact answer but I will still give it a try. I assume you have BioPython working, you can use something like this:

from Bio import SeqIO
with open("input.fa", "rU") as handle, open("output.fa", "a") as output:
    for record in SeqIO.parse(handle, "fasta"):
        newHeader = "create your new header here, maybe split() will help"
        output.write(">"+newHeader+"\n")
        output.write(str(record.seq)+"\n")

Log in to answer this question.