WABI BLAST 使用例

Javaの使用例

コード例

Example.java
// -*- 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
        
blast_condition.fasta
>my query sequence 1
CACCCTCTCTTCACTGGAAAGGACACCATGAGCACGGAAAGCATGATCCAGGACGTGGAA
GCTGGCCGAGGAGGCGCTCCCCAGGAAGACAGCAGGGCCCCAGGGCTCCAGGCGGTGCTG
GTTCCTCAGCCTCTTCTCCTTCCTGCTCGTGGCAGGCGCCGCCAC
        
blast_condition.txt
datasets        ddbjall
database        hum
program blastn
parameters      -v 100 -b 100 -e 10 -F F -W 11
format  json
result  www
        
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apche.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>jp.ac.nig.ddbj</groupId>
  <artifactId>wabi-client</artifactId>
  <version>1</version>

  <properties>
    <compiler.version>1.6</compiler.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>jsonic</groupId>
      <artifactId>jsonic</artifactId>
      <version>1.2.5</version>
      <scope>system</scope>
      <systemPath>${basedir}/src/main/resources/jsonic-1.3.0.jar</systemPath>
    </dependency>
    <dependency>
      <groupId>commons-httpclient</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.1</version>
    </dependency>
  </dependencies>

  <build>

  </build>
</project>
        

実行例

前準備 (一度だけ実施する必要あり)
$ wget 'http://sourceforge.jp/frs/redir.php?m=iij&f=%2Fjsonic%2F56583%2Fjsonic-1.3.0.zip'
$ unzip jsonic-1.3.0.zip
$ mv jsonic-1.3.0/jsonic-1.3.0.jar src/main/resources/
$ tree -F
.
├── blast_condition.fasta
├── blast_condition.txt
├── pom.xml
└── src/
    └── main/
        ├── java/
        │   └── Example.java
        └── resources/
            └── jsonic-1.3.0.jar
        
ビルド
$ mvn clean
$ mvn compile
$ mvn package
$ mvn assembly:assembly -DdescriptorId=jar-with-dependencies
        
実行手順
$ java -classpath 'target/wabi-client-1.jar:target/wabi-client-1-jar-with-dependencies.jar:src/main/resources/jsonic-1.3.0.jar' Example
        

Perlの使用例

コード例

example.pl
# -*- perl -*-

use JSON;
use HTTP::Request::Common;
use HTTP::Status qw(is_success is_client_error);
use LWP::UserAgent;

$WABI_URL_ROOT = "http://ddbj.nig.ac.jp/wabi";
$GET_INTERVAL = 1;              # Note: 1 (sec)

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);
    %cond;
}

sub post_to_wabi(blast_condition) {
    my $ua = LWP::UserAgent->new;
    my $res = $ua->request(POST "${WABI_URL_ROOT}/blast", %blast_condition);
    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 errorn";
        exit 2;
    }
}

sub get_status(request_id) {
    my $ua = LWP::UserAgent->new;
    while (true) {
        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: waitingn";
                next;
            } elsif ("running" eq $response->{"status"}) {
                my $time = localtime;
                print "$time: runningn";
                next;
            } elsif ("finished" eq $response->{"status"}) {
                my $time = localtime;
                print "$time: finishedn";
                return;
            } else {
                warn "No such Request-id. time-outn";
                exit 3;
            }
        } elsif (is_client_error($http_status)) {
            warn "No such Request-id. Cause: time-out or wrong Request-IDn";
            exit 3;
        } else {
            warn "WABI server errorn";
            exit 2;
        }
    }
}

sub get_result(request_id) {
    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-IDn";
        exit 4;
    } else {
        warn "WABI server errorn";
        exit 2;
    }
}

%blast_condition = &read_blast_condition;
print "Load BLAST conditionn";

$request_id = &post_to_wabi(%blast_condition);
print "POST : success (Request-ID=$request_id)n";

&get_status($request_id);
print "Request : finishedn";

$blast_result = &get_result($request_id);
print "GET BLAST result : successn";
open(OUT, "> $request_id.txt");
print OUT $blast_result;
close(OUT);

# example.pl ends here
        
