-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrf-index
More file actions
executable file
·569 lines (376 loc) · 22.2 KB
/
rf-index
File metadata and controls
executable file
·569 lines (376 loc) · 22.2 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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/usr/bin/env perl
##
# RF Index
# RNA Framework [http://www.rnaframework.com]
#
# Author: Danny Incarnato (dincarnato[at]rnaframework.com)
# Summary: Builds/retrieves RF Map transcriptome reference indexes
#
# This program is free software, and can be redistribute and/or modified
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# Please see <http://www.gnu.org/licenses/> for more informations.
##
use strict;
use Digest::MD5 qw(md5_hex);
use FindBin qw($Bin);
use Getopt::Long qw(:config no_ignore_case);
use HTTP::Tiny;
use lib $Bin . "/lib";
use Core::Mathematics qw(:all);
use Core::Utils;
use Data::IO::XML;
use Net::DB::MySQL;
use Term::Constants qw(:screen);
use Term::Progress;
use Term::Table;
$|++;
my ($dbh, $sth, $assembly, $annotation,
$reference, $output, $bb, $bt,
$prefix, $timeout, $help, $ret,
$overwrite, $bowtie2, $name,
$list, $prebuilt, $codonly, $nconly,
$sed, $listanno, $host, $port,
$threads, $unspliced, $nchromosomes,
$refChrOnly, %columns, %chromosomes, %ids);
do {
local $SIG{__WARN__} = sub { };
GetOptions( "h|help" => \$help,
"g|genome-assembly=s" => \$assembly,
"a|annotation=s" => \$annotation,
"r|reference=s" => \$reference,
"rco|ref-chr-only" => \$refChrOnly,
"o|output-dir=s" => \$output,
"ow|overwrite" => \$overwrite,
"t|timeout=i" => \$timeout,
"b|bowtie-build=s" => \$bb,
"e|bedtools=s" => \$bt,
"b2|bowtie2" => \$bowtie2,
"lp|list-prebuilt" => \$list,
"la|list-annotations" => \$listanno,
"pb|prebuilt=i" => \$prebuilt,
"co|coding-only" => \$codonly,
"no|noncoding-only" => \$nconly,
"H|host=s" => \$host,
"P|port=i" => \$port,
"p|processors=i" => \$threads,
"u|unspliced" => \$unspliced,
"n|gene-name" => \$name ) or help(1);
};
help() if ($help);
# Default
$threads ||= 1;
$timeout ||= 180;
$host ||= "genome-mysql.cse.ucsc.edu";
$port ||= 3306;
$assembly ||= "mm9";
$annotation ||= "refFlat";
$bb ||= $bowtie2 ? which("bowtie2-build") : which("bowtie-build");
$bt ||= which("bedtools");
$sed = which("sed");
if ($list || $prebuilt) {
my ($xmlIO, $table, $tree, %indexes);
$xmlIO = Data::IO::XML->new(file=>"http://www.incarnatolab.com/datasets/RNAframework/indexes.xml");
$table = Term::Table->new(indent => 2);
$table->head("ID", "Name", "Description");
$tree = $xmlIO->read();
if ($tree->hasNode("/data/index")) {
foreach my $indexTree ($tree->getNode("/data/index")) {
my $id = $indexTree->attribute("id");
$indexes{$id} = { name => $indexTree->getNode("name")->value(),
desc => $indexTree->getNode("description")->value(),
file => $indexTree->getNode("file")->value(),
folder => $indexTree->getNode("folder")->value(),
date => $indexTree->getNode("date")->value(),
md5 => $bowtie2 ? $indexTree->getNode("md5/bowtie2")->value() : $indexTree->getNode("md5/bowtie1")->value() };
$table->row($id, $indexes{$id}->{name}, $indexes{$id}->{desc});
}
}
else { die "\n [!] Error: Malformed response from server. If the issue persists, please report it on GitHub\n\n"; }
if ($list) {
print "\n";
$table->print();
print "\n\n";
}
else {
die "\n [!] Error: Invalid prebuilt index ID." .
"\n Please use -l (or --list) to list available indexes\n\n" if (!exists $indexes{$prebuilt});
my ($url, $ua, $downloaded, $reply);
$url = "http://www.incarnatolab.com/datasets/RNAframework/indexes/" . ($bowtie2 ? "bowtie2/" : "bowtie1/") . $indexes{$prebuilt}->{file};
$ua = HTTP::Tiny->new(timeout => $timeout);
$downloaded = 0;
if (!defined $output) {
$output = $indexes{$prebuilt}->{folder};
$output .= $bowtie2 ? "_bt2/" : "_bt/";
}
$output =~ s/\/?$/\//;
print "\n[+] Making output directory...";
if (-e $output) {
if ($overwrite) {
my $error = rmtree($output);
die "\n\n [!] Error: " . $error . "\n\n" if ($error);
}
else { die "\n [!] Error: Output directory already exists." .
"\n Please use -ow (or --overwrite) to overwrite output directory\n\n"; }
}
if (my $error = mktree($output . "logs/", "755")) { die "\n\n [!] Error: Unable to create output directory ($error)\n\n"; }
print "\n[+] Downloading " . $indexes{$prebuilt}->{name} . " prebuilt index";
open(my $wh, ">:raw", $output . $indexes{$prebuilt}->{file}) or die "\n\n [!] Error: Unable to write prebuilt index to file (" . $! . ")\n\n";
select((select($wh), $|=1)[0]);
$reply = $ua->get( $url,
{ data_callback => sub {
my ($chunk, $response) = @_;
my $size = $response->{headers}->{"content-length"};
$downloaded += length($chunk);
print CLRRET . "[+] Downloading " . $indexes{$prebuilt}->{name} . " prebuilt index [" . sprintf("%.2f", $downloaded / $size * 100) . "\%]";
print $wh $chunk; },
headers => { "Accept" => "application/x-gzip" } } );
close($wh);
if (!$reply->{success}) {
print CLRRET . "[+] Downloading " . $indexes{$prebuilt}->{name} . " prebuilt index [FAIL]";
unlink(glob($output . "*"));
rmtree($output);
die "\n\n [!] Error: Failed to download " . $indexes{$prebuilt}->{name} . " prebuilt index (" . $reply->{reason} . ")\n\n";
}
print CLRRET . "[+] Downloading " . $indexes{$prebuilt}->{name} . " prebuilt index [Checking MD5 checksum]";
if (md5_hex(slurpFile($output . $indexes{$prebuilt}->{file})) ne $indexes{$prebuilt}->{md5}) {
print CLRRET . "[+] Downloading " . $indexes{$prebuilt}->{name} . " prebuilt index [FAIL]";
die "\n\n [!] Error: Index MD5 checksum validation failed." .
"\n Please report the issue to: dincarnato[at]rnaframework.com.\n\n";
}
print CLRRET . "[+] Downloading " . $indexes{$prebuilt}->{name} . " prebuilt index [Decompressing]";
$ret = system("cd " . $output . " && tar -xzvf " . $indexes{$prebuilt}->{file} . " > \"logs/decompression.log\" 2>&1");
die "\n\n [!] Error: Index decompression failed." .
"\n Please check the log file and ensure that the \"tar\" utility is in PATH.\n\n" if ($ret);
$indexes{$prebuilt}->{folder} .= $bowtie2 ? "_bt2" : "_bt";
$ret = system("cd " . $output . " && mv " . $indexes{$prebuilt}->{folder} . "/* . && " .
"rm -R " . $indexes{$prebuilt}->{folder} . " > /dev/null 2>&1");
print CLRRET . "[+] Downloading " . $indexes{$prebuilt}->{name} . " prebuilt index [DONE]";
unlink($output . $indexes{$prebuilt}->{file});
print "\n[+] Successfully retrieved " . $indexes{$prebuilt}->{name} . " prebuilt index..." .
"\n[+] All done.\n\n";
}
}
else {
$prefix = $assembly . "_" . $annotation;
$prefix .= "_unspliced" if ($unspliced);
$output = $prefix . ($bowtie2 ? "_bt2/" : "_bt/") if (!defined $output);
$output =~ s/\/?$/\//;
##
# Input validation
##
die "\n [!] Error: No genome assembly specified\n\n" unless(defined $assembly);
die "\n [!] Error: No genes annotation specified\n\n" unless(defined $annotation);
die "\n [!] Error: Parameters -co and -no are mutually exclusive\n\n" if ($codonly &&
$nconly);
die "\n [!] Error: Timeout value must be an integer greater than 0\n\n" if (!ispositive($timeout) ||
!isint($timeout) ||
$timeout == 0);
die "\n [!] Error: Provided reference FASTA file doesn't exist\n\n" if (defined $reference &&
!-e $reference);
if (-e $output && !$listanno) {
if ($overwrite) {
my $error = rmtree($output);
die "\n [!] Error: " . $error . "\n\n" if ($error);
}
else { die "\n [!] Error: Output directory already exists." .
"\n Please use -ow (or --overwrite) to overwrite output directory\n\n"; }
}
if (!defined $bb) { die "\n [!] Error: " . ($bowtie2 ? "bowtie2" : "bowtie") . "-build is not in PATH\n\n"; }
elsif (!-e $bb) { die "\n [!] Error: " . ($bowtie2 ? "bowtie2" : "bowtie") . "-build doesn't exist\n\n"; }
elsif (!-x $bb) { die "\n [!] Error: " . ($bowtie2 ? "bowtie2" : "bowtie") . "-build is not executable\n\n"; }
if (!defined $bt) { die "\n [!] Error: BEDTools is not in PATH\n\n"; }
elsif (!-e $bt) { die "\n [!] Error: BEDTools doesn't exist\n\n"; }
elsif (!-x $bt) { die "\n [!] Error: BEDTools is not executable\n\n"; }
$ret = `$bt --version`;
if ($ret =~ m/bedtools v(\d+)\.(\d+)/) {
my ($v1, $v2) = ($1, $2);
die "\n [!] Error: RF Index requires BEDTools v2.31.0 or greater (Detected: v" . $v1 . "." . $v2 . ")\n\n" if ($v1 < 2 || $v2 < 31);
}
else { warn "\n [!] Warning: Unable to detect BEDTools version\n"; }
$bt .= " getfasta"; # aka fastaFromBed
print "\n[+] Connecting to UCSC genome database (" . $host . ":" . $port . ")...";
$dbh = Net::DB::MySQL->new( database => $assembly,
host => $host,
port => $port,
user => "genomep",
password => "password",
timeout => $timeout );
if (!$dbh->connect()) {
die "\n\n [!] Error: Connection to UCSC genome database failed (" . $dbh->error() . ")." .
"\n Please check the genome assembly and try again.\n\n";
}
if ($listanno) {
my (@tables);
if ($dbh->query("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like 'exonCount' AND TABLE_SCHEMA like '$assembly' ORDER by TABLE_NAME")) {
print "\n[+] Listing available gene annotation tables:\n\n";
while (my $table = $dbh->read()) { push(@tables, $table->{TABLE_NAME}); }
if (@tables) { print " [*] " . join("\n [*] ", @tables); }
else { print " [!] Error: No available gene annotation table found"; }
print "\n\n";
}
else { die "\n\n [!] Error: Failed to retrieve gene annotations' list (" . $dbh->error() . ")\n\n"; }
}
else {
print "\n[+] Connected. Searching annotation...";
if (!table_exists($annotation)) {
rmtree($output);
die "\n\n [!] Error: Table \"" . $annotation . "\" doesn't exist." .
"\n Please check annotation's name and try again\n\n";
}
print "\n[+] Annotation found. Validating columns...";
if ($dbh->query("SELECT * FROM `$annotation` LIMIT 0,1")) {
my %columns = map { $_ => 1} $dbh->columns();
for (qw(name chrom strand txStart txEnd cdsStart
cdsEnd exonCount exonStarts exonEnds)) {
die "\n\n [!] Error: Table \"$annotation\" doesn't look like a genes annotation (missing \"" . $_ . "\" column)\n\n" if (!exists $columns{$_});
}
}
else { die "\n\n [!] Error: Failed to obtain annotation from UCSC SQL server (" . $dbh->error() . ")\n\n"; }
print "\n[+] Making output directory...";
if (my $error = mktree($output . "logs/", "755")) { die "\n\n [!] Error: Unable to create output directory ($error)\n\n"; }
print "\n[+] Downloading annotation data. Please wait...";
if ($dbh->query("SELECT * FROM `$annotation`")) {
while (my $ref = $dbh->read()) {
next if ($refChrOnly && ($ref->{chrom} =~ /^chrUn|_(?:fix|random|alt)$/));
my $id = $name && (defined $ref->{name2} || defined $ref->{geneName}) ? $ref->{name2} || $ref->{geneName} : $ref->{name};
my (@starts, @ends, @lengths);
@starts = split(/,/, $ref->{exonStarts});
@ends = split(/,/, $ref->{exonEnds});
for (0 .. $#starts) {
push(@lengths, $ends[$_] - $starts[$_]);
$starts[$_] -= $ref->{txStart};
}
next if (($codonly && $ref->{cdsStart} == $ref->{cdsEnd}) ||
($nconly && $ref->{cdsStart} != $ref->{cdsEnd}));
# Left for debug, should never happen
die "\n\n [!] Error: Truncated SQL response. Please try again.\n\n" if (!@lengths);
if ((exists $ids{$id} && $ids{$id}->{length} < sum(@lengths)) || !exists $ids{$id}) {
$ids{$id} = { length => sum(@lengths),
entry => join("\t", $ref->{chrom}, $ref->{txStart}, $ref->{txEnd}, $id, 0, $ref->{strand}, $ref->{cdsStart}, $ref->{cdsEnd}, "0,0,0", $ref->{exonCount}) .
"\t" . join(",", @lengths) . ",\t" . join(",", @starts) . ",\n"};
}
$chromosomes{$ref->{chrom}} = 1;
}
open(my $wh, ">", $output . $prefix . ".bed") or die "\n\n [!] Error: Unable to write annotation output BED file (" . $! . ")\n\n";
select((select($wh), $|=1)[0]);
print $wh $ids{$_}->{entry} for (sort keys %ids);
close($wh);
$dbh->close();
}
else {
rmtree($output);
die "\n\n [!] Error: Failed to obtain annotation from UCSC SQL server (" . $dbh->error() . ")\n\n";
}
$nchromosomes = scalar(keys %chromosomes);
my $startTime = time;
if (!defined $reference) {
print "\n\n [i] Note: No reference FASTA file has been provided." .
"\n RF Index will now try to download the reference genome sequence" .
"\n from the UCSC DAS server." .
"\n This may take up to hours, depending on your connection's speed.\n" .
"\n[+] Downloading sequence data for " . keys(%chromosomes) . " chromosomes. Please wait...\n\n";
open(my $wh, ">", $output . $assembly . ".fa") or die "\n [!] Error: Unable to write reference output FASTA file (" . $! . ")\n\n";
select((select($wh), $|=1)[0]);
foreach my $chromosome (sort keys %chromosomes) {
my ($ua, $content, $reply, $status,
$xmlIO, $size, $progressBar);
$ua = HTTP::Tiny->new(timeout => $timeout);
$size = 0;
$progressBar = Term::Progress->new(colored => 1);
$reply = $ua->get("http://genome.ucsc.edu/cgi-bin/das/$assembly/dna?segment=$chromosome",
{ data_callback => sub {
my ($chunk, $response) = @_;
# We estimate the size of the data from the DNA tag
if (!$size && $chunk =~ m/<DNA length="(\d+)">/) {
$size = $1;
$progressBar->max($size);
$progressBar->init($chromosome);
}
$progressBar->update(length($chunk));
$content .= $chunk; } } );
($status) = $reply->{headers}->{"x-das-status"} =~ m/(\d+)/;
if (!$reply->{success} || $status != 200) {
die "\n [!] Error: Failed to download chromosome $chromosome (Server response: " . $reply->{reason} . ")\n\n";
}
else {
$progressBar->status("Parsing XML data");
$xmlIO = Data::IO::XML->new(data => $content);
my $tree = $xmlIO->read();
if ($tree->hasNode("/DASDNA/SEQUENCE/DNA")) {
print $wh ">" . $chromosome . "\n" . uc($tree->getNode("/DASDNA/SEQUENCE/DNA")->value()) . "\n";
$progressBar->status("Done");
}
else { die CLRRET . " [!] Error: Malformed response from UCSC DAS server\n\n"; }
}
}
close($wh);
$reference = $output . $assembly . ".fa";
print CLRRET;
}
if (!keys %chromosomes) {
unlink(glob($output . "*"));
rmtree($output);
die "\n [!] Error: Download failed for all chromosomes\n\n";
}
else {
print CLRRET . " [i] All chromosomes downloaded (Elapsed time: " . formatTime(time - $startTime) . ")\n";
}
print "\n[+] Extracting transcript sequences...";
$ret = system($bt . " -fi \"" . $reference . "\" -fo \"" . $output . $prefix . ".fa\" -bed \"" . $output . $prefix . ".bed\" -nameOnly -s" . ($unspliced ? " " : " -split ") . "2> \"" . $output . "logs/fastaFromBed.log\"");
die "\n\n [!] Error: Transcript sequences extraction failed." .
"\n Please check the log file and ensure that the installed bedTools version is >= 2.31.0.\n\n" if ($ret);
# Added to remove strand information left by newer BEDTools versions
system($sed . " -i" . ($^O eq "darwin" ? " '' " : " ") . "'s/([\\+-])//g' \"" . $output . $prefix . ".fa\" 2>/dev/null") if ($sed);
print "\n[+] Building Bowtie " . ($bowtie2 ? "v2" : "v1") . " transcriptome index from sequences. Please wait...";
$ret = system($bb . " --threads " . $threads . " \"" . $output . $prefix . ".fa\" \"" . $output . $prefix . "\" > \"" . $output . "logs/bowtie-build.log\" 2>&1");
die "\n\n [!] Error: Bowtie transcriptome index generation failed. Please check the log file.\n\n" if ($ret);
print "\n[+] Successfully built Bowtie index for " . $annotation . " annotation on assembly " . $assembly . "..." .
"\n[+] All done.\n\n";
}
}
rmtree($output . "logs/");
sub table_exists {
if ($dbh->query("SHOW TABLES LIKE '" . $_[0] . "'")) { return(@{$dbh->columns()}); }
return;
}
sub help {
print "\n [!] Error: Invalid option. Please check the help\n" if ($_[0]);
die <<HELP;
RF Index (v$Core::Utils::VERSION)
RNA Framework [http://www.rnaframework.com]
Author: Danny Incarnato (dincarnato[at]rnaframework.com)
Summary: Builds/retrieves RF Map transcriptome reference indexes
Usage: rf-index [Options]
Options Description
-b2 or --bowtie2 Generates/retrieves a Bowtie v2 index (Default: Bowtie v1)
-p or --processors <int> Number of processors to use (Default: 1)
-o or --output-dir <string> Bowtie index output directory (Default: automatically defined in index retrieval mode,
<assembly>_<annotation> in index building mode)
-ow or --overwrite Overwrites output directory (if the specified path already exists)
Prebuilt indexes retrieval
-lp or --list-prebuilt Lists available RNA Framework prebuilt reference indexes
-pb or --prebuilt <int> Retrieves the prebuilt reference index with the given ID (>=1, Default: none)
Note: to obtain a list of available prebuild indexes, use -lp (or --list-prebuilt)
Reference index building
-H or --host <string> UCSC server hostname (Default: genome-mysql.cse.ucsc.edu)
-P or --port <int> UCSC server port (Default: 3306)
-g or --genome-assembly <string> Genome assembly for the species of interest (Default: mm9)
-a or --annotation <string> Name of the UCSC table containing the gene annotation (Default: refFlat)
-la or --list-annotations Lists available gene annotation UCSC tables
-rco or --ref-chr-only Chromosome patches ("_fix" and "_alt") and unassigned genomic chunks ("_random" and "chrUn")
will be ignored
-n or --gene-name If available, gene name/symbol will be used ("name2"/"geneName" columns)
-co or --coding-only Builds reference index using only protein-coding transcripts
-no or --noncoding-only Builds reference index using only non-coding transcripts
-u or --unspliced Builds reference index using pre-mRNA sequences (including introns)
-t or --timeout <int> Connection's timeout in seconds (Default: 180)
-r or --reference <string> Path to a FASTA file containing chromosome (or scaffold) sequences for the chosen genome assembly
Note: if no file is specified, RF Index will try to obtain sequences from UCSC DAS server.
This process may take up to hours, depending on your connection's speed.
-b or --bowtie-build <string> Path to bowtie-build (or bowtie2-build) executable (Default: assumes bowtie(2)-build is in PATH)
-e or --bedtools <string> Path to bedtools executable (Default: assumes bedtools is in PATH)
HELP
}