Project

General

Profile

« Previous | Next » 

Revision 8716

refactor web service calls to bioportal and orcid outside of the annotator class. test with orcid sandbox server. include orcid uri for the annotations being generated (we can index these and drive our searches on these values down the road). related to this: https://projects.ecoinformatics.org/ecoinfo/issues/6423 and also some semtools tasks.

View differences:

test/edu/ucsb/nceas/metacat/annotation/OrcidServiceTest.java
1
/**  '$RCSfile$'
2
 *  Copyright: 2010 Regents of the University of California and the
3
 *              National Center for Ecological Analysis and Synthesis
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package edu.ucsb.nceas.metacat.annotation;
20

  
21
import junit.framework.Test;
22
import junit.framework.TestSuite;
23
import edu.ucsb.nceas.MCTestCase;
24

  
25
public class OrcidServiceTest extends MCTestCase {
26

  
27
	
28
	/**
29
	 * constructor for the test
30
	 */
31
	public OrcidServiceTest(String name) {
32
		super(name);
33
	}
34

  
35
	/**
36
	 * Establish a testing framework by initializing appropriate objects
37
	 */
38
	public void setUp() throws Exception {
39
		super.setUp();
40
	}
41

  
42
	/**
43
	 * Release any objects after tests are complete
44
	 */
45
	public void tearDown() {
46
	}
47

  
48
	/**
49
	 * Create a suite of tests to be run together
50
	 */
51
	public static Test suite() {
52
		TestSuite suite = new TestSuite();
53
		suite.addTest(new OrcidServiceTest("testLookup"));
54
		return suite;
55
	}
56
	
57
	public void testLookup() {
58
		String[] otherNames = new String[] {"Matthew Jones"};
59
		String orcid = OrcidService.lookupOrcid(null, null, otherNames);
60
		assertEquals("http://sandbox-1.orcid.org/0000-0003-2141-4459", orcid);
61
	}