blast_condition.fasta
>my query sequence 1
CACCCTCTCTTCACTGGAAAGGACACCATGAGCACGGAAAGCATGATCCAGGACGTGGAA
GCTGGCCGAGGAGGCGCTCCCCAGGAAGACAGCAGGGCCCCAGGGCTCCAGGCGGTGCTG
GTTCCTCAGCCTCTTCTCCTTCCTGCTCGTGGCAGGCGCCGCCAC
        
blast_condition.txt
datasets        ddbjall
database        hum
program blastn
parameters      -v 100 -b 100 -e 10 -F F -W 11
format  json
result  www
        

実行例

前準備 (一度だけ実施する必要あり)
$ cpan
cpan[1] install JSON
cpan[2]> install HTTP::Request::Common
cpan[3]> install LWP::UserAgent
cpan[4]> install HTTP::Status
cpan[5]> quit
$ tree -F
.
├── blast_condition.fasta
>├── blast_condition.txt
└── example.pl
        
実行手順
$ perl example.pl
        

Rubyの使用例

コード例

example.rb
# -*- ruby -*-

require "json" 
require "net/http" 
require "uri"

class BlastCondition
  attr_accessor :params
  def initialize
    self.params = {
      "querySequence" => nil,
      "datasets"      => nil,
      "database"      => nil,
      "program"       => nil,
      "parameters"    => nil,
      "format"        => nil,
      "result"        => nil,
      "address"       => nil,
    }
  end
end

WABI_URL_ROOT = "http://ddbj.nig.ac.jp/wabi" 
GET_INTERVAL = 1.0              # Note: 1.0 (sec)

def read_blast_condition
  cond = BlastCondition.new
  cond.params["querySequence"] = File.read("blast_condition.fasta")
  File.foreach("blast_condition.txt") do |line|
    tokens = line.chomp.split("\t")
    next unless cond.params.has_key?(tokens[0])
    cond.params[tokens[0]] = tokens[1]
  end
  cond
end

def post_to_wabi(blast_condition)
  url = URI.parse("#{WABI_URL_ROOT}/blast")
  res = Net::HTTP.post_form(url, blast_condition.params)
   response = JSON.parse(res.body)
  case res
  when Net::HTTPSuccess
    return response["requestId"]
  when Net::HTTPClientError
    warn "BLAST condition errors: #{response['error-messages'].inspect}" 
    exit 1
  else
    warn "WABI server error"
    exit 2
  end
end

def get_status(request_id)
  url = URI.parse("#{WABI_URL_ROOT}/blast/#{request_id}?info=status&format=json")
  while true
    sleep GET_INTERVAL
    res = Net::HTTP.get_response(url)
    case res
    when Net::HTTPSuccess
      response = JSON.parse(res.body)
      case response["status"]
      when "waiting" 
        puts "#{Time.now}: waiting" 
        next
      when "running" 
        puts "#{Time.now}: running" 
        next
      when "finished" 
        puts "#{Time.now}: finished" 
        return
      else
        warn "No such Request-id. time-out" 
        exit 3
      end
    when Net::HTTPClientError
      warn "No such Request-id. Cause: time-out or wrong Request-ID" 
      exit 3
    else
      warn "WABI server error" 
      exit 2
    end
  end
end

def get_result(request_id)
  url = URI.parse("#{WABI_URL_ROOT}/blast/#{request_id}?info=result&format=bigfile")
  res = Net::HTTP.get_response(url)
  case res
  when Net::HTTPSuccess
    return res.body
  when Net::HTTPClientError
    warn "BLAST result not found. Cause: time-out, not-finished, or wrong Request-ID"
    exit 4
  else
    warn "WABI server error" 
    exit 2
  end
end

def main
  blast_condition = read_blast_condition
  puts "Load BLAST condition"

  request_id = post_to_wabi(blast_condition)
  puts "POST : success (Request-ID=#{request_id})" 

  get_status request_id
  puts "Request : finished" 

  blast_result = get_result(request_id)
  puts "GET BLAST result : success"
  File.open("#{request_id}.txt", "w") do |out|
    out.puts blast_result
  end
end

main

# example.rb ends here
        
