From e148d44910a2a4240adbe3c9cb7137fc26334e17 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 22 Apr 2019 16:38:13 +0100 Subject: [PATCH] JAL-3010 relocated ontology search method to OntologyBase --- src/jalview/datamodel/ontology/OntologyBase.java | 59 ++++++++++++++++++++ src/jalview/datamodel/ontology/OntologyI.java | 19 +++++++ src/jalview/gui/FeatureTypeSettings.java | 5 +- .../ontology/OntologyBaseTest.java} | 9 +-- 4 files changed, 86 insertions(+), 6 deletions(-) rename test/jalview/{gui/FeatureTypeSettingsTest.java => datamodel/ontology/OntologyBaseTest.java} (96%) diff --git a/src/jalview/datamodel/ontology/OntologyBase.java b/src/jalview/datamodel/ontology/OntologyBase.java index 8bdebbd..22dc37e 100644 --- a/src/jalview/datamodel/ontology/OntologyBase.java +++ b/src/jalview/datamodel/ontology/OntologyBase.java @@ -1,6 +1,10 @@ package jalview.datamodel.ontology; +import jalview.io.gff.SequenceOntologyFactory; +import jalview.io.gff.SequenceOntologyI; + import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -74,4 +78,59 @@ public abstract class OntologyBase implements OntologyI } return children; } + + /** + * Answers a (possibly empty) map of any Ontology terms (from the given term + * and its parents) which subsume one or more of the target terms. The map key + * is an ontology term, and the entry is the list of target terms that are + * sub-terms of the key. + *

+ * For example if {@code stop_gained} and {@code stop_lost} are known feature + * types, then SO term {@ nonsynonymous_variant} is the first common parent of + * both terms + * + * @param givenTerm + * the term to search from + * @param targetTerms + * candidate terms to 'capture' in ontology groupings + * @return + */ + public Map> findSequenceOntologyGroupings( + String givenTerm, List targetTerms) + { + List sortedTypes = new ArrayList<>(targetTerms); + Collections.sort(sortedTypes); + + Map> parents = new HashMap<>(); + + /* + * method: + * walk up featureType and all of its parents + * find other feature types which are subsumed by each term + * add each distinct aggregation of included feature types to the map + */ + List candidates = new ArrayList<>(); + SequenceOntologyI so = SequenceOntologyFactory.getInstance(); + candidates.add(givenTerm); + while (!candidates.isEmpty()) + { + String term = candidates.remove(0); + List includedFeatures = new ArrayList<>(); + for (String type : sortedTypes) + { + if (!type.equals(givenTerm) && so.isA(type, term)) + { + includedFeatures.add(type); + } + } + if (!includedFeatures.isEmpty() + && !parents.containsValue(includedFeatures)) + { + parents.put(term, includedFeatures); + } + candidates.addAll(so.getParents(term)); + } + + return parents; + } } diff --git a/src/jalview/datamodel/ontology/OntologyI.java b/src/jalview/datamodel/ontology/OntologyI.java index 9ae2ad4..f82adb0 100644 --- a/src/jalview/datamodel/ontology/OntologyI.java +++ b/src/jalview/datamodel/ontology/OntologyI.java @@ -1,6 +1,7 @@ package jalview.datamodel.ontology; import java.util.List; +import java.util.Map; import java.util.Set; public interface OntologyI @@ -79,4 +80,22 @@ public interface OntologyI */ List getRootParents(String term); + /** + * Answers a (possibly empty) map of any Ontology terms (from the given term + * and its parents) which subsume one or more of the target terms. The map key + * is an ontology term, and the entry is the list of target terms that are + * sub-terms of the key. + *

+ * For example if {@code stop_gained} and {@code stop_lost} are known feature + * types, then SO term {@ nonsynonymous_variant} is the first common parent of + * both terms + * + * @param givenTerm + * the term to search from + * @param targetTerms + * candidate terms to 'capture' in ontology groupings + * @return + */ + Map> findSequenceOntologyGroupings(String givenTerm, + List targetTerms); } \ No newline at end of file diff --git a/src/jalview/gui/FeatureTypeSettings.java b/src/jalview/gui/FeatureTypeSettings.java index e7efea9..b5eb9be 100644 --- a/src/jalview/gui/FeatureTypeSettings.java +++ b/src/jalview/gui/FeatureTypeSettings.java @@ -251,8 +251,9 @@ public class FeatureTypeSettings extends JalviewDialog this.featureType = theType; ap = fr.ap; - relatedSoTerms = findSequenceOntologyGroupings(this.featureType, - fr.getRenderOrder()); + SequenceOntologyI so = SequenceOntologyFactory.getInstance(); + relatedSoTerms = so.findSequenceOntologyGroupings( + this.featureType, fr.getRenderOrder()); /* * save original colours and filters for this feature type, diff --git a/test/jalview/gui/FeatureTypeSettingsTest.java b/test/jalview/datamodel/ontology/OntologyBaseTest.java similarity index 96% rename from test/jalview/gui/FeatureTypeSettingsTest.java rename to test/jalview/datamodel/ontology/OntologyBaseTest.java index 6a32b83..937d260 100644 --- a/test/jalview/gui/FeatureTypeSettingsTest.java +++ b/test/jalview/datamodel/ontology/OntologyBaseTest.java @@ -1,4 +1,4 @@ -package jalview.gui; +package jalview.datamodel.ontology; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; @@ -14,7 +14,7 @@ import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -public class FeatureTypeSettingsTest +public class OntologyBaseTest { @BeforeClass(alwaysRun = true) public void setUp() @@ -28,7 +28,7 @@ public class FeatureTypeSettingsTest SequenceOntologyFactory.setInstance(null); } - @Test(groups="Functional") + @Test(groups = "Functional") public void testfindSequenceOntologyGroupings() { /* @@ -48,7 +48,7 @@ public class FeatureTypeSettingsTest * feature_variant further adds upstream_gene_variant * sequence_variant further adds sequence_variant */ - Map> map = FeatureTypeSettings + Map> map = SequenceOntologyFactory.getInstance() .findSequenceOntologyGroupings("stop_gained", featureTypes); assertEquals(map.size(), 10); @@ -144,4 +144,5 @@ public class FeatureTypeSettingsTest + "non_coding_transcript_exon_variant, sequence_variant, splice_region_variant, " + "stop_lost, synonymous_variant, upstream_gene_variant]"); } + } -- 1.7.10.2