Project

General

Profile

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.OntClass;
13
import com.hp.hpl.jena.rdf.model.Resource;
14

    
15
import edu.ucsb.nceas.metacat.replication.ReplicationService;
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
		try {
36
			
37
			String urlParameters = "apikey=" + API_KEY;
38
			urlParameters += "&format=xml";
39
			if (ontologies != null) {
40
				urlParameters += "&ontologies=" + ontologies;
41
			}
42
			urlParameters += "&text=" + URLEncoder.encode(text, "UTF-8");
43
			
44
			String url = REST_URL + "/annotator?" + urlParameters ;
45
			URL restURL = new URL(url);
46
			InputStream is = ReplicationService.getURLStream(restURL);
47
			Document doc = XMLUtilities.getXMLReaderAsDOMDocument(new InputStreamReader(is, "UTF-8"));
48
			NodeList classNodeList = XMLUtilities.getNodeListWithXPath(doc, "//annotation/annotatedClass/id");
49
			if (classNodeList != null && classNodeList.getLength() > 0) {
50
				String classURI = classNodeList.item(0).getFirstChild().getNodeValue();
51
				logMetacat.info("annotator suggested: " + classURI);
52
				Resource subclass = superClass.getModel().getResource(classURI);
53
				// check that it is a subclass of superClass
54
				if (superClass.hasSubClass(subclass)) {
55
					return subclass;
56
				}
57
			}
58
		} catch (Exception e) {
59
			logMetacat.error("Could not lookup BioPortal annotation for text=" + text, e);
60
		}
61
		
62
		return null;
63
	}
64
}
(1-1/3)