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