Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

[#72] added service for single entity #73

Merged
merged 3 commits into from Mar 25, 2017
Jump to file or symbol
Failed to load files and symbols.
+109 −76
Split
@@ -7,6 +7,7 @@
import org.monarchinitiative.owlsim.kb.BMKnowledgeBase;
import org.monarchinitiative.owlsim.kb.LabelMapper;
+import com.google.common.collect.Sets;
import com.googlecode.javaewah.EWAHCompressedBitmap;
/**
@@ -37,6 +38,18 @@ public ClassMatcher(BMKnowledgeBase kb) {
return matchClassSets(qids, tids);
}
+ /**
+ * Find best match for one entity in a whole ontology
+ *
+ * @param entity
+ * @param tOnt
+ * @return list of matches
+ */
+ public List<SimpleClassMatch> matchEntity(String entity, String tOnt) {
+ Set<String> tids = kb.getClassIdsByOntology(tOnt);
+ return matchClassSets(Sets.newHashSet(kb.resolveIri(entity)), tids);
+ }
+
public List<SimpleClassMatch> matchClassSets(Set<String> qids, Set<String> tids) {
ArrayList<SimpleClassMatch> matches = new ArrayList<>();
for (String q : qids) {
@@ -351,11 +351,17 @@
public int getRootIndex();
+ /**
+ * Resolves a CURIE into an IRI, if possible.
+ *
+ * @return IRI version of a CURIE, or the string itself
+ */
+ public String resolveIri(String entity);
+
+
+
public static OwlKnowledgeBase.Loader owlLoader() {
return OwlKnowledgeBase.loader();
}
-
-
-
}
@@ -141,12 +141,8 @@ public void setKbMetdata(KBMetadata kbMetdata) {
}
private String getShortForm(IRI iri) {
- if (curieUtil.getCurieMap().isEmpty()) {
- return iri.toString();
- } else {
- String iriString = iri.toString();
- return curieUtil.getCurie(iriString).orElse(iriString);
- }
+ String iriString = iri.toString();
+ return curieUtil.getCurie(iriString).orElse(iriString);
}
private void populateLabelsFromOntology(LabelMapper labelMapper, OWLOntology ontology) {
@@ -1006,11 +1002,7 @@ protected String getIdentifier(OWLNamedObject obj) {
*/
private OWLClass getOWLClass(String id) {
Preconditions.checkNotNull(id);
- if (curieUtil.getCurieMap().isEmpty()) {
- return getOWLClass(IRI.create(id));
- } else {
- return getOWLClass(IRI.create(curieUtil.getIri(id).orElse(id)));
- }
+ return getOWLClass(IRI.create(curieUtil.getIri(id).orElse(id)));
}
/**
@@ -1036,12 +1028,7 @@ private OWLNamedIndividual getOWLNamedIndividual(IRI iri) {
*/
private OWLNamedIndividual getOWLNamedIndividual(String id) {
Preconditions.checkNotNull(id);
- //TODO: check - this is redundant code simply return getOWLNamedIndividual(IRI.create(curieUtil.getIri(id).orElse(id))); will suffice
- if (curieUtil.getCurieMap().isEmpty()) {
- return getOWLNamedIndividual(IRI.create(id));
- } else {
- return getOWLNamedIndividual(IRI.create(curieUtil.getIri(id).orElse(id)));
- }
+ return getOWLNamedIndividual(IRI.create(curieUtil.getIri(id).orElse(id)));
}
public Attribute getAttribute(String id) {
@@ -1113,4 +1100,9 @@ public EWAHCompressedBitmap getFilteredDirectTypesBM(Set<String> classIds, Strin
}
+ @Override
+ public String resolveIri(String entity) {
+ return curieUtil.getIri(entity).orElse(entity);
+ }
+
}
@@ -14,49 +14,62 @@
import java.util.List;
import java.util.stream.Collectors;
-import static org.junit.Assert.assertEquals;
+import javax.swing.plaf.synth.SynthSplitPaneUI;
+
+import static org.junit.Assert.*;
public class ClassMatcherTest {
- protected BMKnowledgeBase kb;
- protected ClassMatcher classMatcher;
- private Logger LOG = Logger.getLogger(ClassMatcherTest.class);
-
- protected void load(String fn, String... ontfns) throws OWLOntologyCreationException, URISyntaxException, NoRootException {
-// OWLLoader loader = new OWLLoader();
-// LOG.info("Loading: "+fn);
-// loader.load(IRI.create(Resources.getResource(fn)));
-// for (String ontfn : ontfns) {
-// URL res = getClass().getResource(ontfn);
-// LOG.info("RES="+res);
-// loader.ontologies(res.getFile());
-// }
-// kb = loader.createKnowledgeBaseInterface();
- kb = OwlKnowledgeBase.loader()
- .loadOntology(filePath(fn))
- .loadOntologies(Arrays.stream(ontfns).map(ontfn -> filePath(ontfn)).collect(Collectors.toList()))
- .createKnowledgeBase();
- classMatcher = new ClassMatcher(kb);
- }
-
- private String filePath(String filename) {
- return Paths.get("src/test/resources/", filename).toString();
- }
-
- @Test
- public void selfTest() throws OWLOntologyCreationException, URISyntaxException, NoRootException {
- load("mp-subset.ttl");
- LabelMapper lm = kb.getLabelMapper();
-
- List<SimpleClassMatch> matches = classMatcher.matchOntologies("MP", "MP");
-
- int numNonSelfMatches = 0;
- for (SimpleClassMatch m : matches) {
- if (!m.getQueryClassId().equals(m.getMatchClassId())) {
- numNonSelfMatches++;
- }
- }
- assertEquals(0, numNonSelfMatches);
- }
+ protected BMKnowledgeBase kb;
+ private Logger LOG = Logger.getLogger(ClassMatcherTest.class);
+
+ protected ClassMatcher load(String fn, String... ontfns)
+ throws OWLOntologyCreationException, URISyntaxException, NoRootException {
+ // OWLLoader loader = new OWLLoader();
+ // LOG.info("Loading: "+fn);
+ // loader.load(IRI.create(Resources.getResource(fn)));
+ // for (String ontfn : ontfns) {
+ // URL res = getClass().getResource(ontfn);
+ // LOG.info("RES="+res);
+ // loader.ontologies(res.getFile());
+ // }
+ // kb = loader.createKnowledgeBaseInterface();
+ kb = OwlKnowledgeBase.loader().loadOntology(filePath(fn))
+ .loadOntologies(Arrays.stream(ontfns).map(ontfn -> filePath(ontfn)).collect(Collectors.toList()))
+ .createKnowledgeBase();
+ return new ClassMatcher(kb);
+ }
+
+ private String filePath(String filename) {
+ return Paths.get("src/test/resources/", filename).toString();
+ }
+
+ @Test
+ public void selfTest() throws OWLOntologyCreationException, URISyntaxException, NoRootException {
+ ClassMatcher classMatcher = load("mp-subset.ttl");
+ LabelMapper lm = kb.getLabelMapper();
+
+ List<SimpleClassMatch> matches = classMatcher.matchOntologies("MP", "MP");
+
+ int numNonSelfMatches = 0;
+ for (SimpleClassMatch m : matches) {
+ if (!m.getQueryClassId().equals(m.getMatchClassId())) {
+ numNonSelfMatches++;
+ }
+ }
+ assertEquals(0, numNonSelfMatches);
+ }
+
+ @Test
+ public void entityMatchTest() throws OWLOntologyCreationException, URISyntaxException, NoRootException {
+ ClassMatcher classMatcher = load("mp-subset.ttl");
+
+ String testEntity = "http://purl.obolibrary.org/obo/MP_0001951";
+
+ List<SimpleClassMatch> matches = classMatcher.matchEntity(testEntity, "MP");
+
+ assertFalse(matches.isEmpty());
+ assertEquals(testEntity, matches.get(0).getMatchClassId());
+ }
}
@@ -1,48 +1,39 @@
package org.monarchinitiative.owlsim.services.resources;
-import java.util.Collection;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
-import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.monarchinitiative.owlsim.compute.classmatch.ClassMatcher;
import org.monarchinitiative.owlsim.compute.classmatch.SimpleClassMatch;
import org.monarchinitiative.owlsim.compute.cpt.IncoherentStateException;
-import org.monarchinitiative.owlsim.compute.matcher.NegationAwareProfileMatcher;
-import org.monarchinitiative.owlsim.compute.matcher.ProfileMatcher;
-import org.monarchinitiative.owlsim.kb.filter.AnonIndividualFilter;
-import org.monarchinitiative.owlsim.kb.filter.TypeFilter;
import org.monarchinitiative.owlsim.kb.filter.UnknownFilterException;
import org.monarchinitiative.owlsim.model.match.MatchSet;
-import org.monarchinitiative.owlsim.model.match.ProfileQuery;
-import org.monarchinitiative.owlsim.model.match.ProfileQueryFactory;
-import org.monarchinitiative.owlsim.services.exceptions.NonNegatedMatcherException;
-import org.monarchinitiative.owlsim.services.exceptions.UnknownMatcherException;
+import org.prefixcommons.CurieUtil;
import com.codahale.metrics.annotation.Timed;
+
+import io.dropwizard.jersey.caching.CacheControl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
-import io.dropwizard.jersey.caching.CacheControl;
-
@Path("/ontomatch")
@Api(value = "/ontomatch", description = "ontology match services")
@Produces({MediaType.APPLICATION_JSON})
public class OntologyMatchResource {
@Inject
ClassMatcher classMatcher;
+
+ @Inject
+ CurieUtil curieUtil;
@GET
@Path("/{queryOntology}/{targetOntology}")
@@ -61,6 +52,24 @@
return matches;
}
+
// TODO - API for comparing two entities
+ @GET
+ @Path("/{entity}/{ontology}")
+ @Timed
+ @CacheControl(maxAge = 2, maxAgeUnit = TimeUnit.HOURS)
+ @ApiOperation(value = "Match", response = MatchSet.class,
+ notes = "Additional notes on the match resource.")
+ public List<SimpleClassMatch> getEntityMatches(
+ @ApiParam(value = "entity, e.g. MP:0001951",
+ required = true) @PathParam("entity") String entity,
+ @ApiParam(value = "ontology to be matched, e.g. HP",
+ required = true) @PathParam("ontology") String ontology)
+ throws UnknownFilterException, IncoherentStateException {
+ List<SimpleClassMatch> matches =
+ classMatcher.matchEntity(entity, ontology);
+ return matches;
+ }
+
}