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