-
Notifications
You must be signed in to change notification settings - Fork 10
/
calc_pop_divs.pl
178 lines (132 loc) · 4.06 KB
/
calc_pop_divs.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/perl -w
#
# calc_pop_divs.pl -- Calculating population pi values.
#
#
# Author: Nowind
# Created: 2012-05-31
# Updated: 2016-10-05
# Version: 1.1.0
#
# Change logs:
# Version 1.0.0 16/02/10: The initial version.
# Version 1.1.0 16/10/05: Updated: add option "--all-pairs" to output corrected diversities for all pairs.
use strict;
use Data::Dumper;
use Getopt::Long;
use File::Find::Rule;
use File::Basename;
use MyPerl::FileIO qw(:all);
################################# Main ###############################
my $CMDLINE = "perl $0 @ARGV";
my $VERSION = '1.1.0';
my $HEADER = "##$CMDLINE\n##Version: $VERSION\n";
my $min_info_perc = 50;
my ($query_file, $subject_file, $output, $out_all_pairs);
GetOptions(
"query=s" => \$query_file,
"subject=s" => \$subject_file,
"output=s" => \$output,
"min-info-perc=f" => \$min_info_perc,
"all-pairs" => \$out_all_pairs,
);
unless( $query_file && $subject_file ) {
print <<EOF;
$0 -- query fasta sequences
Version: $VERSION
Usage: perl $0 [options]
Options:
-q, --query <filename>
nucleotide differences between each pairs, required
-s, --subject <filename>
informative sites between each pairs required, required
-o, --output <filename>
output file, default to STDOUT
-m, --min-info-perc <float>
windows with informative sites less than this percentage will be
discarded from calculation [default: 50]
-a, --all-pairs
output corrected diversities for all pairs
EOF
exit(1);
}
$|++;
print STDERR "# $0 v$VERSION\n# " . (scalar localtime()) . "\n";
if ($output) {
open (STDOUT, "> $output") || die $!;
}
##
## read and parsing query file
##
print STDERR ">> Start parsing in $query_file ... ";
my %pair_diffs = ();
my @query_pair_ids = ();
my $fh1 = getInputFilehandle($query_file);
while (<$fh1>)
{
next if (/\#\#/ || /^\s+$/);
my ($bin_id, $chrom, $bin_start, $bin_end, @pairs) = (split /\s+/);
if (/^\#BIN_ID/) {
for (my $i=0; $i<@pairs; $i++)
{
push @query_pair_ids, $pairs[$i];
}
next;
}
for (my $i=0; $i<@pairs; $i++)
{
$pair_diffs{"$chrom\t$bin_start\t$bin_end"}->{$query_pair_ids[$i]} = $pairs[$i] * ($bin_end - $bin_start + 1);
}
}
print STDERR "done!\n";
##
## retrieving query records in subject file
##
print STDERR ">> Start parsing $subject_file ... ";
print STDOUT "$HEADER##" . (scalar localtime()) . "\n";
unless ($out_all_pairs) {
print STDOUT "#BIN_ID\tCHROM\tBIN_START\tBIN_END\tNo_Of_Pairs\tPop_Divers\n";
}
my @sub_pair_ids = ();
my $fh2 = getInputFilehandle($subject_file);
while (<$fh2>)
{
next if (/\#\#/ || /^\s+$/);
my ($bin_id, $chrom, $bin_start, $bin_end, @pairs) = (split /\s+/);
if (/^\#BIN_ID/) {
if ($out_all_pairs) {
print STDOUT;
}
for (my $i=0; $i<@pairs; $i++)
{
push @sub_pair_ids, $pairs[$i];
}
next;
}
my $bin_size = $bin_end - $bin_start + 1;
my %pair_divs = ();
$pair_divs{sum} = 0;
$pair_divs{num} = 0;
for (my $i=0; $i<@pairs; $i++)
{
if (100 * $pairs[$i] / $bin_size >= $min_info_perc) {
my $divs = $pair_diffs{"$chrom\t$bin_start\t$bin_end"}->{$sub_pair_ids[$i]} / $pairs[$i];;
$pair_divs{sum} += $divs;
$pair_divs{num} ++;
push @{$pair_divs{all}}, $divs;
}
else {
push @{$pair_divs{all}}, -1;
}
}
my $out_divs = "$pair_divs{num}\t";
if ($out_all_pairs) {
$out_divs = join "\t", @{$pair_divs{all}};
}
else {
$out_divs .= $pair_divs{num} > 0 ? $pair_divs{sum} / $pair_divs{num} : -1;
}
print "$bin_id\t$chrom\t$bin_start\t$bin_end\t$out_divs\n";
}
print STDERR "done!\n";
print STDERR "# " . (scalar localtime()) . "\n";