I have a previous code reading the matrix from a file and storing it in the @matrix array of hashes with the structure I showed in the previous post.
Sorting each row of the original matrix by nucleotide frequency and copying it to another matrix keeping the original position of the row, the nucleotides frequencies and then the nucleotides corresponding to the frequencies in the same order. [OriPos, F1, F2, F3, F4, N1, N2, N3, N4]where F1 is the most frequent nucleotide and N1 is his corresponding letter.
sub sortmatrix(){
for (my $i=0; $i<=$#matrix;$i++){
my @resline = ( $i+1 );
my @keys = ();
for my $k ( sort { $matrix[$i]{$b} <=> $matrix[$i]{$a} } keys %{ $
+matrix[$i] } ) {
push @keys, $k;
push @resline, $matrix[$i]{$k};
}
push @resline, @keys;
push @sorted, \@resline; # nb. pushing the reference not the array
+ this time
}
Sorting the rows of the matrix by the most frequent nucleotide column, then the 2nd, 3rd, 4th and last by original position
@sorted = sort {
$b->[1] <=> $a->[1] ||
$b->[2] <=> $a->[2] ||
$b->[3] <=> $a->[3] ||
$b->[4] <=> $a->[4] ||
$a->[0] <=> $b->[0];
} @sorted;
}
use Data::Dumper;
print Data::Dumper->Dump([ \@sorted ],[ qw/ *matrix / ]),"\n";
And here i get my matrix completely sorted :)
@sortedmatrix = (
[
4,
17,
0,
0,
0,
'C',
'A',
'T',
'G'
],
[
5,
17,
0,
0,
0,
'A',
'T',
'C',
'G'
],
[
6,
17,
0,
0,
0,
'T',
'A',
'C',
'G'
],
[
7,
17,
0,
0,
0,
'G',
'A',
'T',
'C'
],
[
9,
17,
0,
0,
0,
'C',
'A',
'T',
'G'
],
[
10,
17,
0,
0,
0,
'C',
'A',
'T',
'G'
],
[
11,
17,
0,
0,
0,
'G',
'A',
'T',
'C'
],
[
12,
17,
0,
0,
0,
'G',
'A',
'T',
'C'
],
[
14,
17,
0,
0,
0,
'C',
'A',
'T',
'G'
],
[
15,
17,
0,
0,
0,
'A',
'T',
'C',
'G'
],
[
16,
17,
0,
0,
0,
'T',
'A',
'C',
'G'
],
[
17,
17,
0,
0,
0,
'G',
'A',
'T',
'C'
],
[
3,
15,
2,
0,
0,
'A',
'G',
'T',
'C'
],
[
13,
15,
2,
0,
0,
'G',
'A',
'T',
'C'
],
[
18,
15,
2,
0,
0,
'T',
'C',
'A',
'G'
],
[
1,
13,
4,
0,
0,
'G',
'A',
'T',
'C'
],
[
8,
13,
4,
0,
0,
'C',
'T',
'A',
'G'
],
[
19,
13,
4,
0,
0,
'C',
'T',
'A',
'G'
],
[
2,
12,
5,
0,
0,
'G',
'A',
'T',
'C'
],
[
20,
7,
7,
2,
0,
'T',
'C',
'G',
'A'
]
);
|