-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathphylip2ecoevolity.pl
executable file
·710 lines (609 loc) · 17.7 KB
/
phylip2ecoevolity.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
#! /usr/bin/perl
# Contributions by Tyler K. Chafin, Steven M. Mussmann, Max R. Bangs
# Contact: tkchafin@uark.edu
use strict;
use warnings;
use Getopt::Std;
use Data::Dumper;
use List::Util 'shuffle';
#Die if no arguments given
if( scalar( @ARGV ) == 0 ){
&help;
die "No options given\n\n";
}
#Parse arguments
my %opts;
getopts( 'p:i:1:2:sn:N:gPo:hSO:r:', \%opts );
# kill if help option is true
if( $opts{h} ){
&help;
die "Help menu\n\n";
}
#get options
my ($map, $phy, $pop1, $pop2, $threshold, $globalThresh, $gapFalse, $phyNew, $out, $same, $skip, $prefix, $randind) = &parseArgs(\%opts);
#Extract pops into an array
my @pop1list = split(/\+/,$pop1);
my @pop2list = split(/\+/,$pop2);
my $a1 = join(', ', @pop1list);
my $a2 = join(', ', @pop2list);
# hash for loci with too much missing data
my %blacklist;
#parse popmap file into a hash with ind as key and popID as value
my $assignRef = &parsePopmap($map);
#parse phylip file into a hash with ind as key and array of seqs as value
my ($allRef, $ntax, $nchar) = &parsePhylip($phy);
#Print argument report
print "\nPopmap file is: $map\n";
print "Phylip file is: $phy\n";
if (scalar @pop2list <= 0){
print "Selected populations (1 pop model): $a1\n";
}else{
print "Selected populations (2 pop model): $a1 and $a2\n";
}
print "Total taxa in phylip file: $ntax\n";
print "Total characters in phylip data matrix: $nchar\n";
if ($randind > 0){
print "Subsampling a total of $randind individuals per population.\n";
}
print "\n";
#Get pop alignments only with ind as key and array of seqs as value
my $pop1Ref = &getMultPops($assignRef, $allRef, $randind, \@pop1list);
my $pop2Ref = "";
if (scalar @pop2list > 0){
$pop2Ref = &getMultPops($assignRef, $allRef, $randind, \@pop2list);
}
# calculate missing data in admixed population
#&calcMissing($popaRef, \%blacklist);
if ($gapFalse == 1){
&getBlacklist($threshold, scalar(@pop2list), \%blacklist, $pop1Ref, $pop2Ref);
}else{
&getBlacklistGap($threshold, scalar(@pop2list), \%blacklist, $pop1Ref, $pop2Ref);
}
my $nFail = (keys %blacklist);
print($nFail ," loci had greater than ",$threshold, " missing data. Removing them.\n");
#print Dumper(\%blacklist);
#Check if pops contain data
my $num1 = keys %{$pop1Ref};
my $num2 = keys %{$pop2Ref};
if ($num1< 1){
die "Error: No individuals were found <$a1>!\n\n";
}else{
print "Found <$num1> individuals in selected pops\n";
}
if ($num2< 1){
die "Error: No individuals were found <$a2>!\n\n";
}else{
print "Found <$num2> individuals in selected pops\n";
}
my $indnum = 0;
my $locnum = 0;
for (my $loc = 0; $loc < $nchar; $loc++){
if(!exists $blacklist{$loc+1}){
$locnum++;
}
}
foreach my $ind (keys %{$pop1Ref}){
$indnum++;
}
foreach my $ind (keys %{$pop2Ref}){
$indnum++;
}
if ($phyNew){
my $outphy = $out . ".phy";
print("Writing new PHYLIP file $outphy\n");
open (PHY, ">", $outphy);
print PHY $indnum, " ", $locnum, "\n";
#print data for P1
foreach my $ind (sort keys %{$pop1Ref}){
#print $ind, "\n";
if ($skip == 1){
print PHY $ind, "\t";
}else{
print PHY "$prefix","Pop1_",$ind, "\t";
}
for (my $l = 0; $l < $nchar; $l++){
if(!exists $blacklist{$l+1}){
print PHY ${$pop1Ref->{$ind}}->[$l];
}
}
print PHY "\n";
}
if (scalar @pop2list > 0){
#print data for P1
foreach my $ind (sort keys %{$pop2Ref}){
#print $ind, "\n";
if ($skip == 1){
print PHY $ind, "\t";
}else{
if ($same == 1){
print PHY "$prefix","Pop1-",$ind, "\t";
}else{
print PHY "$prefix","Pop2-",$ind, "\t";
}
}
for (my $l = 0; $l < $nchar; $l++){
if(!exists $blacklist{$l+1}){
print PHY ${$pop2Ref->{$ind}}->[$l];
}
}
print PHY "\n";
}
}
close PHY;
}
my $outnex = $out . ".nex";
print("Writing new NEXUS file $outnex\n");
open( OUT, '>', $outnex ) || die "Error\nCan't write to $outnex\n";
print OUT "#NEXUS\n\n";
# print OUT "BEGIN TAXA;\n";
# print OUT "DIMENSIONS NTAX=$indnum;\n\n";
# print OUT "TAXLABELS\n";
# foreach my $ind (sort keys %{$pop1Ref}){
# #print $ind, "\n";
# if ($skip == 1){
# print OUT " $ind";
# }else{
# print OUT " Pop1_$ind";
# }
# print OUT "\n";
# }
# if (scalar @pop2list > 0){
# #print data for P1
# foreach my $ind (sort keys %{$pop2Ref}){
# #print $ind, "\n";
# if ($skip == 1){
# print OUT " $ind";
# }else{
# if ($same == 1){
# print OUT " Pop1_$ind";
# }else{
# print OUT " Pop2_$ind";
# }
# }
# print OUT "\n";
# }
# }
# print OUT ";\nEND;\n\n";
print OUT "BEGIN DATA;
DIMENSIONS NTAX=$indnum NCHAR=$locnum;
FORMAT DATATYPE=DNA MISSING=? GAP=- INTERLEAVE=NO;
MATRIX\n\n";
#print data for P1
foreach my $ind (sort keys %{$pop1Ref}){
#print $ind, "\n";
if ($skip == 1){
print OUT " $ind ";
}else{
print OUT " ",$prefix,"Pop1_$ind ";
}
for (my $l = 0; $l < $nchar; $l++){
if(!exists $blacklist{$l+1}){
if (${$pop1Ref->{$ind}}->[$l] eq "N"){
print OUT "?";
}else{
print OUT ${$pop1Ref->{$ind}}->[$l];
}
}
}
print OUT "\n";
}
if (scalar @pop2list > 0){
#print data for P1
foreach my $ind (sort keys %{$pop2Ref}){
#print $ind, "\n";
if ($skip == 1){
print OUT " $ind ";
}else{
if ($same == 1){
print OUT " ",$prefix,"Pop1_$ind ";;
}else{
print OUT " ",$prefix,"Pop2_$ind ";;
}
}
for (my $l = 0; $l < $nchar; $l++){
if(!exists $blacklist{$l+1}){
if (${$pop2Ref->{$ind}}->[$l] eq "N"){
print OUT "?";
}else{
print OUT ${$pop2Ref->{$ind}}->[$l];
}
}
}
print OUT "\n";
}
}
print OUT ";\n";
print OUT "END;\n\n";
close OUT;
exit 0;
########################### SUBROUTINES ###############################
sub help{
print "\nphylip2ecoevolity.pl\n";
print "\nThis script converts from phylip format to the NEXUS input for Ecoevolity (Oaks, 2019)\n";
print "A population map should be given in a tab-delimited file, formatted as:\n";
print "\n\tSampleName\tPopID\n\n";
print "Where PopID can be a string or integer, and SampleName must exactly match a corresponsing line in the phylip file. Any samples not in the popmap will not be included in the output files.\n\n";
print "Options:\n";
print "\t-p : Path to popmap file (tab-delimited)\n";
print "\t-1 : Identifier for populations to include in output (include multiple as: pop1+pop2)\n";
print "\t-2 : Identifier for populations to include in output, if using a 2-pop comparison\n";
print "\t-s : Toggle on to use the same population identifier for all output individuals.\n";
print "\t-S : Toggle on to skip adding population identifiers\n";
print "\t-i : Path to input file (phylip)\n";
print "\t-n : Proportion missing data allowed per pop per column (default=0.1)\n";
print "\t-N : Proportion missing data allowed globally per column (default=0.1)\n";
print "\t-g : Toggle on to TURN OFF default behavior of treating gaps as missing data\n";
print "\t-P : Toggle on to output a new phylip file with the filtered data. [default=off]\n";
print "\t-O : Output prefix for taxon population labels. [default=\"\"]\n";
print "\t\t--Inds will be labeles as: <prefix>Pop1_Ind1, <prefix>Pop1_Ind2, etc...\n";
print "\t-r : Random sample n individuals per population.\n";
print "\t\t--Note this referes to the aggregated population provided for each of <-1> or <-2>\n";
print "\t-o : Output prefix [default = \"ee_out\"]\n";
print "\t-h : Displays this help message\n";
print "\n\n";
}
#parse arguments
sub parseArgs{
my( $params ) = @_;
my %opts = %$params;
#defaults
my $map = $opts{p} or die "\nPopmap not specified.\n\n";
my $phy = $opts{i} or die "\nPhylip file not specified.\n\n";
my $pops1 = $opts{1} or die "\nNo population specified.\n\n";
my $pops2 = "";
$pops2 = $opts{2};
my $threshold = $opts{n} || 0.1;
my $globalThresh = $opts{N} || 0.1;
my $gapFalse = 0;
my $phyNew = 0;
my $mono = 0;
$opts{m} and $mono = 1;
my $bi = 0;
$opts{b} and $bi = 1;
$opts{g} and $gapFalse = 1;
$opts{P} and $phyNew = 1;
my $out = $opts{o} || "ee_out";
my $prefix = $opts{O} || "";
my $same = 0;
my $skip = 0;
my $randind = $opts{r} || 0;
$opts{s} and $same = 1;
$opts{S} and $skip = 1;
#return
return ($map, $phy, $pops1, $pops2, $threshold, $globalThresh, $gapFalse, $phyNew, $out, $same, $skip, $prefix, $randind);
}
#parse popmap file
sub parsePopmap{
my $toParse = $_[0];
#vars
my %toReturn;
#open popmap
open (POP, $toParse) or die "Oh no! Cannot open $toParse: $!\n";
while (my $line = <POP>){
chomp $line;
#ignore if blank
if( $line =~ /^\w/ ){
my @temp = split( /\s+/, $line);
#push into our hash
$toReturn{$temp[0]} = $temp[1];
}
}
close POP;
return( \%toReturn);
}
#parse phylip file -> This version returns array refs, not strings, of sequences
sub parsePhylip{
my $toParse = shift(@_);
#vars
my %toReturn;
my @seq;
my $ntax;
my $nchar;
#open popmap
open (PHY, $toParse) or die "Oh no! Cannot open $toParse: $!\n";
my $num = 0;
while (my $line = <PHY>){
$num++;
chomp $line;
#Skip first line
if ($num == 1){
my @temp = split( /\s+/, $line);
$ntax = $temp[0];
$nchar = $temp[1];
next;
}
#ignore if blank
if( $line =~ /^\w/ ){
my @temp = split( /\s+/, $line);
my @arr = split(//, $temp[1]);
#push array ref into our hash
$toReturn{$temp[0]} = \@arr;
}
}
close PHY;
return( \%toReturn, $ntax, $nchar);
}
#Get alignments for only populations of interest
sub getPops{
my $pops = $_[0];
my $seqs = $_[1];
my $first = $_[2];
my $second = $_[3];
my %pop1;
my %pop2;
foreach my $key (keys %{$pops}){
#If pop ID matches
if ($pops->{$key} eq $first){
${$pop1{$key}} = $seqs->{$key};
}elsif ($pops->{$key} eq $second){
${$pop2{$key}} = $seqs->{$key};
}
}
return(\%pop1, \%pop2);
}
#Modified getPops subroutine, gets all pops matching array of options, returns as one hash
sub getMultPops{
my $popRef = $_[0];
my $seqRef = $_[1];
my $sample = $_[2];
my $toGetRef = $_[3];
my %pop;
my $selected = 0;
foreach my $key (shuffle keys %{$popRef}){
#If pop ID matches, get sequence
if (grep( /^$popRef->{$key}$/, @{$toGetRef})){
if (exists $seqRef->{$key}){
${$pop{$key}} = $seqRef->{$key};
$selected++;
if ($sample > 0 && $selected >= $sample){
return(\%pop);
}
}else{
print "Warning: Sample <$key> was not found in sequence file. Skipping.\n";
}
}
}
return(\%pop);
}
# subroutine to put sequence alignment into a hash with the index value of the alignment as the key and a string of nucleotides at that index as the value
# modified from a subroutine steve wrote
sub getColumns{
my( $hashref ) = @_;
my %align; # hash of arrays to hold position in alignment (hash key) and all characters at that position in alignment (value)
#For each individual
foreach my $key( sort keys %{ $hashref } ){
my $index = 0;
my @seq = split( //, ${$hashref->{$key}} );
#for each nucleotide
foreach my $item( @seq ){
$align{$index} .= $item;
$index++;
}
}
return( \%align );
}
#Subroutine to parse the alignment
sub parsePopAlignment{
my $p1 = $_[0];
my $p2 = $_[1];
my $thresholdN = $_[2];
my $thresholdG = $_[3];
my @blacklist;
#To track fixed alleles in each pop
my $alleles1 = parseColumn($p1, $thresholdN, $thresholdG, \@blacklist);
my $alleles2 = parseColumn($p2, $thresholdN, $thresholdG, \@blacklist);
#Make sure both pops have same number of columns
if ((scalar(@{$alleles1})) != (scalar(@{$alleles1}))){
die "\nError: Y ur populations have not same sequence leNGTH???\n\n";
}else{
#Only keep loci which are differentially fixed
#Make sure to check anything fixed in pop1 is different
#from fixed in pop2
for(my $i=0; $i < scalar(@{$alleles1}); $i++){
my $check1 = $alleles1->[$i] =~ tr/NV-/NV-/;
my $check2 = $alleles2->[$i] =~ tr/NV-/NV-/;
#If either pop was variable, or fixed for gaps or Ns
if ($check1 > 0 || $check2 > 0){
next;
}else{
#If both fixed for same allele
if ($alleles1->[$i] eq $alleles2->[$i]){
push(@blacklist, $i);
next;
}
}
}
}
return(\@blacklist);
}
# subroutine to remove columns from an alignment, given the alignment contained in a hash and an array of positions in each value to be removed
sub removeColumns{
my( $hashref, $remove ) = @_;
my @blacklist = uniq($remove);
# replace columns to be removed with a special character
foreach my $key( sort keys %{ $hashref } ){
foreach my $item( @blacklist ){
substr(${$hashref}{$key}, $item, 1) = 'z';
}
}
# replace the special characters with nothing
foreach my $key( sort keys %{ $hashref } ){
${$hashref}{$key} =~ s/z//g;
}
}
sub uniq {
my @arr = @{$_[0]};
my %seen;
grep !$seen{$_}++, @arr;
}
# subroutine to print data out to a phylip file
sub phyprint{
my( $out, $hashref ) = @_;
# get the number of sequences
my $seqs = scalar keys %$hashref;
# get the length of the alignment
my $alignlength;
foreach my $key( sort keys %{ $hashref } ){
$alignlength = length( ${$hashref}{$key} );
}
# get the length of the longest
my $keylength = 0;
foreach my $key( sort keys %{ $hashref } ){
my $temp = length( $key );
if( $temp > $keylength ){
$keylength = $temp;
}
}
# open the output file for printing
open( OUT, '>', $out ) or die "Can't open $out: $!\n\n";
# print the first line to the phylip file
print OUT "$seqs $alignlength\n";
# print the hash
foreach my $key( sort keys %{ $hashref } ){
my $templength = length( $key );
my $whitespace = ( ( $keylength + 2 ) - $templength );
print OUT $key;
for( my $i=0; $i<$whitespace; $i++ ){
print OUT " ";
}
print OUT ${$hashref}{$key}, "\n";
}
# close the output file
close OUT;
}
sub calcMissing{
my( $hashref, $blacklistref ) = @_;
foreach my $ind( sort keys %$hashref ){
my $counter = 0;
foreach my $locus( @${$$hashref{$ind}} ){
$counter++;
if($locus eq "N"){
$$blacklistref{$counter}++;
}else{
$$blacklistref{$counter}+=0;
}
}
}
}
sub getBlacklist{
my( $thresh, $p2, $blacklistref, $p1ref, $p2ref) = @_;
my $globalInds = 0;
my %globalCount;
#check loci in admixed
my %ncount;
my $inds1;
foreach my $ind( sort keys %$p1ref ){
$inds1++;
$globalInds++;
my $counter = 0;
foreach my $locus( @${$$p1ref{$ind}} ){
$counter++;
$ncount{$counter} = 0 unless exists $ncount{$counter};
$globalCount{$counter} = 0 unless exists $globalCount{$counter};
if($locus eq "N"){
$ncount{$counter}++;
$globalCount{$counter}++;
}
}
}
#blacklist any loci with too high n proportion
foreach my $loc(sort keys %ncount){
if (($ncount{$loc} / $inds1) > $threshold){
$$blacklistref{$loc} = ($ncount{$loc} / $inds1) unless exists $$blacklistref{$loc};
#print("Locus:",$loc," - Missing data: ",($nAcount{$loc} / $admixinds), "\n");
}
}
if ($p2 > 0){
my %ncount2;
my $inds2;
foreach my $ind( sort keys %$p2ref ){
$inds2++;
$globalInds++;
my $counter = 0;
foreach my $locus( @${$$p2ref{$ind}} ){
$counter++;
$ncount2{$counter} = 0 unless exists $ncount2{$counter};
$globalCount{$counter} = 0 unless exists $globalCount{$counter};
if($locus eq "N"){
$ncount2{$counter}++;
$globalCount{$counter}++;
}
}
}
#blacklist any loci with too high n proportion
foreach my $loc(sort keys %ncount2){
if (($ncount2{$loc} / $inds2) > $threshold){
$$blacklistref{$loc} = ($ncount2{$loc} / $inds2) unless exists $$blacklistref{$loc};
#print("Locus:",$loc," - Missing data: ",($nAcount{$loc} / $admixinds), "\n");
}
}
#Blacklist loci with globally too high missing data
foreach my $loc(sort keys %globalCount){
if (($globalCount{$loc} / $globalInds) > $threshold){
$$blacklistref{$loc} = ($globalCount{$loc} / $globalInds) unless exists $$blacklistref{$loc};
#print("Locus:",$loc," - Missing data: ",($nAcount{$loc} / $admixinds), "\n");
}
}
}
}
sub getBlacklistGap{
my( $thresh, $p2, $blacklistref, $p1ref, $p2ref) = @_;
my $globalInds = 0;
my %globalCount;
#check loci in admixed
my %ncount;
my $inds1;
foreach my $ind( sort keys %$p1ref ){
$inds1++;
$globalInds++;
my $counter = 0;
foreach my $locus( @${$$p1ref{$ind}} ){
$counter++;
$ncount{$counter} = 0 unless exists $ncount{$counter};
$globalCount{$counter} = 0 unless exists $globalCount{$counter};
if($locus eq "N" || $locus eq "-"){
$ncount{$counter}++;
$globalCount{$counter}++;
}
}
}
#blacklist any loci with too high n proportion
foreach my $loc(sort keys %ncount){
if (($ncount{$loc} / $inds1) > $threshold){
$$blacklistref{$loc} = ($ncount{$loc} / $inds1) unless exists $$blacklistref{$loc};
#print("Locus:",$loc," - Missing data: ",($nAcount{$loc} / $admixinds), "\n");
}
}
if ($p2 > 0){
my %ncount2;
my $inds2;
foreach my $ind( sort keys %$p2ref ){
$inds2++;
$globalInds++;
my $counter = 0;
foreach my $locus( @${$$p2ref{$ind}} ){
$counter++;
$ncount2{$counter} = 0 unless exists $ncount2{$counter};
$globalCount{$counter} = 0 unless exists $globalCount{$counter};
if($locus eq "N" || $locus eq "-"){
$ncount2{$counter}++;
$globalCount{$counter}++;
}
}
}
#blacklist any loci with too high n proportion
foreach my $loc(sort keys %ncount2){
if (($ncount2{$loc} / $inds2) > $threshold){
$$blacklistref{$loc} = ($ncount2{$loc} / $inds2) unless exists $$blacklistref{$loc};
#print("Locus:",$loc," - Missing data: ",($nAcount{$loc} / $admixinds), "\n");
}
}
#Blacklist loci with globally too high missing data
foreach my $loc(sort keys %globalCount){
if (($globalCount{$loc} / $globalInds) > $threshold){
$$blacklistref{$loc} = ($globalCount{$loc} / $globalInds) unless exists $$blacklistref{$loc};
#print("Locus:",$loc," - Missing data: ",($nAcount{$loc} / $admixinds), "\n");
}
}
}
}