Last active
January 16, 2020 06:17
-
-
Save ddbj-repo/b2f7e3ac56745a63b719908945518d4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- perl -*- | |
use strict; | |
use warnings; | |
use JSON; | |
use HTTP::Request::Common; | |
use HTTP::Status qw(is_success is_client_error); | |
use LWP::UserAgent; | |
my $WABI_URL_ROOT = "http://ddbj.nig.ac.jp/wabi"; | |
my $GET_INTERVAL = 1; # Note: 1 (sec) | |
&main; | |
sub main { | |
my $blast_condition_ref = &read_blast_condition; | |
print "Load BLAST condition\n"; | |
my $request_id = &post_to_wabi($blast_condition_ref); | |
print "POST : success (Request-ID=$request_id)\n"; | |
&get_status($request_id); | |
print "Request : finished\n"; | |
my $blast_result = &get_result($request_id); | |
print "GET BLAST result : success\n"; | |
open(OUT, "> $request_id.txt"); | |
print OUT $blast_result; | |
close(OUT); | |
} | |
sub read_blast_condition { | |
my %cond = ( | |
"querySequence" => undef, | |
"datasets" => undef, | |
"database" => undef, | |
"program" => undef, | |
"parameters" => undef, | |
"format" => undef, | |
"result" => undef, | |
"address" => undef, | |
); | |
open(IN, "< blast_condition.fasta"); | |
$cond{"querySequence"} = join "", <IN>; | |
close(IN); | |
open(IN, "< blast_condition.txt"); | |
while (<IN>) { | |
chomp; | |
my @tokens = split /\t/; | |
next unless (exists $cond{$tokens[0]}); | |
$cond{$tokens[0]} = $tokens[1]; | |
} | |
close(IN); | |
return \%cond; | |
} | |
sub post_to_wabi { | |
my $blast_condition_ref = $_[0]; | |
my $ua = LWP::UserAgent->new; | |
my $res = $ua->request(POST "${WABI_URL_ROOT}/blast", $blast_condition_ref); | |
my $http_status = $res->code; | |
my $response = decode_json($res->content); | |
if (is_success($http_status)) { | |
return $response->{"requestId"}; | |
} elsif (is_client_error($http_status)) { | |
warn "BLAST condition errors: @{$response->{'error-messages'}}\n"; | |
exit 1; | |
} else { | |
warn "WABI server error\n"; | |
exit 2; | |
} | |
} | |
sub get_status { | |
my $request_id = $_[0]; | |
my $ua = LWP::UserAgent->new; | |
while (1) { | |
sleep $GET_INTERVAL; | |
my $res = $ua->request(GET "${WABI_URL_ROOT}/blast/${request_id}?info=status&format=json"); | |
my $http_status = $res->code; | |
if (is_success($http_status)) { | |
my $response = decode_json($res->content); | |
if ("waiting" eq $response->{"status"}) { | |
my $time = localtime; | |
print "$time: waiting\n"; | |
next; | |
} elsif ("running" eq $response->{"status"}) { | |
my $time = localtime; | |
print "$time: running\n"; | |
next; | |
} elsif ("finished" eq $response->{"status"}) { | |
my $time = localtime; | |
print "$time: finished\n"; | |
return; | |
} else { | |
warn "No such Request-id. time-out\n"; | |
exit 3; | |
} | |
} elsif (is_client_error($http_status)) { | |
warn "No such Request-id. Cause: time-out or wrong Request-ID\n"; | |
exit 3; | |
} else { | |
warn "WABI server error\n"; | |
exit 2; | |
} | |
} | |
} | |
sub get_result { | |
my $request_id = $_[0]; | |
my $ua = LWP::UserAgent->new; | |
my $res = $ua->request(GET "${WABI_URL_ROOT}/blast/${request_id}?info=result&format=bigfile"); | |
my $http_status = $res->code; | |
if (is_success($http_status)) { | |
return $res->content; | |
} elsif (is_client_error($http_status)) { | |
warn "BLAST result not found. Cause: time-out, not-finished, or wrong Request-ID\n"; | |
exit 4; | |
} else { | |
warn "WABI server error\n"; | |
exit 2; | |
} | |
} | |
# example.pl ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment