Example.java
Example.java
1// -*- java -*-
2
3import java.io.BufferedReader;
4import java.io.FileReader;
5import java.io.FileWriter;
6import java.io.IOException;
7import java.util.Map;
8import java.util.HashMap;
9import java.util.Date;
10
11import org.springframework.web.client.RestTemplate;
12import org.springframework.web.client.HttpClientErrorException;
13import org.springframework.web.client.DefaultResponseErrorHandler;
14import org.springframework.http.client.ClientHttpResponse;
15import org.springframework.util.MultiValueMap;
16import org.springframework.util.LinkedMultiValueMap;
17
18import net.arnx.jsonic.JSON;
19
20public class Example {
21 static class BlastCondition {
22 MultiValueMap<String, String> params;
23 BlastCondition() {
24 params = new LinkedMultiValueMap<String, String>();
25 params.set("querySequence", null);
26 params.set("datasets", null);
27 params.set("database", null);
28 params.set("program", null);
29 params.set("parameters", null);
30 params.set("format", null);
31 params.set("result", null);
32 params.set("address", null);
33 }
34 }
35
36 static final String WABI_URL_ROOT = "http://ddbj.nig.ac.jp/wabi";
37 static final long GET_INTERVAL = 1000; // Note: 1000 (milli sec)
38
39 static BlastCondition readBlastCondition() throws IOException {
40 BlastCondition cond = new BlastCondition();
41 BufferedReader reader1 = new BufferedReader(new FileReader("blast_condition.fasta"));
42 StringBuilder buffer1 = new StringBuilder();
43 for (int ch = reader1.read(); reader1.ready(); ch = reader1.read()) {
44 buffer1.append((char)ch);
45 }
46 cond.params.set("querySequence", buffer1.toString());
47 BufferedReader reader2 = new BufferedReader(new FileReader("blast_condition.txt"));
48 StringBuilder buffer2 = new StringBuilder();
49 for (String line = reader2.readLine(); null!=(line = reader2.readLine());) {
50 String[] tokens = line.split("\t");
51 if (!cond.params.containsKey(tokens[0])) continue;
52 cond.params.set(tokens[0], tokens[1]);
53 }
54 return cond;
55 }
56
57 static String postToWabi(BlastCondition blastCondition) {
58 RestTemplate rest = new RestTemplate();
59 rest.setErrorHandler(new DefaultResponseErrorHandler() {
60 public void handleError(ClientHttpResponse response) throws IOException {
61 HashMap<String, Object> result = JSON.decode(response.getBody(), HashMap.class);
62 System.err.println("BLAST condition errors: " + result.get("error-messages"));
63 super.handleError(response);
64 }
65 });
66 String response = rest.postForObject(WABI_URL_ROOT + "/blast", blastCondition.params, String.class);
67 Map<String, Object> result = JSON.decode(response, HashMap.class);
68 return (String)result.get("requestId");
69 }
70
71 static void getStatus(String requestId) throws InterruptedException {
72 try {
73 RestTemplate rest = new RestTemplate();
74 String url = WABI_URL_ROOT + "/blast/" + requestId + "?info=status&format=json";
75 while (true) {
76 Thread.sleep(GET_INTERVAL);
77 String response = rest.getForObject(url, String.class);
78 Map<String, Object> result = JSON.decode(response, HashMap.class);
79 String status = (String)result.get("status");
80 if ("waiting".equals(status)) {
81 System.out.println(new Date() + ": waiting");
82 continue;
83 } else if ("running".equals(status)) {
84 System.out.println(new Date() + ": running");
85 continue;
86 } else if ("finished".equals(status)) {
87 System.out.println(new Date() + ": finished");
88 return;
89 } else {
90 throw new RuntimeException("No such Request-id. time-out");
91 }
92 }
93 } catch (HttpClientErrorException e) {
94 throw new RuntimeException("No such Request-id. Cause: time-out or wrong Request-ID");
95 }
96 }
97
98 static String getResult(String requestId) {
99 try {
100 RestTemplate rest = new RestTemplate();
101 String response = rest.getForObject(WABI_URL_ROOT + "/blast/" + requestId + "?info=result&format=bigfile", String.class);
102 return response;
103 } catch (HttpClientErrorException e) {
104 throw new RuntimeException("BLAST result not found. Cause: time-out, not-finished, or wrong Request-ID");
105 }
106 }
107
108 public static void main(String[] args) {
109 try {
110 BlastCondition blastCondition = readBlastCondition();
111 System.out.println("Load BLAST condition");
112
113 String requestId = postToWabi(blastCondition);
114 System.out.println("POST : success (Request-ID=" + requestId + ")");
115
116 getStatus(requestId);
117 System.out.println("Request : finished");
118
119 String result = getResult(requestId);
120 System.out.println("GET BLAST result : success");
121 FileWriter writer = new FileWriter(requestId + ".txt");
122 writer.write(result);
123 writer.flush();
124 writer.close();
125 } catch (Exception e) {
126 e.printStackTrace();
127 }
128 }
129}
130
131// Example.java ends here
blast_condition.fasta
1>my query sequence 1
2CACCCTCTCTTCACTGGAAAGGACACCATGAGCACGGAAAGCATGATCCAGGACGTGGAA
3GCTGGCCGAGGAGGCGCTCCCCAGGAAGACAGCAGGGCCCCAGGGCTCCAGGCGGTGCTG
4GTTCCTCAGCCTCTTCTCCTTCCTGCTCGTGGCAGGCGCCGCCAC
blast_condition.txt
1datasets ddbjall
2database hum
3program blastn
4parameters -v 100 -b 100 -e 10 -F F -W 11
5format json
6result www
pom.xml
1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xsi:schemaLocation="http://maven.apche.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3 <modelVersion>4.0.0</modelVersion>
4 <groupId>jp.ac.nig.ddbj</groupId>
5 <artifactId>wabi-client</artifactId>
6 <version>1</version>
7
8 <properties>
9 <compiler.version>1.6</compiler.version>
10 </properties>
11
12 <dependencies>
13 <dependency>
14 <groupId>org.springframework</groupId>
15 <artifactId>spring-web</artifactId>
16 <version>3.0.5.RELEASE</version>
17 </dependency>
18 <dependency>
19 <groupId>jsonic</groupId>
20 <artifactId>jsonic</artifactId>
21 <version>1.2.5</version>
22 <scope>system</scope>
23 <systemPath>${basedir}/src/main/resources/jsonic-1.3.0.jar</systemPath>
24 </dependency>
25 <dependency>
26 <groupId>commons-httpclient</groupId>
27 <artifactId>commons-httpclient</artifactId>
28 <version>3.1</version>
29 </dependency>
30 </dependencies>
31
32 <build>
33
34 </build>
35</project>
1$ wget 'http://sourceforge.jp/frs/redir.php?m=iij&f=%2Fjsonic%2F56583%2Fjsonic-1.3.0.zip'
2$ unzip jsonic-1.3.0.zip
3$ mv jsonic-1.3.0/jsonic-1.3.0.jar src/main/resources/
4$ tree -F
5.
6├── blast_condition.fasta
7├── blast_condition.txt
8├── pom.xml
9└── src/
10 └── main/
11 ├── java/
12 │ └── Example.java
13 └── resources/
14 └── jsonic-1.3.0.jar
1$ mvn clean
2$ mvn compile
3$ mvn package
4$ mvn assembly:assembly -DdescriptorId=jar-with-dependencies
1$ java -classpath 'target/wabi-client-1.jar:target/wabi-client-1-jar-with-dependencies.jar:src/main/resources/jsonic-1.3.0.jar' Example
example.pl
1# -*- perl -*-
2
3use JSON;
4use HTTP::Request::Common;
5use HTTP::Status qw(is_success is_client_error);
6use LWP::UserAgent;
7
8$WABI_URL_ROOT = "http://ddbj.nig.ac.jp/wabi";
9$GET_INTERVAL = 1; # Note: 1 (sec)
10
11sub read_blast_condition {
12 my %cond = (
13 "querySequence" => undef,
14 "datasets" => undef,
15 "database" => undef,
16 "program" => undef,
17 "parameters" => undef,
18 "format" => undef,
19 "result" => undef,
20 "address" => undef,
21 );
22 open(IN, "< blast_condition.fasta");
23 $cond{"querySequence"} = join "", <IN>;
24 close(IN);
25 open(IN, "< blast_condition.txt");
26 while (<IN>) {
27 chomp;
28 my @tokens = split /\t/;
29 next unless (exists $cond{$tokens[0]});
30 $cond{$tokens[0]} = $tokens[1];
31 }
32 close(IN);
33 %cond;
34}
35
36sub post_to_wabi(blast_condition) {
37 my $ua = LWP::UserAgent->new;
38 my $res = $ua->request(POST "${WABI_URL_ROOT}/blast", \%blast_condition);
39 my $http_status = $res->code;
40 my $response = decode_json($res->content);
41 if (is_success($http_status)) {
42 return $response->{"requestId"};
43 } elsif (is_client_error($http_status)) {
44 warn "BLAST condition errors: @{$response->{'error-messages'}}\n";
45 exit 1;
46 } else {
47 warn "WABI server error\n";
48 exit 2;
49 }
50}
51
52sub get_status(request_id) {
53 my $ua = LWP::UserAgent->new;
54 while (true) {
55 sleep $GET_INTERVAL;
56 my $res = $ua->request(GET "${WABI_URL_ROOT}/blast/${request_id}?info=status&format=json");
57 my $http_status = $res->code;
58 if (is_success($http_status)) {
59 my $response = decode_json($res->content);
60 if ("waiting" eq $response->{"status"}) {
61 my $time = localtime;
62 print "$time: waiting\n";
63 next;
64 } elsif ("running" eq $response->{"status"}) {
65 my $time = localtime;
66 print "$time: running\n";
67 next;
68 } elsif ("finished" eq $response->{"status"}) {
69 my $time = localtime;
70 print "$time: finished\n";
71 return;
72 } else {
73 warn "No such Request-id. time-out\n";
74 exit 3;
75 }
76 } elsif (is_client_error($http_status)) {
77 warn "No such Request-id. Cause: time-out or wrong Request-ID\n";
78 exit 3;
79 } else {
80 warn "WABI server error\n";
81 exit 2;
82 }
83 }
84}
85
86sub get_result(request_id) {
87 my $ua = LWP::UserAgent->new;
88 my $res = $ua->request(GET "${WABI_URL_ROOT}/blast/${request_id}?info=result&format=bigfile");
89 my $http_status = $res->code;
90 if (is_success($http_status)) {
91 return $res->content;
92 } elsif (is_client_error($http_status)) {
93 warn "BLAST result not found. Cause: time-out, not-finished, or wrong Request-ID\n";
94 exit 4;
95 } else {
96 warn "WABI server error\n";
97 exit 2;
98 }
99}
100
101%blast_condition = &read_blast_condition;
102print "Load BLAST condition\n";
103
104$request_id = &post_to_wabi(%blast_condition);
105print "POST : success (Request-ID=$request_id)\n";
106
107&get_status($request_id);
108print "Request : finished\n";
109
110$blast_result = &get_result($request_id);
111print "GET BLAST result : success\n";
112open(OUT, "> $request_id.txt");
113print OUT $blast_result;
114close(OUT);
115
116# example.pl ends here
blast_condition.fasta
1>my query sequence 1
2CACCCTCTCTTCACTGGAAAGGACACCATGAGCACGGAAAGCATGATCCAGGACGTGGAA
3GCTGGCCGAGGAGGCGCTCCCCAGGAAGACAGCAGGGCCCCAGGGCTCCAGGCGGTGCTG
4GTTCCTCAGCCTCTTCTCCTTCCTGCTCGTGGCAGGCGCCGCCAC
blast_condition.txt
1datasets ddbjall
2database hum
3program blastn
4parameters -v 100 -b 100 -e 10 -F F -W 11
5format json
6result www
1$ cpan
2cpan[1] install JSON
3cpan[2]> install HTTP::Request::Common
4cpan[3]> install LWP::UserAgent
5cpan[4]> install HTTP::Status
6cpan[5]> quit
7$ tree -F
8.
9├── blast_condition.fasta
10├── blast_condition.txt
11└── example.pl
1$ perl example.pl
example.rb
1# -*- ruby -*-
2
3require "json"
4require "net/http"
5require "uri"
6
7class BlastCondition
8 attr_accessor :params
9 def initialize
10 self.params = {
11 "querySequence" => nil,
12 "datasets" => nil,
13 "database" => nil,
14 "program" => nil,
15 "parameters" => nil,
16 "format" => nil,
17 "result" => nil,
18 "address" => nil,
19 }
20 end
21end
22
23WABI_URL_ROOT = "http://ddbj.nig.ac.jp/wabi"
24GET_INTERVAL = 1.0 # Note: 1.0 (sec)
25
26def read_blast_condition
27 cond = BlastCondition.new
28 cond.params["querySequence"] = File.read("blast_condition.fasta")
29 File.foreach("blast_condition.txt") do |line|
30 tokens = line.chomp.split("\t")
31 next unless cond.params.has_key?(tokens[0])
32 cond.params[tokens[0]] = tokens[1]
33 end
34 cond
35end
36
37def post_to_wabi(blast_condition)
38 url = URI.parse("#{WABI_URL_ROOT}/blast")
39 res = Net::HTTP.post_form(url, blast_condition.params)
40 response = JSON.parse(res.body)
41 case res
42 when Net::HTTPSuccess
43 return response["requestId"]
44 when Net::HTTPClientError
45 warn "BLAST condition errors: #{response['error-messages'].inspect}"
46 exit 1
47 else
48 warn "WABI server error"
49 exit 2
50 end
51end
52
53def get_status(request_id)
54 url = URI.parse("#{WABI_URL_ROOT}/blast/#{request_id}?info=status&format=json")
55 while true
56 sleep GET_INTERVAL
57 res = Net::HTTP.get_response(url)
58 case res
59 when Net::HTTPSuccess
60 response = JSON.parse(res.body)
61 case response["status"]
62 when "waiting"
63 puts "#{Time.now}: waiting"
64 next
65 when "running"
66 puts "#{Time.now}: running"
67 next
68 when "finished"
69 puts "#{Time.now}: finished"
70 return
71 else
72 warn "No such Request-id. time-out"
73 exit 3
74 end
75 when Net::HTTPClientError
76 warn "No such Request-id. Cause: time-out or wrong Request-ID"
77 exit 3
78 else
79 warn "WABI server error"
80 exit 2
81 end
82 end
83end
84
85def get_result(request_id)
86 url = URI.parse("#{WABI_URL_ROOT}/blast/#{request_id}?info=result&format=bigfile")
87 res = Net::HTTP.get_response(url)
88 case res
89 when Net::HTTPSuccess
90 return res.body
91 when Net::HTTPClientError
92 warn "BLAST result not found. Cause: time-out, not-finished, or wrong Request-ID"
93 exit 4
94 else
95 warn "WABI server error"
96 exit 2
97 end
98end
99
100def main
101 blast_condition = read_blast_condition
102 puts "Load BLAST condition"
103
104 request_id = post_to_wabi(blast_condition)
105 puts "POST : success (Request-ID=#{request_id})"
106
107 get_status request_id
108 puts "Request : finished"
109
110 blast_result = get_result(request_id)
111 puts "GET BLAST result : success"
112 File.open("#{request_id}.txt", "w") do |out|
113 out.puts blast_result
114 end
115end
116
117main
118
119# example.rb ends here
blast_condition.fasta
1>my query sequence 1
2CACCCTCTCTTCACTGGAAAGGACACCATGAGCACGGAAAGCATGATCCAGGACGTGGAA
3GCTGGCCGAGGAGGCGCTCCCCAGGAAGACAGCAGGGCCCCAGGGCTCCAGGCGGTGCTG
4GTTCCTCAGCCTCTTCTCCTTCCTGCTCGTGGCAGGCGCCGCCAC
blast_condition.txt
1datasets ddbjall
2database hum
3program blastn
4parameters -v 100 -b 100 -e 10 -F F -W 11
5format json
6result www
1$ ruby example.rb
1$ tree -F
2.
3├── blast_condition.fasta
4├── blast_condition.txt
5└── example.rb
6$ ls -l
7合計 12
8-rw-r--r-- 1 user users 189 6月 6 13:28 blast_condition.fasta
9-rw-r--r-- 1 user users 110 6月 6 13:30 blast_condition.txt
10-rw-r--r-- 1 user users 2781 6月 6 13:36 example.rb
11$ cat blast_condition.fasta
12>my query sequence 1
13CACCCTCTCTTCACTGGAAAGGACACCATGAGCACGGAAAGCATGATCCAGGACGTGGAA
14GCTGGCCGAGGAGGCGCTCCCCAGGAAGACAGCAGGGCCCCAGGGCTCCAGGCGGTGCTG
15GTTCCTCAGCCTCTTCTCCTTCCTGCTCGTGGCAGGCGCCGCCAC
16$ cat blast_condition.txt
17datasets ddbjall
18database hum
19program blastn
20parameters -v 100 -b 100 -e 10 -F F -W 11
21format json
22result www
23$ ruby example.rb
24Load BLAST condition
25POST : success (Request-ID=wabi_blast_2013-0606-1336-31-681-634313)
262013-06-06 13:36:54 +0900: waiting
272013-06-06 13:36:56 +0900: waiting
282013-06-06 13:36:57 +0900: running
292013-06-06 13:37:01 +0900: finished
30Request : finished
31GET BLAST result : success
32$ ls -l
33合計 76
34-rw-r--r-- 1 user users 189 6月 6 13:28 blast_condition.fasta
35-rw-r--r-- 1 user users 110 6月 6 13:30 blast_condition.txt
36-rw-r--r-- 1 user users 2781 6月 6 13:36 example.rb
37-rw-r--r-- 1 user users 63806 6月 6 13:37 wabi_blast_2013-0606-1336-31-681-634313.txt
38$ head wabi_blast_2013-0606-1336-31-681-634313.txt
39BLASTN 2.2.26 [Sep-21-2011]
40
41Reference: Altschul, Stephen F., Thomas L. Madden, Alejandro A. Schaffer,
42Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997),
43"Gapped BLAST and PSI-BLAST: a new generation of protein database search
44programs", Nucleic Acids Res. 25:3389-3402.
45
46Query= my query sequence 1
47 (165 letters)
48$ tail wabi_blast_2013-0606-1336-31-681-634313.txt
49Length adjustment: 20
50Effective length of query: 145
51Effective length of database: 5,078,288,990
52Effective search space: 736351903550
53Effective search space used: 736351903550
54X1: 11 (21.8 bits)
55X2: 15 (29.7 bits)
56X3: 50 (99.1 bits)
57S1: 13 (26.3 bits)
58S2: 18 (36.2 bits)
59$
1BLASTN 2.2.26 [Sep-21-2011] 2 3Reference: Altschul, Stephen F., Thomas L. Madden, Alejandro A. Schaffer, 4Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997), 5"Gapped BLAST and PSI-BLAST: a new generation of protein database search 6programs", Nucleic Acids Res. 25:3389-3402. 7 8Query= my query sequence 1 9 (165 letters) 10 11Database: hum 12 602,798 sequences; 5,090,344,950 total letters 13 14Searching..................................................done 15 16 Score E 17Sequences producing significant alignments: (bits) Value 18 19Z15026|Z15026.1 Homo sapiens TNFa and gene for tumor necrosis fa... 232 8e-59 20Y14768|Y14768.1 Homo sapiens DNA, cosmid clones TN62 and TN82. 232 8e-59 21X02910|X02910.1 Human gene for tumor necrosis factor (TNF-alpha). 232 8e-59 22X01394|X01394.1 Human mRNA for tumor necrosis factor. 232 8e-59 23M26331|M26331.1 Human tumor necrosis factor gene, complete cds. 232 8e-59 24M16441|M16441.1 Human tumor necrosis factor and lymphotoxin gene... 232 8e-59 25M10988|M10988.1 Human tumor necrosis factor (TNF) mRNA. 232 8e-59 26CR942185|CR942185.8 Human DNA sequence from clone DAMC-81L12 on ... 232 8e-59 27CR753892|CR753892.5 Human DNA sequence from clone DADB-70P7 on c... 232 8e-59 28BX927320|BX927320.5 Human DNA sequence from clone DAMA-25N12 on ... 232 8e-59 29BX248519|BX248519.7 Human DNA sequence from clone DASS-280D8 on ... 232 8e-59 30BC028148|BC028148.1 Homo sapiens tumor necrosis factor (TNF supe... 232 8e-59 31BA000025|BA000025.2 Homo sapiens genomic DNA, chromosome 6p21.3,... 232 8e-59 32AY214167|AY214167.1 Homo sapiens tumor necrosis factor (TNF supe... 232 8e-59 33AY066019|AY066019.1 Homo sapiens tumor necrosis factor (TNF supe... 232 8e-59 34AL929587|AL929587.5 Human DNA sequence from clone DAQB-87N14 on ... 232 8e-59 35AL662847|AL662847.6 Human DNA sequence from clone CH502-270M2 on... 232 8e-59 36AL662801|AL662801.7 Human DNA sequence from clone CH501-296P20 o... 232 8e-59 37AF129756|AF129756.1 Homo sapiens MSH55 gene, partial cds; and CL... 232 8e-59 38AB202113|AB202113.1 Homo sapiens LTA, TNF genes for lymphotoxin ... 232 8e-59 39AB103618|AB103618.1 Homo sapiens gene for LTA protein, TNFA prot... 232 8e-59 40AB088112|AB088112.1 Homo sapiens LTA, TNFA genes for lymphotoxin... 232 8e-59 41HQ201306|HQ201306.2 Homo sapiens tumor necrosis factor-alpha mRN... 194 2e-47 42FJ795028|FJ795028.1 Homo sapiens tumor necrosis factor alpha (TN... 192 7e-47 43U42625|U42625.1 Human tumor necrosis factor alpha (TNFA) gene, a... 72 2e-10 44AY799806|AY799806.1 Homo sapiens tumor necrosis factor (TNFA) ge... 58 3e-06 45AL353631|AL353631.17 Human DNA sequence from clone RP11-5N16 on ... 42 0.15 46EU332840|EU332840.1 Homo sapiens angiotensin I converting enzyme... 40 0.61 47AY436326|AY436326.1 Homo sapiens angiotensin I converting enzyme... 40 0.61 48AF118569|AF118569.1 Homo sapiens angiotensin I converting enzyme... 40 0.61 49AF022145|AF022145.1 Homo sapiens angiotensin converting enzyme g... 40 0.61 50AC113554|AC113554.9 Homo sapiens chromosome 17, clone CTD-2501B8... 40 0.61 51Y14314|Y14314.1 Homo sapiens mRNA for IgE autoantigen. 38 2.4 52BX648500|BX648500.1 Homo sapiens mRNA; cDNA DKFZp686H1978 (from ... 38 2.4 53BT006637|BT006637.1 Homo sapiens squamous cell carcinoma antigen... 38 2.4 54BC050546|BC050546.1 Homo sapiens MYB binding protein (P160) 1a, ... 38 2.4 55BC001058|BC001058.2 Homo sapiens squamous cell carcinoma antigen... 38 2.4 56BC000641|BC000641.2 Homo sapiens MYB binding protein (P160) 1a, ... 38 2.4 57AY093673|AY093673.1 Homo sapiens p53-activated protein-2 (PAP2) ... 38 2.4 58AL451132|AL451132.9 Human DNA sequence from clone RP11-7N10 on c... 38 2.4 59AL442163|AL442163.5 Human chromosome 14 DNA sequence BAC R-90P16... 38 2.4
(後略)