Hi monks, I'm feeling a bit stuck. I basically am writing a sub-routine that compares two strings of dna, and prints out any mismatches that occur ( where good base-pairs are defined in the sub). My problem is that if the mismatch occurs on the first or last base of the string it messes up. e.g. for a sequence:
ATCG AAGG
Mismatches are present on first and last positions e.g. A/A and G/G. I want the program to also print the bases either side of the mismatches e.g. AT/AA and CG/GG. But if the mismatch is on the first position it prints GA/GA and if it is on the last position it prints G/G. How can I get it to not print [$i -1] if the mismatch has occured on the first position, and to not print [$i+1] if the mismatch is on the last position. Thanks for your help. ;-)
sub get_mismatches { my ($segment, $comp_segment) = @_; my @seg_mismatches; my @seg_mis; my $i; my %good_pairs = ( 'A' => 'T', 'C' => 'G', 'G' => 'C', 'T' => 'A', ); foreach (my $i =0; $i < @$segment; $i++) { if ($segment->[$i] ne $good_pairs {$comp_segment->[$i]}) { push @seg_mismatches,"$segment->[$i-1]$segment->[$i]/$com +p_segment->[$i-1]$comp_segment->[$i]\n"; push @seg_mismatches, "$segment->[$i]$segment->[$i+1]/$co +mp_segment->[$i]$comp_segment->[$i+1]\n"; push @seg_mis, "$segment->[$i-1]$segment->[$i] $segment-> +[$i]$segment->[$i+1]\n"; } } return (\@seg_mismatches, \@seg_mis); }

update (broquaint): escaped square brackets + title change (was strings)


In reply to Finding DNA string mismatches 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.