Difference between revisions of "Annotator R Client"

From NCBO Wiki
Jump to navigation Jump to search
(fix xpath)
Line 64: Line 64:
 
     resultNodes <- getNodeSet(x, "/success/data/annotatorResultBean/annotations/annotationBean/context")
 
     resultNodes <- getNodeSet(x, "/success/data/annotatorResultBean/annotations/annotationBean/context")
 
     for( result in resultNodes ) {
 
     for( result in resultNodes ) {
       localConceptId <- xmlValue(getNodeSet(result,"./term/localConceptId")[[1]])
+
       localConceptId <- xmlValue(getNodeSet(result,"./term/concept/localConceptId")[[1]])
 
       print( paste( "ID = ", localConceptId ) )
 
       print( paste( "ID = ", localConceptId ) )
 
      
 
      

Revision as of 16:52, 21 March 2013

Example

#
# sample client for NCBO Annotator in R
# Author: Andrew Su 
#

AnnotatorURL <- "http://rest.bioontology.org/obs/annotator"

queryText <- "Melanoma is a malignant tumor of melanocytes which are found predominantly in skin but also in the bowel and the eye"

annotate.it( queryText )


annotate.it <- function( queryText ) {

    library(RCurl)
    library(XML)
	
	# Login to BioPortal to get YourAPIKey
    res<- postForm('http://rest.bioontology.org/obs/annotator',
            "textToAnnotate"=queryText,
			"apikey"=YourAPIKey, 
            "style"="POST",
            "longestOnly"="false",
            "wholeWordOnly"="true",
			"filterNumber"="true",
			"withDefaultStopWords"="true",
            "isStopWordsCaseSensitive"="false",
			"minTermSize"="3",
			"scored"="true",
			"withSynonyms"="true",
            "ontologiesToExpand"="1353,1032,1351,1009",
            "ontologiesToKeepInResult"="1353,1032,1351,1009",
			"isVirtualOntologyId"="true", 
            "semanticTypes"="T017,T047,T191,T999",
            "levelMax"="0",
            "mappingTypes"="null",
			"format"="xml",
            "email"="YourEmail@Somewhere.com")
    
    x <- xmlInternalTreeParse(res)
    
    matchedConcepts <- list()
    conceptNodes <- getNodeSet(x,"/success/data/annotatorResultBean/annotations/annotationBean/concept")
    for( concept in conceptNodes ) {
       localConceptId <- xmlValue(getNodeSet(concept,"./localConceptId")[[1]])
       print( paste( "ID = ",localConceptId ))
    
       preferredName <- xmlValue(getNodeSet(concept,"./preferredName")[[1]])
       print( paste( "Name = ",preferredName ))
    
       semanticType <- xmlValue(getNodeSet(concept,"./semanticTypes/semanticTypeBean/semanticType")[[1]])
       print( paste( "Type = ",semanticType ))
    
       description <- xmlValue(getNodeSet(concept,"./semanticTypes/semanticTypeBean/description")[[1]])
       print( paste( "Type name = ",description ))
    
       matchedConcepts[[localConceptId]] <- preferredName
    }
    
    matchedPhrase <- list()
    resultNodes <- getNodeSet(x, "/success/data/annotatorResultBean/annotations/annotationBean/context")
    for( result in resultNodes ) {
       localConceptId <- xmlValue(getNodeSet(result,"./term/concept/localConceptId")[[1]])
       print( paste( "ID = ", localConceptId ) )
    
       term.name <- xmlValue(getNodeSet(result,"./term/name")[[1]])
       print( paste( "Match = ", term.name ) )
      
       from <- xmlValue(getNodeSet(result,"./from")[[1]])
       print( paste( "From = ", from ) )
    
       to <- xmlValue(getNodeSet(result,"./to")[[1]])
       print( paste( "To = ", to ) )
    
       matchedPhrase[[localConceptId]] <- term.name
    }
    
    for( key in names(matchedConcepts) ) {
       print(paste(key,matchedConcepts[[key]],"matched",matchedPhrase[[key]]))
    }
}