JAL-653 Gff tests moved back in to FeaturesFileTest
[jalview.git] / test / jalview / io / FeaturesFileTest.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.io;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertNull;
27 import static org.testng.AssertJUnit.assertTrue;
28
29 import jalview.datamodel.Alignment;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.SequenceDummy;
32 import jalview.datamodel.SequenceFeature;
33 import jalview.datamodel.SequenceI;
34 import jalview.gui.AlignFrame;
35 import jalview.schemes.AnnotationColourGradient;
36 import jalview.schemes.GraduatedColor;
37
38 import java.awt.Color;
39 import java.io.File;
40 import java.io.IOException;
41 import java.util.Map;
42
43 import org.testng.annotations.Test;
44
45 public class FeaturesFileTest
46 {
47
48   private static String exonerateSeqs = "examples/testdata/exonerateseqs.fa",
49           exonerateOutput = "examples/testdata/exonerateoutput.gff",
50           simpleGffFile = "examples/testdata/simpleGff3.gff";
51
52   @Test(groups = { "Functional" })
53   public void testParse() throws Exception
54   {
55     File f = new File("examples/uniref50.fa");
56     AlignmentI al = readAlignmentFile(f);
57     AlignFrame af = new AlignFrame(al, 500, 500);
58     Map<String, Object> colours = af.getFeatureRenderer()
59             .getFeatureColours();
60     FeaturesFile featuresFile = new FeaturesFile(
61             "examples/exampleFeatures.txt", FormatAdapter.FILE);
62     assertTrue("Test " + "Features file test"
63             + "\nFailed to parse features file.",
64             featuresFile.parse(al.getDataset(), colours, true));
65
66     /*
67      * Refetch the colour map from the FeatureRenderer (to confirm it has been
68      * updated - JAL-1904), and verify (some) feature group colours
69      */
70     colours = af.getFeatureRenderer().getFeatureColours();
71     assertEquals("26 feature group colours not found", 26, colours.size());
72     assertEquals(colours.get("Cath"), new Color(0x93b1d1));
73     assertEquals(colours.get("ASX-MOTIF"), new Color(0x6addbb));
74
75     /*
76      * verify (some) features on sequences
77      */
78     SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
79             .getSequenceFeatures(); // FER_CAPAA
80     assertEquals(7, sfs.length);
81     SequenceFeature sf = sfs[0];
82     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
83     assertEquals(39, sf.begin);
84     assertEquals(39, sf.end);
85     assertEquals("uniprot", sf.featureGroup);
86     assertEquals("METAL", sf.type);
87     sf = sfs[1];
88     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
89     assertEquals(44, sf.begin);
90     assertEquals(44, sf.end);
91     assertEquals("uniprot", sf.featureGroup);
92     assertEquals("METAL", sf.type);
93     sf = sfs[2];
94     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
95     assertEquals(47, sf.begin);
96     assertEquals(47, sf.end);
97     assertEquals("uniprot", sf.featureGroup);
98     assertEquals("METAL", sf.type);
99     sf = sfs[3];
100     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
101     assertEquals(77, sf.begin);
102     assertEquals(77, sf.end);
103     assertEquals("uniprot", sf.featureGroup);
104     assertEquals("METAL", sf.type);
105     sf = sfs[4];
106     assertEquals("Fer2 Status: True Positive Pfam 8_8%LINK%",
107             sf.description);
108     assertEquals("Pfam 8_8|http://pfam.sanger.ac.uk/family/PF00111",
109             sf.links.get(0).toString());
110     assertEquals(8, sf.begin);
111     assertEquals(83, sf.end);
112     assertEquals("uniprot", sf.featureGroup);
113     assertEquals("Pfam", sf.type);
114     sf = sfs[5];
115     assertEquals("Ferredoxin_fold Status: True Positive ", sf.description);
116     assertEquals(3, sf.begin);
117     assertEquals(93, sf.end);
118     assertEquals("uniprot", sf.featureGroup);
119     assertEquals("Cath", sf.type);
120     sf = sfs[6];
121     assertEquals(
122             "High confidence server. Only hits with scores over 0.8 are reported. PHOSPHORYLATION (T) 89_8%LINK%",
123             sf.description);
124     assertEquals(
125             "PHOSPHORYLATION (T) 89_8|http://www.cbs.dtu.dk/cgi-bin/proview/webface-link?seqid=P83527&amp;service=NetPhos-2.0",
126             sf.links.get(0).toString());
127     assertEquals(89, sf.begin);
128     assertEquals(89, sf.end);
129     assertEquals("netphos", sf.featureGroup);
130     assertEquals("PHOSPHORYLATION (T)", sf.type);
131   }
132
133   /**
134    * Test parsing a features file with a mix of Jalview and GFF formatted
135    * content
136    * 
137    * @throws Exception
138    */
139   @Test(groups = { "Functional" })
140   public void testParse_mixedJalviewGff() throws Exception
141   {
142     File f = new File("examples/uniref50.fa");
143     AlignmentI al = readAlignmentFile(f);
144     AlignFrame af = new AlignFrame(al, 500, 500);
145     Map<String, Object> colours = af.getFeatureRenderer()
146             .getFeatureColours();
147     String gffData = "METAL\tcc9900\n" + "GFF\n"
148             + "FER_CAPAA\tuniprot\tMETAL\t44\t45\t4.0\t.\t.\n"
149             + "FER1_SOLLC\tuniprot\tPfam\t55\t130\t2.0\t.\t.";
150     FeaturesFile featuresFile = new FeaturesFile(gffData,
151             FormatAdapter.PASTE);
152     assertTrue("Failed to parse features file",
153             featuresFile.parse(al.getDataset(), colours, true));
154
155     // verify colours read or synthesized
156     colours = af.getFeatureRenderer().getFeatureColours();
157     assertEquals("1 feature group colours not found", 1, colours.size());
158     assertEquals(colours.get("METAL"), new Color(0xcc9900));
159
160     // verify feature on FER_CAPAA
161     SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
162             .getSequenceFeatures();
163     assertEquals(1, sfs.length);
164     SequenceFeature sf = sfs[0];
165     assertEquals("uniprot", sf.description);
166     assertEquals(44, sf.begin);
167     assertEquals(45, sf.end);
168     assertEquals("uniprot", sf.featureGroup);
169     assertEquals("METAL", sf.type);
170     assertEquals(4f, sf.getScore(), 0.001f);
171
172     // verify feature on FER1_SOLLC
173     sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
174     assertEquals(1, sfs.length);
175     sf = sfs[0];
176     assertEquals("uniprot", sf.description);
177     assertEquals(55, sf.begin);
178     assertEquals(130, sf.end);
179     assertEquals("uniprot", sf.featureGroup);
180     assertEquals("Pfam", sf.type);
181     assertEquals(2f, sf.getScore(), 0.001f);
182   }
183
184   public static AlignmentI readAlignmentFile(File f) throws IOException
185   {
186     System.out.println("Reading file: " + f);
187     String ff = f.getPath();
188     FormatAdapter rf = new FormatAdapter();
189
190     AlignmentI al = rf.readFile(ff, FormatAdapter.FILE,
191             new IdentifyFile().identify(ff, FormatAdapter.FILE));
192
193     al.setDataset(null); // creates dataset sequences
194     assertNotNull("Couldn't read supplied alignment data.", al);
195     return al;
196   }
197
198   /**
199    * Test various ways of describing a feature colour scheme
200    * 
201    * @throws Exception
202    */
203   @Test(groups = { "Functional" })
204   public void testParseGraduatedColourScheme() throws Exception
205   {
206     FeaturesFile ff = new FeaturesFile();
207
208     // colour by label:
209     GraduatedColor gc = ff.parseGraduatedColourScheme(
210             "BETA-TURN-IR\t9a6a94", "label");
211     assertTrue(gc.isColourByLabel());
212     assertEquals(Color.white, gc.getMinColor());
213     assertEquals(Color.black, gc.getMaxColor());
214     assertTrue(gc.isAutoScale());
215
216     // using colour name, rgb, etc:
217     String spec = "blue|255,0,255|absolute|20.0|95.0|below|66.0";
218     gc = ff.parseGraduatedColourScheme("BETA-TURN-IR\t" + spec, spec);
219     assertFalse(gc.isColourByLabel());
220     assertEquals(Color.blue, gc.getMinColor());
221     assertEquals(new Color(255, 0, 255), gc.getMaxColor());
222     assertFalse(gc.isAutoScale());
223     assertFalse(gc.getTolow());
224     assertEquals(20.0f, gc.getMin(), 0.001f);
225     assertEquals(95.0f, gc.getMax(), 0.001f);
226     assertEquals(AnnotationColourGradient.BELOW_THRESHOLD,
227             gc.getThreshType());
228     assertEquals(66.0f, gc.getThresh(), 0.001f);
229
230     // inverse gradient high to low:
231     spec = "blue|255,0,255|95.0|20.0|below|66.0";
232     gc = ff.parseGraduatedColourScheme("BETA-TURN-IR\t" + spec, spec);
233     assertTrue(gc.isAutoScale());
234     assertTrue(gc.getTolow());
235   }
236
237   /**
238    * Test parsing a features file with GFF formatted content only
239    * 
240    * @throws Exception
241    */
242   @Test(groups = { "Functional" })
243   public void testParse_pureGff() throws Exception
244   {
245     File f = new File("examples/uniref50.fa");
246     AlignmentI al = readAlignmentFile(f);
247     AlignFrame af = new AlignFrame(al, 500, 500);
248     Map<String, Object> colours = af.getFeatureRenderer()
249             .getFeatureColours();
250     String gffData = "##gff-version 2\n"
251             + "FER_CAPAA\tuniprot\tMETAL\t39\t39\t0.0\t.\t.\t"
252             + "Note=Iron-sulfur (2Fe-2S);Note=another note;evidence=ECO:0000255|PROSITE-ProRule:PRU00465\n"
253             + "FER1_SOLLC\tuniprot\tPfam\t55\t130\t3.0\t.\t.";
254     FeaturesFile featuresFile = new FeaturesFile(gffData,
255             FormatAdapter.PASTE);
256     assertTrue("Failed to parse features file",
257             featuresFile.parse(al.getDataset(), colours, true));
258
259     // verify feature on FER_CAPAA
260     SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
261             .getSequenceFeatures();
262     assertEquals(1, sfs.length);
263     SequenceFeature sf = sfs[0];
264     // description parsed from Note attribute
265     assertEquals("Iron-sulfur (2Fe-2S); another note", sf.description);
266     assertEquals(39, sf.begin);
267     assertEquals(39, sf.end);
268     assertEquals("uniprot", sf.featureGroup);
269     assertEquals("METAL", sf.type);
270     assertEquals(
271             "Note=Iron-sulfur (2Fe-2S);Note=another note;evidence=ECO:0000255|PROSITE-ProRule:PRU00465",
272             sf.getValue("ATTRIBUTES"));
273
274     // verify feature on FER1_SOLLC1
275     sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
276     assertEquals(1, sfs.length);
277     sf = sfs[0];
278     assertEquals("uniprot", sf.description);
279     assertEquals(55, sf.begin);
280     assertEquals(130, sf.end);
281     assertEquals("uniprot", sf.featureGroup);
282     assertEquals("Pfam", sf.type);
283     assertEquals(3f, sf.getScore(), 0.001f);
284   }
285
286   /**
287    * Test parsing a features file with Jalview format features (but no colour
288    * descriptors or startgroup to give the hint not to parse as GFF)
289    * 
290    * @throws Exception
291    */
292   @Test(groups = { "Functional" })
293   public void testParse_jalviewFeaturesOnly() throws Exception
294   {
295     File f = new File("examples/uniref50.fa");
296     AlignmentI al = readAlignmentFile(f);
297     AlignFrame af = new AlignFrame(al, 500, 500);
298     Map<String, Object> colours = af.getFeatureRenderer()
299             .getFeatureColours();
300
301     /*
302      * one feature on FER_CAPAA and one on sequence 3 (index 2) FER1_SOLLC
303      */
304     String featureData = "Iron-sulfur (2Fe-2S)\tFER_CAPAA\t-1\t39\t39\tMETAL\n"
305             + "Iron-phosphorus (2Fe-P)\tID_NOT_SPECIFIED\t2\t86\t87\tMETALLIC\n";
306     FeaturesFile featuresFile = new FeaturesFile(featureData,
307             FormatAdapter.PASTE);
308     assertTrue("Failed to parse features file",
309             featuresFile.parse(al.getDataset(), colours, true));
310
311     // verify FER_CAPAA feature
312     SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
313             .getSequenceFeatures();
314     assertEquals(1, sfs.length);
315     SequenceFeature sf = sfs[0];
316     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
317     assertEquals(39, sf.begin);
318     assertEquals(39, sf.end);
319     assertEquals("METAL", sf.type);
320
321     // verify FER1_SOLLC feature
322     sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
323     assertEquals(1, sfs.length);
324     sf = sfs[0];
325     assertEquals("Iron-phosphorus (2Fe-P)", sf.description);
326     assertEquals(86, sf.begin);
327     assertEquals(87, sf.end);
328     assertEquals("METALLIC", sf.type);
329   }
330
331   private void checkDatasetfromSimpleGff3(AlignmentI dataset)
332   {
333     assertEquals("no sequences extracted from GFF3 file", 2,
334             dataset.getHeight());
335   
336     SequenceI seq1 = dataset.findName("seq1"), seq2 = dataset
337             .findName("seq2");
338     assertNotNull(seq1);
339     assertNotNull(seq2);
340     assertFalse(
341             "Failed to replace dummy seq1 with real sequence",
342             seq1 instanceof SequenceDummy
343                     && ((SequenceDummy) seq1).isDummy());
344     assertFalse(
345             "Failed to replace dummy seq2 with real sequence",
346             seq2 instanceof SequenceDummy
347                     && ((SequenceDummy) seq2).isDummy());
348     String placeholderseq = new SequenceDummy("foo").getSequenceAsString();
349     assertFalse("dummy replacement buggy for seq1",
350             placeholderseq.equals(seq1.getSequenceAsString()));
351     assertFalse("dummy replacement buggy for seq2",
352             placeholderseq.equals(seq2.getSequenceAsString()));
353     assertNotNull("No features added to seq1", seq1.getSequenceFeatures());
354     assertEquals("Wrong number of features", 3,
355             seq1.getSequenceFeatures().length);
356     assertNull(seq2.getSequenceFeatures());
357     assertEquals(
358             "Wrong number of features",
359             0,
360             seq2.getSequenceFeatures() == null ? 0 : seq2
361                     .getSequenceFeatures().length);
362     assertTrue(
363             "Expected at least one CDNA/Protein mapping for seq1",
364             dataset.getCodonFrame(seq1) != null
365                     && dataset.getCodonFrame(seq1).size() > 0);
366   
367   }
368
369   @Test(groups = { "Functional" })
370   public void readGff3File() throws IOException
371   {
372     FeaturesFile gffreader = new FeaturesFile(true, simpleGffFile,
373             FormatAdapter.FILE);
374     Alignment dataset = new Alignment(gffreader.getSeqsAsArray());
375     gffreader.addProperties(dataset);
376     checkDatasetfromSimpleGff3(dataset);
377   }
378
379   @Test(groups = { "Functional" })
380   public void simpleGff3FileClass() throws IOException
381   {
382     AlignmentI dataset = new Alignment(new SequenceI[] {});
383     FeaturesFile ffile = new FeaturesFile(simpleGffFile,
384             FormatAdapter.FILE);
385   
386     boolean parseResult = ffile.parse(dataset, null, false, false);
387     assertTrue("return result should be true", parseResult);
388     checkDatasetfromSimpleGff3(dataset);
389   }
390
391   @Test(groups = { "Functional" })
392   public void simpleGff3FileIdentify()
393   {
394     assertEquals("Didn't recognise file correctly.",
395             IdentifyFile.FeaturesFile,
396             new IdentifyFile().identify(simpleGffFile, FormatAdapter.FILE));
397   }
398
399   @Test(groups = { "Functional" })
400   public void simpleGff3FileLoader() throws IOException
401   {
402     AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
403             simpleGffFile, FormatAdapter.FILE);
404     assertTrue(
405             "Didn't read the alignment into an alignframe from Gff3 File",
406             af != null);
407     // FIXME codon mappings are on the alignment but not on the dataset
408     checkDatasetfromSimpleGff3(af.getViewport().getAlignment()/* .getDataset() */);
409   }
410
411   @Test(groups = { "Functional" })
412   public void simpleGff3RelaxedIdMatching() throws IOException
413   {
414     AlignmentI dataset = new Alignment(new SequenceI[] {});
415     FeaturesFile ffile = new FeaturesFile(simpleGffFile,
416             FormatAdapter.FILE);
417   
418     boolean parseResult = ffile.parse(dataset, null, false, true);
419     assertTrue("return result (relaxedID matching) should be true",
420             parseResult);
421     checkDatasetfromSimpleGff3(dataset);
422   }
423
424   @Test(groups = { "Functional" })
425   public void testExonerateImport()
426   {
427     // exonerate does not tag sequences after features, so we have a more
428     // conventional annotation import test here
429   
430     FileLoader loader = new FileLoader(false);
431   
432     AlignFrame af = loader.LoadFileWaitTillLoaded(exonerateSeqs,
433             FormatAdapter.FILE);
434   
435     assertEquals("Unexpected number of DNA protein associations", 0, af
436             .getViewport().getAlignment().getCodonFrames().size());
437   
438     af.loadJalviewDataFile(exonerateOutput, FormatAdapter.FILE, null, null);
439   
440     assertTrue("Expected at least one DNA protein association", 0 != af
441             .getViewport().getAlignment().getDataset().getCodonFrames()
442             .size());
443   }
444 }