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