-
Notifications
You must be signed in to change notification settings - Fork 25
/
score-msp.pl
executable file
·160 lines (126 loc) · 3.39 KB
/
score-msp.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
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/perl -w
use strict;
if (!$ARGV[0] || $ARGV[0] eq "help") {
print STDERR <<EOF;
Script to compare RFMIX (v2) maximum-likelihood state path subpopulation assignments
to known/expected correct results generated from simulation.
Usage: <RFMIX .msp.tsv output file> <Expected results file>
EOF
exit -1;
}
my ($output_fname, $correct_fname) = @ARGV;
exec($0) unless $output_fname && $correct_fname;
open F, "<$output_fname"
or die "Can't open RFMIX MSP output file $output_fname ($!)";
open R, "<$correct_fname"
or die "Can't open correct/expected result file $correct_fname ($!)";
$_ = <R>;
my @m;
my @diploid;
my @haploid12;
my @haploid21;
my $n = 0;
while(my $output_line = <F>) {
chomp $output_line;
next if $output_line =~ m/^#/;
next if $output_line =~ m/^\s*$/;
my ($chm, $spos, $epos, $sgpos, $egpos, $n_snps, @d) = split/\t/,$output_line;
for(my $j=0; $j < @d; $j++) { $d[$j]++; }
my $i = 0;
while($i < $n_snps) {
my $correct_line = <R>;
last unless $correct_line;
chomp $correct_line;
next if $correct_line =~ m/^#/;
next if $correct_line =~ m/^\s*$/;
my (undef, undef, @r) = split/\t/,$correct_line;
my $s = 0;
for(my $j=0; $j < @r; $j+=2) {
if (($r[$j] == $d[$j] && $r[$j+1] == $d[$j+1]) ||
($r[$j] == $d[$j+1] && $r[$j+1] == $d[$j])) {
$diploid[$s]++;
if ($r[$j] == $d[$j] && $r[$j+1] == $d[$j+1]) {
$haploid12[$s]++;
} else {
$haploid21[$s]++;
}
}
$m[$d[$j]]->[$r[$j]]++;
$m[$d[$j+1]]->[$r[$j+1]]++;
$s++;
}
$i++;
$n++;
}
}
close F;
close R;
my ($diploid_accuracy, $haploid_accuracy) = (0, 0);
my ($da_msq, $ha_msq) = (0, 0);
for(my $i=0; $i < @diploid; $i++) {
$diploid[$i] = 0 unless $diploid[$i];
$haploid12[$i] = 0 unless $haploid12[$i];
$haploid21[$i] = 0 unless $haploid21[$i];
my $d = $diploid[$i] / $n;
my $h1 = $haploid12[$i] / $n;
my $h2 = $haploid21[$i] / $n;
my $h = $h1 > $h2 ? $h1 : $h2;
printf "%s\t%1.1f%%\t%1.1f%%\n", $i, $h*100., $d*100.;
$diploid_accuracy += $d;
$da_msq += $d*$d;
$haploid_accuracy += $h;
$ha_msq += $h*$h;
}
$haploid_accuracy /= @diploid;
$diploid_accuracy /= @diploid;
printf "Haploid accuracy = %1.2f%% +/- %1.2f\n", $haploid_accuracy * 100.,
sqrt($ha_msq/@diploid - $haploid_accuracy*$haploid_accuracy)*100.;
printf "Diploid accuracy = %1.2f%% +/- %1.2f\n", $diploid_accuracy * 100.,
sqrt($da_msq/@diploid - $diploid_accuracy*$diploid_accuracy)*100.;
for(my $j=1; $j < @m; $j++) {
my $d = 0;
for(my $i=1; $i < @m; $i++) {
$m[$i]->[$j] = 0 unless $m[$i]->[$j];
$d += $m[$i]->[$j];
}
for(my $i=1; $i < @m; $i++) {
$m[$i]->[$j] /= $d + 0.1;
}
}
print_matrix(\@m);
printf "Determinant = %1.3f\n", det(\@m);
sub print_matrix {
my ($m) = @_;
my $n = @{$m};
for(my $i=1; $i < $n; $i++) {
print "\t$i";
}
print "\n";
for(my $i=1; $i < $n; $i++) {
printf "$i";
for(my $j=1; $j < $n; $j++) {
printf "\t%5.1f", $$m[$i]->[$j] * 100.;
}
printf "\n";
}
}
sub det {
my ($m) = @_;
my $n = @{$m};
for(my $j=1; $j < $n; $j++) {
for(my $i=$j+1; $i < $n; $i++) {
if ($$m[$j]->[$i] > 1e-15) {
my $s = $$m[$j]->[$j] / $$m[$i]->[$j];
for(my $k=1; $k < $n; $k++) {
$$m[$i]->[$k] -= $$m[$j]->[$k] / $s;
}
}
}
}
#print_matrix($m);
my $d = 1.;
for(my $j=1; $j < $n; $j++) {
$d *= $$m[$j]->[$j];
}
return $d;
}