From 8257b7287b84cad607887769195dbbc9601ea852 Mon Sep 17 00:00:00 2001 From: pnrobinson Date: Wed, 13 Dec 2017 16:49:29 -0800 Subject: [PATCH] adding function existsPath together with test --- .../ontology/data/ImmutableOntology.java | 24 +++++++++++++++---- .../ontolib/ontology/data/Ontology.java | 7 ++++-- .../ontology/data/ImmutableOntologyTest.java | 23 ++++++++++++++++++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/ontolib-core/src/main/java/com/github/phenomics/ontolib/ontology/data/ImmutableOntology.java b/ontolib-core/src/main/java/com/github/phenomics/ontolib/ontology/data/ImmutableOntology.java index cd2559b..99f6b2f 100644 --- a/ontolib-core/src/main/java/com/github/phenomics/ontolib/ontology/data/ImmutableOntology.java +++ b/ontolib-core/src/main/java/com/github/phenomics/ontolib/ontology/data/ImmutableOntology.java @@ -1,9 +1,6 @@ package com.github.phenomics.ontolib.ontology.data; -import java.util.Collection; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; import com.github.phenomics.ontolib.graph.algo.BreadthFirstSearch; import com.github.phenomics.ontolib.graph.algo.VertexVisitor; @@ -188,6 +185,25 @@ public Set getObsoleteTermIds() { return obsoleteTermIds; } + @Override + public boolean existsPath(final TermId sourceID, TermId destID){ + // special case -- a term cannot have a path to itself in an ontology (DAG) + if (sourceID.equals(destID)) return false; + List visited = new ArrayList<>(); + BreadthFirstSearch> bfs = new BreadthFirstSearch<>(); + bfs.startFromForward(graph, sourceID, new VertexVisitor>() { + @Override + public boolean visit(DirectedGraph> g, TermId termId) { + visited.add(termId); + return true; + }}); + return visited.contains(destID); + } + + + + + @Override public Ontology subOntology(TermId subOntologyRoot) { final Set childTermIds = OntologyTerms.childrenOf(subOntologyRoot, this); diff --git a/ontolib-core/src/main/java/com/github/phenomics/ontolib/ontology/data/Ontology.java b/ontolib-core/src/main/java/com/github/phenomics/ontolib/ontology/data/Ontology.java index 60efcad..bc11c79 100644 --- a/ontolib-core/src/main/java/com/github/phenomics/ontolib/ontology/data/Ontology.java +++ b/ontolib-core/src/main/java/com/github/phenomics/ontolib/ontology/data/Ontology.java @@ -42,7 +42,7 @@ default TermId getPrimaryTermId(TermId termId) { /** * Return all the {@link TermId}s of all ancestors from {@code termId}. - * + * * @param termId The {@link TermId} to query ancestor {@link TermId}s for. * @param includeRoot Whether or not to include the root. * @return {@link Set} of {@link TermId}s of the ancestors of {@code termId} (including itself), @@ -52,7 +52,7 @@ default TermId getPrimaryTermId(TermId termId) { /** * Return all the {@link TermId}s of all ancestors from {@code termId}, including root. - * + * * @param termId The {@link TermId} to query ancestor {@link TermId}s for. * @return {@link Set} of {@link TermId}s of the ancestors of {@code termId} (including itself), * including root. @@ -97,6 +97,9 @@ default Set getParentTermIds(TermId termId) { return result; } + public boolean existsPath(final TermId sourceID, TermId destID); + + /** * Construct and return sub ontology, starting from {@code subOntologyRoot}. * diff --git a/ontolib-core/src/test/java/com/github/phenomics/ontolib/ontology/data/ImmutableOntologyTest.java b/ontolib-core/src/test/java/com/github/phenomics/ontolib/ontology/data/ImmutableOntologyTest.java index 28aa8f6..9783dd2 100644 --- a/ontolib-core/src/test/java/com/github/phenomics/ontolib/ontology/data/ImmutableOntologyTest.java +++ b/ontolib-core/src/test/java/com/github/phenomics/ontolib/ontology/data/ImmutableOntologyTest.java @@ -1,6 +1,7 @@ package com.github.phenomics.ontolib.ontology.data; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Test; @@ -36,4 +37,26 @@ public void test() { ontology.getParentTermIds(id1).toString()); } + + /** The example graph has id1->id2, id1->id3, id1->id4, id2->id5, id4->id5 */ + @Test + public void testPathExists() { + assertTrue(ontology.existsPath(id1,id2)); + assertFalse(ontology.existsPath(id2,id1)); + assertTrue(ontology.existsPath(id1,id3)); + assertFalse(ontology.existsPath(id3,id1)); + assertTrue(ontology.existsPath(id1,id4)); + assertFalse(ontology.existsPath(id4,id1)); + assertTrue(ontology.existsPath(id1,id5)); + assertFalse(ontology.existsPath(id5,id1)); + assertTrue(ontology.existsPath(id2,id5)); + assertFalse(ontology.existsPath(id5,id2)); + assertTrue(ontology.existsPath(id4,id5)); + assertFalse(ontology.existsPath(id5,id4)); + // test that a term cannot have a path to itself. + assertFalse(ontology.existsPath(id5,id5)); + + } + + }