concatenate sequence with new label in perl
1
0
Entering edit mode
8.8 years ago
cabraham03 ▴ 30

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 • 2.6k views
ADD COMMENT
2
Entering edit mode
8.8 years ago

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
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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!

ADD REPLY
0
Entering edit mode
#!/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

ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 3001 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6