forked from TransDecoder/TransDecoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransDecoder.LongOrfs
executable file
·420 lines (302 loc) · 12.2 KB
/
TransDecoder.LongOrfs
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/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 lib ("$FindBin::RealBin/PerlLib");
use POSIX qw(ceil);
use Gene_obj;
use Nuc_translator;
use Fasta_reader;
use Longest_orf;
use Pipeliner;
#my $VERSION = "__BLEEDING_EDGE__";
my $VERSION = "5.5.0";
my $UTIL_DIR = "$FindBin::RealBin/util";
$ENV{PATH} = "$UTIL_DIR/bin:$ENV{PATH}";
my ($transcripts_file);
my $genetic_code='universal';
my $genetic_code_options = join("\n", &Nuc_translator::get_genetic_codes());
my $MIN_PROT_LENGTH = 100;
my $usage = <<__EOUSAGE__;
########################################################################################
# ______ ___ __
# /_ __/______ ____ ___ / _ \\___ _______ ___/ /__ ____
# / / / __/ _ `/ _\\(_-</ // / -_) __/ _ \\/ _ / -_) __/
# /_/ /_/ \\_,_/_//_/___/____/\\__/\\__/\\___/\\_,_/\\__/_/ .LongOrfs
#
########################################################################################
#
# Transdecoder.LongOrfs|http://transdecoder.github.io> - Transcriptome Protein Prediction
#
#
# Required:
#
# -t <string> transcripts.fasta
#
# Optional:
#
# --gene_trans_map <string> gene-to-transcript identifier mapping file (tab-delimited, gene_id<tab>trans_id<return> )
#
# -m <int> minimum protein length (default: 100)
#
# -G <string> genetic code (default: universal; see PerlDoc; options: Euplotes, Tetrahymena, Candida, Acetabularia)
#
# -S strand-specific (only analyzes top strand)
#
# --output_dir | -O <string> path to intended output directory (default: basename( -t val ) + ".transdecoder_dir")
#
# --genetic_code <string> Universal (default)
#
# Genetic Codes (derived from: https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi)
#
$genetic_code_options
#
#
# --version show version tag ($VERSION)
#
#########################################################################################
__EOUSAGE__
;
my $TOP_STRAND_ONLY = 0;
my $help;
my $verbose;
my $search_pfam = "";
my ($reuse,$pfam_out);
my $gene_trans_map_file;
my $MPI_DEBUG = 1;
my $show_version_flag;
my $output_dir;
&GetOptions( 't=s' => \$transcripts_file,
'm=i' => \$MIN_PROT_LENGTH,
'G=s' => \$genetic_code,
'h' => \$help,
'v' => \$verbose,
'S' => \$TOP_STRAND_ONLY,
'gene_trans_map=s' => \$gene_trans_map_file,
'version' => \$show_version_flag,
'output_dir|O=s' => \$output_dir,
);
if ($help) {
die $usage;
}
if ($show_version_flag) {
print "TransDecoder.LongOrfs $VERSION\n";
exit(0);
}
if (@ARGV) {
die "Error, don't understand options: @ARGV";
}
our $SEE = $verbose;
unless ($transcripts_file && -s $transcripts_file) {
die $usage;
}
if ($genetic_code ne 'universal') {
&Nuc_translator::use_specified_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 = $output_dir;
unless ($workdir) {
$workdir = &Pipeliner::ensure_full_path(basename($transcripts_file) . ".transdecoder_dir");
}
unless (-d $workdir) {
mkdir($workdir) or die "Error, cannot mkdir $workdir.";
}
my $checkpoints_dir = "$workdir.__checkpoints_longorfs";
if (! -d $checkpoints_dir) {
mkdir($checkpoints_dir);
}
my $pipeliner = new Pipeliner('-verbose' => 2);
$pipeliner->set_checkpoint_dir($checkpoints_dir);
my %gene_trans_map;
if ($gene_trans_map_file) {
open(my $fh, $gene_trans_map_file) or die "Error, cannot open file $gene_trans_map_file";
while (<$fh>) {
chomp;
my ($gene_id, $trans_id) = split(/\t/);
$gene_trans_map{$trans_id} = $gene_id;
}
close $fh;
}
my $base_freqs_file = "$workdir/base_freqs.dat";
my $base_freqs_checkpoint = "base_freqs_file.ok";
my $msg = "\n\n-first extracting base frequencies, we'll need them later.\n";
my $cmd = "$UTIL_DIR/compute_base_probs.pl $transcripts_file $TOP_STRAND_ONLY > $base_freqs_file";
$pipeliner->add_commands(new Command($cmd, $base_freqs_checkpoint, $msg));
$pipeliner->run();
my $longorf_checkpoint = "$checkpoints_dir/TD.longorfs.ok";
if (-e $longorf_checkpoint) {
print STDERR "-skipping long orf extraction, already completed earlier as per checkpoint: $longorf_checkpoint\n";
exit(0);
}
my $prefix = "$workdir/longest_orfs";
my $cds_file = "$prefix.cds";
my $gff3_file = "$prefix.gff3";
my $pep_file = "$prefix.pep";
open (PEP, ">$pep_file") or die $!;
open (CDS, ">$cds_file") or die $!;
open (GFF, ">$gff3_file") or die $!;
print STDERR "\n\n- extracting ORFs from transcripts.\n";
my $model_counter = 0;
my $trans_counter = 0;
my $num_total_trans = `grep '>' $transcripts_file | wc -l`;
chomp $num_total_trans;
print STDERR "-total transcripts to examine: $num_total_trans\n";
my %SEEN_PROT_ID;
my $fasta_reader = new Fasta_reader($transcripts_file);
while (my $seq_obj = $fasta_reader->next()) {
$trans_counter++;
my $percent_done = sprintf("%.2f", $trans_counter/$num_total_trans*100);
print STDERR "\r[$trans_counter/$num_total_trans] = $percent_done\% done " if $trans_counter % 100 == 0;
my $acc = $seq_obj->get_accession();
my $sequence = $seq_obj->get_sequence();
my $longest_orf_finder = new Longest_orf();
$longest_orf_finder->allow_5prime_partials();
$longest_orf_finder->allow_3prime_partials();
if ($TOP_STRAND_ONLY) {
$longest_orf_finder->forward_strand_only();
}
my @orf_structs = $longest_orf_finder->capture_all_ORFs($sequence);
@orf_structs = reverse sort {$a->{length}<=>$b->{length}} @orf_structs;
print "checking for fp in $acc \n" if ($SEE);
while (@orf_structs) {
my $orf = shift @orf_structs;
my $start = $orf->{start};
my $stop = $orf->{stop};
my $length = int((abs($start-$stop)+1)/3);
my $orient = $orf->{orient};
my $protein = $orf->{protein};
##################################
# adjust for boundary conditions, since starts and stops run off the ends of the sequences at partial codons
#################################
# adjust at 3' end
if ($stop > length($sequence)) {
$stop -= 3;
}
if ($start > length($sequence)) {
$start -= 3;
}
# adjust at 5' end
if ($stop < 1) {
$stop += 3;
}
if ($start < 1) {
$start += 3;
}
print "Candidate (len $length): ".Dumper($orf) if($SEE);
if ($length < $MIN_PROT_LENGTH) { next; }
my $cds_coords_href = { $start => $stop };
my $exon_coords_href = ($start < $stop) ? { 1 => length($sequence) } : { length($sequence) => 1 };
my $gene_obj = new Gene_obj();
print "dumping coords for $acc $start $stop\n" if($SEE);
print Dumper(%$cds_coords_href) if($SEE);
print Dumper(%$exon_coords_href) if($SEE);
$gene_obj->populate_gene_object($cds_coords_href, $exon_coords_href);
$gene_obj->{asmbl_id} = $acc;
$model_counter++;
my $gene_id;
if (%gene_trans_map) {
$gene_id = $gene_trans_map{$acc} or die "Error, cannot locate gene identifier for transcript acc: [$acc]";
}
elsif (my $parsed_gene_id = &try_parse_gene_id_from_acc($acc)) {
$gene_id = $parsed_gene_id;
}
else {
$gene_id = "GENE.$acc";
}
my $model_id;
{
########### Important #############
## gene ID and model ID must be unique for each entry, but also decipherable for later on.
###################################
my $pcounter = 1;
$model_id = "${acc}.p$pcounter";
while ($SEEN_PROT_ID{$model_id}) {
$pcounter++;
$model_id = "${acc}.p$pcounter";
}
$SEEN_PROT_ID{$model_id} = 1;
$gene_id = "${gene_id}~~${model_id}";
###################################3
}
$gene_obj->{TU_feat_name} = $gene_id;
$gene_obj->{Model_feat_name} = $model_id;
my $cds = $gene_obj->create_CDS_sequence(\$sequence);
$gene_obj->set_CDS_phases(\$sequence);
unless ($cds) {
die "Error, no CDS for gene: " . Dumper($cds_coords_href) . Dumper($exon_coords_href);
}
my $got_start = 0;
my $got_stop = 0;
if ($protein =~ /^M/) {
$got_start = 1;
}
if ($protein =~ /\*$/) {
$got_stop = 1;
}
my $prot_type = "";
if ($got_start && $got_stop) {
$prot_type = "complete";
} elsif ($got_start) {
$prot_type = "3prime_partial";
} elsif ($got_stop) {
$prot_type = "5prime_partial";
} else {
$prot_type = "internal";
}
$gene_obj->{com_name} = "ORF type:$prot_type len:$length ($orient)";
# this header is identical between CDS and PEP (since PEP is just a direct translation of CDS for a specific translation table)
# we are currently not printing this out at the final data but it would be nice to.
my $pep_header = ">$model_id type:$prot_type len:$length gc:$genetic_code $acc:$start-$stop($orient)\n";
my $cds_header = ">$model_id type:$prot_type len:$length $acc:$start-$stop($orient)\n";
print PEP $pep_header."$protein\n";
print CDS $cds_header."$cds\n";
print GFF $gene_obj->to_GFF3_format(source => "transdecoder") . "\n";
}
}
close PEP;
close CDS;
close GFF;
&process_cmd("touch $longorf_checkpoint");
print STDERR "\n\n#################################\n"
. "### Done preparing long ORFs. ###\n"
. "##################################\n\n";
print STDERR "\tUse file: $pep_file for Pfam and/or BlastP searches to enable homology-based coding region identification.\n\n";
print STDERR "\tThen, run TransDecoder.Predict for your final coding region predictions.\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 try_parse_gene_id_from_acc {
my ($acc) = @_;
my $gene_id;
if ($acc =~ /^(\S+_c\d+_g\d+)_i\d+$/) {
$gene_id = $1;
}
elsif ($acc =~ /^(\S+_c\d+)_seq\d+$/) {
$gene_id = $1;
}
return($gene_id);
}