-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalcEucl.pl
147 lines (122 loc) · 4.54 KB
/
calcEucl.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env perl
use warnings;
use strict;
use Data::Dumper;
############################################################################
# Calculate Euclidean distances among a set of R matrices
#
# The tab-delimited text file with R matrix elements should have one matrix
# per row. The first value in each row should be the rate matrix name
#
# Can be used for vectors with any number of elements (e.g., equilibrium
# amino acid frequencies, etc)
############################################################################
############################################################################
# Set the global variables
############################################################################
my($progname) = $0;
my($iter);
my($jter);
my($kter);
my($lter);
my($mter);
my($nter);
my($qter);
my($yter);
my($zter);
my($tempch1);
my($tempch2);
my($tempvar);
my($tempstr);
my @temparray;
my($verbose) = 0;
if ( @ARGV != 1 ) {
print STDERR "Usage:\n \$ $progname <infile>\n";
print STDERR " infile = tab-delimited file with R matrices\n";
print STDERR "exiting...\n";
exit;
}
my($infile) = $ARGV[0];
############################################################################
# Read the inputfile (a tab-delimited file with R matrix data)
############################################################################
my @inlist;
open (my $INF, "$infile") or die "Could not open file $infile for input.\n";
@inlist = <$INF>; # Read the tab-delimited file of R matrices
close($INF) or die "Could not close file $infile\n";
my($matrices) = $#inlist + 1;
# Remove all trailing endlines
for ($iter=0; $iter<$matrices; $iter++) { chomp($inlist[$iter]); }
@temparray = split(/\s+/, $inlist[0]);
my($elements) = @temparray - 1;
print "#NEXUS\n\n";
print "[ Calculating Euclidean distance matrix for $matrices exchangeability (R) matrices with $elements elements ]\n\n";
# Initialize the distance matrix array
my($totalsize) = $matrices * $matrices; # Size of the 2D array
my @distancemat;
for ($iter=0; $iter<$totalsize; $iter++) { $distancemat[$iter] = 0; }
# Save the names of the R matrices
my @names;
my($maxnamelen) = 11;
for ($iter=0; $iter<$matrices; $iter++) {
(@temparray) = split(/\s+/, $inlist[$iter]);
$names[$iter] = $temparray[0];
if ( length($names[$iter]) > $maxnamelen ) { $maxnamelen = length($names[$iter]); }
}
############################################################################
# Calculate the Euclidean distances
############################################################################
my @matrix1;
my @matrix2;
my @sqdiff;
my($sumsq);
for ($iter=0; $iter<$matrices; $iter++) {
for ($jter=0; $jter<$matrices; $jter++) {
if ( $iter == $jter ) { $distancemat[matrix_element($iter,$jter,$matrices)] = 0.0; }
else { # do the Euclidean distance calculation
$sumsq = 0;
# transfer the first R matrix to @matrix1 and the second to @matrix2
(@matrix1) = split(/\s+/, $inlist[$iter]);
(@matrix2) = split(/\s+/, $inlist[$jter]);
for ($zter=0; $zter<$elements; $zter++) {
$sqdiff[$zter] = ( $matrix1[$zter+1] - $matrix2[$zter+1] ) * ( $matrix1[$zter+1] - $matrix2[$zter+1] );
$sumsq = $sumsq + $sqdiff[$zter];
}
$distancemat[matrix_element($iter,$jter,$matrices)] = sqrt($sumsq);
} # end else ...
} # end for ($jter=0; $jter<$matrices ...
} # end for ($iter=0; $iter<$matrices ...
############################################################################
# Output the distance matrix
############################################################################
print "Begin Distances;\n";
print "\tDimensions Newtaxa NTax = $matrices ;\n";
print "\tFormat Triangle = Both ;\n";
print "Matrix\n";
for ($iter=0; $iter<$matrices; $iter++) {
print "$names[$iter] ";
$tempvar = $maxnamelen - length($names[$iter]);
for ($jter=0; $jter<$tempvar; $jter++) { print " "; }
for ($jter=0; $jter<$matrices; $jter++) {
if ( $distancemat[matrix_element($iter,$jter,$matrices)] == 0 ) {
print " 0.0000000000000000";
}
else {
print " $distancemat[matrix_element($iter,$jter,$matrices)]";
}
}
print "\n";
}
print "\t;\nEnd;\n\n";
exit;
############################################################################
# End of main program, subroutines follow
############################################################################
############################################################################
# Return array coordinates
############################################################################
sub matrix_element {
my($ith,$jth,$localmultiplier) = @_;
my($localmat) = ($ith * $localmultiplier) + $jth;
return $localmat;
}