This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Fasta file edition

Is there any way through the command line to add certain specific nucleotide string to the start of the fasta sequence in a multifasta sequence file. For example, I want to add primer sequence to the start of the fasta sequence.

>sequence
ATGCATGCATGC

REQUIRED

>SEQUENCE
PRIMER + ATGCATGCATGC

Thanks in advance.......

sequence

Can you take a look to see if that is what you are looking for (I adjusted the format)?

I think what you want is

>sequence
PRIMER + ATGCATGCATGC

or

>sequence
PRIMERATGCATGCATGC

yes actually i just wanted to represent that I want something to add in front of the sequence.. Pimer sequence - GCANNTGTNNGCATNNGCNAA

i should have written FROM

>fasta identifier
original sequence

TO

>fasta identifier
GCANNTGTNNGCATNNGCNAAoriginal sequence

Then use the answer @Alex provided below.

2 answers

Assuming your FASTA records are single-line:

$ awk -vPRIMER="ACTG" '{if ($0~/^>/) { print $0; } else { print PRIMER$0; }}' in.fa > out.fa

If not, you can pre-process your FASTA file to make it single-line.

Can this code be modified to add primer sequence in the end of the complete sequence, what we have generated in the previous code? And I assume we will already use the reverse complement sequence so that we dont have to include that thing in the code.

The first ever perl script I wrote does this! Adds a different barcode based on a common fasta header identifier, which I think that's what you want?

All you do is add_barcode.pl my_file.fasta then it'll ask you for the shared ID in the header, then ask for what the barcode you want to add is. Not the best code in hindsight, but does the job!

#!/usr/bin/perl    
use 5.010;

print "USAGE: add_barcode.pl my_file.fasta\n"

chomp($file = $ARGV[0]);
$file =~ s/\..*//;

print "what is the match (case-insensitive):\n";
chomp($site = <STDIN>);

print "what is the sites barcode:\n";
chomp($barcode = <STDIN>);


chomp(@lines = <>);

open MODIFIED, ">$file" . "_barc.fas";
select MODIFIED;
foreach (@lines){
        if (/$site/i) {
        print "$_\n$barcode";
        $count += 1;
        }else{
        print "$_\n";
        }
}
close MODIFIED;
select STDOUT;


print "modified $site $count times\n";

vishwaas1704 : If above answer(s) have solved your question then please accept one (or more) of them (green check mark). That indicates that the question has been solved.

Log in to answer this question.