This may point you in a helpful direction:

use strict; use warnings; my $seq = 'GAATGTTTTAGCAATCTCTTTCTGTC' . 'ATGAATCCATGGCAGTGACCATACTAAT' . 'GGTGACTGCCATTGATGGAGGGAGACACA'; my $word = 'CTGGATAAGAATGTTTTAGCAATCTCTT'; print slidingMatch($seq, $word); sub slidingMatch { my ($seq, $word) = @_; return "Complete match at $-[1]\n" if $seq =~ /($word)/; my @bestMatch; # Try matching start my $str = substr $seq, 0, 3; my $wordLen = length $word; while ($word =~ /$str/g) { my $tailLen = length ($word) - (my $start = $-[0]); my $subWord = substr ($word, $wordLen - $tailLen); next if $seq !~ m/^$subWord/; @bestMatch = ($start, $tailLen); last; } # Try matching end my $rword = reverse $word; my $rseq = reverse $seq; $str = substr $rseq, 0, 3; while ($rword =~ /$str/g) { my $tailLen = length ($rword) - (my $start = $-[0]); last if @bestMatch && $tailLen < $bestMatch[1]; my $subWord = substr ($rword, $wordLen - $tailLen); next if $rseq !~ m/^$subWord/; @bestMatch = (length ($seq) - $tailLen, $tailLen); last; } return "Partial match found at $bestMatch[0] for $bestMatch[1] cha +racters\n" if @bestMatch; return "No match found for $word in $seq\n"; }

Prints:

Partial match found at 8 for 20 characters

Update: fixed bug where worse end match replaced start match.

Update: fixed tail match position calculation bug (see BrowserUk's reply).


DWIM is Perl's answer to Gödel

In reply to Re: Searching for a word that may only exist in part by GrandFather
in thread Searching for a word that may only exist in part by seaver

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.