JAL-3010 relocated ontology search method to OntologyBase
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 22 Apr 2019 15:38:13 +0000 (16:38 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 22 Apr 2019 15:38:13 +0000 (16:38 +0100)
src/jalview/datamodel/ontology/OntologyBase.java
src/jalview/datamodel/ontology/OntologyI.java
src/jalview/gui/FeatureTypeSettings.java
test/jalview/datamodel/ontology/OntologyBaseTest.java [moved from test/jalview/gui/FeatureTypeSettingsTest.java with 96% similarity]

index 8bdebbd..22dc37e 100644 (file)
@@ -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.
+   * <p>
+   * 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<String, List<String>> findSequenceOntologyGroupings(
+          String givenTerm, List<String> targetTerms)
+  {
+    List<String> sortedTypes = new ArrayList<>(targetTerms);
+    Collections.sort(sortedTypes);
+  
+    Map<String, List<String>> 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<String> candidates = new ArrayList<>();
+    SequenceOntologyI so = SequenceOntologyFactory.getInstance();
+    candidates.add(givenTerm);
+    while (!candidates.isEmpty())
+    {
+      String term = candidates.remove(0);
+      List<String> 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;
+  }
 }
index 9ae2ad4..f82adb0 100644 (file)
@@ -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<String> 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.
+   * <p>
+   * 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<String, List<String>> findSequenceOntologyGroupings(String givenTerm,
+          List<String> targetTerms);
 }
\ No newline at end of file
index e7efea9..b5eb9be 100644 (file)
@@ -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,
@@ -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<String, List<String>> map = FeatureTypeSettings
+    Map<String, List<String>> 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]");
   }
+
 }