How to replace sequence identifiers in a fasta with OTU IDs from another file? I'm pretty new to Unix and bioinformatics and having a hard time accomplishing the following. I have one FASTA file with sequences and headers, and one OTU ID mapping file (.txt) with OTU IDs and their sequences. The sequence header in the .fasta file partially matches the sequence listed in the OTU ID file. What I want to do is replace the current headers in the FASTA file with just the OTU IDs from the .txt file. The files look like something like this:
OTU_ID.txt
denovo123 U1019.1010.2020_20384
denovo453 U1472.2992.2020_20443
denovo763 U1928.1898.2020_20343

.FASTA
&gt;U1019.1010.2020_20384 0000-AHDJ-38347_223
GAAATACCGAGAGAGAAATACCGAGAGAGAAATACCGAGAGAGAAATACCGAGAGAGAAATACCGAGAGAGAAATACCGAGAGA
&gt;U1928.1898.2020_20343 0000-AHDJ-24247_242
GAAATACCGAGAGAGAAATACCGAGAGAGAAATACCGAGAGAGAAATACCGAGAGAGAAATACCGAGAGAGAAATACCGAGAGA

and my desired output is that the .fasta file would look like this:
&gt;denovo123
GAAATACCGAGAGAGAAATACCGAGAGAGAAAGAAATACCGAGAGAGAAATACCGAGAAATACCGAGAGAGAAATACCGAGAGA
&gt;denovo763
GAAATACCGAGAGAGAAATACCGAGAGAGAAATACCGAGAGAGAAATACCGAGAGAGAAATACCGAGAGAGAAATACCGAGAGA

Is it possible to do this in the command line?
You can do it with bash quiet easy. First create a backup of your fasta file with
cp otu.fasta otu_backup.fasta

Then you can run:
while IFS=&quot; &quot; read -r otu fastaId rest; 
do
sed -E -i &quot;s/&gt;$fastaId .+/&gt;$otu/&quot; otu.fasta; done &lt; ids.txt;
done &lt; OTU_ID.txt

What does the code?
With IFS=&quot; &quot; you declare a whitespace as field seperator and by reading each line of your OTU_ID.txt, you have the two variables otu and fastaId which hold the otu name and the id of the Fasta Header.
Within the while loop, sed is looking each loop for the corresponding id within a RegEx and substitute it with the corresponding otu name. By using the -i option, you overwrite the existing file. This is why you need to create a backup.