1 |
8716
|
leinfelder
|
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 |
8768
|
leinfelder
|
import com.hp.hpl.jena.ontology.ConversionException;
|
13 |
8716
|
leinfelder
|
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 |
8717
|
leinfelder
|
public static Resource lookupAnnotationClass(OntClass superClass, String text, String ontologies) {
|
34 |
8716
|
leinfelder
|
|
35 |
8768
|
leinfelder
|
// no point calling the service
|
36 |
|
|
if (text == null || text.length() == 0) {
|
37 |
|
|
return null;
|
38 |
|
|
}
|
39 |
|
|
|
40 |
8716
|
leinfelder
|
try {
|
41 |
|
|
|
42 |
|
|
String urlParameters = "apikey=" + API_KEY;
|
43 |
|
|
urlParameters += "&format=xml";
|
44 |
8717
|
leinfelder
|
if (ontologies != null) {
|
45 |
|
|
urlParameters += "&ontologies=" + ontologies;
|
46 |
|
|
}
|
47 |
8716
|
leinfelder
|
urlParameters += "&text=" + URLEncoder.encode(text, "UTF-8");
|
48 |
|
|
|
49 |
|
|
String url = REST_URL + "/annotator?" + urlParameters ;
|
50 |
|
|
URL restURL = new URL(url);
|
51 |
8763
|
leinfelder
|
InputStream is = restURL.openStream();
|
52 |
8716
|
leinfelder
|
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 |
8768
|
leinfelder
|
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 |
8716
|
leinfelder
|
}
|
72 |
8768
|
leinfelder
|
|
73 |
8716
|
leinfelder
|
}
|
74 |
|
|
} catch (Exception e) {
|
75 |
8723
|
leinfelder
|
logMetacat.error("Could not lookup BioPortal annotation for text=" + text, e);
|
76 |
8716
|
leinfelder
|
}
|
77 |
|
|
|
78 |
|
|
return null;
|
79 |
|
|
}
|
80 |
|
|
}
|