Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I'm trying to write a simple program that reads in a 50bp DNA sequence and splits it into pairs of two bases - e.g. ATCG would be AT, TC, CG ... with a counter moving along the sequence one base at a time. Anyway, my program does this but forgets about the last base, so with the above example it will find AT and TC but not CG. Here's the code below, hope someone can spot the mistake. Thanks :->
# @dna contains the sequence foreach my $base (@dna) { print "$base\n"; ++$counter; my $pair = @dna1[$counter-1]; print "$pair"; }

Replies are listed 'Best First'.
Re: loop and counter question
by Mr. Muskrat (Canon) on Feb 25, 2003 at 14:13 UTC

    Here is just a small snippet that you can use inside your main program.

    #!/usr/bin/perl -w use strict; my $base = 'ATCGGTAC'; print "base is $base\n"; my @pairs = SplitPairs($base); print "pairs are @pairs\n"; sub SplitPairs { my $base = shift; my $baselen = (length $base) - 2; my @pairs; for my $i (0 .. $baselen) { $pairs[$i] = substr $base, $i, 2; } return @pairs; }

    This snippet outputs:

    base is ATCGGTAC pairs are AT TC CG GG GT TA AC

    Updated: Put the code snippet into a subroutine.

Re: loop and counter question
by artist (Parson) on Feb 25, 2003 at 14:00 UTC
    Please make sure that you cut and paste the code here, instead of re-writing or writing from scratch in the question box. That would eliminate typographical errors.

    my @dna = qw(A T C G); my $counter; foreach my $i (1..$#dna){ my $base = $dna[$i-1]; my $pair = $dna[$i]; print "$base$pair\n"; }
Re: loop and counter question
by Abigail-II (Bishop) on Feb 25, 2003 at 13:51 UTC
    How can we tell where the mistake is? We don't know what's in either of @dna and @dna1.

    Abigail

      $dna1 was a typo
        So, what's in @dna then? Letters? Strings? If you have the sequence in a sequence $dna, you could do:
        print "$_ " for $dna =~ /(?=(..))/g;

        If you have individual letters in an array, you could use:

        print @dna [$_ - 1, $_], " " for 1 .. $#dna;

        Abigail