Merge branch 'features/JAL-3010ontologyFeatureSettings' into
[jalview.git] / src / jalview / datamodel / ontology / OntologyBase.java
1 package jalview.datamodel.ontology;
2
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.List;
6 import java.util.Set;
7
8 /**
9  * A base class for models of Sequence Ontology and others
10  * 
11  * @author gmcarstairs
12  *
13  */
14 public abstract class OntologyBase implements OntologyI
15 {
16   @Override
17   public Set<String> getParentTerms(Set<String> terms)
18   {
19     Set<String> parents = new HashSet<>(terms);
20
21     boolean childRemoved = true;
22     while (childRemoved)
23     {
24       childRemoved = removeChild(parents);
25     }
26     return parents;
27   }
28
29   /**
30    * Removes the first term in the given set found which is a child of another
31    * term in the set. Answers true if a child was found and removed, else false.
32    * 
33    * @param terms
34    * @return
35    */
36   boolean removeChild(Set<String> terms)
37   {
38     for (String t1 : terms)
39     {
40       for (String t2 : terms)
41       {
42         if (t1 != t2)
43         {
44           if (isA(t1, t2))
45           {
46             terms.remove(t1);
47             return true;
48           }
49           if (isA(t2, t1))
50           {
51             terms.remove(t2);
52             return true;
53           }
54         }
55       }
56     }
57     return false;
58   }
59
60   @Override
61   public List<String> getChildTerms(String parent, List<String> terms)
62   {
63     List<String> children = new ArrayList<>();
64     for (String term : terms)
65     {
66       if (!term.equals(parent) && isA(term, parent))
67       {
68         children.add(term);
69       }
70     }
71     return children;
72   }
73 }