1
|
package edu.ucsb.nceas.metacat.annotation;
|
2
|
|
3
|
import java.io.InputStream;
|
4
|
import java.io.InputStreamReader;
|
5
|
import java.net.URL;
|
6
|
import java.net.URLEncoder;
|
7
|
|
8
|
import org.apache.log4j.Logger;
|
9
|
import org.w3c.dom.Document;
|
10
|
import org.w3c.dom.NodeList;
|
11
|
|
12
|
import com.hp.hpl.jena.ontology.ConversionException;
|
13
|
import com.hp.hpl.jena.ontology.OntClass;
|
14
|
import com.hp.hpl.jena.rdf.model.Resource;
|
15
|
|
16
|
import edu.ucsb.nceas.utilities.XMLUtilities;
|
17
|
|
18
|
public class BioPortalService {
|
19
|
|
20
|
private static Logger logMetacat = Logger.getLogger(BioPortalService.class);
|
21
|
|
22
|
// for looking up concepts in BioPortal
|
23
|
private static final String REST_URL = "http://data.bioontology.org";
|
24
|
private static final String API_KEY = "24e4775e-54e0-11e0-9d7b-005056aa3316";
|
25
|
|
26
|
/**
|
27
|
* Look up possible concept from BioPortal annotation service.
|
28
|
* @see "http://data.bioontology.org/documentation"
|
29
|
* @param superClass
|
30
|
* @param text
|
31
|
* @return
|
32
|
*/
|
33
|
public static Resource lookupAnnotationClass(OntClass superClass, String text, String ontologies) {
|
34
|
|
35
|
// no point calling the service
|
36
|
if (text == null || text.length() == 0) {
|
37
|
return null;
|
38
|
}
|
39
|
|
40
|
try {
|
41
|
|
42
|
String urlParameters = "apikey=" + API_KEY;
|
43
|
urlParameters += "&format=xml";
|
44
|
if (ontologies != null) {
|
45
|
urlParameters += "&ontologies=" + ontologies;
|
46
|
}
|
47
|
urlParameters += "&text=" + URLEncoder.encode(text, "UTF-8");
|
48
|
|
49
|
String url = REST_URL + "/annotator?" + urlParameters ;
|
50
|
URL restURL = new URL(url);
|
51
|
InputStream is = restURL.openStream();
|
52
|
Document doc = XMLUtilities.getXMLReaderAsDOMDocument(new InputStreamReader(is, "UTF-8"));
|
53
|
NodeList classNodeList = XMLUtilities.getNodeListWithXPath(doc, "//annotation/annotatedClass/id");
|
54
|
if (classNodeList != null && classNodeList.getLength() > 0) {
|
55
|
for (int i = 0; i < classNodeList.getLength(); i++) {
|
56
|
String classURI = classNodeList.item(i).getFirstChild().getNodeValue();
|
57
|
logMetacat.info("annotator suggested: " + classURI);
|
58
|
Resource subclass = superClass.getModel().getResource(classURI);
|
59
|
// check that it is a subclass of superClass
|
60
|
boolean isSubclass = false;
|
61
|
try {
|
62
|
isSubclass = superClass.hasSubClass(subclass);
|
63
|
} catch (ConversionException ce) {
|
64
|
logMetacat.warn("Skipping unknown subclass: " + classURI, ce);
|
65
|
// try the next one
|
66
|
continue;
|
67
|
}
|
68
|
if (isSubclass) {
|
69
|
return subclass;
|
70
|
}
|
71
|
}
|
72
|
|
73
|
}
|
74
|
} catch (Exception e) {
|
75
|
logMetacat.error("Could not lookup BioPortal annotation for text=" + text, e);
|
76
|
}
|
77
|
|
78
|
return null;
|
79
|
}
|
80
|
}
|