Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

— Ken


In reply to Re: substrings that consist of repeating characters by kcott
in thread substrings that consist of repeating characters by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-28 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found