This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Comparing Alignments In An Iterative Process With Perl

Hi people!

I have a fasta file with multiple sequence alignments. I´m trying to run my code with 2 alignments at a time and compare each with each. So alignment 1 with 2 runs the code then 1 with 3 and so on (after each comparison the results should be saved in the same hash). The problem is that my code compare for example alignment 1 and 2 .. and then later on again 2 with 1 so I get redundant information.

Any suggestions would be very appreciated!

thanks!

Here's my code:

$datei = '/Users/Maxi/Desktop/LernenPerl/alignmentlargo.txt';

open(my $fastd,'<', $datei) or die "die datei $datei wurde nicht geöffnet: $!\n";


my $sequences = ();
my $header = '';
my $reverse='';
my $dnaheaders= 0;
my $length=0;

while (my $line = <$fastd>) {
    chomp $line;
    if ($line =~ /^>/) {
        $header = $line;
        $header =~ s/>//g;

    }
    else {
        $sequences->{$header} .= $line
    }
}

my $seq1;
my $seq2;
foreach my $key (sort keys %$sequences) {       ##interacting with all alignments in the file
    for my $key2 (sort grep { $_ ne $key } keys %$sequences) {
        $seq1 =$sequences->{$key};
        $seq2 =$sequences->{$key2};




my $matrix={};
my @aminos= qw(A C D E F G H I K L M N P Q R S T V W Y);


foreach my $substr1 (@aminos){      
        foreach my $substr2 (@aminos){
            $matrix->{$substr1}->{$substr2}=0;
        }
    }

my $eq={};
my $else={};
LINE: for (my $i=0; $i< length $seq1; $i++){     
    my ($substr1,$substr2) = (substr($seq1,$i,1),substr($seq2, $i,1));
    next LINE if ("$substr1$substr2" =~ /-/);
    if("$substr1$substr2"=~ /[^@aminos]/){
        $else->{$substr1}->{$substr2}+=1;
        $else->{$substr2}->{$substr1}+=1;
        next LINE;
    }
if ($substr1 eq $substr2){
        $matrix->{$substr1}->{$substr2}++;
        $eq->{$substr1}++;

    }elsif ($substr1 ne $substr2){
        $matrix->{$substr1}->{$substr2} += 1/2;
        $matrix->{$substr2}->{$substr1} += 1/2;
    }

}
perl alignment

1 answer

You could also call this "Computing the upper triangle of a distance matrix". You need to change your first two nested foreach loops. To avoid 'exhausted' keys for symmetric operators, you have to mark them, easiest by simply removing them. Example:

#!/usr/bin/env perl

my @keys = ('A'..'E'); # only needs one keys array

while (my $key1 = shift @keys) {
      foreach my $key2 (@keys) {
            print ("$key1:$key2\t")
            ### do stuff ###
      }
}

Prints:

  A:B    A:C    A:D    A:E    B:C    B:D    B:E    C:D    C:E    D:E

Log in to answer this question.