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

In reply to Re: printing fasta sequences from Hash by biohisham
in thread printing fasta sequences from Hash by Bijgom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.