This is a test version of Biostars. For the public version, visit https://www.biostars.org.
concatenate sequence with new label in perl

Hi, I have a code to extract sequence from multiple fasta sequences and concatenate, and I want to add a new label; something like:

From

>seq1
ACAACCAGCTAGTCACCAAGTTACTGGCAGCACAAAAACAGTCGGCTGCGAATGACGATC
>seq2
CAACTGGCGTGTCATTCGCGGTTGCATCTCTGCTGATGAAAACGCAAACCTCGACAGAAG

to:

>new_label
ACAACCAGCTAGTCACCAAGTTACTGGCAGCACAAAAACAGTCGGCTGCGAATGACGATCCAACTGGCGTGTCATTCGCGGTTGCATCTCTGCTGATGAAAACGCAAACCTCGACAGAAG

My problem is in the print section, I want to add a new label just ones an all the sequences; I have try to use like

print 'New_label' . $seq;         # but it don't work
#!/usr/bin/perl -w
use strict;

open FASTA, "file.fasta" or die;
while(my $seq= <FASTA>) {
    chomp($seq);
    if ($seq =~  m/^>/ ) {
        next;
    }
    elsif ($seq =~ s/n|-//gi){
        next;
    }
    else {
         my $sequence =$seq;
        print $sequence;            #here is my problem
    }
}
close FASTA;
print "\n";

Thanks so much

fasta perl

1 answer

if I understand your problem correctly, you need to print it at the beginning of the process, right just after the open and before the while, just as follows:

open FASTA, "file.fasta" or die;
print "New_label\n";
while(my $seq= <FASTA>) {

but if I really understand your problem correctly, a simple echo and grep combination would do:

echo "New_label" > newfile.fasta
grep -v "^>" file.fasta >> newfile.fasta

Thanks so much Jorge; it's works well : ))) !!!

If your problem got solved, please accept the answer by clicking on the green tick, which only you could see below the answer.

This form of open works for historical reasons, but this should never be used in practice. It's much better to write it as:

open my $in, '<', "file.fasta" or die $!;

Specifying the mode and a lexical file handle will save you a lot of trouble!

#!/usr/bin/perl -w
use strict;
open FASTA, "file.fasta" or die;
print "New_label\n";
while(my $seq= <FASTA>) 
{
    chomp($seq);
    if ($seq =~  m/^>/ )
 {
        next;
    }
    elsif ($seq =~ s/n|-//gi)
{
        next;
    }
    else 
{
         echo "New_label" > newfile.fasta
        grep -v "^>" file.fasta >> newfile.fasta   
 }
}
close FASTA;
print "\n";

now its not working

the echo and grep is an independent bash solution, not to be used inside a perl script. choose whatever you prefer:

bash:

echo ">New_label" > newfile.fasta
grep -v "^>" file.fasta >> newfile.fasta

perl (the whole previous code can be rewritten to this):

#!/usr/bin/perl -w
print ">New_label\n";
while(<>) { chomp; ! /^>/ and print }

you can remove the chomp if you don't need all the sequences to be in a single line. save it as script.pl, and call it as perl script.pl input.fasta

Log in to answer this question.