-
Notifications
You must be signed in to change notification settings - Fork 0
/
paf2euclideandist
executable file
·151 lines (122 loc) · 2.55 KB
/
paf2euclideandist
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
#!/usr/bin/perl
use warnings;
use strict;
use fralib;
use Getopt::Long;
use Cwd;
use File::Basename;
use Pod::Usage;
use DBI;
use POSIX;
=head1 NAME
paf2euclideandist
=head1 SYNOPSIS
paf2euclideandist [options] <paf-file>
-h help
paf-file population allele frequency file
example: paf2euclideandist pscalare.paf
Calculates the Euclidean distance between populations.
Note that the populations are sorted in the output gd-file.
=head1 DESCRIPTION
=cut
my $help;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions ('h'=>\$help) || scalar(@ARGV) != 1)
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
foreach my $file (@ARGV)
{
open(IN, $file) || die "Cannot open $file\n";
my $lineno = 0;
my @line;
my %POP_SNP_FREQ;
my @col2pop;
my @row2snp;
my @POP;
my @SNP;
my %POP_GD;
#reads in population allele frequency information
while (<IN>)
{
$lineno++;
s/\r?\n?$//;
@line = split("\t");
if ($lineno==1)
{
#reads in the populations
foreach my $col (1 .. $#line)
{
$col2pop[$col] = $line[$col];
$POP_SNP_FREQ{$line[$col]} = ();
$POP_GD{$line[$col]} = ();
}
@POP = sort(keys(%POP_SNP_FREQ));
}
else
{
my $snp = $line[0];
$row2snp[$lineno] = $snp;
#reads in the population allele frequencies
foreach my $col (1 .. $#line)
{
$POP_SNP_FREQ{$col2pop[$col]}{$snp} = $line[$col];
}
}
}
@SNP = sort(keys(%{$POP_SNP_FREQ{$POP[0]}}));
my $pop1A;
my $pop2A;
my $pop1B;
my $pop2B;
my $SGM;
my $GD;
my $invalid_snps;
for (my $pop1=0; $pop1<=$#POP; $pop1++)
{
for (my $pop2=$pop1; $pop2<=$#POP; $pop2++)
{
$SGM = 0;
$invalid_snps = 0;
foreach my $snp (@SNP)
{
$pop1A = $POP_SNP_FREQ{$POP[$pop1]}{$snp};
$pop2A = $POP_SNP_FREQ{$POP[$pop2]}{$snp};
if ($pop1A != -1 && $pop2A != -1)
{
$SGM += ($pop1A > $pop2A)? $pop1A - $pop2A : $pop2A - $pop1A;
}
else
{
$invalid_snps++;
}
}
$GD = $SGM;
$GD /= scalar(@SNP)- $invalid_snps;
$POP_GD{$POP[$pop1]}{$POP[$pop2]} = $GD;
}
}
my ($name, $dir, $ext) = fileparse($file, '\..*');
my $distanceFile = "euclidean-dist-$name.gd";
open(OUT, ">$distanceFile") || die "Cannot open $distanceFile\n";
my $header = join("\t", @POP);
print OUT "\t$header\n";
for (my $pop1=0; $pop1<=$#POP; $pop1++)
{
print OUT $POP[$pop1];
for (my $pop2=0; $pop2<=$pop1; $pop2++)
{
printf(OUT "\t%2.10f", $POP_GD{$POP[$pop2]}{$POP[$pop1]});
}
print OUT "\n";
}
close(OUT);
}