blast_condition.fasta
>my query sequence 1
CACCCTCTCTTCACTGGAAAGGACACCATGAGCACGGAAAGCATGATCCAGGACGTGGAA
GCTGGCCGAGGAGGCGCTCCCCAGGAAGACAGCAGGGCCCCAGGGCTCCAGGCGGTGCTG
GTTCCTCAGCCTCTTCTCCTTCCTGCTCGTGGCAGGCGCCGCCAC
        
blast_condition.txt
datasets        ddbjall
database        hum
program blastn
parameters      -v 100 -b 100 -e 10 -F F -W 11
format  json
result  www
        

実行例

実行手順
$ ruby example.rb
        
実際の例
$ tree -F
.
├── blast_condition.fasta
├── blast_condition.txt
└── example.rb
$ ls -l
合計 12
-rw-r--r-- 1 user users  189  6  6 13:28 blast_condition.fasta
-rw-r--r-- 1 user users  110  6  6 13:30 blast_condition.txt
-rw-r--r-- 1 user users 2781  6  6 13:36 example.rb
$ cat blast_condition.fasta
>my query sequence 1
CACCCTCTCTTCACTGGAAAGGACACCATGAGCACGGAAAGCATGATCCAGGACGTGGAA
GCTGGCCGAGGAGGCGCTCCCCAGGAAGACAGCAGGGCCCCAGGGCTCCAGGCGGTGCTG
GTTCCTCAGCCTCTTCTCCTTCCTGCTCGTGGCAGGCGCCGCCAC
$ cat blast_condition.txt
datasets        ddbjall
database        hum
program blastn
parameters      -v 100 -b 100 -e 10 -F F -W 11
format  json
result  www
$ ruby example.rb
Load BLAST condition
POST : success (Request-ID=wabi_blast_2013-0606-1336-31-681-634313)
2013-06-06 13:36:54 +0900: waiting
2013-06-06 13:36:56 +0900: waiting
2013-06-06 13:36:57 +0900: running
2013-06-06 13:37:01 +0900: finished
Request : finished
GET BLAST result : success
$ ls -l
合計 76
-rw-r--r-- 1 user users   189  6  6 13:28 blast_condition.fasta
-rw-r--r-- 1 user users   110  6  6 13:30 blast_condition.txt
-rw-r--r-- 1 user users  2781  6  6 13:36 example.rb
-rw-r--r-- 1 user users 63806  6  6 13:37 wabi_blast_2013-0606-1336-31-681-634313.txt
$ head wabi_blast_2013-0606-1336-31-681-634313.txt
BLASTN 2.2.26 [Sep-21-2011]

Reference: Altschul, Stephen F., Thomas L. Madden, Alejandro A. Schaffer,
Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997),
"Gapped BLAST and PSI-BLAST: a new generation of protein database search
programs",  Nucleic Acids Res. 25:3389-3402.

Query= my query sequence 1
         (165 letters)
$ tail wabi_blast_2013-0606-1336-31-681-634313.txt
Length adjustment: 20
Effective length of query: 145
Effective length of database: 5,078,288,990
Effective search space: 736351903550
Effective search space used: 736351903550
X1: 11 (21.8 bits)
X2: 15 (29.7 bits)
X3: 50 (99.1 bits)
S1: 13 (26.3 bits)
S2: 18 (36.2 bits)
$
        

実行結果例

wabi_blast_2013-0606-1336-31-681-634313.txt
BLASTN 2.2.26 [Sep-21-2011]

Reference: Altschul, Stephen F., Thomas L. Madden, Alejandro A. Schaffer,
Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997),
"Gapped BLAST and PSI-BLAST: a new generation of protein database search
programs",  Nucleic Acids Res. 25:3389-3402.

Query= my query sequence 1
         (165 letters)

Database: hum
           602,798 sequences; 5,090,344,950 total letters

Searching..................................................done

                                                                 Score    E
Sequences producing significant alignments:                      (bits) Value

