-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreparation.pl
557 lines (452 loc) · 18.7 KB
/
reparation.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
#!/usr/bin/perl -w
#####################################
## REPARARTION: Ribosome Profiling Assisted (Re-) Annotation of Bacterial genomes
##
## Copyright (C) 2017 Elvis Ndah
##
## 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/>.
##
## contact: [email protected]
#####################################
use strict;
use warnings;
use diagnostics;
use Getopt::Long;
use Cwd;
use File::stat;
use File::Basename;
my $startRun = time(); # track processing time
########################
## Usage: ./reparation.pl -sam riboseq_alignment_files_sam_format -g bacteria_genome_fasta_file -sdir scripts_directory -db curated_protein_db(fasta)
########################
#----------------------------------------------------
# VARIBLES
#----------------------------------------------------
my %translationHash =
(GCA => "A", GCG => "A", GCT => "A", GCC => "A",
TGC => "C", TGT => "C",
GAT => "D", GAC => "D",
GAA => "E", GAG => "E",
TTT => "F", TTC => "F",
GGA => "G", GGG => "G", GGC => "G", GGT => "G",
CAT => "H", CAC => "H",
ATA => "I", ATT => "I", ATC => "I",
AAA => "K", AAG => "K",
CTA => "L", CTG => "L", CTT => "L", CTC => "L", TTA => "L", TTG => "L",
ATG => "M",
AAT => "N", AAC => "N",
CCA => "P", CCT => "P", CCG => "P", CCC => "P",
CAA => "Q", CAG => "Q",
CGA => "R", CGG => "R", CGC => "R", CGT => "R",
AGA => "R", AGG => "R",
TCA => "S", TCG => "S", TCC => "S", TCT => "S",
AGC => "S", AGT => "S",
ACA => "T", ACG => "T", ACC => "T", ACT => "T",
GTA => "V", GTG => "V", GTC => "V", GTT => "V",
TGG => "W",
TAT => "Y", TAC => "Y");
# Mandatory variables
my $genome; # Prokaryotic genome file in fasta format
my $sam_file; # Ribosome profiling alignment file [sam formate]
my $blastdb; # protein blast database in fasta format
my $dirname = dirname(__FILE__); # get tool directory for default script directory
my $script_dir = $dirname."/scripts"; # Directory where the script files are stored (defaults to the current directory)
# Input variables
my $threads = 1; # number of threads used by the USEARCH tool
my $workdir; # working directory to store files (defaults to current directoy)
my $experiment; # Experiment name
my $gtf; # Genome annotation file (gtf) [if avialable]
my $occupancy = 1; # p-site of on reads (1 = plastid estimated p-site (default), 3 = 3 prime end of read and 5 = 5 prime end of read)
my $min_read_len = 22; # Minimum RPF read length
my $max_read_len = 40; # Maximum RPF read length
my $MINORF = 30; # Minimum ORF length
my $MINREAD = 3; # Only ORFs with at least this number of RPF reads
my $OFFSET_START= 45; # Offset at the start of the ORF
my $OFFSET_STOP = 21; # Offset at stop of the ORF
my $OFFSET_SD = 5; # distance uptream of start codon to start search for SD sequence and position
my $SEED = "GGAGG"; # The seed shine dalgano sequence
my $USESD = "Y"; # Flag to determine if RBS energy is included in the predictions [Y = use RBS, N = do not use RBS]
my $identity = 0.75; # identity threshold for comparative psotive set selection
my $evalue = 1e-5; # e value threshold for comparative psotive set selection
my $pgm = 1; # Program to generate positive set prodigal=1, glimmer=2
my $start_codons = "ATG,GTG,TTG"; # Comma seperated list of possible start codons
my $start_codon_nset = "CTG"; # Start codon for the negative set
my $start_codon_pset; # Start codon for the positive set. Defualts to start codons set. Should be a subste of the standard genetic code for bacteria
my $genetic_code = 11; # the genetic code [1-25] that determines the allowed start codons
my $seedBYpass = "N"; # Bypass Shine-Dalgarno trainer and force a full motif scan (default = N(o)). Valid only for -pg 1
my $score = 0.5; # Random forest classifier threshold to classify ORF as protein copding (defualt is 0.5).
# Output files
my $bedgraphS;
my $bedgraphAS;
my $predicted_ORFs;
my $predicted_ORFs_bed;
my $predicted_ORFs_fasta;
my $plastid_image;
# Get command line arguments
GetOptions(
'g=s'=>\$genome,
'sam=s'=>\$sam_file,
'db=s'=>\$blastdb,
'sdir=s'=>\$script_dir,
'wdir=s'=>\$workdir,
'gtf=s'=>\$gtf,
'en=s'=>\$experiment,
'p=i'=>\$occupancy,
'mn=i'=>\$min_read_len,
'mx=i'=>\$max_read_len,
'mo=i'=>\$MINORF,
'mr=i'=>\$MINREAD,
'ost=i'=>\$OFFSET_START,
'osp=i'=>\$OFFSET_STOP,
'osd=i'=>\$OFFSET_SD,
'seed=s'=>\$SEED,
'sd=s'=>\$USESD,
'id=f'=>\$identity,
'ev=f'=>\$evalue,
'pg=i'=>\$pgm,
'cdn=s'=>\$start_codons,
'ncdn=s'=>\$start_codon_nset,
'pcdn=s'=>\$start_codon_pset,
'bgS=s'=>\$bedgraphS,
'bgAS=s'=>\$bedgraphAS,
'orf=s'=>\$predicted_ORFs,
'bed=s'=>\$predicted_ORFs_bed,
'fa=s'=>\$predicted_ORFs_fasta,
'ps=s'=>\$plastid_image,
'gcode=i'=>\$genetic_code,
'by=s' => \$seedBYpass,
'score=f' =>\$score
);
#----------------------------------------------------
# Evaluate input variables
#----------------------------------------------------
# check mandatory variables
my %params = (
g=>$genome,
sam=>$sam_file,
db=>$blastdb,
sdir=>$script_dir
);
my @invalid = grep uninitialized_param($params{$_}), keys %params;
die "Not properly initialized: @invalid\n" if @invalid;
# check if mandatory variables are truly files
unless (-e $genome) {
print "'$genome' is not a file. Please ensure the file exist\n";
exit(1);
}
unless (-e $sam_file) {
print "'$sam_file' is not a file. Please ensure the file exist\n";
exit(1);
}
unless (-e $blastdb) {
print "'$blastdb' is not a file. Please ensure the file exist\n";
exit(1);
}
if ($gtf) {
unless (-e $gtf) {
print "'$gtf' is not a file. Please ensure the file exist\n";
exit(1);
}
}
unless ($occupancy == 1 or $occupancy == 3 or $occupancy == 5) {
print "Option -p not properly initialize. -pg requires either 1, 3 or 5.\n";
exit(1);
}
unless ($pgm == 1 or $pgm == 2) {
print "Option -pg not properly initialize. -pg requires either 1 or 2.\n";
exit(1);
}
if (uc($SEED) =~ m/^actgACTG/) {
print "Option -seed not properly initialize. -seed string should contain only charatcers A,C,T, G.\n";
exit(1);
}
unless (uc($USESD) eq 'Y' or uc($USESD) eq 'N') {
print "Option -sd not properly initialize. -sd requires either Y or N.\n";
exit(1);
}
unless ( $genetic_code >= 1 and $genetic_code <= 25) {
print "Option -gcode not properly initialize. -gcode can take integer values between 1 and 25 (inclusive).\n";
exit(1);
}
unless (uc($seedBYpass) eq 'Y' or uc($seedBYpass) eq 'N') {
print "Option -by not properly initialize. -by requires either Y or N.\n";
exit(1);
}
# check if script directoryis properly initialized and contains all scripts
if ($script_dir) {
unless (-e $script_dir."/positive_set.pl") {
print "Script 'positive_set.pl' not found in script directory.\nEnsure the directory '$script_dir' contains all required file (see readme).\n";
exit(1);
}
unless (-e $script_dir."/Ribo_seq_occupancy.py") {
print "Script 'Ribo_seq_occupancy.py' not found in script directory.\nEnsure the directory '$script_dir' contains all required file (see readme).\n";
exit(1);
}
unless (-e $script_dir."/generate_all_ORFs.pl") {
print "Script 'generate_all_ORFs.pl' not found in script directory.\nEnsure the directory '$script_dir' contains all required file (see readme).\n";
exit(1);
}
unless (-e $script_dir."/profiles.pl") {
print "Script 'profiles.pl' not found in script directory.\nEnsure the directory '$script_dir' contains all required file (see readme).\n";
exit(1);
}
unless (-e $script_dir."/plot_profile.R") {
print "Script 'plot_profile.R' not found in script directory.\nEnsure the directory '$script_dir' contains all required file (see readme).\n";
exit(1);
}
unless (-e $script_dir."/Random_Forest.R") {
print "Script 'Random_Forest.R' not found in script directory.\nEnsure the directory '$script_dir' contains all required file (see readme).\n";
exit(1);
}
unless (-e $script_dir."/post_processing.pl") {
print "Script 'post_processing.pl' not found in script directory.\nEnsure the directory '$script_dir' contains all required file (see readme).\n";
exit(1);
}
# Check prerequisits
if ($pgm == 1) {
check_if_pgm_exist('prodigal');
} elsif ($pgm == 2) {
check_if_pgm_exist('glimmer3');
}
} else {
print "Script directory not properly initialized. Please ensure the script directory is properly defined\n";
exit(1);
}
# Prepare input variables
$experiment = ($experiment) ? $experiment."_": "";
$seedBYpass = uc($seedBYpass);
$USESD = uc($USESD);
$start_codons = uc($start_codons); # convert to uppercase
$start_codon_nset = uc($start_codon_nset); # convert to uppercase
unless($start_codon_pset) {$start_codon_pset =$start_codons}
$start_codon_pset = uc($start_codon_pset);
# Check start codons input
my $start_cdn = {};
my @scodons = split /,/, $start_codons;
foreach my $codon(@scodons) {
if (length($codon) != 3 or !(exists $translationHash{$codon})) {
print "Codon '$codon' not a valid start codon\n";
print "Start codons must be 3 nucleotides long and contain either of A,C,G or T [example: -cdn ATG,GTG,TTG]\n";
exit(1);
}
$start_cdn->{$codon} = 1;
}
# Check start codons for positive set
my $positive_codons = {};
my @pcodons = split /,/, $start_codon_pset;
foreach my $codon(@pcodons) {
if (length($codon) != 3 or !(exists $translationHash{$codon})) {
print "Codon '$codon' not a valid start codon for the positive set\n";
print "Positive set codon must be one or more of the standard Bacterial, Archaeal and Plant Plastid Code (transl_table=11 ATG,GTG,TTG).\n";
exit(1);
}
$positive_codons->{$codon} = 1;
}
# Check start codons for negative set
my $negative_codons = {};
my @ncodons = split /,/, $start_codon_nset;
foreach my $codon(@ncodons) {
if (length($codon) != 3 or !(exists $translationHash{$codon})) {
print "Codon '$codon' not a valid start codon for the negative set\n";
print "Codons must be 3 nucleotides long and contain either of A,C,G or T [example: -ncdn ATG,GTG,TTG]\n";
exit(1);
}
$negative_codons->{$codon} = 1;
}
# Check if working directory exist
my ($work_dir, $tmp_dir) = check_working_dir($workdir);
# append work_dir to output files
unless($bedgraphS) {$bedgraphS = $work_dir."/".$experiment."Ribo-seq_Sense_".$occupancy.".bedgraph";}
unless($bedgraphAS) {$bedgraphAS = $work_dir."/".$experiment."Ribo-seq_AntiSense_".$occupancy.".bedgraph";}
unless($predicted_ORFs) {$predicted_ORFs= $work_dir."/".$experiment."Predicted_ORFs.txt";}
unless($predicted_ORFs_bed) {$predicted_ORFs_bed = $work_dir."/".$experiment."Predicted_ORFs.bed";}
unless($predicted_ORFs_fasta) {$predicted_ORFs_fasta = $work_dir."/".$experiment."Predicted_ORFs.fasta";}
unless($plastid_image) {$plastid_image = $work_dir."/".$experiment."p_site_offset.png";}
# generate positive set
my $positive_set = $work_dir."/tmp/positive_set.txt";
print "Generate positive set...\n";
my $positive_set_gtf = $work_dir.'/tmp/positive.gtf';
my $cmd_positive = "perl ".$script_dir."/positive_set.pl $genome $blastdb $positive_set $min_read_len $max_read_len $MINORF $identity $evalue $start_codon_pset $pgm $work_dir $script_dir $threads $seedBYpass $genetic_code";
print "$cmd_positive\n\n";
system($cmd_positive);
print "Done.\n\n";
# section to implement plastid
my $psite_offset_file = $work_dir.$experiment."p_offsets.txt";
if ($occupancy == 1) {
# check if plastid is installed
my $search_psite = `which psite 2>&1`;
chomp($search_psite);
if ($search_psite =~ /^which: no/) {
print "Could not locate ' psite '. Please ensure the plastid python package is installed.\n";
exit(1);
}
my $search_metagene = `which metagene 2>&1`;
chomp($search_metagene);
if ($search_metagene =~ /^which: no/) {
print "Could not locate ' metagene '. Please ensure the plastid python package is installed.\n";
exit(1);
}
# find p-site offsets
$psite_offset_file = generate_p_site($positive_set_gtf,$sam_file,$min_read_len,$max_read_len);
}
# Generate occupancy file
print "Generating ribosome occupancy file..\n";
my $occupancyFile = $work_dir."/".$experiment."Ribo-seq_".$occupancy."_occupancy.txt";
my $bedgraphS_prefix = $experiment."Ribo-seq_Sense_".$occupancy;
my $bedgraphAS_prefix = $experiment."Ribo-seq_AntiSense_".$occupancy;
my $cmd_occupancy = "python ".$script_dir."/Ribo_seq_occupancy.py $sam_file $occupancy $min_read_len $max_read_len $bedgraphS $bedgraphAS $occupancyFile $bedgraphS_prefix $bedgraphAS_prefix $psite_offset_file";
print "$cmd_occupancy\n\n";
system($cmd_occupancy);
unless (-e $occupancyFile) {
print "'$occupancyFile' file does not exist.\n";
exit(1);
}
print "Done.\n\n";
# Generate all possible ORFs
print "Generate all possible ORFs...\n";
my $codons = $start_codons.",".$start_codon_pset.",".$start_codon_nset; # combine the start codons
my $ORF_file = $work_dir."/tmp/all_valid_ORFs.txt";
my $cmd_orf_gen = "perl ".$script_dir."/generate_all_ORFs.pl $genome $occupancyFile $positive_set $ORF_file $MINORF $OFFSET_START $OFFSET_STOP $OFFSET_SD $SEED $codons $work_dir $script_dir $threads";
print "$cmd_orf_gen\n\n";
print "\n";
system($cmd_orf_gen);
print "Done.\n\n";
# Generate metagenic profile
print "Meta-genic plots\n";
my $cmd_meta = "perl ".$script_dir."/profiles.pl $positive_set $occupancyFile $MINREAD $work_dir $script_dir";
print "$cmd_meta\n\n";
print "\n";
system($cmd_meta);
print "Done.\n\n";
# ORF prediction
print "Performing ORF prediction analysis..\n";
my $RF_prediction = $work_dir."/tmp/RF_predicted_ORFs.txt";
my $threshold = $work_dir."/tmp/threshold.txt";
my $RF_command = "Rscript ".$script_dir."/Random_Forest.R $ORF_file $positive_set $work_dir $start_codons $start_codon_nset $USESD $score";
print "$RF_command\n\n";
print "\n";
system($RF_command);
unless (-e $RF_prediction) {
print "'$RF_prediction' file does not exist.\n";
exit(1);
}
print "Done.\n\n";
# Post processing
print "Cleaning up RF predictions..\n";
my $output_prefix = $work_dir."/".$experiment."Predicted_ORFs";
my $processing_cmd = "";
if ($gtf) {
$processing_cmd = "perl ".$script_dir."/post_processing.pl $RF_prediction $genome $occupancyFile $output_prefix $threshold $OFFSET_START $MINREAD $predicted_ORFs $predicted_ORFs_bed $predicted_ORFs_fasta $gtf";
} else {
$processing_cmd = "perl ".$script_dir."/post_processing.pl $RF_prediction $genome $occupancyFile $output_prefix $threshold $OFFSET_START $MINREAD $predicted_ORFs $predicted_ORFs_bed $predicted_ORFs_fasta";
}
print "$processing_cmd\n\n";
system($processing_cmd);
print "Done.\n\n";
timer($startRun); # Get total Run time
##################
## SUBS
##################
## Generate metagene and p-site estimates
sub generate_p_site {
my $genes_gtf = $_[0];
my $sam = $_[1];
my $min_l = $_[2];
my $max_l =$_[3];
# temporary files
my $run_name = $work_dir."/tmp/plastid";
my $bam = $work_dir."/tmp/ribo_bam.bam";
my $sort_tmp_file = $work_dir."/tmp/check_sort.txt";
my $cmd_sam2bam = "samtools view -bS $sam | samtools sort -o $bam";
system($cmd_sam2bam) == 0
or die ("Error running samtools. Please ensure the samtools is properly installed\n");
my $command_index = "samtools index ".$bam;
system($command_index) == 0
or die ("Error running samtools. Please ensure the samtools is properly installed\n");
#Build command
my $command_meta = "metagene generate -q ".$run_name." --landmark cds_start --annotation_files ".$genes_gtf." 2> /dev/null";
print "$command_meta\n";
system($command_meta) == 0
or die ("Error running metagene. Please ensure the Plastid tool is properly installed\n");
#Build command
my $psitefile = $run_name."_rois.txt";
my $command_psite = "psite -q ".$run_name."_rois.txt ".$run_name." --min_length ".$min_l." --max_length ".$max_l." --require_upstream --count_files ".$bam." 2> /dev/null";
print "$command_psite\n";
system($command_psite) == 0
or die ("Error running psite. Please ensure the Plastid tool is properly installed\n");
my $psite_off_output = $work_dir."/".$experiment."p_site_offsets.txt";
system("cp ".$run_name."_p_offsets.txt $psite_off_output");
system("cp ".$run_name."_p_offsets.png ".$plastid_image);
return $psite_off_output;
}
sub check_if_pgm_exist {
my $pgm = $_[0];
# check for prodigal or glimmer
my $search = `which $pgm 2>&1`;
print "$search\n";
chomp($search);
if ($search =~ /^which: no/) {
if ($pgm eq "prodigal") {
unless (-x $script_dir."/bin/prodigal") { # if prodigal not install
print "Could not locate ' $pgm '. Please ensure the program is installed or present in the script directory and it is executable\n";
exit(1);
}
} elsif ($pgm eq "glimmer3") {
print "$script_dir"."/bin/glimmer/glimmer3\n";
unless (-e $script_dir."/bin/glimmer/glimmer3" and -e $script_dir."/bin/glimmer/build-icm") {
print "Could not locate ' $pgm '. Please ensure the program is installed or present in the script directory and it is executable\n";
exit(1);
}
}
}
}
sub check_working_dir {
my $work_dir = $_[0];
my $tmp_dir;
my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
if ($work_dir) {
$tmp_dir = $work_dir."/tmp";
if (!-d $work_dir) {
system("mkdir -p $work_dir" or die "Couldn't create '$work_dir.\n");
system("mkdir -p $tmp_dir" or die "Couldn't create '$tmp_dir'.\n");
} else {
system("rm -rf $work_dir" or die "Can delete '$work_dir': $!\n");
system("mkdir -p $work_dir" or die "Couldn't create '$tmp_dir'.\n");
system("mkdir -p $tmp_dir" or die "Couldn't create '$tmp_dir'.\n");
}
} else {
$work_dir = getcwd();
$work_dir = $work_dir."/".$experiment."reparation_".$months[$mon].$mday;
$tmp_dir = $work_dir."/tmp";
if (!-d $work_dir) {
system("mkdir -p $work_dir");
system("mkdir -p $tmp_dir");
}
}
$work_dir = $work_dir."/";
$tmp_dir = $tmp_dir."/";
return $work_dir, $tmp_dir;
}
sub uninitialized_param {
my ($v) = @_;
not ( defined($v) and length $v );
}
sub timer {
my $startRun = shift;
my $endRun = time();
my $runTime = $endRun - $startRun;
printf("\nTotal running time: %02d:%02d:%02d\n\n", int($runTime / 3600), int(($runTime % 3600) / 60), int($runTime % 60));
}