This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Adding numbers after duplicate headers in fasta files

If I have the .fasta file consisting of a sequence of genes from certain species, how do I add numbers after duplicate headers in such a manner:

i.e. before

>Homo Sapiens
ABCDEFG

>Mus Musculus
EDFGHIK

>Homo Sapiens
XYGFS

after

>Homo Sapiens_1
ABCDEFG

>Mus Musculus
EDFGHIK

>Homo Sapiens_2
XYGFS
linux

2 answers

Here's a seqkit answer too.

seqkit rename -n file.fasta

 awk '/^>/ {printf("%s%s\t",(N>0?"\n":""),$0);N++;next;} {printf("%s",$0);} END {printf("\n");}' < in.fa |\
sort -t $'\t' -k1,1 |\
awk -F '\t' '{N++;if($1!=P) N=1;printf("%s_%d\t%s\n",$1,N,$2);P=$1;}' |\
tr "\t" "\n"

that still adds the one to non-replicate header species.

Log in to answer this question.