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