Using Bio::SeqIO from BioPerl provides a powerful way of dealing with I/O operations on biological data files having the FastA format or any other format for that matter. The input file object is created as well as three output file objects, each one of these objects has information about the format to read from or write into and the file name to read from or direct the output to, in case the output file doesn't exist, it is created for you too.

Using an appropriate BioPerl interface will eliminate the need to construct regexes to detect sequence identifiers and strings, it will also allow you to flexibly migrate among different biological data formats on the go. That will add up to saving time focusing on the data manipulation tasks rather than coding techniques implementation...

#!/usr/local/bin/perl #title "Compare hash with arrays and print" use strict; use warnings; use Bio::SeqIO; my %hash = ( aw1=>10, qs2=>20, dd3=>30, de4=>10, hg5=>30, dfd6=>20, gf4=>20, hgh5=>30, hgy3=>10, ); my $file = "Sample.fa"; my $file10 = "10.fa"; my $file20 = "20.fa"; my $file30 = "30.fa"; my $seq = Bio::SeqIO->new(-file => "<$file", -format=>'fasta'); # inpu +t object #output objects my $seqOut10 = Bio::SeqIO->new(-file => ">$file10", -format=>'fasta'); my $seqOut20 = Bio::SeqIO->new(-file => ">$file20", -format=>'fasta'); my $seqOut30 = Bio::SeqIO->new(-file => ">$file30", -format=>'fasta'); while(my $seqIn = $seq->next_seq()){ for my $key (keys %hash){ if($seqIn->id eq $key && $hash{$key}==10){ $seqOut10->write_seq($seqIn); }elsif($seqIn->id eq $key && $hash{$key} == 20 +){ $seqOut20->write_seq($seqIn); }elsif($seqIn->id eq $key && $hash{$ke +y} == 30){ $seqOut30->write_seq($seqIn); } } }


Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

In reply to Re: Compare hash with arrays and print by biohisham
in thread Compare hash with arrays and print by ad23

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.