Hello Pasan and welcome to the Monastery.

Of course your code will print "match found" because that is what you are telling it to do. Dump the content of $fastaH1 through Data::Dumper to see how your hash looks like. My piece of advice is that if you are reading from a Bio::SeqIO object then you got to write your output through a Bio::SeqIO object as well. That way you will save yourself from a lot of the unexpected Bio::* behaviors.

other things I noticed in your code is that your open statement doesn't capture failure. Alsoe, if you're using a Bio::SeqIO then you don't need to explicitly import all the parent modules.

use strict; #a good habit to start your prog use warnings; use Bio::SeqIO; #load your codes.txt into an array open(my $fh, "<","codes.txt") or die("could not open codes.txt $!"); # +a more proper way to opening files my @codes=<$fh>; #slurping codes.txt into an array my $obj=Bio::SeqIO->new( #Bio::SeqIO for input -file=>"fasta.fa", -format=>"fasta" ); my $out_obj=Bio::SeqIO->new( #Bio::SeqIO for output -file=>">matching.fa", -format=>"fasta" ); while(my $seq=$obj->next_seq){ foreach my $element (@codes){ chomp $element; if($seq->id=~/$element*?/gi){ #regular-expression based mat +ching $out_obj->write_seq($seq); #output written to matching.fa } } }

David R. Gergen said "We know that second terms have historically been marred by hubris and by scandal." and I am a two y.o. monk today :D, June,12th, 2011...

In reply to Re: comparing sequence fasta headers by biohisham
in thread comparing sequence fasta headers by Pasan

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.