Comparing Alignments In An Iterative Process With Perl
1
2
Entering edit mode
10.7 years ago
loicatraile ▴ 50

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 • 2.8k views
ADD COMMENT
3
Entering edit mode
10.7 years ago
Michael 54k

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
ADD COMMENT
0
Entering edit mode

Thank you! that solved my problem! :)

ADD REPLY

Login before adding your answer.

Traffic: 2473 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6