Annotator Client Examples

From NCBO Wiki

Jump to: navigation, search

Contents

HTML client example

HTML http://rest.bioontology.org/test_oba.html

Java client example

This is a Java client example to programmatically access the NCBO Annotator web service. Optional parameters have been commented out.

You can find this client on the NCBO Gforge server at : https://bmir-gforge.stanford.edu/gf/project/obs/scmsvn/?action=browse&path=%2FOBS_v1_client%2F

NOTE: The Apache HttpClient is needed and can be downloaded at: http://hc.apache.org/downloads.cgi

AnnotatorSimpleClient.java

   

package obs_hibernate.client;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

public class AnnotatorSimpleClient {
  
  public static final String PROD_URL = "http://rest.bioontology.org/obs/annotator?email=example@example.org";

  private static String text = "Melanoma is a malignant tumor of melanocytes which are found predominantly in skin but also in the bowel and the eye";
  
  public static void mainString[] args ) {
     System.out.println("********************* NCBO ANNOTATOR CLIENT TEST ************************* \n");
     try {
       HttpClient client = new HttpClient();
       PostMethod method = new PostMethod(PROD_URL);

       // Configure the form parameters
       method.addParameter("longestOnly""true");
       //method.addParameter("wholeWordOnly", "true");
       //method.addParameter("stopWords", "choubala");
       //method.addParameter("withDefaultStopWords", "true");
       method.addParameter("scored""true");
       //method.addParameter("ontologiesToExpand", "38802,13578,40644,40403");
       //method.addParameter("ontologiesToKeepInResult", "40403");
       //method.addParameter("semanticTypes", "T999");
       //method.addParameter("levelMax", "50");
       method.addParameter("mappingTypes""null");
       method.addParameter("textToAnnotate", text);
       //method.addParameter("format", "asText");
       
       // Execute the POST method
        int statusCode = client.executeMethod(method);
         ifstatusCode != -) {
           String contents = method.getResponseBodyAsString();
           method.releaseConnection();
           System.out.println(contents);
         }  
     }
     catchException e ) {
       e.printStackTrace();
     }
   }

}

Perl client example


use strict;
use LWP::UserAgent;
use URI::Escape;
use XML::LibXML;

$|=1;

# The desired service URL
# http://rest.bioontology.org/obs/annotator


my $AnnotatorURL = 'http://rest.bioontology.org/obs/annotator';

# Sample text
my $text = uri_escape("Melanoma is a malignant tumor of melanocytes which are found predominantly in skin but also in the bowel and the eye");

# create a user agent
my $ua = new LWP::UserAgent;

# create a parse to handle the output
my $parser = XML::LibXML->new();

# create a POST request
my $req = new HTTP::Request POST => "$AnnotatorURL";
   $req->content_type('application/x-www-form-urlencoded');


# Set parameters
$req->content("longestOnly=true&"
			 ."wholeWordOnly=true&"
			 ."scored=true&"
			 ."ontologiesToExpand=40403,40644,40401&"
			 ."ontologiesToKeepInResult=40403,40644,40401&"
			 ."semanticTypes=T017,T047,T191&"
			 ."textToAnnotate=$text&"
			 ."withDefaultStopWords=true&"
			 ."format=xml&"
			 ."levelMax=0&"
			 ."mappingTypes=null&"
			 ."email=nigam\@stanford.edu");

# send request and get response.
my $res = $ua->request($req);

# Check the outcome of the response
if ($res->is_success) {
	my $time = localtime();
    print "Call successful at $time\n";
    
    # Parse the response
    my ($M_ConceptREF, $M_PhraseREF) = ParseOBAResponse($res, $parser);

	# Print something for the user
    print scalar (keys %{$M_ConceptREF}), " concepts found\n";
    foreach my $c (keys %{$M_ConceptREF}){
    	print $c,"\t", $$M_ConceptREF{$c},"\t","matched\t","$$M_PhraseREF{$c}","\n";
    }    
}
else {
	my $time = localtime();
    print $res->status_line, " at $time\n";
}

sub ParseOBAResponse {
	my ($res, $parser) = @_;
        my $dom = $parser->parse_string($res->decoded_content);
	my $root = $dom->getDocumentElement();
	my %MatchedConcepts;
	my %MatchedPhrase;
		
	my $results = $root->findnodes('/success/data/annotatorResultBean/annotations/annotationBean/concept');
	foreach my $c_node ($results->get_nodelist){
                # Sample XPATH to extract concept info if needed
		print "ID = ", $c_node->findvalue('localConceptId'),"\n";
		print "Name = ", $c_node->findvalue('preferredName'),"\n";
		print "Type = ", $c_node->findvalue('./semanticTypes/semanticTypeBean[1]/localSemanticTypeId'),"\n";
		print "Type name = ", $c_node->findvalue('./semanticTypes/semanticTypeBean[1]/name'),"\n";
		
		$MatchedConcepts{$c_node->findvalue('localConceptId')} = $c_node->findvalue('preferredName');
	}
		
	my $results = $root->findnodes('/success/data/annotatorResultBean/annotations/annotationBean/context');
	foreach my $c_node ($results->get_nodelist){
                # Sample XPATH to extract concept info if needed
		print "ID = ", $c_node->findvalue('./term/localConceptId'),"\n";
		print "Match = ", $c_node->findvalue('./term/name'),"\n";
		print "From = ", $c_node->findvalue('from'),"\n";
		print "To = ", $c_node->findvalue('to'),"\n";
			
		$MatchedPhrase{$c_node->findvalue('./term/localConceptId')} = $c_node->findvalue('./term/name');
	}		
	return (\%MatchedConcepts, \%MatchedPhrase);
}
Personal tools