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