Question: Comparing Alignments In An Iterative Process With Perl
2
gravatar for loicatraile
6.4 years ago by
loicatraile40
Germany
loicatraile40 wrote:

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 schould 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 bioinformatics alignment • 2.1k views
ADD COMMENTlink modified 6.4 years ago by Michael Dondrup47k • written 6.4 years ago by loicatraile40
3
gravatar for Michael Dondrup
6.4 years ago by
Bergen, Norway
Michael Dondrup47k wrote:

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 COMMENTlink modified 6.4 years ago • written 6.4 years ago by Michael Dondrup47k

Thank you! that solved my problem! :)

ADD REPLYlink written 6.4 years ago by loicatraile40
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1095 users visited in the last hour