-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_ID16S.pl
executable file
·438 lines (336 loc) · 11.5 KB
/
run_ID16S.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
#!/usr/bin/env perl
## Pombert Lab 2022
my $name = "run_ID16S.pl";
my $version = "0.3a";
my $updated = "2024-06-01";
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use File::Basename;
use File::Path qw(make_path);
#########################################################################
### Command line options
#########################################################################
my $usage = <<"EXIT";
NAME ${name}
VERSION ${version}
UPDATED ${updated}
SYNOPSIS Master script that runs the ID16S pipeline on FASTA/FASTQ files
USAGE ${name} \\
-fq *.fastq.gz \\
-headcrop 50
OPTIONS:
-o (--outdir) Output directory [Default = ./ID16S]
-fa (--fasta) FASTA files to run
-fq (--fastq) FASTQ files to convert then run
-hd (--headcrop) Remove the first X nucleotides from 5' end of FASTQ sequences ## Useful for Nanopore data
-m (--min_length) Minimum read length to keep from FASTQ files [Default: 1000]
-d (--db) Path to ID16S databases [Default = \$ID16S_DB]
-v (--version) Show script version
--replot Replot figures with different parameters (skip homology searches)
# BLAST
-k (--tasks) megablast, dc-megablast, blastn [default = megablast]
-t (--threads) CPUs to use [default = 10]
-cu (--culling) Culling limit [default = 10]
-h (--hits) Number of hits to return [Default = 1]
-pe (--p_evalue) Preliminary e-value cutoff for BLAST results [Default = 1e-05]
# Output
-r (--ranks) Output files by taxonomic ranks [Default: species genus family order class]
-fe (--f_evalue) Final e-value cutoff for BLAST results [Default = 1e-75]
-co (--concat) Concatenate all results into a single file [Default: off]
# Plots
--height Figure height in inches [Default: 10]
--width Figure width in inches [Default: 10]
--fontsize Figure fontsize [Default: 12]
--legend Legend label [Default: NonNormalized]
--cutoff Hide taxons below specified threshold [Default = 0.01]
--format Output formats: png, eps, jpeg, pdf, svg, and/or tiff [Default: png svg]
EXIT
my @commands = @ARGV;
unless (@ARGV){
print "\n$usage\n";
exit(0);
};
my $outdir = './ID16S';
my @fastq;
my @fasta;
my $headcrop;
my $min_length = 1000;
my $sc_version;
my $replot;
## ID16S settings
my $db;
## BLAST
my $task = "megablast";
my $threads = 10;
my $culling = 10;
my $hits = 1;
my $p_evalue = "1e-05";
## Output
my @ranks = ("species","genus","family","order","class");
my $f_evalue = "1e-75";
my $concat;
## Plots
my $height = 10;
my $width = 10;
my $fontsize = 12;
my @formats;
my $cutoff = 0.01;
my $legend_label = 'NonNormalized';
GetOptions(
# General
'o|outdir=s' => \$outdir,
'fq|fastq=s@{0,}' => \@fastq,
'fa|fasta=s@{0,}' => \@fasta,
'hd|headcrop=i' => \$headcrop,
'm|min_length=i' => \$min_length,
'v|version' => \$sc_version,
'replot' => \$replot,
# ID16S settings
'd|db=s' => \$db,
# BLAST
'k|tasks=s' => \$task,
't|threads=s' => \$threads,
'cu|culling=s' => \$culling,
'h|hits=s' => \$hits,
'pe|p_evalue=s' => \$p_evalue,
# Output
'r|ranks=s@{0,}' => \@ranks,
'fe|f_evalue=s' => \$f_evalue,
'co|concat=s' => \$concat,
# Plots
'height=s' => \$height,
'width=s' => \$width,
'fontsize=i' => \$fontsize,
'legend=s' => \$legend_label,
'format=s@{0,}' => \@formats,
'cutoff=s' => \$cutoff,
);
my @format = ('png', 'svg');
if (@formats){
@format = @formats;
}
#########################################################################
### Version
#########################################################################
if ($sc_version){
print "\n";
print "Script: $name\n";
print "Version: $version\n";
print "Updated: $updated\n\n";
exit(0);
}
#########################################################################
### Environment variable(s) check
#########################################################################
if (exists $ENV{"ID16S_DB"}){
$db = $ENV{"ID16S_DB"};
}
else {
unless ($db){
print STDERR ("\$ID16S_DB is not set as an environment variable and -d (--db) was not provided.\n");
print("To use run_ID16S.pl, please add \$ID16S_DB to the environment or specify path with -d (--db)\n");
exit;
}
}
#########################################################################
### Output directory/sub-directories + logs
#########################################################################
### Outdir/subdirs
unless (-d $outdir){
make_path($outdir,{mode=>0755}) or die "Can't create $outdir: $!\n";
}
my $fasta_dir = $outdir.'/FASTA';
my $blast_dir = $outdir.'/BLAST';
my $nonnormal_dir = $outdir.'/NonNormalized';
my $normal_dir = $outdir.'/Normalized';
my $plot_dir = $outdir.'/Plots';
my @output_directories = ($fasta_dir,$blast_dir,$nonnormal_dir,$normal_dir,$plot_dir);
foreach my $dirs (@output_directories){
unless (-d $dirs){
make_path($dirs,{mode=>0755});
}
}
my ($run_ID16S,$ID16S_dir) = fileparse($0);
### Log file
my $log_file = $outdir.'/ID16S.log';
open LOG, '>', $log_file or die "Can't create $log_file: $!\n";
my $time = localtime();
my $start_time = time();
my $tstart = time();
print LOG "ID16S started on: ".$time."\n\n";
logs(\*LOG, 'Command line', 'header');
print LOG "$0 @commands\n\n";
logs(\*LOG, 'Runtime', 'header');
#########################################################################
### Check for replot
#########################################################################
if ($replot){
goto PLOTS;
}
#########################################################################
### run fastq2fasta.pl
#########################################################################
if (@fasta){
$tstart = time();
logs(\*LOG, 'Copy files to FASTA subdir', 'start');
foreach my $file (@fasta){
system ("cp $file $fasta_dir/$file");
}
logs(\*LOG, 'Copy files to FASTA subdir', 'end');
}
if (@fastq){
$tstart = time();
logs(\*LOG, 'fastq2fasta.pl', 'start');
print ("\n### Converting FASTQ to FASTA with fastq2fasta.pl\n\n");
my $crop_5end = '';
if ($headcrop){
$crop_5end = "--headcrop $headcrop";
}
system ("$ID16S_dir/Core_scripts/fastq2fasta.pl \\
--fastq @fastq \\
--outdir $fasta_dir \\
--min_length $min_length \\
$crop_5end
") == 0 or checksig();
logs(\*LOG, 'fastq2fasta.pl', 'end');
}
else {
die ("FASTQ or FASTA files are required by run_ID16S.pl\n");
}
#########################################################################
## run megablast.pl
#########################################################################
$tstart = time();
logs(\*LOG, 'megablast.pl', 'start');
print ("\n### Running BLAST search on FASTA files with megablast.pl\n\n");
system ("$ID16S_dir/Core_scripts/megablast.pl \\
--task $task \\
--query $fasta_dir/*.fasta \\
--evalue $p_evalue \\
--culling $culling \\
--threads $threads \\
--outdir $blast_dir
") == 0 or checksig();
logs(\*LOG, 'megablast.pl', 'end');
#########################################################################
## run taxid_dist.pl
#########################################################################
$tstart = time();
logs(\*LOG, 'taxid_dist.pl', 'start');
print ("\n### Acquiring TaxIDs from BLAST hits\n\n");
system ("$ID16S_dir/Core_scripts/taxid_dist.pl \\
--blast $outdir/BLAST/*.$task \\
--nodes $db/TaxDump/nodes.dmp \\
--names $db/TaxDump/names.dmp \\
--evalue $f_evalue \\
--hits $hits \\
--ranks @ranks \\
--outdir $nonnormal_dir
") == 0 or checksig();
logs(\*LOG, 'taxid_dist.pl', 'end');
#########################################################################
## get_organism_statistics.pl
#########################################################################
$tstart = time();
logs(\*LOG, 'get_organism_statistics.pl', 'start');
print ("\n### Getting statistics per organism (raw + normalized)\n\n");
opendir (NNORM,$nonnormal_dir) or die("Unable to open $nonnormal_dir: $!\n");
foreach my $file (readdir(NNORM)){
unless (-d $file){
system ("$ID16S_dir/Normalization_scripts/get_organism_statistics.pl \\
--sample $nonnormal_dir/$file \\
--db $db/Normalization_DB \\
--name $db/TaxDump/names.dmp \\
--output $normal_dir;
") == 0 or checksig();
}
}
logs(\*LOG, 'get_organism_statistics.pl', 'end');
#########################################################################
## Plotting results
#########################################################################
PLOTS:
$tstart = time();
logs(\*LOG, 'create_figure.py', 'start');
print ("\n### Plotting normalized statistics per organism\n\n");
my $nlog_dir = $normal_dir.'/Figure_files';
opendir (LDIR, $nlog_dir) or die("Unable to open $nlog_dir: $!\n");
foreach my $file (readdir(LDIR)){
unless (-d $file){
if ($file =~ /^(.*)_fasta_(?:\w+)_(\w+)_Normalized/){
my $sample = $1;
my $rank = $2;
my $plot_subdir = $plot_dir.'/'.$sample;
unless (-d $plot_subdir){
mkdir ($plot_subdir, 0755) or die "$!\n";
}
print ("Plotting $sample (rank = $rank) distributions with matplotlib\n");
system ("
create_figure.py \\
--input $nlog_dir/$file \\
--out $plot_subdir/${sample}_rank_${rank} \\
--rank $rank \\
--save \\
--format @format \\
--fontsize $fontsize \\
--height $height \\
--width $width \\
--legend $legend_label \\
--cutoff $cutoff
");
}
}
}
logs(\*LOG, 'create_figure.py', 'end');
###################################################################################################
## Completion
###################################################################################################
print "\n";
my $end_time = localtime();
my $run_time = time - $start_time;
logs(\*LOG, 'Total runtime', 'header');
print LOG "ID16S completed on: ".$end_time."\n";
print LOG "Total runtime: ".$run_time." seconds\n";
close LOG;
#########################################################################
## Subroutine(s)
#########################################################################
sub checksig {
my $exit_code = $?;
my $modulo = $exit_code % 255;
print "\nExit code = $exit_code; modulo = $modulo \n";
if ($modulo == 2) {
print "\nSIGINT detected: Ctrl+C => exiting...\n";
exit(2);
}
elsif ($modulo == 131) {
print "\nSIGTERM detected: Ctrl+\\ => exiting...\n";
exit(131);
}
}
sub logs {
my $fh = $_[0];
my $analysis = $_[1];
my $type = $_[2];
my $len = length($analysis);
my $pad = 60 - $len;
my $spacer = ' ' x $pad;
my $mend = localtime();
if ($type eq 'header'){
my $barsize = '#' x 96;
print $fh $barsize."\n";
print LOG "##### $analysis\n";
print LOG $barsize."\n\n";
}
elsif ($type eq 'start'){
print $fh "$analysis:".$spacer."started on $mend\n";
}
elsif ($type eq 'end'){
my $run_time = time - $tstart;
my $tlen = length($run_time);
my $tpad = $pad + $len - $tlen - 18;
my $tspacer = ' ' x $tpad;
print $fh "Runtime: $run_time seconds".$tspacer."completed on $mend\n\n";
}
}