Hello Monks!
I have some thousands of strings like the following:
>id1 ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss +sssssssssssssssssssssssssssssssssssssssssssssssss >id_2 ssDDDDDDDDDDDDDDDDDDDDDDssssssssDDDDDDDDDDssssssssDDDDDssssssssssssssD +DDDDDDDDDDDDDDDsDDDDDDDDDssssssssssssssUUUUUUUUUs ...

where I would like to do the following substitution:
1. replace all s prior to U with i and all s following a U with o
2. replace all s prior to D with o and all s following a D with i
3. replace all D and U with M
4. if no D or U are present, then all s becomes

I wrote the following, but it does not seem to work all the time properly (probably could be written more simply too):
while(<>) { if($_=~/^>/) { $id=$_; $seq=<>; print $id; if($seq=~/^s+$/) { $seq=~s/s/I/g; } else { while($seq=~/(s+)(U+)(s+)/g) { $part_before_U=$1; $len1_U=length($part_before_U); $part_U=$2; $part_after_U=$3; $len2_U=length($part_after_U); $in_part_U='I' x $len1_U; $out_part_U = 'O' x $len2_U; $seq=~s/$part_before_U/$in_part_U/; $seq=~s/$part_after_U/$out_part_U/; } while($seq=~/(s+)(D+)(s+)/g) { $part_before_D=$1; $len1_D=length($part_before_D); $part_D=$2; $part_after_D=$3; $len2_D=length($part_after_D); $out_part_D = 'O' x $len1_D; $in_part_D='I' x $len2_D; $seq=~s/$part_before_D/$out_part_D/; $seq=~s/$part_after_D/$in_part_D/; } $seq=~s/U/M/g; $seq=~s/D/M/g; } print $seq; } }

In reply to Pattern matching simultaneous substitution 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":



  • 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.