This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Add Specific Word To First word in Fasta header

I have fasta file with different headers. Basically assembled fasta files some are assembled with different versions so they got same fasta names. When I tried to make a blast database with -parse_seqids its complaining me of duplicate id's. So I would like to add a extension to with version of assembly to its fasta headers.

Examples

Input fasta sequences: (I am just showing headers here)

>Contig1_Node1_length20_cov30 Date:03/01/2015 Sequence_Organism:Other
>Contig2_deg1 Date:03/01/2015 Sequence_Organism:Other
>Contig3_jcg20839 Date:03/01/2015 Sequence_Organism:Other

Output fasta sequences:

>Contig1_Node1_length20_cov30_V2 Date:03/01/2015 Sequence_Organism:Other
>Contig2_deg1_V2 Date:03/01/2015 Sequence_Organism:Other
>Contig3_jcg20839_V2 Date:03/01/2015 Sequence_Organism:Other
sed awk unix perl

Exclude the -parse_seqids while creating blast database. It will not give any error.

True but I need -parse_seqids to extract sequences from fasta file.

2 answers

Hi, here is a sed solution:

sed -e '/^>/ s/ /_V2 /' input.fa > output.fa

and a awk solution:

awk '{printf (/^>/) ? $1"_V2 "$2" "$3"\n" : $0"\n"}' input.fa > output.fa

OK, the simplest way is again either a perl of awk script:

perl -lne 'chomp;if(/>(.*?)\s+(.*)/){print ">$1_V2 $2"}else{print $_}' input.fa > output.fa

_V2 in the above line is what you are adding as an extension. If there are several different extensions then you should create a key table and preloaded as a hash table. Again everything can be done in a single line.

Hope this helps

Cheers
mxs

PS: please ask if anything is unclear regarding the above solution

Log in to answer this question.