Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: printing fasta sequences from Hash

by biohisham (Priest)
on Feb 25, 2015 at 03:14 UTC ( #1117775=note: print w/replies, xml ) Need Help??

in reply to printing fasta sequences from Hash

Your problem is that each fasta sequence spans multiple lines. Knowing that the fasta header is ">" you can use that to your advantage by telling your program to split around the fasta separator and slurp all that is in between. Another neater solution is achievable through Bio::SeqIO which requires BioPerl installation on your system. So for the first case, slurping each fasta record the following code gets you there with few modifications, improtantly, note the default separator variable $/was locally modified to ">"

use strict; use warnings; use Data::Dumper; local $/=">"; while(my $fasta_record=<DATA>){ chomp $fasta_record; $fasta_record =~ s/(^>*.+\n)/$1: /; #add a space after the fasta +header $fasta_record =~ s/\n//g; # remove endlines print $fasta_record,"\n"; } __DATA__ >protein1 WWWWTCTG TTWTTTCT TTWWWC >protein2 WWWWTCTG TTWWWC TTWTTTCT >protein3 TTWWWC WWWWTCTG TTWTTTCT

I prefer using a BioPerl module to do this kind of tasks since I can get more time to focus on the more important stuff of doing something or the other with the sequences rather than grapple with how to read them in

Assuming that your sequences are in a fasta file called database_filename.fa

use strict; use warnings; use Bio::SeqIO; #initialize a bioperl object instance my $in = Bio::SeqIO->new(-file=> "database_filename.fa", -format=>"fas +ta"); while(my $seq = $in->next_seq){ #read the sequences in the bioperl +object print $seq->id," ",$seq->seq; #output,ID space-separated from sequ +ences print "\n"; }

A 4 year old monk

Replies are listed 'Best First'.
Re^2: printing fasta sequences from Hash
by Bijgom (Initiate) on Feb 25, 2015 at 09:07 UTC

    Many thanks bioisham for kindly replying to my post. I will indeed install BioPerl. Hope it will make things smother. Regards, B

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1117775]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (1)
As of 2021-10-19 02:30 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (76 votes). Check out past polls.