This is a test version of Biostars. For the public version, visit https://www.biostars.org.
renaming fasta file with part of fasta header

I have about 100 multiple fasta files (e.g., file.faa), which I have to rename with the species name mentioned in the fasta header. The fasta headers of these files are in the following format:

>XP_003072227.1 aminopeptidase N [Encephalitozoon intestinalis ATCC 50506]

How do I rename 'file.faa' to 'Encephalitozoon intestinalis.faa'?

I saw that people have used awk and sed for a similar purpose but could not figure out what I have to do. Any help is appreciated.

Thank you.

awk sed

I have tried doing

for i in *.faa; do 
 mv $i $(head -1 $i | cut -f1 -d ' ' | tr -d '>' ).faa
done

This changed 'file.faa' to 'XP_003072227.1.faa' but I need 'Encephalitozoon intestinalis.faa'.

How to modify the script?

1 answer

Create a backup copy of your files before trying the following. Replace this line below in your command above

head -1 $i | cut -f1 -d ' ' | tr -d '>'

with following

head -1 test.fa | cut -f2 -d [ | cut -f1,2 -d " " | awk -F " " '{OFS="_"}{print $1,$2}' /dev/stdin

With your example this produces:

$ head -1 test.fa | cut -f2 -d [ | cut -f1,2 -d " " | awk -F " " '{OFS="_"}{print $1,$2}' /dev/stdin
Encephalitozoon_intestinalis

Log in to answer this question.