Shell script for changing the accession number of DNA sequences in a FASTA file

Hi,

I am having a file of dna sequences in fasta format which look like this:
>admin_1_45
atatagcaga
>admin_1_46
atatagcagaatatatat

with many such thousands of sequences in a single file. I want to the replace the accession Id "admin_1_45" similarly in following sequences to "mydesired_name_45" replacing serially 46,47, 48 with my desired name as static next to each of the > symbol.

kindly help me with the script

Welcome to Forum.

How do you expect output ? Like this ?

Input file :

$ cat fasta_file
>admin_1_45
atatagcaga
>admin_1_46
atatagcagaatatatat

Code to run on terminal :

$ awk '(/^>/ && sub(/>.*_/,">"new)) + 1' new="mydesired_name_" fasta_file

Resulting :

>mydesired_name_45
atatagcaga
>mydesired_name_46
atatagcagaatatatat
1 Like
sed 's/>admin_1_/>mydesired_name_/' filename

---------- Post updated at 08:44 AM ---------- Previous update was at 08:44 AM ----------

it worked nicely. Thank you

Please use CODE TAGS for data you provided in post1. Select data and press code option which is available between QUOTE and HTML, I attached screen shot of it see.

Hello,

One more approach with sed may help.

sed 's/\(^>.*_\)\(.*\)/my_desired_text_\2/g' file_name

Output will be as follows.

my_desired_text_45
atatagcaga
my_desired_text_46
atatagcagaatatatat

Thanks,
R. Singh

---------- Post updated at 11:34 AM ---------- Previous update was at 10:30 AM ----------

One more approach for same as follows.

awk -vs1="my_desired_ouput_" -F"_" '/^>/ {print s1$NF} !/^>/' file

Output will be as follows.

my_desired_ouput_45
atatagcaga
my_desired_ouput_46
atatagcagaatatatat

Thanks,
R. Singh