Question: concatenate sequence with new label in perl
0
gravatar for cabraham03
3.7 years ago by
cabraham0320
Mexico
cabraham0320 wrote:

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.4k views
ADD COMMENTlink modified 3.7 years ago by Jorge Amigo11k • written 3.7 years ago by cabraham0320
2
gravatar for Jorge Amigo
3.7 years ago by
Jorge Amigo11k
Santiago de Compostela, Spain
Jorge Amigo11k wrote:

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 COMMENTlink modified 3.7 years ago • written 3.7 years ago by Jorge Amigo11k

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

ADD REPLYlink written 3.7 years ago by cabraham0320
1

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

ADD REPLYlink written 3.7 years ago by Deepak Tanwar3.9k

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 REPLYlink written 3.7 years ago by SES8.1k

#!/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 REPLYlink written 3.3 years ago by akhilvbioinfo130

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 REPLYlink modified 3.3 years ago • written 3.3 years ago by Jorge Amigo11k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2000 users visited in the last hour