Question

I tried to find some clue in the list but I couldn't, so sorry if I ask a repeated topic

I am PERL beginner and I am trying to write a program in PERL that take two DNA sequences, calculate the reverse of the second one and find the maximum complementary regions between them, that is:

input:

CGTAAATCTATCTT
CATGCGTCTTTACG

output:

CGTAAATCTATCTT
GCATTT--------

I have no problem to find the reverse of the second sequence, however my programming skills in PERL are rudimentary. Do I need to use a combined for an foreach loops?

Was it helpful?

Solution

Perhaps this is what you want (crudely):

#!/usr/bin/env perl
use strict;
use warnings;
die unless @ARGV == 2 && length $ARGV[0] == length $ARGV[1];
my @seq1 = split //, $ARGV[0];
my @seq2 = split //, reverse $ARGV[1];
my @comp;
for my $n (0..@seq1-1) {
    if   ( ($seq1 [$n] eq 'A' && $seq2 [$n] eq 'T') 
        || ($seq1 [$n] eq 'T' && $seq2 [$n] eq 'A') 
        || ($seq1 [$n] eq 'G' && $seq2 [$n] eq 'C') 
        || ($seq1 [$n] eq 'C' && $seq2 [$n] eq 'G') ) {
        push @comp, $seq2[$n];
    }
    else {
        push @comp, '-';
    }
}
print @seq1, "\n", @comp, "\n";

...which when run:

# ./compseq CGTAAATCTATCTT CATGCGTCTTTACG
CGTAAATCTATCTT
GCATTT------A-

OTHER TIPS

Does this work for you?

#!/usr/bin/perl
use warnings;
use strict;

sub complement {
    $_[0] =~ y/CGAT/GCTA/;
    return $_[0];
}

sub match {
    my ($s1, $s2) = @_;
    $s2 = reverse $s2;
    complement $s2;
    print "$s1\n";
    my $s2l = length $s2;
    for (my $length = $s2l; $length; $length--) { # start from the longest possible substring
        for my $start (0 .. $s2l - $length) {     # starting position of the matching substring
            my $substr = substr $s2, $start, $length;
            my $pos = index $s1, $substr;
            if ($pos + 1) {
                return ('-' x $pos) . complement "$substr" . ('-' x ($s2l - $length - $pos));
            }
        }
    }
}

print match('CGTAAATCTATCTT',
            'CATGCGTCTTTACG')
    ,"\n";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top