JAL-3010 discover root parent SO terms at runtime
[jalview.git] / test / jalview / ext / so / SequenceOntologyTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ext.so;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertFalse;
25 import static org.testng.Assert.assertNull;
26 import static org.testng.Assert.assertTrue;
27
28 import jalview.datamodel.ontology.OntologyI;
29 import jalview.gui.JvOptionPane;
30
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
36
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.Test;
39
40 public class SequenceOntologyTest
41 {
42
43   @BeforeClass(alwaysRun = true)
44   public void setUpJvOptionPane()
45   {
46     JvOptionPane.setInteractiveMode(false);
47     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
48   }
49
50   private OntologyI so;
51
52   @BeforeClass(alwaysRun = true)
53   public void setUp()
54   {
55     long now = System.currentTimeMillis();
56     try
57     {
58       so = new SequenceOntology();
59     } catch (Throwable t)
60     {
61       System.out.println("SOTest error ");
62       t.printStackTrace(System.err);
63     }
64     long elapsed = System.currentTimeMillis() - now;
65     System.out.println("Load and cache of Sequence Ontology took "
66             + elapsed + "ms");
67   }
68
69   @Test(groups = "Functional")
70   public void testIsA()
71   {
72     assertFalse(so.isA(null, null));
73     assertFalse(so.isA(null, "SO:0000087"));
74     assertFalse(so.isA("SO:0000087", null));
75     assertFalse(so.isA("complete", "garbage"));
76
77     assertTrue(so.isA("SO:0000087", "SO:0000704"));
78     assertFalse(so.isA("SO:0000704", "SO:0000087"));
79     assertTrue(so.isA("SO:0000736", "SO:0000735"));
80
81     // same thing:
82     assertTrue(so.isA("micronuclear_sequence", "micronuclear_sequence"));
83     // direct parent:
84     assertTrue(so.isA("micronuclear_sequence", "organelle_sequence"));
85     // grandparent:
86     assertTrue(so.isA("micronuclear_sequence", "sequence_location"));
87     // great-grandparent:
88     assertTrue(so.isA("micronuclear_sequence", "sequence_attribute"));
89
90     // same thing by name / description:
91     assertTrue(so.isA("micronuclear_sequence", "SO:0000084"));
92     assertTrue(so.isA("SO:0000084", "micronuclear_sequence"));
93     assertTrue(so.isA("SO:0000084", "SO:0000084"));
94
95     // SO name to description:
96     assertTrue(so.isA("SO:0000084", "organelle_sequence"));
97     assertTrue(so.isA("SO:0000084", "sequence_location"));
98     assertTrue(so.isA("SO:0000084", "sequence_attribute"));
99
100     // description to SO name:
101     assertTrue(so.isA("micronuclear_sequence", "SO:0000736"));
102     assertTrue(so.isA("micronuclear_sequence", "SO:0000735"));
103     assertTrue(so.isA("micronuclear_sequence", "SO:0000400"));
104   }
105
106   @Test(groups = "Functional")
107   public void testIsCDS()
108   {
109     assertTrue(so.isA("CDS", "CDS"));
110     assertTrue(so.isA("CDS_predicted", "CDS"));
111     assertTrue(so.isA("transposable_element_CDS", "CDS"));
112     assertTrue(so.isA("edited_CDS", "CDS"));
113     assertTrue(so.isA("CDS_independently_known", "CDS"));
114     assertTrue(so.isA("CDS_fragment", "CDS"));
115     assertFalse(so.isA("CDS_region", "CDS"));// part_of
116     assertFalse(so.isA("polypeptide", "CDS")); // derives_from
117   }
118
119   @Test(groups = "Functional")
120   public void testIsSequenceVariant()
121   {
122     assertFalse(so.isA("CDS", "sequence_variant"));
123     assertTrue(so.isA("sequence_variant", "sequence_variant"));
124
125     /*
126      * these should all be sub-types of sequence_variant
127      */
128     assertTrue(so.isA("structural_variant", "sequence_variant"));
129     assertTrue(so.isA("feature_variant", "sequence_variant"));
130     assertTrue(so.isA("gene_variant", "sequence_variant"));
131     assertTrue(so.isA("transcript_variant", "sequence_variant"));
132     assertTrue(so.isA("NMD_transcript_variant", "sequence_variant"));
133     assertTrue(so.isA("missense_variant", "sequence_variant"));
134     assertTrue(so.isA("synonymous_variant", "sequence_variant"));
135     assertTrue(so.isA("frameshift_variant", "sequence_variant"));
136     assertTrue(so.isA("5_prime_UTR_variant", "sequence_variant"));
137     assertTrue(so.isA("3_prime_UTR_variant", "sequence_variant"));
138     assertTrue(so.isA("stop_gained", "sequence_variant"));
139     assertTrue(so.isA("stop_lost", "sequence_variant"));
140     assertTrue(so.isA("inframe_deletion", "sequence_variant"));
141     assertTrue(so.isA("inframe_insertion", "sequence_variant"));
142   }
143
144   @Test(groups = "Functional")
145   public void testGetChildTerms()
146   {
147     List<String> terms = Collections.<String> emptyList();
148     List<String> children = so.getChildTerms("exon", terms);
149     assertTrue(children.isEmpty());
150   
151     terms = Arrays.asList("gene", "transcript", "snRNA", "junk", "mRNA");
152     children = so.getChildTerms("exon", terms);
153     assertTrue(children.isEmpty());
154     children = so.getChildTerms("transcript", terms);
155     assertEquals(children.size(), 2);
156     assertTrue(children.contains("snRNA"));
157     assertTrue(children.contains("mRNA"));
158   
159     terms = Arrays.asList("gene", "transcript", "synonymous_variant",
160             "stop_lost", "chain");
161     children = so.getChildTerms("sequence_variant", terms);
162     assertEquals(children.size(), 2);
163     assertTrue(children.contains("synonymous_variant"));
164     assertTrue(children.contains("stop_lost"));
165   }
166
167   @Test(groups = "Functional")
168   public void testGetParentTerms()
169   {
170     Set<String> terms = new HashSet<>();
171     terms.add("sequence_variant");
172     terms.add("NMD_transcript_variant");
173     terms.add("stop_lost");
174     terms.add("chain"); // not an SO term
175   
176     Set<String> parents = so.getParentTerms(terms);
177     assertEquals(parents.size(), 2);
178     assertTrue(parents.contains("sequence_variant"));
179     assertTrue(parents.contains("chain"));
180   }
181
182   @Test(groups = "Functional")
183   public void testGetRootParents()
184   {
185     List<String> roots = so.getRootParents("xyz");
186     assertNull(roots);
187     roots = so.getRootParents(null);
188     assertNull(roots);
189
190     roots = so.getRootParents("stop_gained");
191     assertEquals(roots.size(), 1);
192     assertEquals(roots.get(0), "sequence_variant");
193
194     roots = so.getRootParents("sequence_variant");
195     assertEquals(roots.size(), 1);
196     assertEquals(roots.get(0), "sequence_variant");
197
198     roots = so.getRootParents("alanine");
199     assertEquals(roots.size(), 1);
200     assertEquals(roots.get(0), "sequence_feature");
201   }
202 }