TMTOWTDI
Given biological data can be huge, using Perl's builtin string-handling functions
can often be far more efficient than using regexes.
Using Benchmark can help when choosing a solution.
The following code still uses regexes but only minimally:
#!/usr/bin/env perl
use 5.014;
use warnings;
my $string = 'AAATTTAGTTCTTAAGGCTGACATCGGTTTACGTCAGCGTTACCCCCCAAGTTATT
+GGGGACTTT';
my $min_repeat = 2;
for my $base (qw{A C G T}) {
say "$base: ", get_longest_length($string, $base, $min_repeat);
}
sub get_longest_length {
my ($str, $base, $min) = @_;
my $re = '[' . 'ACGT' =~ s/$base//r . ']+';
return (
sort { length $b <=> length $a }
grep length $_ >= $min, split /$re/, $str
)[0];
}
Output:
A: AAA
C: CCCCCC
G: GGGG
T: TTT
Notes:
-
I've specified v5.14 to use the 'r' modifier.
See "perl5140delta: Non-destructive substitution".
-
You can use index to find the number and position(s)
of maximum-length substring(s).
-
There are a number of optimisations that could be applied,
but that will largely depend on your intended usage of this code.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|