JAL-2114 explicit invalid test cases added
[jalview.git] / test / jalview / ext / ensembl / EnsemblGeneTest.java
1 package jalview.ext.ensembl;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertSame;
6 import static org.testng.AssertJUnit.assertTrue;
7
8 import jalview.api.FeatureSettingsModelI;
9 import jalview.datamodel.SequenceDummy;
10 import jalview.datamodel.SequenceFeature;
11 import jalview.datamodel.SequenceI;
12 import jalview.io.gff.SequenceOntologyFactory;
13 import jalview.io.gff.SequenceOntologyLite;
14 import jalview.util.MapList;
15
16 import java.awt.Color;
17 import java.util.List;
18
19 import org.testng.annotations.AfterClass;
20 import org.testng.annotations.BeforeClass;
21 import org.testng.annotations.Test;
22
23 public class EnsemblGeneTest
24 {
25   @BeforeClass
26   public void setUp()
27   {
28     SequenceOntologyFactory.setInstance(new SequenceOntologyLite());
29   }
30
31   @AfterClass
32   public void tearDown()
33   {
34     SequenceOntologyFactory.setInstance(null);
35   }
36
37   /**
38    * Test that the gene part of genomic sequence is uniquely identified by a
39    * 'gene' features (or subtype) with the correct gene ID
40    */
41   @Test(groups = "Functional")
42   public void testGetGenomicRangesFromFeatures()
43   {
44     EnsemblGene testee = new EnsemblGene();
45     SequenceI genomic = new SequenceDummy("chr7");
46     genomic.setStart(10000);
47     genomic.setEnd(50000);
48     String geneId = "ABC123";
49
50     // gene at (start+10000) length 501
51     SequenceFeature sf = new SequenceFeature("gene", "", 20000, 20500, 0f,
52             null);
53     sf.setValue("ID", "gene:" + geneId);
54     sf.setStrand("+");
55     genomic.addSequenceFeature(sf);
56
57     // gene at (start + 10500) length 101
58     // should be ignored - the first 'gene' found defines the whole range
59     sf = new SequenceFeature("gene", "", 10500, 10600, 0f, null);
60     sf.setValue("ID", "gene:" + geneId);
61     sf.setStrand("+");
62     genomic.addSequenceFeature(sf);
63
64     MapList ranges = testee.getGenomicRangesFromFeatures(genomic, geneId,
65             23);
66     List<int[]> fromRanges = ranges.getFromRanges();
67     assertEquals(1, fromRanges.size());
68     assertEquals(20000, fromRanges.get(0)[0]);
69     assertEquals(20500, fromRanges.get(0)[1]);
70     // to range should start from given start numbering
71     List<int[]> toRanges = ranges.getToRanges();
72     assertEquals(1, toRanges.size());
73     assertEquals(23, toRanges.get(0)[0]);
74     assertEquals(523, toRanges.get(0)[1]);
75   }
76
77   /**
78    * Test variant using a sub-type of gene from the Sequence Ontology
79    */
80   @Test(groups = "Functional")
81   public void testGetGenomicRangesFromFeatures_ncRNA_gene_reverseStrand()
82   {
83     EnsemblGene testee = new EnsemblGene();
84     SequenceI genomic = new SequenceDummy("chr7");
85     genomic.setStart(10000);
86     genomic.setEnd(50000);
87     String geneId = "ABC123";
88
89     // gene at (start+10000) length 501
90     SequenceFeature sf = new SequenceFeature("ncRNA_gene", "", 20000,
91             20500, 0f, null);
92     sf.setValue("ID", "gene:" + geneId);
93     sf.setStrand("-");
94     genomic.addSequenceFeature(sf);
95
96     // gene at (start + 10500) length 101
97     // should be ignored - the first 'gene' found defines the whole range
98     // (real data would only have one such feature)
99     sf = new SequenceFeature("gene", "", 10500, 10600, 0f, null);
100     sf.setValue("ID", "gene:" + geneId);
101     sf.setStrand("+");
102     genomic.addSequenceFeature(sf);
103
104     MapList ranges = testee.getGenomicRangesFromFeatures(genomic, geneId,
105             23);
106     List<int[]> fromRanges = ranges.getFromRanges();
107     assertEquals(1, fromRanges.size());
108     // from range on reverse strand:
109     assertEquals(20500, fromRanges.get(0)[0]);
110     assertEquals(20000, fromRanges.get(0)[1]);
111     // to range should start from given start numbering
112     List<int[]> toRanges = ranges.getToRanges();
113     assertEquals(1, toRanges.size());
114     assertEquals(23, toRanges.get(0)[0]);
115     assertEquals(523, toRanges.get(0)[1]);
116   }
117
118   /**
119    * Test the method that extracts transcript (or subtype) features with a
120    * specified gene as parent
121    */
122   @Test(groups = "Functional")
123   public void testGetTranscriptFeatures()
124   {
125     SequenceI genomic = new SequenceDummy("chr7");
126     genomic.setStart(10000);
127     genomic.setEnd(50000);
128     String geneId = "ABC123";
129
130     // transcript feature
131     SequenceFeature sf1 = new SequenceFeature("transcript", "", 20000,
132             20500, 0f, null);
133     sf1.setValue("Parent", "gene:" + geneId);
134     sf1.setValue("transcript_id", "transcript1");
135     genomic.addSequenceFeature(sf1);
136
137     // transcript sub-type feature
138     SequenceFeature sf2 = new SequenceFeature("snRNA", "", 20000,
139             20500, 0f, null);
140     sf2.setValue("Parent", "gene:" + geneId);
141     sf2.setValue("transcript_id", "transcript2");
142     genomic.addSequenceFeature(sf2);
143
144     // NMD_transcript_variant treated like transcript in Ensembl
145     SequenceFeature sf3 = new SequenceFeature("NMD_transcript_variant", "",
146             20000, 20500, 0f, null);
147     sf3.setValue("Parent", "gene:" + geneId);
148     sf3.setValue("transcript_id", "transcript3");
149     genomic.addSequenceFeature(sf3);
150
151     // transcript for a different gene - ignored
152     SequenceFeature sf4 = new SequenceFeature("snRNA", "", 20000, 20500,
153             0f, null);
154     sf4.setValue("Parent", "gene:XYZ");
155     sf4.setValue("transcript_id", "transcript4");
156     genomic.addSequenceFeature(sf4);
157
158     EnsemblGene testee = new EnsemblGene();
159
160     /*
161      * with no filter
162      */
163     List<SequenceFeature> features = testee.getTranscriptFeatures(geneId,
164             genomic);
165     assertEquals(3, features.size());
166     assertSame(sf1, features.get(0));
167     assertSame(sf2, features.get(1));
168     assertSame(sf3, features.get(2));
169   }
170
171   /**
172    * Test the method that retains features except for 'gene', or 'transcript'
173    * with parent other than the given id
174    */
175   @Test(groups = "Functional")
176   public void testRetainFeature()
177   {
178     String geneId = "ABC123";
179     EnsemblGene testee = new EnsemblGene();
180     SequenceFeature sf = new SequenceFeature("gene", "", 20000,
181             20500, 0f, null);
182     sf.setValue("ID", "gene:" + geneId);
183     assertFalse(testee.retainFeature(sf, geneId));
184
185     sf.setType("transcript");
186     sf.setValue("Parent", "gene:" + geneId);
187     assertTrue(testee.retainFeature(sf, geneId));
188
189     sf.setType("mature_transcript");
190     sf.setValue("Parent", "gene:" + geneId);
191     assertTrue(testee.retainFeature(sf, geneId));
192
193     sf.setType("NMD_transcript_variant");
194     sf.setValue("Parent", "gene:" + geneId);
195     assertTrue(testee.retainFeature(sf, geneId));
196
197     sf.setValue("Parent", "gene:XYZ");
198     assertFalse(testee.retainFeature(sf, geneId));
199
200     sf.setType("anything");
201     assertTrue(testee.retainFeature(sf, geneId));
202   }
203
204   /**
205    * Test the method that picks out 'gene' (or subtype) features with the
206    * accession id as ID
207    */
208   @Test(groups = "Functional")
209   public void testIdentifiesSequence()
210   {
211     String accId = "ABC123";
212     EnsemblGene testee = new EnsemblGene();
213   
214     // gene with no ID not valid
215     SequenceFeature sf = new SequenceFeature("gene", "", 1, 2, 0f, null);
216     assertFalse(testee.identifiesSequence(sf, accId));
217   
218     // gene with wrong ID not valid
219     sf.setValue("ID", "gene:XYZ");
220     assertFalse(testee.identifiesSequence(sf, accId));
221   
222     // gene with right ID is valid
223     sf.setValue("ID", "gene:" + accId);
224     assertTrue(testee.identifiesSequence(sf, accId));
225   
226     // gene sub-type with right ID is valid
227     sf.setType("snRNA_gene");
228     assertTrue(testee.identifiesSequence(sf, accId));
229   
230     // transcript not valid:
231     sf.setType("transcript");
232     assertFalse(testee.identifiesSequence(sf, accId));
233   
234     // exon not valid:
235     sf.setType("exon");
236     assertFalse(testee.identifiesSequence(sf, accId));
237   }
238
239   /**
240    * Check behaviour of feature colour scheme for EnsemblGene sequences.
241    * Currently coded to display exon and sequence_variant (or sub-types) only,
242    * with sequence_variant in red above exon coloured by label.
243    */
244   @Test(groups = "Functional")
245   public void testGetFeatureColourScheme()
246   {
247     FeatureSettingsModelI fc = new EnsemblGene().getFeatureColourScheme();
248     assertTrue(fc.isFeatureDisplayed("exon"));
249     assertTrue(fc.isFeatureDisplayed("coding_exon")); // subtype of exon
250     assertTrue(fc.isFeatureDisplayed("sequence_variant"));
251     assertTrue(fc.isFeatureDisplayed("feature_variant")); // subtype
252     assertFalse(fc.isFeatureDisplayed("transcript"));
253     assertEquals(Color.RED, fc.getFeatureColour("sequence_variant")
254             .getColour());
255     assertEquals(Color.RED, fc.getFeatureColour("feature_variant")
256             .getColour());
257     assertTrue(fc.getFeatureColour("exon").isColourByLabel());
258     assertTrue(fc.getFeatureColour("coding_exon").isColourByLabel());
259     assertEquals(1, fc.compare("sequence_variant", "exon"));
260     assertEquals(-1, fc.compare("exon", "sequence_variant"));
261     assertEquals(1, fc.compare("feature_variant", "coding_exon"));
262     assertEquals(-1, fc.compare("coding_exon", "feature_variant"));
263     assertEquals(1f, fc.getTransparency());
264   }
265 }