Created
December 14, 2017 06:22
-
-
Save ddbj-repo/d2ead08e9b9664418c34e292392f1ca8 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
// -*- java -*- | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.Map; | |
import java.util.HashMap; | |
import java.util.Date; | |
import org.springframework.web.client.RestTemplate; | |
import org.springframework.web.client.HttpClientErrorException; | |
import org.springframework.web.client.DefaultResponseErrorHandler; | |
import org.springframework.http.client.ClientHttpResponse; | |
import org.springframework.util.MultiValueMap; | |
import org.springframework.util.LinkedMultiValueMap; | |
import net.arnx.jsonic.JSON; | |
public class Example { | |
static class BlastCondition { | |
MultiValueMap<String, String> params; | |
BlastCondition() { | |
params = new LinkedMultiValueMap<String, String>(); | |
params.set("querySequence", null); | |
params.set("datasets", null); | |
params.set("database", null); | |
params.set("program", null); | |
params.set("parameters", null); | |
params.set("format", null); | |
params.set("result", null); | |
params.set("address", null); | |
} | |
} | |
static final String WABI_URL_ROOT = "http://ddbj.nig.ac.jp/wabi"; | |
static final long GET_INTERVAL = 1000; // Note: 1000 (milli sec) | |
static BlastCondition readBlastCondition() throws IOException { | |
BlastCondition cond = new BlastCondition(); | |
BufferedReader reader1 = new BufferedReader(new FileReader("blast_condition.fasta")); | |
StringBuilder buffer1 = new StringBuilder(); | |
for (int ch = reader1.read(); reader1.ready(); ch = reader1.read()) { | |
buffer1.append((char)ch); | |
} | |
cond.params.set("querySequence", buffer1.toString()); | |
BufferedReader reader2 = new BufferedReader(new FileReader("blast_condition.txt")); | |
StringBuilder buffer2 = new StringBuilder(); | |
for (String line = reader2.readLine(); null!=(line = reader2.readLine());) { | |
String[] tokens = line.split("\t"); | |
if (!cond.params.containsKey(tokens[0])) continue; | |
cond.params.set(tokens[0], tokens[1]); | |
} | |
return cond; | |
} | |
static String postToWabi(BlastCondition blastCondition) { | |
RestTemplate rest = new RestTemplate(); | |
rest.setErrorHandler(new DefaultResponseErrorHandler() { | |
public void handleError(ClientHttpResponse response) throws IOException { | |
HashMap<String, Object> result = JSON.decode(response.getBody(), HashMap.class); | |
System.err.println("BLAST condition errors: " + result.get("error-messages")); | |
super.handleError(response); | |
} | |
}); | |
String response = rest.postForObject(WABI_URL_ROOT + "/blast", blastCondition.params, String.class); | |
Map<String, Object> result = JSON.decode(response, HashMap.class); | |
return (String)result.get("requestId"); | |
} | |
static void getStatus(String requestId) throws InterruptedException { | |
try { | |
RestTemplate rest = new RestTemplate(); | |
String url = WABI_URL_ROOT + "/blast/" + requestId + "?info=status&format=json"; | |
while (true) { | |
Thread.sleep(GET_INTERVAL); | |
String response = rest.getForObject(url, String.class); | |
Map<String, Object> result = JSON.decode(response, HashMap.class); | |
String status = (String)result.get("status"); | |
if ("waiting".equals(status)) { | |
System.out.println(new Date() + ": waiting"); | |
continue; | |
} else if ("running".equals(status)) { | |
System.out.println(new Date() + ": running"); | |
continue; | |
} else if ("finished".equals(status)) { | |
System.out.println(new Date() + ": finished"); | |
return; | |
} else { | |
throw new RuntimeException("No such Request-id. time-out"); | |
} | |
} | |
} catch (HttpClientErrorException e) { | |
throw new RuntimeException("No such Request-id. Cause: time-out or wrong Request-ID"); | |
} | |
} | |
static String getResult(String requestId) { | |
try { | |
RestTemplate rest = new RestTemplate(); | |
String response = rest.getForObject(WABI_URL_ROOT + "/blast/" + requestId + "?info=result&format=bigfile", String.class); | |
return response; | |
} catch (HttpClientErrorException e) { | |
throw new RuntimeException("BLAST result not found. Cause: time-out, not-finished, or wrong Request-ID"); | |
} | |
} | |
public static void main(String[] args) { | |
try { | |
BlastCondition blastCondition = readBlastCondition(); | |
System.out.println("Load BLAST condition"); | |
String requestId = postToWabi(blastCondition); | |
System.out.println("POST : success (Request-ID=" + requestId + ")"); | |
getStatus(requestId); | |
System.out.println("Request : finished"); | |
String result = getResult(requestId); | |
System.out.println("GET BLAST result : success"); | |
FileWriter writer = new FileWriter(requestId + ".txt"); | |
writer.write(result); | |
writer.flush(); | |
writer.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
// Example.java ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment