Annotator R Client

From NCBO Wiki
Revision as of 10:41, 19 December 2013 by Andrew Su (talk | contribs) (→‎Example: minor tweaks for clarity)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Example

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


annotate.it <- function( queryText, yourAPIKey ) {

    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]]))
    }
}

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

# Instructions on getting an API key are at http://www.bioontology.org/wiki/index.php/Annotator_User_Guide#Annotator_Web_service_Validation
yourAPIKey <- "XXXXXXXXXXXXX"

annotate.it( queryText, yourAPIKey )