Using a BioPerl module to parse sequences files lifts away the burden of having to manually detect what constitutes a sequence start and end for many of the available formats, also, it takes care of handling the sequence objects and IO tasks efficiently and succinctly.

While learning Perl, make sure you are learning it the right way by building on the good habits that would at the end lead you to becoming an established hacker, great emphasis is there to always

use strict; use warnings;
At the top of your programs, these stricture would enable you to save time debugging by giving you warnings about potential pitfalls and enforcing rules like "variable localization and scoping", that would really save you from a lot of errors. Also, make sure that your variables are named in a self-descriptive fashion and that your code is indented in a readable way.

Here is how you can perform the same job by using the library Bio::SeqIO from the BioPerl suite.

use strict; use warnings; use Bio::SeqIO; my $seq_file = 'D:/Motif Search/sequence.txt'; my $mot_file = 'D:/Motif Search/motif.txt'; my $in = Bio::SeqIO->new( #sequences object -format => 'fasta', -file => $seq_file, ); my $mot = Bio::SeqIO->new( #motifs object -format => 'fasta', -file => $mot_file ); my @motifs; while ( my $motif = $mot->next_seq ) { #read in motifs my $motif_string = $motif->seq; #stringify motifs push @motifs, $motif_string; } while ( my $seq = $in->next_seq ) { my $string = $seq->seq; foreach my $motif (@motifs) { if ( $string =~ /$motif([agct]+)[AGCT]/ ) { print "'$motif' ", length $1, " bases away at ", $seq->id, "\n"; } } }

Have a great Perl journey ...

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

In reply to Re: How to find any of many motifs? by biohisham
in thread How to find any of many motifs? by Nikulina

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.