The subroutine in the code below will handle sequence strings of varying length and will also allow for placeholder letters in the sequence. It loops over each letter in the sequence and uses a translation hash and the four-argument form of substr in a map.

use strict; use warnings; use feature qw{ say }; say q{-} x 9; say for @{ expand( q{AGCT} ) }; say q{-} x 9; say for @{ expand( q{AXCT} ) }; say q{-} x 9; say for @{ expand( q{CTACAGGAA} ) }; say q{-} x 9; sub expand { my $seq = shift; my %translate = ( A => [ qw{ G C T } ], G => [ qw{ A C T } ], C => [ qw{ A G T } ], T => [ qw{ A G C } ], ); my $raExpands = []; for my $posn ( 0 .. length( $seq ) - 1 ) { my $letter = substr $seq, $posn, 1; next unless $letter =~ m{[AGCT]}; my $copy = $seq; push @{ $raExpands }, map { substr $copy, $posn, 1, $_; $copy; } @{ $translate{ $letter } }; } return $raExpands; }

The output.

--------- GGCT CGCT TGCT AACT ACCT ATCT AGAT AGGT AGTT AGCA AGCG AGCC --------- GXCT CXCT TXCT AXAT AXGT AXTT AXCA AXCG AXCC --------- ATACAGGAA GTACAGGAA TTACAGGAA CAACAGGAA CGACAGGAA CCACAGGAA CTGCAGGAA CTCCAGGAA CTTCAGGAA CTAAAGGAA CTAGAGGAA CTATAGGAA CTACGGGAA CTACCGGAA CTACTGGAA CTACAAGAA CTACACGAA CTACATGAA CTACAGAAA CTACAGCAA CTACAGTAA CTACAGGGA CTACAGGCA CTACAGGTA CTACAGGAG CTACAGGAC CTACAGGAT ---------

I hope this is useful.

Cheers,

JohnGG


In reply to Re: Issues with substr and substituting by johngg
in thread Issues with substr and substituting by lecb

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.