Repository navigation
Expand file tree
/
Copy pathTransDecoder.Predict
More file actions
executable file
·545 lines (411 loc) · 19.5 KB
/
Copy pathTransDecoder.Predict
File metadata and controls
executable file
·545 lines (411 loc) · 19.5 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
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
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use Pod::Usage;
use Getopt::Long qw(:config posix_default no_ignore_case bundling pass_through);
use Data::Dumper;
use List::Util qw (min max);
use File::Basename;
use File::Spec;
use Carp;
use Digest::MD5;
our $ROOT_DIR;
BEGIN {
$ROOT_DIR = File::Spec->rel2abs(File::Spec->catdir($FindBin::RealBin, File::Spec->updir()));
}
use lib ("$ROOT_DIR/PerlLib");
use POSIX qw(ceil);
use Gene_obj;
use Nuc_translator;
use Fasta_reader;
use Longest_orf;
use Pipeliner;
use DelimParser;
use Cwd;
my $RETAIN_LONG_ORFS_MIN_LENGTH = 1000000; # so essentially, off by default
srand(1234);
my $genetic_code = "Universal";
my $genetic_code_options = join("\n", &Nuc_translator::get_genetic_codes());
my $usage = <<__EOUSAGE__;
########################################################################################
# ______ ___ __
# /_ __/______ ____ ___ / _ \\___ _______ ___/ /__ ____
# / / / __/ _ `/ _\\(_-</ // / -_) __/ _ \\/ _ / -_) __/
# /_/ /_/ \\_,_/_//_/___/____/\\__/\\__/\\___/\\_,_/\\__/_/ .Predict
#
########################################################################################
#
# Transdecoder.LongOrfs|http://transdecoder.github.io> - Transcriptome Protein Prediction
#
#
# Required:
#
# -t <string> transcripts.fasta
#
# Common options:
#
#
# --retain_long_orfs_mode <string> 'dynamic' or 'strict' (default: dynamic)
# In dynamic mode, sets the length to the 99.9th percentile of ORF lengths in random sequence of the same GC content
# (only for genetic codes with stop codons TAA/TAG/TGA, which the table was simulated with;
# otherwise no ORF is retained on length alone).
#
#
# --retain_long_orfs_length <int> under 'strict' mode, retain all ORFs found that are equal or longer than these many nucleotides even if no other evidence
# marks it as coding (default: 1000000) so essentially turned off by default.)
#
# --retain_pfam_hits <string> domain table output file from running hmmscan to search Pfam (see transdecoder.github.io for info)
# Any ORF with a pfam domain hit will be retained in the final output.
#
# --retain_blastp_hits <string> blastp output in '-outfmt 6' format.
# Any ORF with a blast match will be retained in the final output.
#
# --retain_multiple_orfs Report every non-overlapping ORF passing selection; by default only the single
# best ORF per transcript is reported (prioritized by homology, coding score, then length)
#
# --output_dir | -O <string> same output directory from the TransDecoder.LongOrfs
#
# --no_refine_starts start refinement decides whether an ORF open at its 5' end is truncated (5' partial)
# or extends into the 5' UTR, in which case it starts at its first in-frame ATG.
# On by default.
#
## Advanced options
#
# --start_contrast_alpha <float> start refinement: an open 5' end stays 5' partial only if the segment before its
# first in-frame ATG reaches this fraction of the training ORFs' per-base frame
# contrast (default: 0.4)
#
# --start_kozak_weight <float> start refinement: bases of that per-base contrast one unit of the ATG's Kozak
# log-odds is worth in the cutoff (default: 25)
#
# --short_orf_length <int> ORFs with fewer codons need --short_orf_min_contrast (default: 100)
#
# --short_orf_min_contrast <float> frame contrast (in-frame score minus the better shifted frame) a short ORF needs
# without homology support, in bases of the training ORFs' median per-base
# contrast; 0 applies only the usual frame test (default: 200)
#
# -T <int> Top longest ORFs to train Markov Model (hexamer stats) (default: 500)
# Note, 10x this value are first selected for removing redundancies,
# and then this -T value of longest ORFs are selected from the non-redundant set.
# --genetic_code | -G <string> genetic code (default: universal; see PerlDoc; options: Euplotid, Tetrahymena, Candida, Acetabularia, ...)
# Genetic Codes (derived from: https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi)
$genetic_code_options
#
#
#########################################################################################
__EOUSAGE__
;
my $UTIL_DIR = "$ROOT_DIR/util";
$ENV{PATH} = "$UTIL_DIR/bin:$ENV{PATH}";
my ($transcripts_file,$train_file);
my $top_ORFs_train = 500;
my $max_prot_length_for_training = 5000;
my $help;
my $verbose;
my $search_pfam = "";
my ($reuse,$pfam_out);
my $RETAIN_LONG_ORFS_MODE = 'dynamic';
#################################
# Lengths (nt) of ORFs found by LongOrfs in random sequence of a given GC%:
# i.i.d. bases, both strands, 5'/3' partial ORFs allowed, >= 300 nt; quantiles of
# >= 85,000 ORFs per GC level. Regenerated 2026-10 (TransDecoderPaper/analyses/long_orf_threshold).
#
# GC% 0.95 0.99 0.999 0.9999
# 80 1233 1731 2436 3153
# 75 924 1257 1746 2202
# 70 750 993 1347 1707
# 65 642 822 1083 1356
# 60 570 714 927 1150
# 55 522 639 804 978
# 50 483 585 726 852
# 45 456 540 663 768
# 40 435 507 615 708
# 35 417 483 573 663
# 30 408 462 552 621
# 25 396 447 519 580
# ORFs at least as long as the 0.999 quantile are retained regardless of coding score:
my @GC_TO_MIN_LONG_ORF_LENGTH = ( [25, 519],
[30, 552],
[35, 573],
[40, 615],
[45, 663],
[50, 726],
[55, 804],
[60, 927],
[65, 1083],
[70, 1347],
[75, 1746],
[80, 2436] );
my $retain_pfam_hits_file;
my $retain_blastp_hits_file;
my $cpu = 1;
my $MPI_DEBUG = 1;
my $single_best_flag = 0; # deprecated: single best is the default
my $retain_multiple_orfs_flag = 0;
my $start_contrast_alpha = 0.4;
my $start_kozak_weight = 25;
my $short_orf_length = 100;
my $short_orf_min_contrast = 200;
my $NO_REFINE_START_CODONS_FLAG = 0;
my $output_dir = &Pipeliner::ensure_full_path(cwd()); # current working directory by default.
&GetOptions( 't=s' => \$transcripts_file,
'h' => \$help,
'v' => \$verbose,
'T=i' => \$top_ORFs_train,
'search_pfam=s' => \$search_pfam,
'reuse' => \$reuse,
'retain_long_orfs_mode=s' => \$RETAIN_LONG_ORFS_MODE,
'retain_long_orfs_length=i' => \$RETAIN_LONG_ORFS_MIN_LENGTH,
'debug' => \$MPI_DEBUG,
'retain_pfam_hits=s' => \$retain_pfam_hits_file,
'retain_blastp_hits=s' => \$retain_blastp_hits_file,
'cpu=i' => \$cpu,
'single_best_only' => \$single_best_flag,
'retain_multiple_orfs' => \$retain_multiple_orfs_flag,
'start_contrast_alpha=f' => \$start_contrast_alpha,
'start_kozak_weight=f' => \$start_kozak_weight,
'short_orf_length=i' => \$short_orf_length,
'short_orf_min_contrast=f' => \$short_orf_min_contrast,
'genetic_code|G=s' => \$genetic_code,
'no_refine_starts' => \$NO_REFINE_START_CODONS_FLAG,
'output_dir|O=s' => \$output_dir,
);
if ($help) {
die $usage;
}
if (@ARGV) {
die "Error, don't understand options: @ARGV";
}
our $SEE = $verbose;
unless ($transcripts_file && -s $transcripts_file) {
die $usage;
}
if ($genetic_code) {
$genetic_code = " --genetic_code $genetic_code";
}
main: {
if ($transcripts_file =~ /\.gz$/) {
# create an uncompressed version locally and use that instead
my $uncompressed_transcripts_file = basename($transcripts_file);
$uncompressed_transcripts_file =~ s/\.gz$//;
if (! -s $uncompressed_transcripts_file) {
&process_cmd("gunzip -c $transcripts_file > $uncompressed_transcripts_file");
}
$transcripts_file = $uncompressed_transcripts_file;
}
my $workdir = &Pipeliner::ensure_full_path("$output_dir/" . basename($transcripts_file) . ".transdecoder_dir");
unless (-d $workdir) {
die "Error, cannot find directory: $workdir, be sure to first run TransDecoder.LongOrfs before TransDecoder.Predict\n\n";
}
my $checkpoints_dir = "$workdir/__checkpoints_TDpredict";
if (! -d $checkpoints_dir) {
mkdir($checkpoints_dir);
}
my $pipeliner = new Pipeliner('-verbose' => 2, '-checkpoint_dir' => $checkpoints_dir);
my $prefix = "$workdir/longest_orfs";
my $cds_file = "$prefix.cds";
my $gff3_file = "$prefix.gff3";
my $pep_file = "$prefix.pep";
## Train a Markov model based on user-provided file or longest candidate CDS sequences, score all candidates, and select the final set.
my $top_cds_file = "$cds_file.top_${top_ORFs_train}_longest";
{
# to speed things up only check for redundancy up to x the number of entries we want
my $red_num = $top_ORFs_train * 10;
my $red_num_cds_longest_file = "$cds_file.top_longest_${red_num}";
$pipeliner->add_commands(new Command("$UTIL_DIR/get_top_longest_fasta_entries.pl $cds_file $red_num $max_prot_length_for_training > $red_num_cds_longest_file",
"get_longest_orfs.ok"));
$pipeliner->add_commands(new Command("$UTIL_DIR/exclude_similar_proteins.pl $red_num_cds_longest_file $genetic_code > $red_num_cds_longest_file.nr", "nr.ok"));
$pipeliner->add_commands(new Command("$UTIL_DIR/get_top_longest_fasta_entries.pl $red_num_cds_longest_file.nr $top_ORFs_train > $top_cds_file",
"top_train_select.ok"));
$pipeliner->run();
}
# get hexamer scores
my $hexamer_scores_file = "$workdir/hexamer.scores";
my $hexamer_checkpoint = "hexamer_scores_file.ok";
my $base_freqs_file = "$workdir/base_freqs.dat";
if ($RETAIN_LONG_ORFS_MODE eq 'dynamic') {
if (&genetic_code_has_standard_stops()) {
$RETAIN_LONG_ORFS_MIN_LENGTH = &get_dynamic_retain_long_orf_length($base_freqs_file, \@GC_TO_MIN_LONG_ORF_LENGTH);
}
else {
# the GC table was simulated with TAA/TAG/TGA; with fewer stops random ORFs are much
# longer (e.g. TGA-only ciliate code), so no ORF is retained on length alone
print STDERR "-genetic code stop codons differ from TAA/TAG/TGA: no length-based ORF retention\n";
$RETAIN_LONG_ORFS_MIN_LENGTH = 1000000000;
}
}
# score kmers for Markov model
$pipeliner->add_commands(new Command("$UTIL_DIR/seq_n_baseprobs_to_loglikelihood_vals.pl $top_cds_file $base_freqs_file $genetic_code > $hexamer_scores_file",
"hexamer_scores.ok") );
# score all cds entries
my $cds_scores_file = "$cds_file.scores";
my $cds_scores_checkpoint = "$cds_scores_file.ok";
$pipeliner->add_commands(new Command("$UTIL_DIR/score_CDS_likelihood_all_6_frames.pl $cds_file $hexamer_scores_file $genetic_code > $cds_scores_file",
"score_cdss.ok"));
$pipeliner->run();
##############
## select orfs
## Steps below may be rerun if input parameters are set differently (such as including pfam or blast results in a subsequent run).
my @checkpoint_inputs = ($transcripts_file, "short_orf:$short_orf_length:$short_orf_min_contrast");
if ($retain_pfam_hits_file) {
push (@checkpoint_inputs, $retain_pfam_hits_file);
}
if ($retain_blastp_hits_file) {
push (@checkpoint_inputs, $retain_blastp_hits_file);
}
my $big_checkpoint_string = join("^", @checkpoint_inputs);
my $checkpoint_hashcode = Digest::MD5::md5_hex($big_checkpoint_string);
my $select_cmd = "$UTIL_DIR/select_best_ORFs_per_transcript.pl --gff3_file $gff3_file --cds_scores $cds_scores_file "
. " --min_length_auto_accept $RETAIN_LONG_ORFS_MIN_LENGTH "
. " --training_orfs $top_cds_file --hexamer_scores $hexamer_scores_file $genetic_code "
. " --short_orf_length $short_orf_length --short_orf_min_contrast $short_orf_min_contrast ";
if ($retain_pfam_hits_file) {
$select_cmd .= " --pfam_hits $retain_pfam_hits_file ";
}
if ($retain_blastp_hits_file) {
$select_cmd .= " --blast_hits $retain_blastp_hits_file ";
}
unless ($retain_multiple_orfs_flag) {
$select_cmd .= " --single_best_orf ";
}
$select_cmd .= " > $cds_file.best_candidates.gff3 ";
$pipeliner->add_commands(new Command($select_cmd, "select_best_orfs.$checkpoint_hashcode.ok", "Selecting best orfs"));
my $final_output_prefix = &Pipeliner::ensure_full_path("$output_dir/" . basename($transcripts_file) . ".transdecoder");
my $target_final_file = "$cds_file.best_candidates.gff3";
## start codon refinement
unless ($NO_REFINE_START_CODONS_FLAG) {
my $cmd = "$UTIL_DIR/train_kozak_model.pl --transcripts $transcripts_file --training_orfs $top_cds_file "
. " --out_prefix $workdir/start_refinement";
$pipeliner->add_commands(new Command($cmd, "train_kozak_model.$checkpoint_hashcode.ok", "Training start codon (Kozak) context model"));
my $revised_orf_gff3_file = "$target_final_file.revised_starts.gff3";
$cmd = "$UTIL_DIR/start_codon_refinement.pl --transcripts $transcripts_file --gff3_file $target_final_file --workdir $workdir "
. " --training_orfs $top_cds_file --hexamer_scores $hexamer_scores_file $genetic_code "
. " --kozak_model $workdir/start_refinement.kozak.tsv "
. " --contrast_alpha $start_contrast_alpha --kozak_weight $start_kozak_weight > $revised_orf_gff3_file";
$pipeliner->add_commands(new Command($cmd, "revise_starts.$checkpoint_hashcode.ok", "Refining start codon selections."));
$target_final_file = $revised_orf_gff3_file;
}
$pipeliner->add_commands(new Command("cp $target_final_file $final_output_prefix.gff3",
"copy_to_final_outputfile.$checkpoint_hashcode.ok",
"copying output to final output file: $final_output_prefix.gff3") );
## write final outputs:
{
## make a BED file for viewing in IGV
my $gff3_file = "$final_output_prefix.gff3";
my $bed_file = $gff3_file;
$bed_file =~ s/\.gff3$/\.bed/;
my $cmd = "$UTIL_DIR/gff3_file_to_bed.pl $gff3_file > $bed_file";
$pipeliner->add_commands(new Command($cmd, "make_final_bed.$checkpoint_hashcode.ok", "Making bed file: $bed_file"));
# make a peptide file:
my $best_pep_file = $gff3_file;
$best_pep_file =~ s/\.gff3$/\.pep/;
$cmd = "$UTIL_DIR/gff3_file_to_proteins.pl --gff3 $gff3_file --fasta $transcripts_file $genetic_code > $best_pep_file";
$pipeliner->add_commands(new Command($cmd, "make_final_pep.$checkpoint_hashcode.ok", "Making pep file: $best_pep_file"));
# make a CDS file:
my $best_cds_file = $best_pep_file;
$best_cds_file =~ s/\.pep$/\.cds/;
$cmd = "$UTIL_DIR/gff3_file_to_proteins.pl --gff3 $gff3_file --fasta $transcripts_file --seqType CDS $genetic_code > $best_cds_file";
$pipeliner->add_commands(new Command($cmd, "make_final_cds.$checkpoint_hashcode.ok", "Making cds file: $best_cds_file"));
}
$pipeliner->run();
print STDERR "transdecoder is finished. See output files $final_output_prefix.\*\n\n\n";
exit(0);
}
####
sub process_cmd {
my ($cmd) = @_;
print "CMD: $cmd\n";
my $ret = system($cmd);
if ($ret) {
die "Error, cmd: $cmd died with ret $ret";
}
return;
}
####
sub parse_pfam_hits {
my ($pfam_hits_file) = @_;
my %has_pfam_hit;
if (! -e $pfam_hits_file) {
die "Error, cannot find pfam hits file: $pfam_hits_file";
}
print "PFAM output found and processing...\n";
# capture those proteins having pfam hits
open (my $fh, $pfam_hits_file) or die "Error, cannot open file: $pfam_hits_file";
while (my $ln=<$fh>) {
next if $ln=~/^\#/;
my @x = split(/\s+/,$ln);
next unless $x[3]; # domtbl
my $orf_acc = $x[3];
$has_pfam_hit{$orf_acc} = 1;
}
close $fh;
return(%has_pfam_hit);
}
####
sub parse_blastp_hits_file {
my ($blastp_file) = @_;
unless (-e $blastp_file) {
die "Error, cannot find file $blastp_file";
}
my %blastp_hits;
open (my $fh, $blastp_file) or die "Error, cannot open file $blastp_file";
while (<$fh>) {
chomp;
my @x = split(/\t/);
my $id = $x[0];
$blastp_hits{$id} = 1;
}
close $fh;
return(%blastp_hits);
}
sub check_program() {
my @paths;
foreach my $prog (@_) {
my $path = `which $prog`;
unless ($path =~ /\w/) {
die "Error, path to a required program ($prog) cannot be found\n\n"
}
chomp($path);
$path = readlink($path) if -l $path;
push( @paths, $path );
}
return @paths;
}
####
sub genetic_code_has_standard_stops {
# $genetic_code holds the option string passed to the util scripts
my ($code) = $genetic_code =~ /--genetic_code\s+(\S+)/;
&Nuc_translator::use_specified_genetic_code($code || "Universal");
my $stops = join(",", sort grep { !/U/ } &Nuc_translator::get_stop_codons()); # DNA spellings
&Nuc_translator::use_specified_genetic_code("Universal");
return($stops eq "TAA,TAG,TGA");
}
####
sub get_dynamic_retain_long_orf_length {
my ($base_freqs_file, $GC_to_min_orf_length_aref) = @_;
my $pct_GC = -1;
open(my $fh, $base_freqs_file) or confess "Error, cannot open file: $base_freqs_file";
while (<$fh>) {
chomp;
my ($base, $count, $ratio) = split(/\t/);
if ($base eq 'C') {
$pct_GC = 2 * $ratio * 100;
last;
}
}
close $fh;
print STDERR "PCT_GC: $pct_GC\n";
unless ($pct_GC > 0) {
confess "Error, didn't parse percent C from file: $base_freqs_file";
}
foreach my $bin (@$GC_to_min_orf_length_aref) {
my ($gc, $min_len) = @$bin;
if ($pct_GC <= $gc) {
return($min_len);
}
}
# if we got here, then GC content must be crazy high!
# in this case, rely entirely on Markov model for coding determination.
return(1000000); #effectively infinity here.
}