There's more than one way to do things PerlMonks

### Re^4: How do you match a stretch of at least N characters

 on Sep 11, 2017 at 14:52 UTC ( #1199118=note: print w/replies, xml ) Need Help??

Aha, ok, I see, probably I just ran into the exact ones by chance... I know I am pushing my luck a bit, but since you have been so helpful and the code is yours, is there a way that I can see which of the 10-mers are consecutive and "paste" them into one larger match?
```AGCAATATTTCAAG
AGCAATATTTCAAG

GCAATATTTCAAGA
GCAATATTTCAAGA

For example, these 2 are basically the same, just going 1 position further.
Hm, now that I am thinking it better, was the problem I asked for a question of finding the longest possible match with one mismatch allowed? Would that have been easier and I was wasting everyone's time here?

Replies are listed 'Best First'.
Re^5: How do you match a stretch of at least N characters
by choroba (Bishop) on Sep 11, 2017 at 17:04 UTC
> is there a way that I can see which of the 10-mers are consecutive and "paste" them into one larger match?

It's not so easy. You can't paste them together, as that might increase the number of mismatches over the threshold.

But you can change the algorithm totally if you want to find the longest matches. Just move the shorter string alongside the longer one, and then for each position try to find the longest match starting there.

```#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

my \$stretch = 10;
my \$mismatches = 1;

substr \$reference_str, 0, 0, 'x' x (length(\$small_str) - 1);
\$reference_str .= 'x' x (length(\$small_str) - 1);

for my \$start_pos (0 .. length(\$reference_str) - length(\$small_str)) {
my \$substr = substr \$reference_str, \$start_pos, length(\$small_str)
+;

my (\$from, \$diffs) = (0, 0);
for my \$inner_pos (0 .. length(\$small_str) ) {
++\$diffs if substr(\$substr, \$inner_pos, 1)
ne substr \$small_str, \$inner_pos, 1;

if (\$diffs > \$mismatches || \$inner_pos == length(\$small_str) )
+ {
say join "\n\t",
\$start_pos + \$from + 1 - length \$small_str,
substr(\$small_str, \$from, \$inner_pos - \$from),
substr(\$reference_str, \$from + \$start_pos, \$inner_pos
+- \$from)
if \$inner_pos - \$from >= \$stretch;

++\$from until (substr \$small_str, \$from, 1) ne substr(\$sub
+str, \$from, 1) || \$from > \$inner_pos;
++\$from;
--\$diffs;
}
}
}

> was the problem I asked for a question of finding the longest possible match with one mismatch allowed?

Basically, yes.

```(\$q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord
}map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+\$1,++print+eval\$q,q,a,
I think it does what I needed!
I will now study the code you kindly provided, thank you very much!

Create A New User
Node Status?
node history
Node Type: note [id://1199118]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2017-11-23 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (327 votes). Check out past polls.

Notices?