7eb01c98fa37d02030b2f960035f3aabb82f569f
[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.Map;
36 import java.util.Set;
37
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.Test;
40
41 import junit.extensions.PA;
42
43 public class SequenceOntologyTest
44 {
45
46   @BeforeClass(alwaysRun = true)
47   public void setUpJvOptionPane()
48   {
49     JvOptionPane.setInteractiveMode(false);
50     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
51   }
52
53   private OntologyI so;
54
55   @BeforeClass(alwaysRun = true)
56   public void setUp()
57   {
58     long now = System.currentTimeMillis();
59     try
60     {
61       so = new SequenceOntology();
62     } catch (Throwable t)
63     {
64       System.out.println("SOTest error ");
65       t.printStackTrace(System.err);
66     }
67     long elapsed = System.currentTimeMillis() - now;
68     System.out.println("Load and cache of Sequence Ontology took "
69             + elapsed + "ms");
70   }
71
72   @Test(groups = "Functional")
73   public void testIsA()
74   {
75     assertFalse(so.isA(null, null));
76     assertFalse(so.isA(null, "SO:0000087"));
77     assertFalse(so.isA("SO:0000087", null));
78     assertFalse(so.isA("complete", "garbage"));
79
80     assertTrue(so.isA("SO:0000087", "SO:0000704"));
81     assertFalse(so.isA("SO:0000704", "SO:0000087"));
82     assertTrue(so.isA("SO:0000736", "SO:0000735"));
83
84     // same thing:
85     assertTrue(so.isA("micronuclear_sequence", "micronuclear_sequence"));
86     // direct parent:
87     assertTrue(so.isA("micronuclear_sequence", "organelle_sequence"));
88     // grandparent:
89     assertTrue(so.isA("micronuclear_sequence", "sequence_location"));
90     // great-grandparent:
91     assertTrue(so.isA("micronuclear_sequence", "sequence_attribute"));
92
93     // same thing by name / description:
94     assertTrue(so.isA("micronuclear_sequence", "SO:0000084"));
95     assertTrue(so.isA("SO:0000084", "micronuclear_sequence"));
96     assertTrue(so.isA("SO:0000084", "SO:0000084"));
97
98     // SO name to description:
99     assertTrue(so.isA("SO:0000084", "organelle_sequence"));
100     assertTrue(so.isA("SO:0000084", "sequence_location"));
101     assertTrue(so.isA("SO:0000084", "sequence_attribute"));
102
103     // description to SO name:
104     assertTrue(so.isA("micronuclear_sequence", "SO:0000736"));
105     assertTrue(so.isA("micronuclear_sequence", "SO:0000735"));
106     assertTrue(so.isA("micronuclear_sequence", "SO:0000400"));
107   }
108
109   @Test(groups = "Functional")
110   public void testIsCDS()
111   {
112     assertTrue(so.isA("CDS", "CDS"));
113     assertTrue(so.isA("CDS_predicted", "CDS"));
114     assertTrue(so.isA("transposable_element_CDS", "CDS"));
115     assertTrue(so.isA("edited_CDS", "CDS"));
116     assertTrue(so.isA("CDS_independently_known", "CDS"));
117     assertTrue(so.isA("CDS_fragment", "CDS"));
118     assertFalse(so.isA("CDS_region", "CDS"));// part_of
119     assertFalse(so.isA("polypeptide", "CDS")); // derives_from
120   }
121
122   @Test(groups = "Functional")
123   public void testIsSequenceVariant()
124   {
125     assertFalse(so.isA("CDS", "sequence_variant"));
126     assertTrue(so.isA("sequence_variant", "sequence_variant"));
127
128     /*
129      * these should all be sub-types of sequence_variant
130      */
131     assertTrue(so.isA("structural_variant", "sequence_variant"));
132     assertTrue(so.isA("feature_variant", "sequence_variant"));
133     assertTrue(so.isA("gene_variant", "sequence_variant"));
134     assertTrue(so.isA("transcript_variant", "sequence_variant"));
135     assertTrue(so.isA("NMD_transcript_variant", "sequence_variant"));
136     assertTrue(so.isA("missense_variant", "sequence_variant"));
137     assertTrue(so.isA("synonymous_variant", "sequence_variant"));
138     assertTrue(so.isA("frameshift_variant", "sequence_variant"));
139     assertTrue(so.isA("5_prime_UTR_variant", "sequence_variant"));
140     assertTrue(so.isA("3_prime_UTR_variant", "sequence_variant"));
141     assertTrue(so.isA("stop_gained", "sequence_variant"));
142     assertTrue(so.isA("stop_lost", "sequence_variant"));
143     assertTrue(so.isA("inframe_deletion", "sequence_variant"));
144     assertTrue(so.isA("inframe_insertion", "sequence_variant"));
145   }
146
147   @Test(groups = "Functional")
148   public void testGetChildTerms()
149   {
150     List<String> terms = Collections.<String> emptyList();
151     List<String> children = so.getChildTerms("exon", terms);
152     assertTrue(children.isEmpty());
153   
154     terms = Arrays.asList("gene", "transcript", "snRNA", "junk", "mRNA");
155     children = so.getChildTerms("exon", terms);
156     assertTrue(children.isEmpty());
157     children = so.getChildTerms("transcript", terms);
158     assertEquals(children.size(), 2);
159     assertTrue(children.contains("snRNA"));
160     assertTrue(children.contains("mRNA"));
161   
162     terms = Arrays.asList("gene", "transcript", "synonymous_variant",
163             "stop_lost", "chain");
164     children = so.getChildTerms("sequence_variant", terms);
165     assertEquals(children.size(), 2);
166     assertTrue(children.contains("synonymous_variant"));
167     assertTrue(children.contains("stop_lost"));
168   }
169
170   @Test(groups = "Functional")
171   public void testGetParentTerms()
172   {
173     Set<String> terms = new HashSet<>();
174     terms.add("sequence_variant");
175     terms.add("NMD_transcript_variant");
176     terms.add("stop_lost");
177     terms.add("chain"); // not an SO term
178   
179     Set<String> parents = so.getParentTerms(terms);
180     assertEquals(parents.size(), 2);
181     assertTrue(parents.contains("sequence_variant"));
182     assertTrue(parents.contains("chain"));
183   }
184
185   @Test(groups = "Functional")
186   public void testGetParents()
187   {
188     // invalid term
189     List<String> roots = so.getParents("xyz");
190     assertTrue(roots.isEmpty());
191
192     roots = so.getParents("stop_gained");
193     assertEquals(roots.size(), 2);
194     assertTrue(roots.contains("nonsynonymous_variant"));
195     assertTrue(roots.contains("feature_truncation"));
196
197     // top level term
198     roots = so.getParents("sequence_variant");
199     assertTrue(roots.isEmpty());
200
201     roots = so.getParents(null);
202     assertTrue(roots.isEmpty());
203   }
204
205   @Test(groups = "Functional")
206   public void testGetRootParents()
207   {
208     List<String> roots = so.getRootParents("xyz");
209     assertNull(roots);
210     roots = so.getRootParents(null);
211     assertNull(roots);
212
213     roots = so.getRootParents("stop_gained");
214     assertEquals(roots.size(), 1);
215     assertEquals(roots.get(0), "sequence_variant");
216
217     roots = so.getRootParents("sequence_variant");
218     assertEquals(roots.size(), 1);
219     assertEquals(roots.get(0), "sequence_variant");
220
221     roots = so.getRootParents("alanine");
222     assertEquals(roots.size(), 1);
223     assertEquals(roots.get(0), "sequence_feature");
224
225     /*
226      * verify these are now cached
227      */
228     Map<String, List<String>> cached = (Map<String, List<String>>) PA
229             .getValue(so, "rootParents");
230     List<String> parents = cached.get("stop_gained");
231     assertEquals(parents.size(), 1);
232     parents = cached.get("sequence_variant");
233     assertEquals(parents.size(), 1);
234     assertTrue(parents.contains("sequence_variant"));
235     parents = cached.get("alanine");
236     assertEquals(parents.size(), 1);
237     assertTrue(parents.contains("sequence_feature"));
238   }
239 }