1 package jalview.datamodel.ontology;
3 import jalview.io.gff.SequenceOntologyFactory;
4 import jalview.io.gff.SequenceOntologyI;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.List;
15 * A base class for models of Sequence Ontology and others
20 public abstract class OntologyBase implements OntologyI
22 protected Map<String, List<String>> rootParents = new HashMap<>();
25 public Set<String> getParentTerms(Set<String> terms)
27 Set<String> parents = new HashSet<>(terms);
29 boolean childRemoved = true;
32 childRemoved = removeChild(parents);
38 * Removes the first term in the given set found which is a child of another
39 * term in the set. Answers true if a child was found and removed, else false.
44 boolean removeChild(Set<String> terms)
46 for (String t1 : terms)
48 for (String t2 : terms)
69 public List<String> getChildTerms(String parent, List<String> terms)
71 List<String> children = new ArrayList<>();
72 for (String term : terms)
74 if (!term.equals(parent) && isA(term, parent))
83 * Answers a (possibly empty) map of any Ontology terms (from the given term
84 * and its parents) which subsume one or more of the target terms. The map key
85 * is an ontology term, and the entry is the list of target terms that are
86 * sub-terms of the key.
88 * For example if {@code stop_gained} and {@code stop_lost} are known feature
89 * types, then SO term {@ nonsynonymous_variant} is the first common parent of
93 * the term to search from
95 * candidate terms to 'capture' in ontology groupings
99 public Map<String, List<String>> findSequenceOntologyGroupings(
100 String givenTerm, List<String> targetTerms)
102 List<String> sortedTypes = new ArrayList<>(targetTerms);
103 Collections.sort(sortedTypes);
105 Map<String, List<String>> parents = new HashMap<>();
106 if (!isValidTerm(givenTerm))
113 * walk up featureType and all of its parents
114 * find other feature types which are subsumed by each term
115 * add each distinct aggregation of included feature types to the map
117 List<String> candidates = new ArrayList<>();
118 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
120 candidates.add(givenTerm);
121 while (!candidates.isEmpty())
123 String term = candidates.remove(0);
124 List<String> includedFeatures = new ArrayList<>();
125 for (String type : sortedTypes)
127 if (!type.equals(givenTerm) && so.isA(type, term))
129 includedFeatures.add(type);
132 if (!includedFeatures.isEmpty()
133 && !parents.containsValue(includedFeatures))
135 parents.put(term, includedFeatures);
137 candidates.addAll(so.getParents(term));