-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstructure2newhyb.pl
More file actions
executable file
·378 lines (330 loc) · 7.96 KB
/
structure2newhyb.pl
File metadata and controls
executable file
·378 lines (330 loc) · 7.96 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
my %opts;
getopts('s:o:a:b:x:r:h', \%opts);
if ($opts{h}){
&help;
die "Exiting because help menu was called.\n\n"
}
my ($str, $out, $A, $B, $X, $rand) = &parseArgs(\%opts);
#Arrays of pop IDs for each class
my @popA = split /\+/, $A;
my %mapA = map {$_ => 1} @popA;
my @popB = split /\+/, $B;
my %mapB = map {$_ => 1} @popB;
my @popX = split /\+/, $X;
my %mapX = map {$_ => 1} @popX;
print "Parental population 1: ";
foreach (@popA){
print $_, " ";
}
print "\n";
print "Parental population 2: ";
foreach (@popB){
print $_, " ";
}
print "\n";
print "Admixed population(s): ";
foreach (@popX){
print $_, " ";
}
print "\n\n";
#Instantiate hashes to hold the data
my %dataA;
my %dataB;
my %dataX;
my $numLoci;
print "Parsing structure file... ";
open (my $fhs, $str) or die "Can't open $str\n";
my @line1 = ();
my @line2 = ();
while (my $row = <$fhs>){
chomp $row;
if (!@line1){
@line1 = split "\t", $row;
if (!$numLoci){
$numLoci = (scalar(@line1)-2);
}else{
if ($numLoci != (scalar(@line1)-2)){
die "Number of columns in each row not the same.\n"
}
}
next;
}elsif(!@line2){
@line2 = split "\t", $row;
if ($numLoci != (scalar(@line2)-2)){
die "Number of columns in each row not the same.\n"
}
if ($line1[0] ne $line2[0]){
die "$line1[0] not equal to $line2[0] something is wrong!\n";
}
}else{
@line1 = ();
@line2 = ();
@line1 = split "\t", $row;
next;
}
my $sample = $line2[0];
my $pop = $line2[1];
if (exists($mapA{$pop})){
$dataA{$sample} = &fetchNewHybLine(\@line1, \@line2);
}elsif (exists($mapB{$pop})){
$dataB{$sample} = &fetchNewHybLine(\@line1, \@line2);
}elsif (exists($mapX{$pop})){
$dataX{$sample} = &fetchNewHybLine(\@line1, \@line2);
}else{
@line1 = ();
@line2 = ();
next;
}
}
close $fhs;
print ("Done.\n");
#Check that all hashes have data.
%dataA or die "Error: No data for parental population 1!\n";
%dataB or die "Error: No data for parental population 2!\n";
%dataX or die "Error: No data for admixed population!\n";
print ("Removing loci with no data for any population... ");
my @blacklist;
for (my $k = 0; $k < $numLoci; $k++){
#Population A
my $bad = 1;
for my $key (keys %dataA){
if ($dataA{$key}->[$k] != 0){
$bad = 0;
}
}
if ($bad == 1){
push @blacklist, $k;
}
#Population B
$bad = 1;
for my $key (keys %dataB){
if ($dataB{$key}->[$k] != 0){
$bad = 0;
}
}
if ($bad == 1){
push @blacklist, $k;
}
#Population X
$bad = 1;
for my $key (keys %dataX){
if ($dataX{$key}->[$k] != 0){
$bad = 0;
}
}
if ($bad == 1){
push @blacklist, $k;
}
}
#Removing monomorphic loci
print "Removing monomorphic loci... ";
my $countM = 0;
for (my $k = 0; $k < $numLoci; $k++){
#Population A
my $var = 0;
my $obs = 0;
for my $key (keys %dataA){
if ($obs != 0){
$obs = $dataA{$key}->[$k];
}
if ($obs != $dataA{$key}->[$k]){
$var = 1;
}
}
if ($var == 1){
next;
}
#Population B
for my $key (keys %dataB){
if ($obs != 0){
$obs = $dataB{$key}->[$k];
}
if ($obs != $dataB{$key}->[$k]){
$var = 1;
}
}
if ($var == 1){
next;
}
#Population X
for my $key (keys %dataX){
if ($obs != 0){
$obs = $dataX{$key}->[$k];
}
if ($obs != $dataX{$key}->[$k]){
$var = 1;
}
}
if ($var == 0){
$countM++;
push @blacklist, $k;
}
}
print "Done.\n";
my %seen;
@seen{@blacklist} = ();
my $numLost = scalar(keys %seen);
print "$numLost loci total loci dropped for missing data, or lacking variation.\n";
#Check if random sampling selected
my %skip;
if ($rand){
my $tot = $numLoci - $numLost;
if ($tot < $rand){
print "Warning: You chose to randomly sample $rand loci, but only $tot loci remain. Skipping random sampling.\n";
%skip = %seen;
}else{
print "Randomly sampling $rand loci... ";
%skip = randomSampleLoci($numLoci, \%seen, $rand);
my $len = scalar(keys %skip);
#print "There are $len loci to skip of $numLoci total...\n";
print "Done.\n";
}
}else{
%skip = %seen;
}
print ("Writing NewHybrids outputs with prefix \"$out\"... ");
my $ind = $out . "_individuals.txt";
my $dat = $out . ".txt";
open (my $ifh, ">$ind") or die "Can't open $ind for writing\n";
open (my $dfh, ">$dat") or die "Can't open $dat for writing\n";
my $numInds = scalar(keys %dataA) + scalar(keys %dataB) + scalar (keys %dataX);
#Print header to data file
my $keptLoci = $numLoci - scalar(keys %skip);
print $dfh "NumIndivs $numInds\n";
print $dfh "NumLoci $keptLoci\n";
print $dfh "Digits 1\n";
print $dfh "Format Lumped\n";
print $dfh "LocusNames";
for (my $i = 0; $i < $numLoci; $i++){
print $dfh " Locus$i" unless exists $skip{$i};
}
print $dfh "\n";
#Print values for Pop A
my $indCount = 1;
for my $key (keys %dataA){
print $ifh "Pure_PopA_$key\n";
print $dfh "$indCount";
my $index = 0;
for my $loc (@{$dataA{$key}}){
print $dfh " $loc" unless exists $skip{$index};
$index++;
}
print $dfh "\n";
$indCount++;
}
#Values for pop B
for my $key (keys %dataB){
print $ifh "Pure_PopB_$key\n";
print $dfh "$indCount";
my $index = 0;
for my $loc (@{$dataB{$key}}){
print $dfh " $loc" unless exists $skip{$index};
$index++;
}
print $dfh "\n";
$indCount++;
}
#Values for pop X
for my $key (keys %dataX){
print $ifh "Admix_$key\n";
print $dfh "$indCount";
my $index = 0;
for my $loc (@{$dataX{$key}}){
print $dfh " $loc" unless exists $skip{$index};
$index++;
}
print $dfh "\n";
$indCount++;
}
close $ifh;
close $dfh;
print "Done.\n\n";
exit;
###############################################################################
################################ Subroutines ##################################
###############################################################################
# subroutine to print help
sub help{
print "\nPerl script to convert 2-line format Structure file to inputs for NewHybrids. Structure format should have no extra columns or rows, and 1 column with a population identifier.\n\n";
print "Author: Tyler K. Chafin\n";
print "Program Options:\n";
print "\t-s:\tTab-delimited Structure file\n";
print "\t-o:\tPrefix for output files [default=newhyb]\n";
print "\t-a:\tInteger identifier(s) for Parent population 1 [Multiple as: -a 1+2+3]\n";
print "\t-b:\tInteger identifier(s) for Parent population 2 [Multiple as: -b 1+2+3]\n";
print "\t-x:\tInteger identifier(s) for admixed population [Multiple as: -x 1+2+3]\n";
print "\t-r:\tInteger to randomly sample from loci [default=not used]\n";
print "\t-h:\tBoolean. Calls help menu.\n\n";
}
# subroutine to parse the command line options
sub parseArgs{
my( $params ) = @_;
my %opts = %$params;
my $s = $opts{s} or die "Structure file not given\n";
my $o = $opts{o} || "newhyb";
my $a = $opts{a} or die "Identifiers for parent 1 not given\n";
my $b = $opts{b} or die "Identifiers for parent 2 not given\n";
my $r = $opts{r} || "";
my $x = $opts{x} or die "Identifiers for admixed pop not given\n";
return( $s, $o, $a, $b, $x, $r);
}
# subroutine to parse the command line options
sub fetchNewHybLine{
my $line1 = $_[0];
my $line2 = $_[1];
my @export;
my $counter = 0;
for my $col (@{$line1}){
if ($counter < 2){
$counter++;
next;
}
my $col_s = NHsanitize($col);
my $other = $line2->[$counter];
my $other_s = NHsanitize($other);
#$other =~ s/-9/0/g;
my $nh = $col_s . $other_s;
push @export, $nh;
$counter++;
}
return (\@export);
}
sub NHsanitize{
my $s = $_[0];
my $new = $s;
$new =~ s/0/\!/g;
$new =~ s/1/\@/g;
$new =~ s/2/\#/g;
$new =~ s/3/\$/g;
$new =~ s/-9/0/g;
$new =~ s/\!/1/g;
$new =~ s/\@/2/g;
$new =~ s/\#/3/g;
$new =~ s/\$/4/g;
return ($new);
}
# subroutine to randomly sample X loci to keep, and return new blacklist
sub randomSampleLoci{
my $total = $_[0];
my $blacklist = $_[1];
my $sample = $_[2];
my %new_blacklist = %$blacklist;
my %kept;
my $pick;
while (scalar(keys %kept) < $sample){
$pick = int(rand($total))+1;
if (!$new_blacklist{$pick}){
$kept{$pick} = undef;
}
}
for (my $i=1;$i<=$total; $i++){
if (!exists $kept{$i}){
$new_blacklist{$i} = undef;
}
}
return (%new_blacklist);
}