-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhaplotypes_provided_sites.pl
executable file
·422 lines (330 loc) · 12.4 KB
/
haplotypes_provided_sites.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
#! /usr/bin/perl
# PROGRAM: determines all haplotypes in a given FASTA alignment for the sites provided.
#########################################################################################
# EXAMPLE CALL:
#########################################################################################
# haplotypes_provided_sites.pl --sites_file=PBS_neg_sites.txt --fasta_file=A1.fa > PBShaplo_A1.txt
#########################################################################################
# Copyright (C) 2018 Chase W. Nelson
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# DATE CREATED: March 30, 2018
# AUTHOR: Chase W. Nelson
# CONTACT1: [email protected]
# CONTACT2: [email protected]
# AFFILIATION1: Sackler Institute for Comparative Genomics, American Museum of Natural
# History, New York, NY 10024, USA
# ACKNOWLEDGMENTS: written by C.W.N. with support from a Gerstner Scholars Fellowship from
# the Gerstner Family Foundation at the American Museum of Natural History, New York.
use strict;
#use warnings;
use Data::Dumper;
use Getopt::Long;
use List::Util qw(max);
STDOUT->autoflush(1);
#########################################################################################
# Get the time and begin
my $time1 = time;
my $local_time1 = localtime;
print "\n##########################################################################################";
print "\n## Haplotype analysis initiated at local time $local_time1";
print "\n##########################################################################################\n";
#########################################################################################
# INITIALIZE VARIABLES
my $fasta_file; # containing MULTIPLE ALIGNED sequences, e.g., of a sublineage
my $sites_file;
#my $procs_per_node;
my $num_bootstraps = 10000; # default 10000
# Get user input, if given. If a Boolean argument is passed, its value is 1; else undef
GetOptions( "fasta_file=s" => \$fasta_file,
"sites_file=s" => \$sites_file,
# "procs_per_node=i" => \$procs_per_node,
"num_bootstraps=i" => \$num_bootstraps)
or die "\n### WARNING: Error in command line arguments. Script terminated.\n\n";
unless(-f "$fasta_file") {
die "\n### WARNING: An existing --fasta_file option must be provided\n".
"### Script terminated.\n\n";
}
unless(-f "$sites_file") {
die "\n### WARNING: An existing --sites_file option must be provided\n".
"### Script terminated.\n\n";
}
#unless($procs_per_node >= 1) {
# die "\n### WARNING: A positive --procs_per_node must be provided for parallelism.\n".
# "### Script terminated.\n\n";
#}
#########################################################################################
# DETERMINE OUTPUT FILE PREFIX
my $new_file_prefix;
if($sites_file =~ '.txt') {
$new_file_prefix = $`;
} else {
$new_file_prefix = "INPUT";
}
#########################################################################################
# STORE FASTA SEQUENCE & FULL-SEQUENCE HAPLOTYPES
my $seq = '';
my @seqs_arr;
#my $header = '';
#my @headers_arr;
my $seq_num = 0;
my $last_seq_length;
open(IN_FASTA, "$fasta_file") or die "Could not open FASTA file $fasta_file\n";
print "\nReading in sequences from FASTA...\n";
while(<IN_FASTA>) {
chomp;
if(/>/) {
if($seq_num == 0) {
#$header = $_;
$seq_num ++;
} else {
push(@seqs_arr,$seq);
#push(@headers_arr,$header);
#$header = $_;
$seq_num ++;
my $this_seq_length = length($seq);
#print "\nseq $seq_num is of length $this_seq_length\n";
if($last_seq_length && ($last_seq_length != $this_seq_length)) {
die "\n\nDIE: The sequences must be aligned, i.e., must be the same length. TERMINATED.\n\n";
} else {
$last_seq_length = $this_seq_length;
#print "\nseq: $seq\n";
$seq = '';
}
}
} else {
$seq .= $_;
}
}
push(@seqs_arr, $seq);
#push(@headers_arr,$header);
close IN_FASTA;
$seq_num = scalar(@seqs_arr);
my $genome_length = length($seq);
#########################################################################################
# READ IN AND STORE SITES
my @sites;
open(IN_SITES, "$sites_file") or die "Could not open sites file $sites_file\n";
print "\n\nReading in sites from file...\n\n";
while(<IN_SITES>) {
chomp;
my @sites_line_arr = split(/\s+/, $_, -1);
foreach (@sites_line_arr) {
if($_ =~ /\d+/) {
push(@sites, $_);
}
}
}
close IN_SITES;
@sites = sort {$a <=> $b} @sites;
my $num_sites = scalar(@sites);
print "\nANALYZING $num_sites SITES:\n@sites\n\n";
my $max_site = max(@sites);
if($max_site > $genome_length) {
die "\n### WARNING: sites fall outside of genome length $genome_length\.\n".
"### SCRIPT TERMINATED.\n\n";
}
foreach (@sites) {
print "site_$_\t";
}
print "\n";
##########################################################################################
# Loop through sequences and record haplotypes for sites
my %haplotypes;
foreach my $seq (@seqs_arr) {
my $haplotype;
foreach my $site (@sites) {
$haplotype .= substr($seq, $site - 1, 1);
}
$haplotypes{$haplotype}++;
}
## ONLY DEFINED HAPLOTYPES
# Determine the most common haplotype
my $maj_haplotype = '';
my $maj_haplotype_count = 0;
foreach my $haplotype (keys %haplotypes) {
my $curr_haplotype_count = $haplotypes{$haplotype};
unless($haplotype =~ /N/) { # only fully defined ones
if($curr_haplotype_count > $maj_haplotype_count) {
$maj_haplotype_count = $curr_haplotype_count;
$maj_haplotype = $haplotype;
}
}
}
print "\nMAJOR HAPLOTYPE (FULLY-DEFINED ONLY): $maj_haplotype\n\n";
print "haplotype\tcount\tdiffs_from_maj_haplotype\n";
foreach my $haplotype (keys %haplotypes) {
unless($haplotype =~ /N/) { # only fully defined ones
# Count differences from major haplotype
my $diffs_from_maj_haplotype = 0;
for(my $i = 0; $i < length($haplotype); $i++) {
if(substr($haplotype, $i, 1) ne substr($maj_haplotype, $i, 1)) {
$diffs_from_maj_haplotype++;
}
}
print "$haplotype\t$haplotypes{$haplotype}\t$diffs_from_maj_haplotype\n";
}
}
## ALL HAPLOTYPES
# Determine the most common haplotype_undef
my $maj_haplotype_undef = '';
my $maj_haplotype_undef_count = 0;
foreach my $haplotype (keys %haplotypes) {
my $curr_haplotype_undef_count = $haplotypes{$haplotype};
if($curr_haplotype_undef_count > $maj_haplotype_undef_count) {
$maj_haplotype_undef_count = $curr_haplotype_undef_count;
$maj_haplotype_undef = $haplotype;
}
}
print "\nMAJOR HAPLOTYPE (ALL): $maj_haplotype_undef\n\n";
print "haplotype\tcount\tdiffs_from_maj_haplotype\n";
foreach my $haplotype (keys %haplotypes) {
# Count differences from major haplotype
my $diffs_from_maj_haplotype_undef = 0;
for(my $i = 0; $i < length($haplotype); $i++) {
if(substr($haplotype, $i, 1) ne substr($maj_haplotype_undef, $i, 1)) {
$diffs_from_maj_haplotype_undef++;
}
}
print "$haplotype\t$haplotypes{$haplotype}\t$diffs_from_maj_haplotype_undef\n";
}
# Print a completion message to screen
&end_the_program;
exit;
#########################################################################################
#########################################################################################
#################################### ####################################
#################################### SUBROUTINES ####################################
#################################### ####################################
#########################################################################################
#########################################################################################
#########################################################################################
sub get_header_names {
# Originally assumed that we've received a tempfile ending in "_snpg9temp.txt"
# However, we're now calling it at least once before creating the tempfile to
# see what kind of processing (e.g., Geneious to CLC) is needed prior to tempfile
# creation. Must include capability to get headers for .CSV file
my ($curr_snp_report_filename,$filename) = @_;
#print "\n$curr_snp_report_filename\n";
#my $newline_char = &detect_newline_char($curr_snp_report_filename);
#my $old_newline = $/;
#$/ = $newline_char;
my $seen_tab_delimited = 0;
my $seen_comma_delimited = 0;
my $seen_vcf_tab_delimited = 0;
my @line_arr;
my $line = 0;
open (CURRINFILE, $curr_snp_report_filename);
#seek(CURRINFILE,0,0);
while (<CURRINFILE>) {
#print "$_";
if($line == 0) {
chomp;
# CHOMP for 3 operating systems
if($_ =~ /\r\n$/) {
$_ =~ s/\r\n//;
} elsif($_ =~ /\r$/) {
$_ =~ s/\r//;
} elsif($_ =~ /\n$/) {
$_ =~ s/\n//;
}
if($_ =~/\t\w+\t/) { # it's TAB-delimited
@line_arr = split(/\t/,$_);
#print "TAB!!!!!";
last;
} elsif($_ =~/,\w+,/) { # it's COMMA-delimited
@line_arr = split(/,/,$_);
#print "COMMA!!!!!";
last;
}
$line++;
} elsif($line > 0 && $_ =~ /^##/) {
$line++;
} elsif($line > 0 && ($_ =~ /^#CHROM/)) {
chomp;
# CHOMP for 3 operating systems
if($_ =~ /\r\n$/) {
$_ =~ s/\r\n//;
} elsif($_ =~ /\r$/) {
$_ =~ s/\r//;
} elsif($_ =~ /\n$/) {
$_ =~ s/\n//;
}
if($_ =~/\t/) { # it's TAB-delimited
@line_arr = split(/\t/,$_);
#print "TAB!!!!!";
last;
} elsif($_ =~/,/) { # it's COMMA-delimited
@line_arr = split(/,/,$_);
#print "COMMA!!!!!";
last;
} else {
chdir('SNPGenie_Results');
open(ERROR_FILE,">>SNPGenie\_LOG\.txt");
# FILE | PRODUCT | SITE | CODON | WARNING
# No change OR error should occur if the file does not, in fact, end
# with this SUFFIX
my $file_nm = $curr_snp_report_filename;
#$file_nm =~ s/_snpg9temp.txt/.txt/;
$file_nm =~ s/_\w\w\w\w.txt/.txt/;
print ERROR_FILE "$filename\tN/A\tN/A\t".
"File not TAB(\\t)- or COMMA-delimited, or there is only one column. SNPGenie terminated.\n";
close ERROR_FILE;
chdir('..');
#unlink $curr_snp_report_filename;
die "\n\n## WARNING: The SNP Report $filename is ".
"not TAB(\\t)- or COMMA-delimited, or there is only one column. SNPgenie ".
"terminated\n\n";
}
} else {
chdir('SNPGenie_Results');
open(ERROR_FILE,">>SNPGenie\_LOG\.txt");
# FILE | PRODUCT | SITE | CODON | WARNING
# No change OR error should occur if the file does not, in fact, end
# with this SUFFIX
my $file_nm = $curr_snp_report_filename;
#$file_nm =~ s/_snpg9temp.txt/.txt/;
$file_nm =~ s/_\w\w\w\w.txt/.txt/;
print ERROR_FILE "$filename\tN/A\tN/A\t".
"File not TAB(\\t)- or COMMA-delimited, or there is only one column. SNPGenie terminated.\n";
close ERROR_FILE;
chdir('..');
#unlink $curr_snp_report_filename;
die "\n\n## WARNING: The SNP Report $filename is ".
"not TAB(\\t)- or COMMA-delimited, or there is only one column. SNPgenie ".
"terminated\n\n";
}
}
seek(CURRINFILE,0,0);
close CURRINFILE;
#$/ = $old_newline;
return @line_arr;
}
#########################################################################################
# End the program by notifying the screen at command line
sub end_the_program {
my $time2 = time;
my $local_time2 = localtime;
my $time_diff = ($time2 - $time1);
my $time_diff_rounded = sprintf("%.2f",$time_diff);
my $mins_elapsed = ($time_diff / 60);
my $whole_mins_elapsed = int($mins_elapsed);
my $whole_mins_in_secs = ($whole_mins_elapsed * 60);
my $secs_remaining = ($time_diff - $whole_mins_in_secs);
my $secs_remaining_rounded = sprintf("%.2f",$secs_remaining);
print "LinkGe analysis completed at local time $local_time2. The process took $time_diff_rounded secs, i.e., ".
"$whole_mins_elapsed mins and $secs_remaining_rounded secs\n";
print "\n################################################################################".
"\n## Haplotype analysis completed successfully. ##".
"\n## Please have fun now. ##".
"\n################################################################################".
"\n\n\n";
}