/
sra2fastq.pl
executable file
·436 lines (345 loc) · 13.8 KB
/
sra2fastq.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
#!/usr/bin/perl
# sra2fastq.pl
# ver 0.5
# 2014/12/19
#
# Po-E (Paul) Li
# B-11
# Los Alamos National Lab.
# Change log
# ver 0.5 (2016/11/02)
# - support downloading sequences from NCBI-SRA, EBI-ENA and DDBJ.
# - auto switch download sites.
# ver 0.3 (2016/10/28)
# - Switch sequecne source to ERA
# ver 0.2 (2015/01/06)
# - Input SRA accessions support studies (SRP*/ERP*/DRP*), experiments (SRX*/ERX*/DRX*), samples (SRS*/ERS*/DRS*), runs (SRR*/ERR*/DRR*), or submissions (SRA*/ERA*/DRA*)
# - Provide "--platform-restrict" option to limit the platform of SRAs
# - Provide "--concat" option to concatenate multiple FASTQ files into a singal (single-end) or two (paired-end) files
# - Remove dependency of File::Which
# ver 0.1
# - Initial release
use strict;
use Getopt::Long;
use FindBin qw($Bin);
$|=1;
$ENV{PATH} = "$Bin:$Bin/../bin/:$ENV{PATH}";
checkRequiredExec();
my $local_mode = 0;
my $OUTDIR = ".";
my $PLAT_R;
my $CLEAN;
my $FLSZ_R;
my $RUN_R;
my $http_proxy = $ENV{HTTP_PROXY} || $ENV{http_proxy};
my $ftp_proxy = $ENV{FTP_PROXY} || $ENV{ftp_proxy};
$http_proxy = "--proxy $http_proxy " if ($http_proxy);
$ftp_proxy = "--proxy $ftp_proxy " if ($ftp_proxy);
my $curl = "curl -A \"Mozilla/5.0\" -L";
my $gzip;
my $res=GetOptions(
'outdir|d=s' => \$OUTDIR,
'platform-restrict|pr=s' => \$PLAT_R,
'filesize-restrict|fr=s' => \$FLSZ_R,
'runs-restrict|r|rr=s' => \$RUN_R,
'clean|n' => \$CLEAN,
'help|?' => sub{usage()}
) || &usage();
if ( !scalar @ARGV ) { &usage(); }
## init temp directory
`rm -rf $OUTDIR/sra2fastq_temp/`;
`mkdir -p $OUTDIR/sra2fastq_temp/merged/`;
## Main
my $readInfo;
my $total_runs=0;
my $total_size=0;
my ($dl_status, $dl_runs, $dl_size);
foreach my $acc ( @ARGV ){
die "ERROR: $acc is not a valid SRA/ERA/DRA number." if $acc !~ /^(SR|ER|DR)/;
my $sra_type = "ByRun";
$sra_type = "ByExp" if $acc =~ /^(SRX|ERX|DRX)/;
$sra_type = "BySample" if $acc =~ /^(SRS|ERS|DRS)/;
$sra_type = "ByStudy" if $acc =~ /^(SRP|ERP|DRP)/;
print STDERR "Processing $acc ($sra_type)...\n";
#gathering reads information from NCBI-SRA / EBI-ENA
$readInfo = getReadInfo( $acc, $readInfo); #retrieve runs infor for each accession
if( !defined $readInfo->{$acc} ){
die "ERROR: No sequence found. Please check if $acc is a valid SRA/ERA/DRA number or your internet connection.\n";
}
foreach my $run_acc (keys %{$readInfo->{$acc}})
{
#check if runs exceed the limit
$total_runs++;
if( $RUN_R && $RUN_R < $total_runs ){
die "ERROR: Run(s) exceed the limit ($RUN_R MB).\n";
}
#check platform
my $platform = $readInfo->{$acc}->{$run_acc}->{platform};
if( $PLAT_R && $platform !~ /$PLAT_R/i ){
die "ERROR: $readInfo->{$acc}->{$run_acc}->{platform} platfrom detected. Only $PLAT_R is allowed.\n";
}
#download FASTQ
$dl_status = getSraFastqToolkits( $readInfo->{$acc}->{$run_acc}, $run_acc );
$dl_status = getDdbjFastq( $readInfo->{$acc}->{$run_acc}, $run_acc ) if $dl_status eq 'failed';
$dl_status = getSraFastq( $readInfo->{$acc}->{$run_acc}, $run_acc ) if $dl_status eq 'failed';
$dl_status = getEnaFastq( $readInfo->{$acc}->{$run_acc}, $run_acc ) if $dl_status eq 'failed';
die "ERROR: Please check your internet connection.\n" if $dl_status eq 'failed';
#merging fastqs in multiple runs into a single file
if( -e "$OUTDIR/sra2fastq_temp/${run_acc}_1.fastq.gz" ){
`cat $OUTDIR/sra2fastq_temp/${run_acc}_1.fastq.gz >> $OUTDIR/sra2fastq_temp/merged/${acc}.1.fastq.gz`;
$total_size += -s "$OUTDIR/sra2fastq_temp/${run_acc}_1.fastq.gz";
`rm -rf ${run_acc}_1.fastq.gz`;
}
if( -e "$OUTDIR/sra2fastq_temp/${run_acc}_2.fastq.gz" ){
`cat $OUTDIR/sra2fastq_temp/${run_acc}_2.fastq.gz >> $OUTDIR/sra2fastq_temp/merged/${acc}.2.fastq.gz`;
$total_size += -s "$OUTDIR/sra2fastq_temp/${run_acc}_2.fastq.gz";
`rm -rf ${run_acc}_2.fastq.gz`;
}
if( -e "$OUTDIR/sra2fastq_temp/${run_acc}.fastq.gz" ){
`cat $OUTDIR/sra2fastq_temp/${run_acc}.fastq.gz >> $OUTDIR/sra2fastq_temp/merged/${acc}.fastq.gz`;
$total_size += -s "$OUTDIR/sra2fastq_temp/${run_acc}.fastq.gz";
`rm -rf ${run_acc}.fastq.gz`;
}
if( $FLSZ_R && $FLSZ_R < $total_size/1024/1024 ){
die "ERROR: downloaded file size exceed limitation ($FLSZ_R MB).\n";
}
else{
print STDERR "Succesfully downloaded $run_acc.\n";
}
}
print STDERR "Finished downloading acc# $acc.\n";
}
`mv $OUTDIR/sra2fastq_temp/merged/*.gz $OUTDIR`;
if( $CLEAN ){
print STDERR "\nCleaning up...";
`rm -rf $OUTDIR/sra2fastq_temp`;
print STDERR "Done.\n";
}
## Subroutines ########################################################################
sub getSraFastq {
my ($info, $run_acc) = @_;
print STDERR "Retrieving FASTQ for $run_acc from NCBI SRA (online converting)...\n";
my $platform = $info->{platform};
my $library = $info->{library};
my $url = "https://trace.ncbi.nlm.nih.gov/Traces/sra/sra.cgi?cmd=dload&run_list=$run_acc&format=fastq";
print STDERR "Downloading $url...\n";
my $ec = system("$curl $http_proxy -o $OUTDIR/sra2fastq_temp/$run_acc.fastq.gz \"$url\"");
#Deinterleaving if paired-end reads
if( $platform =~ /illu/i && $library =~ /pair/i ){
print STDERR "Paired-end reads found. Deinterleaving...";
my $di_flag = system("gzip -dc $OUTDIR/sra2fastq_temp/$run_acc.fastq.gz | deinterleave_fastq.sh $OUTDIR/sra2fastq_temp/${run_acc}_1.fastq.gz $OUTDIR/sra2fastq_temp/${run_acc}_2.fastq.gz compress");
return "failed" if $di_flag > 0;
print STDERR "Done.\n";
`rm -f $OUTDIR/sra2fastq_temp/$run_acc.fastq.gz`;
}
return "success";
}
sub getSraFastqToolkits {
my ($info, $run_acc) = @_;
print STDERR "Retrieving FASTQ for $run_acc with NCBI SRA Toolkit...\n";
my $platform = $info->{platform};
my $url = $info->{url};
my $filename = $run_acc;
print STDERR "Downloading $url...\n";
my $ec = system("$curl $http_proxy -o $OUTDIR/sra2fastq_temp/$filename \"$url\"");
if( $ec > 0 ){
print STDERR "Failed to download SRA file from $url.\n";
return "failed";
}
print STDERR "Done.\n";
#check downloaded file
my $filesize = -s "$OUTDIR/sra2fastq_temp/$filename";
if( !$filesize ){
print STDERR "Failed to download SRA file from $url.\n";
return "failed";
}
#dump fastq from SRA file
my $options = "-gzip ";
$options .= "--split-files " if $platform =~ /illu/i;
$options .= "--split-files -B " if $platform =~ /solid/;
print STDERR "Running fastq-dump with options $options...\n";
$ec = system("fastq-dump $options --outdir '$OUTDIR/sra2fastq_temp' $OUTDIR/sra2fastq_temp/$filename");
if( $ec > 0 ){
print STDERR "Failed to run fastq-dump from $OUTDIR/sra2fastq_temp/$filename.\n";
return "failed";
}
print STDERR "Done.\n";
#clean up temp files
`rm $OUTDIR/sra2fastq_temp/$filename`;
return "success";
}
sub getDdbjFastq {
my ($info, $run_acc) = @_;
print STDERR "Retrieving FASTQ for $run_acc from DDBJ...\n";
my $platform = $info->{platform};
my $library = $info->{library};
my $exp_acc = $info->{exp_acc};
my $sub_acc = $info->{sub_acc};
my $platform = $info->{platform};
my ($sra_acc_first6) = $sub_acc =~ /^(\w{3}\d{3})/;
my $ec;
my $url = "ftp://ftp.ddbj.nig.ac.jp/ddbj_database/dra/fastq/$sra_acc_first6/$sub_acc/$exp_acc";
if( $platform =~ /illu/i && $library =~ /pair/i ){
print STDERR "Downloading $url/${run_acc}_1.fastq.bz2...\n";
$ec = system("$curl $ftp_proxy -o $OUTDIR/sra2fastq_temp/${run_acc}_1.fastq.bz2 \"$url/${run_acc}_1.fastq.bz2\"");
print STDERR "finished.\n";
print STDERR "Downloading $url/${run_acc}_2.fastq.bz2...\n";
$ec = system("$curl $ftp_proxy -o $OUTDIR/sra2fastq_temp/${run_acc}_2.fastq.bz2 \"$url/${run_acc}_2.fastq.bz2\"");
print STDERR "finished.\n";
}
print STDERR "Downloading $url/$run_acc.fastq.bz2...\n";
$ec = system("$curl $ftp_proxy -o $OUTDIR/sra2fastq_temp/$run_acc.fastq.bz2 \"$url/$run_acc.fastq.bz2\"");
print STDERR "finished.\n";
my $total_size = 0;
if( -s "$OUTDIR/sra2fastq_temp/${run_acc}_1.fastq.bz2" ){
print STDERR "Convering bz2 to gz...\n";
$ec = system("bunzip2 -c < $OUTDIR/sra2fastq_temp/${run_acc}_1.fastq.bz2 | gzip -c > $OUTDIR/sra2fastq_temp/${run_acc}_1.fastq.gz");
if( $ec > 0 ){
print STDERR "ERROR: failed to convert bz2 to gz.\n";
return "failed";
}
$total_size += -s "$OUTDIR/sra2fastq_temp/${run_acc}.fastq.gz";
print STDERR "Done.\n";
}
if( -s "$OUTDIR/sra2fastq_temp/${run_acc}_2.fastq.bz2" ){
print STDERR "Convering bz2 to gz...\n";
$ec = system("bunzip2 -c < $OUTDIR/sra2fastq_temp/${run_acc}_2.fastq.bz2 | gzip -c > $OUTDIR/sra2fastq_temp/${run_acc}_2.fastq.gz");
if( $ec > 0 ){
print STDERR "ERROR: failed to convert bz2 to gz.\n";
return "failed";
}
$total_size += -s "$OUTDIR/sra2fastq_temp/${run_acc}.fastq.gz";
print STDERR "Done.\n";
}
if( -s "$OUTDIR/sra2fastq_temp/${run_acc}.fastq.bz2" ){
print STDERR "Convering bz2 to gz...\n";
$ec = system("bunzip2 -c < $OUTDIR/sra2fastq_temp/${run_acc}.fastq.bz2 | gzip -c > $OUTDIR/sra2fastq_temp/${run_acc}.fastq.gz");
if( $ec > 0 ){
print STDERR "ERROR: failed to convert bz2 to gz.\n";
return "failed";
}
$total_size += -s "$OUTDIR/sra2fastq_temp/${run_acc}.fastq.gz";
print STDERR "Done.\n";
}
if( $total_size == 0 ){
print STDERR "ERROR: failed to download FASTQ and convert FASTQ files from DDBJ.\n";
return "failed";
}
return "success";
}
sub getEnaFastq {
my ($info, $run_acc) = @_;
print STDERR "Retrieving FASTQ for $run_acc from EBI-ENA...\n";
foreach my $i ( keys %{$info->{ena_fastq_ftp}} ){
my $url = $info->{ena_fastq_ftp}->{$i}->{url};
my $md5 = $info->{ena_fastq_ftp}->{$i}->{md5};
my $size = $info->{ena_fastq_ftp}->{$i}->{size};
my ($filename) = $url =~ /\/([^\/]+)$/;
print STDERR "Downloading $url...\n";
system("$curl $ftp_proxy -o $OUTDIR/sra2fastq_temp/$filename \"ftp://$url\"");
#check downloaded file
my $filesize = -s "$OUTDIR/sra2fastq_temp/$filename";
if( !$filesize ){
print STDERR "Failed to download $filename from $url.\n";
return "failed";
}
if( $filesize != $size ){
print STDERR "$OUTDIR/sra2fastq_temp/$filename incompleted/corrupted -- file sizes mismatch.\n";
return "failed";
}
#check md5
my $md5sum = `md5sum $OUTDIR/sra2fastq_temp/$filename`;
if( $md5sum !~ /^$md5/ ){
print STDERR "$OUTDIR/sra2fastq_temp/$filename corrupted -- md5 checksum mismatch.\n";
return "failed";
}
print STDERR "Done.\n";
}
return "success";
}
sub getReadInfo {
my ($acc, $readInfo) = @_;
my $url;
my $web_result;
my @lines;
print STDERR "Retrieving run(s) information from NCBI-SRA...\n";
#get info from NCBI-SRA
$url = "https://trace.ncbi.nlm.nih.gov/Traces/sra/sra.cgi?save=efetch&db=sra&rettype=runinfo&term=$acc";
print STDERR "Retrieving run acc# from NCBI-SRA $url...\n";
$web_result = `$curl $http_proxy "$url" 2>/dev/null`;
my @lines = split '\n', $web_result;
print STDERR "$#lines run(s) found from NCBI-SRA.\n";
foreach my $line (@lines){
next if $line =~ /^Run/;
chomp;
my @f = split ',', $line;
my $sub_acc = $f[42]; #submission
my $exp_acc = $f[10]; #experiment_accession
my $run_acc = $f[0]; #run
my $platform = $f[18]; #platform
my $library = $f[15]; #LibraryLayout
my $url = $f[9]; #download_path
$readInfo->{$acc}->{$run_acc}->{exp_acc} = $exp_acc;
$readInfo->{$acc}->{$run_acc}->{sub_acc} = $sub_acc;
$readInfo->{$acc}->{$run_acc}->{platform} = $platform;
$readInfo->{$acc}->{$run_acc}->{library} = $library;
$readInfo->{$acc}->{$run_acc}->{url} = $url;
}
##### get info from EBI-ENA when NCBI-SRA fails ####
print STDERR "Retrieving run(s) information from EBI-ENA...\n";
$url = "https://www.ebi.ac.uk/ena/data/warehouse/filereport?accession=$acc&result=read_run";
print STDERR "Retrieving run acc# from EBI-ENA $url...\n";
$web_result = `$curl $http_proxy "$url" 2>/dev/null`;
die "ERROR: Failed to retrieve sequence information for $acc.\n" if $web_result !~ /^study_accession/;
my @lines = split '\n', $web_result;
print STDERR "$#lines run(s) found from EBI-ENA.\n";
foreach my $line (@lines){
next if $line =~ /^study_accession/;
chomp;
my @f = split '\t', $line;
my $sub_acc = $f[6]; #submission_accession
my $exp_acc = $f[4]; #experiment_accession
my $run_acc = $f[5]; #run_accession
my $platform = $f[9]; #instrument_platform
my $library = $f[13]; #library_layout
my @url = split ';', $f[29]; #fastq_ftp
my @md5 = split ';', $f[28]; #fastq_md5
my @size = split ';', $f[27]; #fastq_bytes
$readInfo->{$acc}->{$run_acc}->{platform} = $platform;
$readInfo->{$acc}->{$run_acc}->{exp_acc} = $exp_acc;
$readInfo->{$acc}->{$run_acc}->{sub_acc} = $sub_acc;
$readInfo->{$acc}->{$run_acc}->{library} = $library;
for( my $i=0; $i<=$#url; $i++ ){
$readInfo->{$acc}->{$run_acc}->{fastq_ftp}->{$i}->{url} = $url[$i];
$readInfo->{$acc}->{$run_acc}->{fastq_ftp}->{$i}->{md5} = $md5[$i];
$readInfo->{$acc}->{$run_acc}->{fastq_ftp}->{$i}->{size} = $size[$i];
}
}
return $readInfo;
}
sub checkRequiredExec {
die "ERROR: 'gzip' not found.\n" unless `which gzip`;
die "ERROR: 'curl' not found.\n" unless `which curl`;
}
sub usage {
print <<__END__;
[DESCRIPTION]
A script retrieves sequence project in FASTQ files from
NCBI-SRA/EBI-ENA/DDBJ database using `curl`. Input accession number
supports studies (SRP*/ERP*/DRP*), experiments (SRX*/ERX*/DRX*),
samples (SRS*/ERS*/DRS*), runs (SRR*/ERR*/DRR*), or submissions
(SRA*/ERA*/DRA*).
[USAGE]
$0 [OPTIONS] <Accession#> (<Accession# 2> <Accession# 3>...)
[OPTIONS]
--outdir|d Output directory
--clean clean up temp directory
--platform-restrict Only allow a specific platform
--filesize-restrict (in MB) Only allow to download less than a specific
total size of files.
--run-restrict Only allow download less than a specific number
of runs.
--help/h/? display this help
__END__
exit();
}