Merge branch 'develop' into features/JAL-3010ontologyFeatureSettings
[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.assertTrue;
26
27 import jalview.datamodel.ontology.OntologyI;
28 import jalview.gui.JvOptionPane;
29
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Set;
35
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.Test;
38
39 public class SequenceOntologyTest
40 {
41
42   @BeforeClass(alwaysRun = true)
43   public void setUpJvOptionPane()
44   {
45     JvOptionPane.setInteractiveMode(false);
46     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
47   }
48
49   private OntologyI so;
50
51   @BeforeClass(alwaysRun = true)
52   public void setUp()
53   {
54     long now = System.currentTimeMillis();
55     try
56     {
57       so = new SequenceOntology();
58     } catch (Throwable t)
59     {
60       System.out.println("SOTest error ");
61       t.printStackTrace(System.err);
62     }
63     long elapsed = System.currentTimeMillis() - now;
64     System.out.println("Load and cache of Sequence Ontology took "
65             + elapsed + "ms");
66   }
67
68   @Test(groups = "Functional")
69   public void testIsA()
70   {
71     assertFalse(so.isA(null, null));
72     assertFalse(so.isA(null, "SO:0000087"));
73     assertFalse(so.isA("SO:0000087", null));
74     assertFalse(so.isA("complete", "garbage"));
75
76     assertTrue(so.isA("SO:0000087", "SO:0000704"));
77     assertFalse(so.isA("SO:0000704", "SO:0000087"));
78     assertTrue(so.isA("SO:0000736", "SO:0000735"));
79
80     // same thing:
81     assertTrue(so.isA("micronuclear_sequence", "micronuclear_sequence"));
82     // direct parent:
83     assertTrue(so.isA("micronuclear_sequence", "organelle_sequence"));
84     // grandparent:
85     assertTrue(so.isA("micronuclear_sequence", "sequence_location"));
86     // great-grandparent:
87     assertTrue(so.isA("micronuclear_sequence", "sequence_attribute"));
88
89     // same thing by name / description:
90     assertTrue(so.isA("micronuclear_sequence", "SO:0000084"));
91     assertTrue(so.isA("SO:0000084", "micronuclear_sequence"));
92     assertTrue(so.isA("SO:0000084", "SO:0000084"));
93
94     // SO name to description:
95     assertTrue(so.isA("SO:0000084", "organelle_sequence"));
96     assertTrue(so.isA("SO:0000084", "sequence_location"));
97     assertTrue(so.isA("SO:0000084", "sequence_attribute"));
98
99     // description to SO name:
100     assertTrue(so.isA("micronuclear_sequence", "SO:0000736"));
101     assertTrue(so.isA("micronuclear_sequence", "SO:0000735"));
102     assertTrue(so.isA("micronuclear_sequence", "SO:0000400"));
103   }
104
105   @Test(groups = "Functional")
106   public void testIsCDS()
107   {
108     assertTrue(so.isA("CDS", "CDS"));
109     assertTrue(so.isA("CDS_predicted", "CDS"));
110     assertTrue(so.isA("transposable_element_CDS", "CDS"));
111     assertTrue(so.isA("edited_CDS", "CDS"));
112     assertTrue(so.isA("CDS_independently_known", "CDS"));
113     assertTrue(so.isA("CDS_fragment", "CDS"));
114     assertFalse(so.isA("CDS_region", "CDS"));// part_of
115     assertFalse(so.isA("polypeptide", "CDS")); // derives_from
116   }
117
118   @Test(groups = "Functional")
119   public void testIsSequenceVariant()
120   {
121     assertFalse(so.isA("CDS", "sequence_variant"));
122     assertTrue(so.isA("sequence_variant", "sequence_variant"));
123
124     /*
125      * these should all be sub-types of sequence_variant
126      */
127     assertTrue(so.isA("structural_variant", "sequence_variant"));
128     assertTrue(so.isA("feature_variant", "sequence_variant"));
129     assertTrue(so.isA("gene_variant", "sequence_variant"));
130     assertTrue(so.isA("transcript_variant", "sequence_variant"));
131     assertTrue(so.isA("NMD_transcript_variant", "sequence_variant"));
132     assertTrue(so.isA("missense_variant", "sequence_variant"));
133     assertTrue(so.isA("synonymous_variant", "sequence_variant"));
134     assertTrue(so.isA("frameshift_variant", "sequence_variant"));
135     assertTrue(so.isA("5_prime_UTR_variant", "sequence_variant"));
136     assertTrue(so.isA("3_prime_UTR_variant", "sequence_variant"));
137     assertTrue(so.isA("stop_gained", "sequence_variant"));
138     assertTrue(so.isA("stop_lost", "sequence_variant"));
139     assertTrue(so.isA("inframe_deletion", "sequence_variant"));
140     assertTrue(so.isA("inframe_insertion", "sequence_variant"));
141   }
142
143   @Test(groups = "Functional")
144   public void testGetChildTerms()
145   {
146     List<String> terms = Collections.<String> emptyList();
147     List<String> children = so.getChildTerms("exon", terms);
148     assertTrue(children.isEmpty());
149   
150     terms = Arrays.asList("gene", "transcript", "snRNA", "junk", "mRNA");
151     children = so.getChildTerms("exon", terms);
152     assertTrue(children.isEmpty());
153     children = so.getChildTerms("transcript", terms);
154     assertEquals(children.size(), 2);
155     assertTrue(children.contains("snRNA"));
156     assertTrue(children.contains("mRNA"));
157   
158     terms = Arrays.asList("gene", "transcript", "synonymous_variant",
159             "stop_lost", "chain");
160     children = so.getChildTerms("sequence_variant", terms);
161     assertEquals(children.size(), 2);
162     assertTrue(children.contains("synonymous_variant"));
163     assertTrue(children.contains("stop_lost"));
164   }
165
166   @Test(groups = "Functional")
167   public void testGetParentTerms()
168   {
169     Set<String> terms = new HashSet<>();
170     terms.add("sequence_variant");
171     terms.add("NMD_transcript_variant");
172     terms.add("stop_lost");
173     terms.add("chain"); // not an SO term
174   
175     Set<String> parents = so.getParentTerms(terms);
176     assertEquals(parents.size(), 2);
177     assertTrue(parents.contains("sequence_variant"));
178     assertTrue(parents.contains("chain"));
179   }
180 }