<p>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.</p>

<pre><code>sed 's/ /_/g'
</code></pre>

<p>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. <code>NP_567483.1</code>). From here you can parse out the species name, or whatever other ID you would like.</p>

<p>Hope this is helpful! :)</p>

CC BY-SA 3.0
2017-10-15T19:39:57.407Z
2017-10-15T20:31:39.220Z
2017-10-15T20:31:39.220Z
G_Hannigan
<p>With <code>sed</code>, first test it:</p>

<pre><code>echo '&gt;NP_567483.1 transcriptional regulator [Pseudomonas aeruginosa PAO1]' | sed "s/&gt;.*\[\(.*\)\]/&gt;\1/"
</code></pre>

<p>Then modify your fasta:</p>

<pre><code>sed -i.bak "s/&gt;.*\[\(.*\)\]/&gt;\1/" file.fasta
</code></pre>

CC BY-SA 3.0
2017-10-18T02:12:59.017Z
2017-10-18T02:12:59.017Z
h.mon
<p>You can replace fasta header of each sequence with the string that is inside the brackets using awk:</p>

<pre><code>awk -F '[][]' '{if ($0~/&gt;/) {print "&gt;"$2} else {print $0}}' input.fasta &gt; output.fasta
</code></pre>

<p>For lines starting with <code>&gt;</code> (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.</p>

CC BY-SA 3.0
2017-10-16T04:23:31.660Z
2017-10-16T04:23:31.660Z
Mehrdad Bakhtiari
<p>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 <code>&gt;NP_567483.1 transcriptional regulator [Pseudomonas aeruginosa PAO1]</code>. 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 <code>NP_567483.1</code>), and what I want is to have the name of the strain instead (here <code>Pseudomonas aeruginosa PAO1</code>) which is part of the sequence description. </p>

<p>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</p>

CC BY-SA 3.0
2017-10-18T02:53:21.260Z
Should we assume one sequence per species? Otherwise species name is no good as an identifier.
neilfws
<fasta><blast><phylogenetics>
How can I only get the species name for fasta sequences from blast results?
# How can I only get the species name for fasta sequences from blast results?

(Asked by: username_0 on 2017-10-15)

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

**Tags:** fasta, blast, phylogenetics
> **Comments:**
> **username_1**: Should we assume one sequence per species? Otherwise species name is no good as an identifier.  

---

## 3 answers

### Answer by username_2 on 2017-10-15. Score: 3

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.
Hope this is helpful! :)


---

### Answer by username_3 on 2017-10-18. Score: 1

With sed, first test it:
echo '>NP_567483.1 transcriptional regulator [Pseudomonas aeruginosa PAO1]' | sed "s/>.*\[\(.*\)\]/>\1/"

Then modify your fasta:
sed -i.bak "s/>.*\[\(.*\)\]/>\1/" file.fasta


---

### Answer by username_4 on 2017-10-16. Score: 1

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.


---

