JAL-2490 test coverage for printGffFormat before refactoring
[jalview.git] / test / jalview / ext / ensembl / EnsemblSeqProxyTest.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.ensembl;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertSame;
26 import static org.testng.AssertJUnit.assertTrue;
27
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceI;
31 import jalview.datamodel.features.SequenceFeatures;
32 import jalview.gui.JvOptionPane;
33 import jalview.io.DataSourceType;
34 import jalview.io.FastaFile;
35 import jalview.io.FileParse;
36 import jalview.io.gff.SequenceOntologyFactory;
37 import jalview.io.gff.SequenceOntologyLite;
38
39 import java.lang.reflect.Method;
40 import java.util.Arrays;
41 import java.util.List;
42
43 import org.testng.Assert;
44 import org.testng.annotations.AfterClass;
45 import org.testng.annotations.BeforeClass;
46 import org.testng.annotations.DataProvider;
47 import org.testng.annotations.Test;
48
49 public class EnsemblSeqProxyTest
50 {
51
52   @BeforeClass(alwaysRun = true)
53   public void setUpJvOptionPane()
54   {
55     JvOptionPane.setInteractiveMode(false);
56     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
57   }
58
59   private static final Object[][] allSeqs = new Object[][] {
60       {
61           new EnsemblProtein(),
62           "CCDS5863.1",
63           ">CCDS5863.1\n"
64                   + "MAALSGGGGGGAEPGQALFNGDMEPEAGAGAGAAASSAADPAIPEEVWNIKQMIKLTQEH\n"
65                   + "IEALLDKFGGEHNPPSIYLEAYEEYTSKLDALQQREQQLLESLGNGTDFSVSSSASMDTV\n"
66                   + "TSSSSSSLSVLPSSLSVFQNPTDVARSNPKSPQKPIVRVFLPNKQRTVVPARCGVTVRDS\n"
67                   + "LKKALMMRGLIPECCAVYRIQDGEKKPIGWDTDISWLTGEELHVEVLENVPLTTHNFVRK\n"
68                   + "TFFTLAFCDFCRKLLFQGFRCQTCGYKFHQRCSTEVPLMCVNYDQLDLLFVSKFFEHHPI\n"
69                   + "PQEEASLAETALTSGSSPSAPASDSIGPQILTSPSPSKSIPIPQPFRPADEDHRNQFGQR\n"
70                   + "DRSSSAPNVHINTIEPVNIDDLIRDQGFRGDGGSTTGLSATPPASLPGSLTNVKALQKSP\n"
71                   + "GPQRERKSSSSSEDRNRMKTLGRRDSSDDWEIPDGQITVGQRIGSGSFGTVYKGKWHGDV\n"
72                   + "AVKMLNVTAPTPQQLQAFKNEVGVLRKTRHVNILLFMGYSTKPQLAIVTQWCEGSSLYHH\n"
73                   + "LHIIETKFEMIKLIDIARQTAQGMDYLHAKSIIHRDLKSNNIFLHEDLTVKIGDFGLATV\n"
74                   + "KSRWSGSHQFEQLSGSILWMAPEVIRMQDKNPYSFQSDVYAFGIVLYELMTGQLPYSNIN\n"
75                   + "NRDQIIFMVGRGYLSPDLSKVRSNCPKAMKRLMAECLKKKRDERPLFPQILASIELLARS\n"
76                   + "LPKIHRSASEPSLNRAGFQTEDFSLYACASPKTPIQAGGYGAFPVH\n" },
77       {
78           new EnsemblCdna(),
79           "CCDS5863.1",
80           ">CCDS5863.1\n"
81                   + "ATGGCGGCGCTGAGCGGTGGCGGTGGTGGCGGCGCGGAGCCGGGCCAGGCTCTGTTCAAC\n"
82                   + "GGGGACATGGAGCCCGAGGCCGGCGCCGGCGCCGGCGCCGCGGCCTCTTCGGCTGCGGAC\n"
83                   + "CCTGCCATTCCGGAGGAGGTGTGGAATATCAAACAAATGATTAAGTTGACACAGGAACAT\n"
84                   + "ATAGAGGCCCTATTGGACAAATTTGGTGGGGAGCATAATCCACCATCAATATATCTGGAG\n"
85                   + "GCCTATGAAGAATACACCAGCAAGCTAGATGCACTCCAACAAAGAGAACAACAGTTATTG\n"
86                   + "GAATCTCTGGGGAACGGAACTGATTTTTCTGTTTCTAGCTCTGCATCAATGGATACCGTT\n"
87                   + "ACATCTTCTTCCTCTTCTAGCCTTTCAGTGCTACCTTCATCTCTTTCAGTTTTTCAAAAT\n"
88                   + "CCCACAGATGTGGCACGGAGCAACCCCAAGTCACCACAAAAACCTATCGTTAGAGTCTTC\n"
89                   + "CTGCCCAACAAACAGAGGACAGTGGTACCTGCAAGGTGTGGAGTTACAGTCCGAGACAGT\n"
90                   + "CTAAAGAAAGCACTGATGATGAGAGGTCTAATCCCAGAGTGCTGTGCTGTTTACAGAATT\n"
91                   + "CAGGATGGAGAGAAGAAACCAATTGGTTGGGACACTGATATTTCCTGGCTTACTGGAGAA\n"
92                   + "GAATTGCATGTGGAAGTGTTGGAGAATGTTCCACTTACAACACACAACTTTGTACGAAAA\n"
93                   + "ACGTTTTTCACCTTAGCATTTTGTGACTTTTGTCGAAAGCTGCTTTTCCAGGGTTTCCGC\n"
94                   + "TGTCAAACATGTGGTTATAAATTTCACCAGCGTTGTAGTACAGAAGTTCCACTGATGTGT\n"
95                   + "GTTAATTATGACCAACTTGATTTGCTGTTTGTCTCCAAGTTCTTTGAACACCACCCAATA\n"
96                   + "CCACAGGAAGAGGCGTCCTTAGCAGAGACTGCCCTAACATCTGGATCATCCCCTTCCGCA\n"
97                   + "CCCGCCTCGGACTCTATTGGGCCCCAAATTCTCACCAGTCCGTCTCCTTCAAAATCCATT\n"
98                   + "CCAATTCCACAGCCCTTCCGACCAGCAGATGAAGATCATCGAAATCAATTTGGGCAACGA\n"
99                   + "GACCGATCCTCATCAGCTCCCAATGTGCATATAAACACAATAGAACCTGTCAATATTGAT\n"
100                   + "GACTTGATTAGAGACCAAGGATTTCGTGGTGATGGAGGATCAACCACAGGTTTGTCTGCT\n"
101                   + "ACCCCCCCTGCCTCATTACCTGGCTCACTAACTAACGTGAAAGCCTTACAGAAATCTCCA\n"
102                   + "GGACCTCAGCGAGAAAGGAAGTCATCTTCATCCTCAGAAGACAGGAATCGAATGAAAACA\n"
103                   + "CTTGGTAGACGGGACTCGAGTGATGATTGGGAGATTCCTGATGGGCAGATTACAGTGGGA\n"
104                   + "CAAAGAATTGGATCTGGATCATTTGGAACAGTCTACAAGGGAAAGTGGCATGGTGATGTG\n"
105                   + "GCAGTGAAAATGTTGAATGTGACAGCACCTACACCTCAGCAGTTACAAGCCTTCAAAAAT\n"
106                   + "GAAGTAGGAGTACTCAGGAAAACACGACATGTGAATATCCTACTCTTCATGGGCTATTCC\n"
107                   + "ACAAAGCCACAACTGGCTATTGTTACCCAGTGGTGTGAGGGCTCCAGCTTGTATCACCAT\n"
108                   + "CTCCATATCATTGAGACCAAATTTGAGATGATCAAACTTATAGATATTGCACGACAGACT\n"
109                   + "GCACAGGGCATGGATTACTTACACGCCAAGTCAATCATCCACAGAGACCTCAAGAGTAAT\n"
110                   + "AATATATTTCTTCATGAAGACCTCACAGTAAAAATAGGTGATTTTGGTCTAGCTACAGTG\n"
111                   + "AAATCTCGATGGAGTGGGTCCCATCAGTTTGAACAGTTGTCTGGATCCATTTTGTGGATG\n"
112                   + "GCACCAGAAGTCATCAGAATGCAAGATAAAAATCCATACAGCTTTCAGTCAGATGTATAT\n"
113                   + "GCATTTGGAATTGTTCTGTATGAATTGATGACTGGACAGTTACCTTATTCAAACATCAAC\n"
114                   + "AACAGGGACCAGATAATTTTTATGGTGGGACGAGGATACCTGTCTCCAGATCTCAGTAAG\n"
115                   + "GTACGGAGTAACTGTCCAAAAGCCATGAAGAGATTAATGGCAGAGTGCCTCAAAAAGAAA\n"
116                   + "AGAGATGAGAGACCACTCTTTCCCCAAATTCTCGCCTCTATTGAGCTGCTGGCCCGCTCA\n"
117                   + "TTGCCAAAAATTCACCGCAGTGCATCAGAACCCTCCTTGAATCGGGCTGGTTTCCAAACA\n"
118                   + "GAGGATTTTAGTCTATATGCTTGTGCTTCTCCAAAAACACCCATCCAGGCAGGGGGATAT\n"
119                   + "GGTGCGTTTCCTGTCCACTGA\n" },
120       {
121           new EnsemblProtein(),
122           "ENSP00000288602",
123           ">ENSP00000288602\n"
124                   + "MAALSGGGGGGAEPGQALFNGDMEPEAGAGAGAAASSAADPAIPEEVWNIKQMIKLTQEH\n"
125                   + "IEALLDKFGGEHNPPSIYLEAYEEYTSKLDALQQREQQLLESLGNGTDFSVSSSASMDTV\n"
126                   + "TSSSSSSLSVLPSSLSVFQNPTDVARSNPKSPQKPIVRVFLPNKQRTVVPARCGVTVRDS\n"
127                   + "LKKALMMRGLIPECCAVYRIQDGEKKPIGWDTDISWLTGEELHVEVLENVPLTTHNFVRK\n"
128                   + "TFFTLAFCDFCRKLLFQGFRCQTCGYKFHQRCSTEVPLMCVNYDQLDLLFVSKFFEHHPI\n"
129                   + "PQEEASLAETALTSGSSPSAPASDSIGPQILTSPSPSKSIPIPQPFRPADEDHRNQFGQR\n"
130                   + "DRSSSAPNVHINTIEPVNIDDLIRDQGFRGDGGSTTGLSATPPASLPGSLTNVKALQKSP\n"
131                   + "GPQRERKSSSSSEDRNRMKTLGRRDSSDDWEIPDGQITVGQRIGSGSFGTVYKGKWHGDV\n"
132                   + "AVKMLNVTAPTPQQLQAFKNEVGVLRKTRHVNILLFMGYSTKPQLAIVTQWCEGSSLYHH\n"
133                   + "LHIIETKFEMIKLIDIARQTAQGMDYLHAKSIIHRDLKSNNIFLHEDLTVKIGDFGLATV\n"
134                   + "KSRWSGSHQFEQLSGSILWMAPEVIRMQDKNPYSFQSDVYAFGIVLYELMTGQLPYSNIN\n"
135                   + "NRDQIIFMVGRGYLSPDLSKVRSNCPKAMKRLMAECLKKKRDERPLFPQILASIELLARS\n"
136                   + "LPKIHRSASEPSLNRAGFQTEDFSLYACASPKTPIQAGGYGAFPVH" } };
137
138   @BeforeClass(alwaysRun = true)
139   public void setUp()
140   {
141     SequenceOntologyFactory.setInstance(new SequenceOntologyLite());
142   }
143
144   @AfterClass(alwaysRun = true)
145   public void tearDown()
146   {
147     SequenceOntologyFactory.setInstance(null);
148   }
149
150   @DataProvider(name = "ens_seqs")
151   public Object[][] createData(Method m)
152   {
153     System.out.println(m.getName());
154     return allSeqs;
155   }
156
157   @Test(dataProvider = "ens_seqs", suiteName = "live")
158   public void testGetOneSeqs(EnsemblRestClient proxy, String sq,
159           String fastasq) throws Exception
160   {
161     FileParse fp = proxy.getSequenceReader(Arrays
162             .asList(new String[] { sq }));
163     SequenceI[] sqs = new FastaFile(fp).getSeqsAsArray();
164     FastaFile trueRes = new FastaFile(fastasq, DataSourceType.PASTE);
165     SequenceI[] trueSqs = trueRes.getSeqsAsArray();
166     Assert.assertEquals(sqs.length, trueSqs.length,
167             "Different number of sequences retrieved for query " + sq);
168     Alignment ral = new Alignment(sqs);
169     for (SequenceI tr : trueSqs)
170     {
171       // 12/05/2017 failing for EnsemblCdna which is returning protein
172       // Ensembl helpdesk ticket 187998
173       SequenceI[] rseq;
174       Assert.assertNotNull(
175               rseq = ral.findSequenceMatch(tr.getName()),
176               "Couldn't find sequences matching expected sequence "
177                       + tr.getName());
178       Assert.assertEquals(rseq.length, 1,
179               "Expected only one sequence for sequence ID " + tr.getName());
180       Assert.assertEquals(
181               rseq[0].getSequenceAsString(),
182               tr.getSequenceAsString(),
183               "Sequences differ for " + tr.getName() + "\n" + "Exp:"
184                       + tr.getSequenceAsString() + "\n" + "Got:"
185                       + rseq[0].getSequenceAsString());
186
187     }
188   }
189
190   @Test(groups = "Functional")
191   public void getGenomicRangesFromFeatures()
192   {
193
194   }
195
196   @Test(groups = "Functional")
197   public void testIsTranscriptIdentifier()
198   {
199     EnsemblSeqProxy testee = new EnsemblGene();
200     assertFalse(testee.isTranscriptIdentifier(null));
201     assertFalse(testee.isTranscriptIdentifier(""));
202     assertFalse(testee.isTranscriptIdentifier("ENSG00000012345"));
203     assertTrue(testee.isTranscriptIdentifier("ENST00000012345"));
204     assertTrue(testee.isTranscriptIdentifier("ENSMUST00000012345"));
205     assertFalse(testee.isTranscriptIdentifier("enst00000012345"));
206     assertFalse(testee.isTranscriptIdentifier("ENST000000123456"));
207     assertFalse(testee.isTranscriptIdentifier("ENST0000001234"));
208   }
209
210   @Test(groups = "Functional")
211   public void testIsGeneIdentifier()
212   {
213     EnsemblSeqProxy testee = new EnsemblGene();
214     assertFalse(testee.isGeneIdentifier(null));
215     assertFalse(testee.isGeneIdentifier(""));
216     assertFalse(testee.isGeneIdentifier("ENST00000012345"));
217     assertTrue(testee.isGeneIdentifier("ENSG00000012345"));
218     assertTrue(testee.isGeneIdentifier("ENSMUSG00000012345"));
219     assertFalse(testee.isGeneIdentifier("ensg00000012345"));
220     assertFalse(testee.isGeneIdentifier("ENSG000000123456"));
221     assertFalse(testee.isGeneIdentifier("ENSG0000001234"));
222   }
223
224   /**
225    * Test the method that appends a single allele's reverse complement to a
226    * string buffer
227    */
228   @Test(groups = "Functional")
229   public void testReverseComplementAllele()
230   {
231     StringBuilder sb = new StringBuilder();
232     EnsemblSeqProxy.reverseComplementAllele(sb, "G"); // comp=C
233     EnsemblSeqProxy.reverseComplementAllele(sb, "g"); // comp=c
234     EnsemblSeqProxy.reverseComplementAllele(sb, "C"); // comp=G
235     EnsemblSeqProxy.reverseComplementAllele(sb, "T"); // comp=A
236     EnsemblSeqProxy.reverseComplementAllele(sb, "A"); // comp=T
237     assertEquals("C,c,G,A,T", sb.toString());
238
239     sb = new StringBuilder();
240     EnsemblSeqProxy.reverseComplementAllele(sb, "-GATt"); // revcomp=aATC-
241     EnsemblSeqProxy.reverseComplementAllele(sb, "hgmd_mutation");
242     EnsemblSeqProxy.reverseComplementAllele(sb, "PhenCode_variation");
243     assertEquals("aATC-,hgmd_mutation,PhenCode_variation", sb.toString());
244   }
245
246   /**
247    * Test the method that computes the reverse complement of the alleles in a
248    * sequence_variant feature
249    */
250   @Test(groups = "Functional")
251   public void testReverseComplementAlleles()
252   {
253     String alleles = "C,G,-TAC,HGMD_MUTATION,gac";
254     SequenceFeature sf = new SequenceFeature("sequence_variant", alleles,
255             1, 2, 0f, null);
256     sf.setValue("alleles", alleles);
257     sf.setAttributes("x=y,z;alleles=" + alleles + ";a=b,c");
258
259     EnsemblSeqProxy.reverseComplementAlleles(sf);
260     String revcomp = "G,C,GTA-,HGMD_MUTATION,gtc";
261     // verify description is updated with reverse complement
262     assertEquals(revcomp, sf.getDescription());
263     // verify alleles attribute is updated with reverse complement
264     assertEquals(revcomp, sf.getValue("alleles"));
265     // verify attributes string is updated with reverse complement
266     assertEquals("x=y,z;alleles=" + revcomp + ";a=b,c", sf.getAttributes());
267   }
268
269   @Test(groups = "Functional")
270   public void testSortFeatures()
271   {
272     SequenceFeature sf1 = new SequenceFeature("", "", 10, 15, 0f, null);
273     SequenceFeature sf2 = new SequenceFeature("", "", 8, 12, 0f, null);
274     SequenceFeature sf3 = new SequenceFeature("", "", 8, 13, 0f, null);
275     SequenceFeature sf4 = new SequenceFeature("", "", 11, 11, 0f, null);
276     List<SequenceFeature> sfs = Arrays.asList(new SequenceFeature[] { sf1,
277         sf2, sf3, sf4 });
278
279     // sort by start position ascending (forward strand)
280     // sf2 and sf3 tie and should not be reordered by sorting
281     SequenceFeatures.sortFeatures(sfs, true);
282     assertSame(sfs.get(0), sf2);
283     assertSame(sfs.get(1), sf3);
284     assertSame(sfs.get(2), sf1);
285     assertSame(sfs.get(3), sf4);
286
287     // sort by end position descending (reverse strand)
288     SequenceFeatures.sortFeatures(sfs, false);
289     assertSame(sfs.get(0), sf1);
290     assertSame(sfs.get(1), sf3);
291     assertSame(sfs.get(2), sf2);
292     assertSame(sfs.get(3), sf4);
293   }
294 }