62

  
63
}
0 64

  
test/edu/ucsb/nceas/metacat/annotation/DatapackageSummarizerTest.java
19 19
package edu.ucsb.nceas.metacat.annotation;
20 20

  
21 21
import java.io.ByteArrayInputStream;
22
import java.io.FileInputStream;
23 22
import java.io.InputStream;
24 23

  
25 24
import junit.framework.Test;
src/edu/ucsb/nceas/metacat/annotation/OrcidService.java
1
package edu.ucsb.nceas.metacat.annotation;
2

  
3
import java.io.InputStream;
4
import java.io.StringReader;
5
import java.net.URL;
6
import java.net.URLEncoder;
7

  
8
import org.apache.commons.io.IOUtils;
9
import org.apache.log4j.Logger;
10
import org.w3c.dom.Node;
11

  
12
import edu.ucsb.nceas.metacat.replication.ReplicationService;
13
import edu.ucsb.nceas.utilities.XMLUtilities;
14

  
15
public class OrcidService {
16
	
17
	private static Logger logMetacat = Logger.getLogger(OrcidService.class);
18
	
19
    private static final String REST_URL = "http://pub.sandbox.orcid.org/v1.1/search/orcid-bio";
20

  
21
    /**
22
	 * Look up possible ORCID from orcid service.
23
	 * @see "http://support.orcid.org/knowledgebase/articles/132354-searching-with-the-public-api"
24
	 * @param surName
25
	 * @param givenNames
26
	 * @param otherNames
27
	 * @return
28
	 */
29
	public static String lookupOrcid(String surName, String[] givenNames, String[] otherNames) {
30
		
31
		try {
32
			
33
			String urlParameters = "";
34
			if (surName != null) {
35
				urlParameters += "+family-name:\"" + surName + "\"";
36
			}
37
			if (otherNames != null) {
38
				for (String otherName: otherNames) {
39
					urlParameters += "+other-names:\"" + otherName + "\""; 
40
				}
41
			}
42
			if (givenNames != null) {
43
				for (String givenName: givenNames) {
44
					urlParameters += "+given-names:\"" + givenName + "\""; 
45
				}
46
			}
47
			
48
			urlParameters = URLEncoder.encode(urlParameters, "UTF-8");
49
			
50
			String url = REST_URL + "?q=" + urlParameters + "&rows=1";
51
			URL restURL = new URL(url);
52
			//InputStream is = restURL.openStream();
53
			InputStream is = ReplicationService.getURLStream(restURL);
54
			String results = IOUtils.toString(is);
55
			logMetacat.debug("RESULTS: " + results);
56
			Node doc = XMLUtilities.getXMLReaderAsDOMTreeRootNode(new StringReader(results));
57
			Node orcidUriNodeList = XMLUtilities.getNodeWithXPath(doc, "//*[local-name()=\"uri\"]");
58
			if (orcidUriNodeList != null) {
59
				String orcidUri = orcidUriNodeList.getFirstChild().getNodeValue();
60
				logMetacat.info("Found ORCID URI: " + orcidUri);
61
				return orcidUri;
62
			}
63
		} catch (Exception e) {
64
			logMetacat.error("Could not lookup ORCID for surName=" + surName, e);
65
		}
66
		
67
		return null;
68
	}
69
}
0 70

  
src/edu/ucsb/nceas/metacat/annotation/DatapackageSummarizer.java
1 1
package edu.ucsb.nceas.metacat.annotation;
2 2

  
3 3
import java.io.InputStream;
4
import java.io.InputStreamReader;
5 4
import java.io.StringWriter;
6
import java.net.URL;
7
import java.net.URLEncoder;
8 5
import java.sql.PreparedStatement;
9 6
import java.sql.SQLException;
10 7
import java.util.ArrayList;
......
24 21
import org.ecoinformatics.datamanager.parser.Entity;
25 22
import org.ecoinformatics.datamanager.parser.generic.DataPackageParserInterface;
26 23
import org.ecoinformatics.datamanager.parser.generic.Eml200DataPackageParser;
27
import org.w3c.dom.Document;
28
import org.w3c.dom.NodeList;
29 24

  
30 25
import com.hp.hpl.jena.ontology.AllValuesFromRestriction;
31 26
import com.hp.hpl.jena.ontology.Individual;
......
49 44
import edu.ucsb.nceas.metacat.replication.ReplicationService;
50 45
import edu.ucsb.nceas.metacat.util.DocumentUtil;
51 46
import edu.ucsb.nceas.utilities.SortedProperties;
52
import edu.ucsb.nceas.utilities.XMLUtilities;
53 47

  
54 48
public class DatapackageSummarizer {
55 49

  
......
70 64
    public static String prov_source = "http://www.w3.org/ns/prov.owl";
71 65
    public static String cito =  "http://purl.org/spar/cito/";
72 66
    
73
    // for looking up concepts in BioPortal
74
    static final String REST_URL = "http://data.bioontology.org";
75
    static final String API_KEY = "LOGIN_TO_BIOPORTAL";
76
    
77 67
    // package visibility for testing only
78 68
    boolean randomize = false;
79 69

  
......
133 123
		
134 124
		// these apply to every attribute annotation
135 125
		Individual meta1 = m.createIndividual(ont.getURI() + "#meta", entityClass);
136
		Individual p1 = m.createIndividual(ont.getURI() + "#person", personClass);
137
		p1.addProperty(nameProperty, "Ben Leinfelder");
138 126
		meta1.addProperty(identifierProperty, metadataPid.getValue());
139 127

  
128
		// who should we attribute the annotation to?
129
		Individual p1 = m.createIndividual(ont.getURI() + "#person", personClass);
130
		
131
		// add an orcid annotation if we can find one from their system
132
		List<String> creators = dataPackage.getCreators();
133
		//creators = Arrays.asList("Matthew Jones");
134
		if (creators != null && creators.size() > 0) {
135
			p1.addProperty(nameProperty, creators.get(0));
136
			String orcidId = OrcidService.lookupOrcid(null, null, creators.toArray(new String[0]));
137
			if (orcidId != null) {
138
				p1.addProperty(identifierProperty, orcidId);
139
			}
140
		}
141
		
140 142
		// loop through the tables and attributes
141 143
		int entityCount = 1;
142 144
		Entity[] entities = dataPackage.getEntityList();
......
244 246
			}
245 247
		}
