How can I only get the species name for fasta sequences from blast results?

I am trying to make a phylogenetic tree from sequences obtained with blast. I have files that contains hundreds of fasta protein sequences which are all named like >NP_567483.1 transcriptional regulator [Pseudomonas aeruginosa PAO1]. But if I use these files to do multiple alignments or phylogenetic trees using either ClustalX or MEGA7, every sequence are named after the first ID in the sequence names (for this example it would be NP_567483.1), and what I want is to have the name of the strain instead (here Pseudomonas aeruginosa PAO1) which is part of the sequence description.

So I don't know whether the softwares I'm using understand that the information between the [ ] is the species and that I just didn't find where to set this as the name or if I have to write some kind of a script to change my fasta files before. I'm really new to this and don't know much about it. Thanks for your help in advance. Julian

• Should we assume one sequence per species? Otherwise species name is no good as an identifier. Oct 18 '17 at 2:53

There might be a "cleaner" way to do this, but what I would do is first remove all of the spaces from the fasta sequence names and replace them with underscores using something like sed.

sed 's/ /_/g'


Run your BLAST after you remove the spaces and you will get the whole ID string in the result instead of only that first part (e.g. NP_567483.1). From here you can parse out the species name, or whatever other ID you would like.

You can replace fasta header of each sequence with the string that is inside the brackets using awk:

awk -F '[][]' '{if ($0~/>/) {print ">"$2} else {print \$0}}' input.fasta > output.fasta


For lines starting with > (fasta header), it replaces the line with the string inside the brackets and for other lines (which contain the sequence), it prints the line without making any changes.

With sed, first test it:

echo '>NP_567483.1 transcriptional regulator [Pseudomonas aeruginosa PAO1]' | sed "s/>.*$$$.*$$$/>\1/"


sed -i.bak "s/>.*$$$.*$$$/>\1/" file.fasta