Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Does this do what you want? There is no need to split the sequence into an array as pos will allow you to find where in a string a match has been made. Note that [^ACGT] is a negative character class, i.e. match anything that isn't A, C, G or T. Using capturing parentheses, ( ... ), and matching globally, m{ ... }g or / ... /g will advance along the sequence looking for invalid letters.

I am opening a file that is held inside the script just to keep things tidy on my system but the code will work fine with STDIN. The code.

use 5.026; use warnings; open my $dnaFH, q{<}, \ <<__EOD__ or die $!; TAAGAACAATAAGAACAAGAACAATAA GAACAATAAGXAATAAGAAXXAACAAGAACAATAA ACAATAAAAGAACAATAAGAA __EOD__ while ( my $sequence = <$dnaFH> ) { chomp $sequence; my $length = length $sequence; say qq{Sequence: $sequence -- Length $length}; if ( $sequence =~ m{^[ACGT]+$} ) { say q{ Sequence is GOOD!}; } else { my @badPosns; push @badPosns, pos $sequence while $sequence =~ m{(?x) (?= ( [^ACGT] ) )}g; my $nBad = scalar @badPosns; my $perc = sprintf q{%.2f}, $nBad / $length * 100; say qq{ Sequence is BAD at @badPosns}; say qq{ $nBad bad positions, $perc\% of total}; } } close $dnaFH or die $!;

The output.

Sequence: TAAGAACAATAAGAACAAGAACAATAA -- Length 27 Sequence is GOOD! Sequence: GAACAATAAGXAATAAGAAXXAACAAGAACAATAA -- Length 35 Sequence is BAD at 10 19 20 3 bad positions, 8.57% of total Sequence: ACAATAAAAGAACAATAAGAA -- Length 21 Sequence is GOOD!

I hope this is helpful. Please ask further if you need more help.

Update: There was a mistake in the code, I should have used a look-ahead assertion as without that pos gives the position after the match, not that of the match itself. Added extended syntax ((?x)) to make the regex clearer. My bad :-(

Update 2: I should also have corrected the output, now done.



In reply to Re: Find element in array by johngg
in thread Find element in array by Sofie

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (5)
    As of 2020-08-05 11:22 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (35 votes). Check out past polls.