Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: printing fasta sequences from Hash

by biohisham (Priest)
on Feb 25, 2015 at 03:14 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-18 23:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found