-
Notifications
You must be signed in to change notification settings - Fork 7
/
GGDCrobot.pl
executable file
·283 lines (223 loc) · 7.58 KB
/
GGDCrobot.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
#!/usr/bin/env perl
#
# INGLÊS/ENGLISH
# 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.
# http://www.gnu.org/copyleft/gpl.html
#
#
# PORTUGUÊS/PORTUGUESE
# Este programa é distribuído na expectativa de ser útil aos seus
# usuários, porém NÃO TEM NENHUMA GARANTIA, EXPLÍCITAS OU IMPLÍCITAS,
# COMERCIAIS OU DE ATENDIMENTO A UMA DETERMINADA FINALIDADE. Consulte
# a Licença Pública Geral GNU para maiores detalhes.
# http://www.gnu.org/copyleft/gpl.html
#
# Copyright (C) 2019 Universidade Estadual Paulista "Júlio de Mesquita Filho"
#
# Universidade Estadual Paulista "Júlio de Mesquita Filho" (UNESP)
# Faculdade de Ciências Agrárias e Veterinárias (FCAV)
# Laboratório de Bioinformática (LB)
#
# Daniel Guariz Pinheiro
# http://www.fcav.unesp.br
#
# $Id$
=head1 NAME
=head1 SYNOPSIS
=head1 ABSTRACT
=head1 DESCRIPTION
Arguments:
-h/--help Help
-l/--level Log level [Default: FATAL]
OFF
FATAL
ERROR
WARN
INFO
DEBUG
TRACE
ALL
=head1 AUTHOR
Daniel Guariz Pinheiro E<lt>[email protected]<gt>
Copyright (C) 2019 Universidade Estadual Paulista "Júlio de Mesquita Filho"
=head1 LICENSE
GNU General Public License
http://www.gnu.org/copyleft/gpl.html
=cut
use strict;
use warnings;
use Readonly;
use Getopt::Long;
use vars qw/$LOGGER/;
INIT {
use Log::Log4perl qw/:easy/;
Log::Log4perl->easy_init($FATAL);
$LOGGER = Log::Log4perl->get_logger($0);
}
my ($level, $qfile, $rfiles, $email, $blast, $threshold, $wait, $gfile);
Usage("Too few arguments") if $#ARGV < 0;
GetOptions( "h|?|help" => sub { &Usage(); },
"l|level=s"=> \$level,
"q|qfile=s"=>\$qfile,
"r|rfiles=s"=>\$rfiles,
"g|gfile=s"=>\$gfile,
"e|email=s"=>\$email,
"b|blast=s"=>\$blast,
"t|threshold=i"=>\$threshold,
"w|wait=i"=>\$wait
) or &Usage();
if ($level) {
my %LEVEL = (
'OFF' =>$OFF,
'FATAL' =>$FATAL,
'ERROR' =>$ERROR,
'WARN' =>$WARN,
'INFO' =>$INFO,
'DEBUG' =>$DEBUG,
'TRACE' =>$TRACE,
'ALL' =>$ALL);
$LOGGER->logdie("Wrong log level ($level). Choose one of: ".join(', ', keys %LEVEL)) unless (exists $LEVEL{$level});
Log::Log4perl->easy_init($LEVEL{$level});
}
$wait||=5;
$threshold||=50;
$LOGGER->logdie("Threshold must be in this range: 0 - 100") if (($threshold>100)||($threshold<0));
$LOGGER->logdie("Missing query file") unless ($qfile);
$LOGGER->logdie("Wrong query file ($qfile)") unless (-e $qfile);
my @multipleRefGenomes;
my @rfile;
my $gcontent;
if ($rfiles) {
@rfile=split(/,/, $rfiles);
foreach my $rf (@rfile) {
$LOGGER->logdie("Wrong reference file ($rf) in reference file list") unless (-e $rf);
my $rcontent='';
open(REF, "<", $rf);
while(<REF>) {
$rcontent.=$_;
}
close(REF);
push(@multipleRefGenomes, {
filename => basename($rf),
content => $rcontent,
content_type => 'text/plain'
}
);
}
} elsif ($gfile) {
$gcontent='';
open(REFACCS, "<", $gfile);
while(<REFACCS>) {
if ($_=~/^\S+/) {
$gcontent.=$_;
}
}
close(REFACCS);
} else {
$LOGGER->logdie("Missing at least one reference fasta file or one text file with Genbank accs");
}
$blast||='GBDP2_BLASTPLUS';
$LOGGER->logdie("Missing email") unless ($email);
$LOGGER->logdie("Wrong email ($email)") unless ($email=~/^[^\@ :;,\]\[\{\}\?\\\/\<\>\|\#]+\@[^\@ :;,\]\[\{\}\?\\\/\<\>\|\#]+$/);
my %avail;
@avail{'GBDP2_BLASTPLUS', 'GBDP2_BLAT', 'GBDP2_BLASTZ', 'GBDP2_WU-BLAST', 'GBDP2_MUMMER'} = ();
$LOGGER->logdie("Wrong program $blast. Please select one of these: GBDP2_BLASTPLUS,GBDP2_BLAT,GBDP2_BLASTZ,GBDP2_WU-BLAST,GBDP2_MUMMER") unless (exists $avail{$blast});
use strict;
use warnings;
use File::Basename;
use HTTP::Tiny;
use HTTP::Tiny::Multipart;
while (&try()>$threshold) {
print STDERR "WAIT! Current slot usage is above $threshold ...\n";
&waitmin($wait);
}
my $url = 'https://ggdc.dsmz.de/submit_ggdc_job.php';
my $http = HTTP::Tiny->new();
my $qcontent='';
open(QUERY, "<", $qfile);
while(<QUERY>) {
$qcontent.=$_;
}
close(QUERY);
my $hr_input_params = {
targetName => '',
targetGenome => {
filename => basename($qfile),
content => $qcontent,
content_type => 'text/plain'
},
'email' => $email,
'blastVariant' => $blast
};
if (scalar(@rfile)) {
$LOGGER->info("Job: ".basename($qfile)." X ".scalar(@rfile)." genome files\n");
$hr_input_params->{'multipleRefGenomes[]'} = \@multipleRefGenomes;
} elsif ($gcontent) {
my $nlines = () = $gcontent =~ /\n/g;
# Add 1 if the last line doesn't end with a newline
$nlines++ if $gcontent !~ /\n\z/;
$LOGGER->info("Job: ".basename($qfile)." X ".$nlines." genome accessions\n");
$hr_input_params->{'refGenbank'} = $gcontent;
} else {
$LOGGER->logdie("Job sent failed. Wrong input parameters");
}
my $response = $http->post_multipart( $url, $hr_input_params);
if (length $response->{content}) {
if ($response->{content}=~/your job with ID <b>([^<]+)<\/b> has been successfully submitted/) {
print "Your job with ID $1 has been successfully submitted and the results should be found at $email email box\n";
} else {
my $randnum=rand(10000);
open(OUT,">", "/tmp/GGDCrobot_error_$randnum.html") or $LOGGER->logdie("$!");
print OUT $response->{content};
close(OUT);
$LOGGER->logdie("Something wrong with this submission. See the content of /tmp/GGDCrobot_error_$randnum.html");
}
}
# Subroutines
sub Usage {
my ($msg) = @_;
Readonly my $USAGE => <<"END_USAGE";
Daniel Guariz Pinheiro (dgpinheiro\@gmail.com)
(c)2019 Universidade Estadual Paulista "Júlio de Mesquita Filho"
Usage
$0 [-h/--help] [-l/--level <LEVEL>]
Argument(s)
-h --help Help
-l --level Log level [Default: FATAL]
-q --qfile Query fasta file
-r --rfiles Reference fasta files separated by comma
-g --gfile Text file with Genbank references. One line per genome. If genome consists of multiple accessions provide these either separated by blanks (e.g. 'AE000782 AE000783 AE000784') or as a range (e.g. 'AE000782-AE000784')
-e --email Email
-b --blast blastVariant {GBDP2_BLASTPLUS,GBDP2_BLAT,GBDP2_BLASTZ,GBDP2_WU-BLAST,GBDP2_MUMMER} [Default: GBDP2_BLASTPLUS]
-t --threshold Threshold for current slot usage (wait if current slot usage > than this threshold to send a job) Range: 0-100 [Default: 50]
-w --wait Wait time for check current slot usage in minutes [Default: 5]
END_USAGE
print STDERR "\nERR: $msg\n\n" if $msg;
print STDERR qq[$0 ] . q[$Revision$] . qq[\n];
print STDERR $USAGE;
exit(1);
}
sub try {
my $resp = HTTP::Tiny->new->get('https://ggdc.dsmz.de/ggdc.php');
die "Failed!\n" unless $resp->{success};
if (length $resp->{content}) {
my @content=split(/\n/, $resp->{content});
foreach my $l (@content) {
if ($l=~/div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="3" aria-valuemin="0" aria-valuemax="100" style="width: (\d+)%;">/) {
my $n = $1;
return $n;
}
}
} else {
die "Error. Cannot get content";
}
}
sub waitmin {
my ($n) = $_;
$n||=1;
sleep($n*60);
}