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
implemented a simple boolean matcher. #74
Open
Jump to file or symbol
Failed to load files and symbols.
| @@ -0,0 +1,90 @@ | ||
| +package org.monarchinitiative.owlsim.compute.matcher.impl; | ||
| + | ||
| +import java.util.List; | ||
| + | ||
| +import javax.inject.Inject; | ||
| + | ||
| +import org.apache.log4j.Logger; | ||
| +import org.monarchinitiative.owlsim.compute.matcher.NegationAwareProfileMatcher; | ||
| +import org.monarchinitiative.owlsim.compute.matcher.ProfileMatcher; | ||
| +import org.monarchinitiative.owlsim.kb.BMKnowledgeBase; | ||
| +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.QueryWithNegation; | ||
| +import org.monarchinitiative.owlsim.model.match.impl.MatchSetImpl; | ||
| + | ||
| +import com.googlecode.javaewah.EWAHCompressedBitmap; | ||
| + | ||
| +/** | ||
| + * Implements a standard boolean query | ||
| + * | ||
| + * @author cjm | ||
| + * | ||
| + */ | ||
| +public class BooleanProfileMatcher extends AbstractProfileMatcher implements NegationAwareProfileMatcher { | ||
| + | ||
| + private Logger LOG = Logger.getLogger(BooleanProfileMatcher.class); | ||
| + | ||
| + /** | ||
| + * @param kb | ||
| + */ | ||
| + @Inject | ||
| + public BooleanProfileMatcher(BMKnowledgeBase kb) { | ||
| + super(kb); | ||
| + } | ||
| + | ||
| + | ||
| + /** | ||
| + * @param kb | ||
| + * @return new instance | ||
| + */ | ||
| + public static ProfileMatcher create(BMKnowledgeBase kb) { | ||
| + return new BooleanProfileMatcher(kb); | ||
| + } | ||
| + | ||
| + @Override | ||
| + public String getShortName() { | ||
| + return "boolean"; | ||
| + } | ||
| + | ||
| + /** | ||
| + * @param q | ||
| + * @return match profile containing probabilities of each individual | ||
| + * @throws UnknownFilterException | ||
| + */ | ||
| + public MatchSet findMatchProfileImpl(ProfileQuery q) throws UnknownFilterException { | ||
| + | ||
| + EWAHCompressedBitmap queryProfileBM = getDirectProfileBM(q); | ||
| + boolean hasNegationQuery = false; | ||
| + EWAHCompressedBitmap negatedQueryProfileBM = null; | ||
| + if (q instanceof QueryWithNegation) { | ||
| + negatedQueryProfileBM = getDirectNegatedProfileBM((QueryWithNegation) q); | ||
| + hasNegationQuery = negatedQueryProfileBM.cardinality() > 0; | ||
| + } | ||
| + | ||
| + // TODO | ||
| + MatchSet mp = MatchSetImpl.create(q); | ||
| + int qcard = queryProfileBM.cardinality(); | ||
| + List<String> indIds = getFilteredIndividualIds(q.getFilter()); | ||
| + for (String itemId : indIds) { | ||
| + EWAHCompressedBitmap targetProfileBM = knowledgeBase.getTypesBM(itemId); | ||
| + int numInQueryAndInTarget = queryProfileBM.andCardinality(targetProfileBM); | ||
| + if (numInQueryAndInTarget == qcard) { | ||
| + if (!hasNegationQuery || | ||
| + negatedQueryProfileBM.andCardinality(targetProfileBM) == 0) { | ||
| + String label = knowledgeBase.getLabelMapper().getArbitraryLabel(itemId); | ||
| + mp.add(createMatch(itemId, label, 1)); | ||
| + | ||
| + } | ||
| + } | ||
| + } | ||
| + mp.sortMatches(); | ||
| + return mp; | ||
| + } | ||
| + | ||
| + | ||
| + | ||
| + | ||
| + | ||
| +} |
| @@ -0,0 +1,73 @@ | ||
| +package org.monarchinitiative.owlsim.compute.matcher; | ||
| + | ||
| +import org.apache.log4j.Logger; | ||
| +import org.junit.Assert; | ||
| +import org.junit.Test; | ||
| +import org.monarchinitiative.owlsim.compute.matcher.impl.BooleanProfileMatcher; | ||
| +import org.monarchinitiative.owlsim.eval.TestQuery; | ||
| +import org.monarchinitiative.owlsim.kb.BMKnowledgeBase; | ||
| +import org.monarchinitiative.owlsim.model.match.ProfileQuery; | ||
| + | ||
| +public class BooleanProfileMatcherTest extends AbstractProfileMatcherTest { | ||
| + | ||
| + private Logger LOG = Logger.getLogger(BooleanProfileMatcherTest.class); | ||
| + | ||
| + protected ProfileMatcher createProfileMatcher(BMKnowledgeBase kb) { | ||
| + return BooleanProfileMatcher.create(kb); | ||
| + } | ||
| + @Test | ||
| + public void testBoolean() throws Exception { | ||
| + loadSimplePhenoWithNegation(); | ||
| + //LOG.info("INDS="+kb.getIndividualIdsInSignature()); | ||
| + ProfileMatcher profileMatcher = createProfileMatcher(kb); | ||
| + | ||
| + int nOk = 0; | ||
| + for (String i : kb.getIndividualIdsInSignature()) { | ||
| + LOG.info("I: "+i); | ||
| + if (i.equals("http://x.org/ind-no-brain-phenotype")) { | ||
| + continue; | ||
| + } | ||
| + if (i.equals("http://x.org/ind-unstated-phenotype")) { | ||
| + continue; | ||
| + } | ||
| + ProfileQuery pq = profileMatcher.createProfileQuery(i); | ||
| + TestQuery tq = new TestQuery(pq, i, 1); // self should always be ranked first | ||
| + String fn = i.replaceAll(".*/", ""); | ||
| + eval.writeJsonTo("target/boolean-test-results-"+fn+".json"); | ||
| + | ||
| + LOG.info("Evaluating for "+i); | ||
| + eval.evaluateTestQuery(profileMatcher, tq); | ||
| + //Assert.assertTrue(eval.evaluateTestQuery(profileMatcher, tq)); | ||
| + | ||
| + if (i.equals("http://x.org/ind-dec-all")) { | ||
| + Assert.assertTrue(isNotInMatchSet("http://x.org/ind-unstated-phenotype", tq.matchSet)); | ||
| + nOk++; | ||
| + } | ||
| + if (i.equals("http://x.org/ind-small-heart-big-brain")) { | ||
| + Assert.assertTrue(isNotInMatchSet("http://x.org/ind-bone", tq.matchSet)); | ||
| + nOk++; | ||
| + } | ||
| + | ||
| + } | ||
| + Assert.assertEquals(2, nOk); | ||
| + } | ||
| + | ||
| + @Test | ||
| + public void testExampleWithNegation() throws Exception { | ||
| + loadSimplePhenoWithNegation(); | ||
| + //LOG.info("INDS="+kb.getIndividualIdsInSignature()); | ||
| + ProfileMatcher profileMatcher = createProfileMatcher(kb); | ||
| + | ||
| + int nOk = 0; | ||
| + String i = "http://x.org/ind-small-heart-big-brain"; | ||
| + | ||
| + ProfileQuery pq = profileMatcher.createProfileQuery(i); | ||
| + TestQuery tq = new TestQuery(pq, i, 1); // self should always be ranked first | ||
| + String fn = i.replaceAll(".*/", ""); | ||
| + eval.writeJsonTo("target/boolean-extra-test-results-"+fn+".json"); | ||
| + Assert.assertTrue(eval.evaluateTestQuery(profileMatcher, tq)); | ||
| + | ||
| + Assert.assertTrue(isNotInMatchSet("http://x.org/ind-no-brain-phenotype", tq.matchSet)); | ||
| + | ||
| + } | ||
| +} |