-
Notifications
You must be signed in to change notification settings - Fork 2
/
hmp2CSS_sw_v2_singles.pl
336 lines (318 loc) · 11.2 KB
/
hmp2CSS_sw_v2_singles.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/perl
#This script takes in a hapmap file and a population label file, and calculates the genetic distance between each pair of samples for a sliding window.
#Eventually it will call an R script to run a PCoA and extract euclidean distances between pairs.
#V2: This version creates 95% confidence intervals around the parental groups and then asks if the samples are within the ellipses and if the ellipses overlap
#Make sure that a sample is not intermediate because it doesn't have any data.
#This version is for running on a single hybrid sample.
use warnings;
use strict;
use File::Basename;
use Cwd 'abs_path';
use List::Util qw(shuffle);
use lib '/home/owens/bin/pop_gen/'; #For GObox server
my ( $name, $path, $suffix ) = fileparse( $0, qr{\.[^.]*$} );
#print "NAME=$name\n";
#print "PATH=$path\n";
#print "SFFX=$suffix\n";
my %t; #Convert from IUPAC to normal
$t{"N"} = "NN";
$t{"A"} = "AA";
$t{"T"} = "TT";
$t{"G"} = "GG";
$t{"C"} = "CC";
$t{"W"} = "AT";
$t{"R"} = "AG";
$t{"M"} = "AC";
$t{"S"} = "CG";
$t{"K"} = "GT";
$t{"Y"} = "CT";
my %flip; #Flip bases so they're always in alphabetical order
$flip{"TA"} = "AT";
$flip{"GA"} = "AG";
$flip{"CA"} = "AC";
$flip{"GC"} = "CG";
$flip{"TC"} = "CT";
$flip{"TG"} = "GT";
my $min_sites = 10; #Minumum number of sites with data for a pair within a window.
my $min_variable_sites = 10; #Number of sites in the dataset in a window. Skips the window if they don't have it. This is before considering missing data.
my $window_size = 1000000; #Number of bases in each sliding window
my $max_null_pairs; #max number of excluded pairs for a sliding window. Calculated as half of possible pairs.
my $permute_number = 200; #Number of permutations.
my $minimum_hybrid_comparisons = 5; #Minumum number of comparisons with each parental for hybrid calculation.
my $popfile = $ARGV[0]; #A population file.
my $out = $ARGV[1]; #outfile prefix
open (FINALOUT, "> $out.final.txt") or die "Could not open a file\n"; #open outfile
open (POPFILE, "> $out.popfile.txt") or die "Could not open a file\n"; #open population file
my %pop;
my %popList;
my %samplepop;
my %samplename;
#Window variables
my $current_chrom;
my $end = $window_size;
open POP, $popfile;
while (<POP>){ #Load in population information linked with sample name
chomp;
my @a = split (/\t/,$_);
$pop{$a[0]}=$a[1];
$popList{$a[1]}++;
}
close POP;
#require "countbadcolumns.pl"; #Identify the number of columns before genotype data starts
#my ($iupac_coding, $badcolumns) = count_bad_columns($infile);
#$. = 0;
my $iupac_coding= "False";
my $badcolumns="11";
#Distance hashes
my %nullcounter;
my %sitecount;
my %distance;
my %took_average_dist;
my $finalcolumn;
my $pop1;
my $pop2;
my $popH;
my $parenttotal;
my %parenthash;
my %hybridhash;
my @hybridarray;
my $variablesites;
while (<STDIN>){
chomp;
my @a = split(/\t/,$_);
if ($. == 1){ #Load in sample names associated with column numbers, as well as population
$finalcolumn = $#a;
print POPFILE "population";
my $number_samples = (($#a - $badcolumns) + 1);
$max_null_pairs = ($number_samples * ($number_samples -1)) / 2;
foreach my $i($badcolumns..$#a){
if ($pop{$a[$i]}){
$samplepop{$i} = $pop{$a[$i]};
$samplename{$i} = $a[$i];
print POPFILE "\n$samplepop{$i}";
if ($samplepop{$i} eq "P1"){
$pop1++;
$parenthash{$i} = $samplepop{$i};
}elsif ($samplepop{$i} eq "P2"){
$pop2++;
$parenthash{$i} = $samplepop{$i};
}elsif ($samplepop{$i} eq "H"){
$popH++;
$hybridhash{$i} = $samplepop{$i};
push (@hybridarray, $i);
}
}
}
$parenttotal = $pop1 + $pop2;
print FINALOUT "chrom\tstart\tend\tn_sites\toverlap";
foreach my $hybrid_number (@hybridarray){
print FINALOUT "\t$samplename{$hybrid_number}";
}
}else{
my $loc = $a[0];
my $chrom = $a[2];
my $pos = $a[3];
unless ($current_chrom){
$current_chrom = $chrom;
}
if ($. == 2){
until($end > $pos){ #move the end point of the window until it is after the current marker.
$end += $window_size;
}
}
if (($current_chrom ne $chrom) or ($end < $pos)){ #If it's starting a new window, do all the calculations
my $start = $end - $window_size;
if ($variablesites < $min_variable_sites){
print FINALOUT "\n$current_chrom\t$start\t$end\tNA\tNA";
foreach my $hybrid_number (@hybridarray){
print FINALOUT "\tNA";
}
goto RESET;
}
print "Calculating distances for $current_chrom, $start to $end\n";
open (OUTFILE, "> $out.$current_chrom.$end.txt") or die "Could not open a file\n"; #open outfile
my $paircount = 0;
my $nullpairs = 0;
my $totaldist = 0;
foreach my $i ($badcolumns..($finalcolumn-1)){ #This is to create the average distance between objects for missing data
foreach my $j (($i+1)..$finalcolumn){
unless ($sitecount{$i}{$j}){
$sitecount{$i}{$j} = 0;
}
if ($sitecount{$i}{$j} <= $min_sites){
$distance{$i}{$j} = "NA"; #Delete pairs without sufficient data within the window.
$nullpairs++;
#print "Too many null on $i $j\n";
}
else{
unless($distance{$i}{$j}){
$distance{$i}{$j} = 0;
}
}
if ($distance{$i}{$j} ne "NA"){
#print "For $i $j nullcounts=$nullcounter{$i}{$j}\n";
#print "For $i $j distance=$distance{$i}{$j}, sitcount=$sitecount{$i}{$j}\n";
$distance{$i}{$j} = ($distance{$i}{$j} / $sitecount{$i}{$j});
if ($i ne $j){
$paircount++;
$totaldist += $distance{$i}{$j} #Add up all distances, not including self distances
}
}
}
}
if ($paircount == "0"){
goto NODATA;
}
#calculate average distance for window
my $average_dist = ($totaldist / $paircount);
#print "$average_dist\t$nullpairs\t$max_null_pairs\n";
if ($nullpairs < $max_null_pairs){
foreach my $i ($badcolumns..$finalcolumn){
foreach my $j ($badcolumns..$finalcolumn){
if ($i eq $j){
$distance{$i}{$j} = 0;
}
unless (defined $distance{$i}{$j}){
$distance{$i}{$j} = $distance{$j}{$i};
}if ($distance{$i}{$j} eq "NA"){
$distance{$i}{$j} = $average_dist;
$took_average_dist{$i}{$j}++;
}if ($j eq $badcolumns){
print OUTFILE "$distance{$i}{$j}";
}else{
print OUTFILE "\t$distance{$i}{$j}";
}
}
print OUTFILE "\n";
}
}else{
NODATA:
print OUTFILE "Too much missing data\n";
close OUTFILE;
my $start = $end - $window_size;
print FINALOUT "\n$current_chrom\t$start\t$end\tNA\tNA";
foreach my $hybrid_number (@hybridarray){
print FINALOUT "\tNA";
}
goto RESET;
}
close OUTFILE;
#Use cmdscale in R to compress to 2 dimensions, then calculate the euclidean distance between each sample.
print "Calculating euclidean distances for $current_chrom, $start to $end\n";
my $cmd = "Rscript $path/MDS_ellipse.R $out.$current_chrom.$end.txt $out.popfile.txt $out.$current_chrom.$end.P1hyb.txt $out.$current_chrom.$end.P2hyb.txt $out.$current_chrom.$end.overlap.txt";
system($cmd);
my @conf_levels = ("0.75", "0.80", "0.85", "0.90", "0.95", "0.99");
open P1HYB, "$out.$current_chrom.$end.P1hyb.txt"; #Load in distance scores from R
my $hybcounter = 0;
my %hyb_conf;
while (<P1HYB>){
chomp;
my @a = split(/\t/, $_);
$hybcounter++;
$hyb_conf{"P1"}{$hybcounter} = "No";
foreach my $i (4){
if ($a[$i] > 0){
$hyb_conf{"P1"}{$hybcounter} = "Yes";
last;
}
}
}
close P1HYB;
my $hybnumber = $hybcounter;
open P2HYB, "$out.$current_chrom.$end.P2hyb.txt"; #Load in distance scores from R
$hybcounter = 0;
while (<P2HYB>){
chomp;
my @a = split(/\t/, $_);
$hybcounter++;
$hyb_conf{"P2"}{$hybcounter} = "No";
foreach my $i (4){
if ($a[$i] > 0){
$hyb_conf{"P2"}{$hybcounter} = "Yes";
last;
}
}
}
close P2HYB;
open OVERLAP, "$out.$current_chrom.$end.overlap.txt"; #Load in distance scores from R
my $overlap = "No";
while (<OVERLAP>){
chomp;
my @a = split(/\t/, $_);
foreach my $i (4){
if ($a[$i] eq "TRUE"){
$overlap = "Yes";
last;
}
}
}
close OVERLAP;
print FINALOUT "\n$current_chrom\t$start\t$end\t$variablesites\t$overlap";
foreach my $i (1..$hybnumber){
if (($hyb_conf{'P1'}{$i} eq "No") and ($hyb_conf{'P2'}{$i} eq "No")){
print FINALOUT "\tNeither";
}elsif (($hyb_conf{'P1'}{$i} eq "No") and ($hyb_conf{'P2'}{$i} eq "Yes")){
print FINALOUT "\tP2";
}elsif (($hyb_conf{'P1'}{$i} eq "Yes") and ($hyb_conf{'P2'}{$i} eq "No")){
print FINALOUT "\tP1";
}elsif (($hyb_conf{'P1'}{$i} eq "Yes") and ($hyb_conf{'P2'}{$i} eq "Yes")){
print FINALOUT "\tBoth";
}
}
#Reset the variables
RESET:
my $cleaner = "rm $out.$current_chrom.$end*";
system($cleaner);
undef %distance;
undef %sitecount;
undef $variablesites;
undef %took_average_dist;
if ($current_chrom ne $chrom){ #If on the next chromosome, reset the end point for the window
$current_chrom = $chrom;
$end = $window_size;
}
until($end > $pos){ #move the end point of the window until it is after the current marker.
$end += $window_size;
}
}
foreach my $i($badcolumns..$#a){
if ($samplepop{$i} eq "H"){
if ($a[$i] eq "NN"){
goto SKIP;
}
}
}
$variablesites++;
foreach my $i($badcolumns..$#a){ #If IUPAC coding used, convert to paired nucleotide.
if ($iupac_coding eq "TRUE"){
$a[$i] = $t{$a[$i]};
}
else{
if ($flip{$a[$i]}){
$a[$i] = $flip{$a[$i]};
}
}
}
foreach my $i($badcolumns..$#a){
foreach my $j ($i..$#a){
if (($a[$i] eq "NN") or ($a[$j] eq "NN")){ #If either is missing, count as missing for that pair.
#nothing happens
}else{
my @tmp1 = split('',$a[$i]);
my @tmp2 = split('',$a[$j]);
if ($a[$i] eq $a[$j]){ #If they're identical, don't add to the distance but do add to the site count.
$distance{$i}{$j} += 0;
$sitecount{$i}{$j}++;
}elsif (($tmp1[0] eq $tmp2[0]) or ($tmp1[0] eq $tmp2[1]) or ($tmp1[1] eq $tmp2[0]) or ($tmp1[1] eq $tmp2[1])){ #If any have any matches, they are 0.5 distance away.
$sitecount{$i}{$j}++;
$distance{$i}{$j} += 0.5;
}else{
$sitecount{$i}{$j}++;
$distance{$i}{$j}++;
}
}
}
}
}
SKIP:
}