246 248
		// try to look it up if we got this far
247
		return this.lookupRemoteAnnotationClass(standardClass, unit);
249
		return BioPortalService.lookupAnnotationClass(standardClass, unit);
248 250
	}
249 251
	
250 252
	private Resource lookupCharacteristic(OntClass characteristicClass, Attribute attribute) {
......
278 280
		}
279 281
		
280 282
		// try to look it up if we got this far
281
		return this.lookupRemoteAnnotationClass(characteristicClass, attribute.getDefinition());
283
		return BioPortalService.lookupAnnotationClass(characteristicClass, attribute.getDefinition());
282 284
		
283 285
	}
284 286
	
285
	/**
286
	 * Look up possible concept from BioPortal annotation service.
287
	 * @see "http://data.bioontology.org/documentation"
288
	 * @param superClass
289
	 * @param text
290
	 * @return
291
	 */
292
	private Resource lookupRemoteAnnotationClass(OntClass superClass, String text) {
293
		
294
		try {
295
			
296
			String urlParameters = "apikey=" + API_KEY;
297
			urlParameters += "&format=xml";
298
			urlParameters += "&ontologies=OBOE-SBC";
299
//			urlParameters += "&ontologies=SWEET";
300
			urlParameters += "&text=" + URLEncoder.encode(text, "UTF-8");
301
			
302
			String url = REST_URL + "/annotator?" + urlParameters ;
303
			URL restURL = new URL(url);
304
			InputStream is = ReplicationService.getURLStream(restURL);
305
			Document doc = XMLUtilities.getXMLReaderAsDOMDocument(new InputStreamReader(is, "UTF-8"));
306
			NodeList classNodeList = XMLUtilities.getNodeListWithXPath(doc, "//annotation/annotatedClass/id");
307
			if (classNodeList != null && classNodeList.getLength() > 0) {
308
				String classURI = classNodeList.item(0).getFirstChild().getNodeValue();
309
				logMetacat.info("annotator suggested: " + classURI);
310
				Resource subclass = superClass.getModel().getResource(classURI);
311
				// check that it is a subclass of superClass
312
				if (superClass.hasSubClass(subclass)) {
313
					return subclass;
314
				}
315
			}
316
		} catch (Exception e) {
317
			logMetacat.error("Could not lookup BioPortal annotation for text= " + text, e);
318
		}
319
		
320
		return null;
321
	}
322
	
323 287
	private DataPackage getDataPackage(Identifier pid) throws Exception {
324 288
		// for using the MN API as the MN itself
325 289
		MockHttpServletRequest request = new MockHttpServletRequest(null, null, null);
src/edu/ucsb/nceas/metacat/annotation/BioPortalService.java
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) {
34
		
35
		try {
36
			
37
			String urlParameters = "apikey=" + API_KEY;
38
			urlParameters += "&format=xml";
39
			urlParameters += "&ontologies=OBOE-SBC";
40
//			urlParameters += "&ontologies=SWEET";
41
			urlParameters += "&text=" + URLEncoder.encode(text, "UTF-8");
42
			
43
			String url = REST_URL + "/annotator?" + urlParameters ;
44
			URL restURL = new URL(url);
45
			InputStream is = ReplicationService.getURLStream(restURL);
46
			Document doc = XMLUtilities.getXMLReaderAsDOMDocument(new InputStreamReader(is, "UTF-8"));
47
			NodeList classNodeList = XMLUtilities.getNodeListWithXPath(doc, "//annotation/annotatedClass/id");
48
			if (classNodeList != null && classNodeList.getLength() > 0) {
49
				String classURI = classNodeList.item(0).getFirstChild().getNodeValue();
50
				logMetacat.info("annotator suggested: " + classURI);
51
				Resource subclass = superClass.getModel().getResource(classURI);
52
				// check that it is a subclass of superClass
53
				if (superClass.hasSubClass(subclass)) {
54
					return subclass;
55
				}
56
			}
57
		} catch (Exception e) {
58
			logMetacat.error("Could not lookup BioPortal annotation for text= " + text, e);
59
		}
60
		
61
		return null;
62
	}
63
}
0 64

  

Also available in: Unified diff