Z15026|Z15026.1 Homo sapiens TNFa and gene for tumor necrosis fa...   232   8e-59
Y14768|Y14768.1 Homo sapiens DNA, cosmid clones TN62 and TN82.        232   8e-59
X02910|X02910.1 Human gene for tumor necrosis factor (TNF-alpha).     232   8e-59
X01394|X01394.1 Human mRNA for tumor necrosis factor.                 232   8e-59
M26331|M26331.1 Human tumor necrosis factor gene, complete cds.       232   8e-59
M16441|M16441.1 Human tumor necrosis factor and lymphotoxin gene...   232   8e-59
M10988|M10988.1 Human tumor necrosis factor (TNF) mRNA.               232   8e-59
CR942185|CR942185.8 Human DNA sequence from clone DAMC-81L12 on ...   232   8e-59
CR753892|CR753892.5 Human DNA sequence from clone DADB-70P7 on c...   232   8e-59
BX927320|BX927320.5 Human DNA sequence from clone DAMA-25N12 on ...   232   8e-59
BX248519|BX248519.7 Human DNA sequence from clone DASS-280D8 on ...   232   8e-59
BC028148|BC028148.1 Homo sapiens tumor necrosis factor (TNF supe...   232   8e-59
BA000025|BA000025.2 Homo sapiens genomic DNA, chromosome 6p21.3,...   232   8e-59
AY214167|AY214167.1 Homo sapiens tumor necrosis factor (TNF supe...   232   8e-59
AY066019|AY066019.1 Homo sapiens tumor necrosis factor (TNF supe...   232   8e-59
AL929587|AL929587.5 Human DNA sequence from clone DAQB-87N14 on ...   232   8e-59
AL662847|AL662847.6 Human DNA sequence from clone CH502-270M2 on...   232   8e-59
AL662801|AL662801.7 Human DNA sequence from clone CH501-296P20 o...   232   8e-59
AF129756|AF129756.1 Homo sapiens MSH55 gene, partial cds; and CL...   232   8e-59
AB202113|AB202113.1 Homo sapiens LTA, TNF genes for lymphotoxin ...   232   8e-59
AB103618|AB103618.1 Homo sapiens gene for LTA protein, TNFA prot...   232   8e-59
AB088112|AB088112.1 Homo sapiens LTA, TNFA genes for lymphotoxin...   232   8e-59
HQ201306|HQ201306.2 Homo sapiens tumor necrosis factor-alpha mRN...   194   2e-47
FJ795028|FJ795028.1 Homo sapiens tumor necrosis factor alpha (TN...   192   7e-47
U42625|U42625.1 Human tumor necrosis factor alpha (TNFA) gene, a...    72   2e-10
AY799806|AY799806.1 Homo sapiens tumor necrosis factor (TNFA) ge...    58   3e-06
AL353631|AL353631.17 Human DNA sequence from clone RP11-5N16 on ...    42   0.15
EU332840|EU332840.1 Homo sapiens angiotensin I converting enzyme...    40   0.61
AY436326|AY436326.1 Homo sapiens angiotensin I converting enzyme...    40   0.61
AF118569|AF118569.1 Homo sapiens angiotensin I converting enzyme...    40   0.61
AF022145|AF022145.1 Homo sapiens angiotensin converting enzyme g...    40   0.61
AC113554|AC113554.9 Homo sapiens chromosome 17, clone CTD-2501B8...    40   0.61
Y14314|Y14314.1 Homo sapiens mRNA for IgE autoantigen.                 38   2.4
BX648500|BX648500.1 Homo sapiens mRNA; cDNA DKFZp686H1978 (from ...    38   2.4
BT006637|BT006637.1 Homo sapiens squamous cell carcinoma antigen...    38   2.4
BC050546|BC050546.1 Homo sapiens MYB binding protein (P160) 1a, ...    38   2.4
BC001058|BC001058.2 Homo sapiens squamous cell carcinoma antigen...    38   2.4
BC000641|BC000641.2 Homo sapiens MYB binding protein (P160) 1a, ...    38   2.4
AY093673|AY093673.1 Homo sapiens p53-activated protein-2 (PAP2) ...    38   2.4
AL451132|AL451132.9 Human DNA sequence from clone RP11-7N10 on c...    38   2.4
AL442163|AL442163.5 Human chromosome 14 DNA sequence BAC R-90P16...    38   2.4

(後略)
        
ページの先頭へ戻る