JAL-2079 update examples to new pfam.xfam.org domain
[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(7, sfs.length);
80     SequenceFeature sf = sfs[0];
81     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
82     assertEquals(39, sf.begin);
83     assertEquals(39, sf.end);
84     assertEquals("uniprot", sf.featureGroup);
85     assertEquals("METAL", sf.type);
86     sf = sfs[1];
87     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
88     assertEquals(44, sf.begin);
89     assertEquals(44, sf.end);
90     assertEquals("uniprot", sf.featureGroup);
91     assertEquals("METAL", sf.type);
92     sf = sfs[2];
93     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
94     assertEquals(47, sf.begin);
95     assertEquals(47, sf.end);
96     assertEquals("uniprot", sf.featureGroup);
97     assertEquals("METAL", sf.type);
98     sf = sfs[3];
99     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
100     assertEquals(77, sf.begin);
101     assertEquals(77, sf.end);
102     assertEquals("uniprot", sf.featureGroup);
103     assertEquals("METAL", sf.type);
104     sf = sfs[4];
105     assertEquals("Fer2 Status: True Positive Pfam 8_8%LINK%",
106             sf.description);
107     assertEquals("Pfam 8_8|http://pfam.xfam.org/family/PF00111",
108             sf.links.get(0).toString());
109     assertEquals(8, sf.begin);
110     assertEquals(83, sf.end);
111     assertEquals("uniprot", sf.featureGroup);
112     assertEquals("Pfam", sf.type);
113     sf = sfs[5];
114     assertEquals("Ferredoxin_fold Status: True Positive ", sf.description);
115     assertEquals(3, sf.begin);
116     assertEquals(93, sf.end);
117     assertEquals("uniprot", sf.featureGroup);
118     assertEquals("Cath", sf.type);
119     sf = sfs[6];
120     assertEquals(
121             "High confidence server. Only hits with scores over 0.8 are reported. PHOSPHORYLATION (T) 89_8%LINK%",
122             sf.description);
123     assertEquals(
124             "PHOSPHORYLATION (T) 89_8|http://www.cbs.dtu.dk/cgi-bin/proview/webface-link?seqid=P83527&amp;service=NetPhos-2.0",
125             sf.links.get(0).toString());
126     assertEquals(89, sf.begin);
127     assertEquals(89, sf.end);
128     assertEquals("netphos", sf.featureGroup);
129     assertEquals("PHOSPHORYLATION (T)", sf.type);
130   }
131
132   /**
133    * Test parsing a features file with a mix of Jalview and GFF formatted
134    * content
135    * 
136    * @throws Exception
137    */
138   @Test(groups = { "Functional" })
139   public void testParse_mixedJalviewGff() throws Exception
140   {
141     File f = new File("examples/uniref50.fa");
142     AlignmentI al = readAlignmentFile(f);
143     AlignFrame af = new AlignFrame(al, 500, 500);
144     Map<String, Object> colours = af.getFeatureRenderer()
145             .getFeatureColours();
146     // GFF2 uses space as name/value separator in column 9
147     String gffData = "METAL\tcc9900\n" + "GFF\n"
148             + "FER_CAPAA\tuniprot\tMETAL\t44\t45\t4.0\t.\t.\tNote Iron-sulfur; Note 2Fe-2S\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("Iron-sulfur,2Fe-2S", 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_pureGff3() 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     // GFF3 uses '=' separator for name/value pairs in colum 9
251     String gffData = "##gff-version 3\n"
252             + "FER_CAPAA\tuniprot\tMETAL\t39\t39\t0.0\t.\t.\t"
253             + "Note=Iron-sulfur (2Fe-2S);Note=another note;evidence=ECO:0000255|PROSITE-ProRule:PRU00465\n"
254             + "FER1_SOLLC\tuniprot\tPfam\t55\t130\t3.0\t.\t.\tID=$23";
255     FeaturesFile featuresFile = new FeaturesFile(gffData,
256             FormatAdapter.PASTE);
257     assertTrue("Failed to parse features file",
258             featuresFile.parse(al.getDataset(), colours, true));
259
260     // verify feature on FER_CAPAA
261     SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
262             .getSequenceFeatures();
263     assertEquals(1, sfs.length);
264     SequenceFeature sf = sfs[0];
265     // description parsed from Note attribute
266     assertEquals("Iron-sulfur (2Fe-2S),another note", sf.description);
267     assertEquals(39, sf.begin);
268     assertEquals(39, sf.end);
269     assertEquals("uniprot", sf.featureGroup);
270     assertEquals("METAL", sf.type);
271     assertEquals(
272             "Note=Iron-sulfur (2Fe-2S);Note=another note;evidence=ECO:0000255|PROSITE-ProRule:PRU00465",
273             sf.getValue("ATTRIBUTES"));
274
275     // verify feature on FER1_SOLLC1
276     sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
277     assertEquals(1, sfs.length);
278     sf = sfs[0];
279     // ID used for description if available
280     assertEquals("$23", sf.description);
281     assertEquals(55, sf.begin);
282     assertEquals(130, sf.end);
283     assertEquals("uniprot", sf.featureGroup);
284     assertEquals("Pfam", sf.type);
285     assertEquals(3f, sf.getScore(), 0.001f);
286   }
287
288   /**
289    * Test parsing a features file with Jalview format features (but no colour
290    * descriptors or startgroup to give the hint not to parse as GFF)
291    * 
292    * @throws Exception
293    */
294   @Test(groups = { "Functional" })
295   public void testParse_jalviewFeaturesOnly() throws Exception
296   {
297     File f = new File("examples/uniref50.fa");
298     AlignmentI al = readAlignmentFile(f);
299     AlignFrame af = new AlignFrame(al, 500, 500);
300     Map<String, Object> colours = af.getFeatureRenderer()
301             .getFeatureColours();
302
303     /*
304      * one feature on FER_CAPAA and one on sequence 3 (index 2) FER1_SOLLC
305      */
306     String featureData = "Iron-sulfur (2Fe-2S)\tFER_CAPAA\t-1\t39\t39\tMETAL\n"
307             + "Iron-phosphorus (2Fe-P)\tID_NOT_SPECIFIED\t2\t86\t87\tMETALLIC\n";
308     FeaturesFile featuresFile = new FeaturesFile(featureData,
309             FormatAdapter.PASTE);
310     assertTrue("Failed to parse features file",
311             featuresFile.parse(al.getDataset(), colours, true));
312
313     // verify FER_CAPAA feature
314     SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
315             .getSequenceFeatures();
316     assertEquals(1, sfs.length);
317     SequenceFeature sf = sfs[0];
318     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
319     assertEquals(39, sf.begin);
320     assertEquals(39, sf.end);
321     assertEquals("METAL", sf.type);
322
323     // verify FER1_SOLLC feature
324     sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
325     assertEquals(1, sfs.length);
326     sf = sfs[0];
327     assertEquals("Iron-phosphorus (2Fe-P)", sf.description);
328     assertEquals(86, sf.begin);
329     assertEquals(87, sf.end);
330     assertEquals("METALLIC", sf.type);
331   }
332
333   private void checkDatasetfromSimpleGff3(AlignmentI dataset)
334   {
335     assertEquals("no sequences extracted from GFF3 file", 2,
336             dataset.getHeight());
337   
338     SequenceI seq1 = dataset.findName("seq1");
339     SequenceI seq2 = dataset.findName("seq2");
340     assertNotNull(seq1);
341     assertNotNull(seq2);
342     assertFalse(
343             "Failed to replace dummy seq1 with real sequence",
344             seq1 instanceof SequenceDummy
345                     && ((SequenceDummy) seq1).isDummy());
346     assertFalse(
347             "Failed to replace dummy seq2 with real sequence",
348             seq2 instanceof SequenceDummy
349                     && ((SequenceDummy) seq2).isDummy());
350     String placeholderseq = new SequenceDummy("foo").getSequenceAsString();
351     assertFalse("dummy replacement buggy for seq1",
352             placeholderseq.equals(seq1.getSequenceAsString()));
353     assertFalse("dummy replacement buggy for seq2",
354             placeholderseq.equals(seq2.getSequenceAsString()));
355     assertNotNull("No features added to seq1", seq1.getSequenceFeatures());
356     assertEquals("Wrong number of features", 3,
357             seq1.getSequenceFeatures().length);
358     assertNull(seq2.getSequenceFeatures());
359     assertEquals(
360             "Wrong number of features",
361             0,
362             seq2.getSequenceFeatures() == null ? 0 : seq2
363                     .getSequenceFeatures().length);
364     assertTrue(
365             "Expected at least one CDNA/Protein mapping for seq1",
366             dataset.getCodonFrame(seq1) != null
367                     && dataset.getCodonFrame(seq1).size() > 0);
368   
369   }
370
371   @Test(groups = { "Functional" })
372   public void readGff3File() throws IOException
373   {
374     FeaturesFile gffreader = new FeaturesFile(true, simpleGffFile,
375             FormatAdapter.FILE);
376     Alignment dataset = new Alignment(gffreader.getSeqsAsArray());
377     gffreader.addProperties(dataset);
378     checkDatasetfromSimpleGff3(dataset);
379   }
380
381   @Test(groups = { "Functional" })
382   public void simpleGff3FileClass() throws IOException
383   {
384     AlignmentI dataset = new Alignment(new SequenceI[] {});
385     FeaturesFile ffile = new FeaturesFile(simpleGffFile,
386             FormatAdapter.FILE);
387   
388     boolean parseResult = ffile.parse(dataset, null, false, false);
389     assertTrue("return result should be true", parseResult);
390     checkDatasetfromSimpleGff3(dataset);
391   }
392
393   @Test(groups = { "Functional" })
394   public void simpleGff3FileLoader() throws IOException
395   {
396     AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
397             simpleGffFile, FormatAdapter.FILE);
398     assertTrue(
399             "Didn't read the alignment into an alignframe from Gff3 File",
400             af != null);
401     checkDatasetfromSimpleGff3(af.getViewport().getAlignment());
402   }
403
404   @Test(groups = { "Functional" })
405   public void simpleGff3RelaxedIdMatching() throws IOException
406   {
407     AlignmentI dataset = new Alignment(new SequenceI[] {});
408     FeaturesFile ffile = new FeaturesFile(simpleGffFile,
409             FormatAdapter.FILE);
410   
411     boolean parseResult = ffile.parse(dataset, null, false, true);
412     assertTrue("return result (relaxedID matching) should be true",
413             parseResult);
414     checkDatasetfromSimpleGff3(dataset);
415   }
416
417   @Test(groups = { "Functional" })
418   public void testPrintJalviewFormat() throws Exception
419   {
420     File f = new File("examples/uniref50.fa");
421     AlignmentI al = readAlignmentFile(f);
422     AlignFrame af = new AlignFrame(al, 500, 500);
423     Map<String, Object> colours = af.getFeatureRenderer()
424             .getFeatureColours();
425     String features = "METAL\tcc9900\n"
426             + "GAMMA-TURN\tred|0,255,255|20.0|95.0|below|66.0\n"
427             + "Pfam\tred\n"
428             + "STARTGROUP\tuniprot\n"
429             + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\n"
430             + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\n"
431             + "<html>Pfam domain<a href=\"http://pfam.xfam.org/family/PF00111\">Pfam_3_4</a></html>\tFER_CAPAA\t-1\t20\t20\tPfam\n"
432             + "ENDGROUP\tuniprot\n";
433     FeaturesFile featuresFile = new FeaturesFile(features,
434             FormatAdapter.PASTE);
435     featuresFile.parse(al.getDataset(), colours, false);
436
437     /*
438      * first with no features displayed
439      */
440     FeatureRenderer fr = af.alignPanel.getFeatureRenderer();
441     Map<String, Object> visible = fr
442             .getDisplayedFeatureCols();
443     String exported = featuresFile.printJalviewFormat(
444             al.getSequencesArray(), visible);
445     String expected = "No Features Visible";
446     assertEquals(expected, exported);
447
448     /*
449      * set METAL (in uniprot group) and GAMMA-TURN visible, but not Pfam
450      */
451     fr.setVisible("METAL");
452     fr.setVisible("GAMMA-TURN");
453     visible = fr.getDisplayedFeatureCols();
454     exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
455             visible);
456     expected = "METAL\tcc9900\n"
457             + "GAMMA-TURN\tff0000|00ffff|20.0|95.0|below|66.0\n"
458             + "\nSTARTGROUP\tuniprot\n"
459             + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
460             + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
461             + "ENDGROUP\tuniprot\n";
462     assertEquals(expected, exported);
463
464     /*
465      * now set Pfam visible
466      */
467     fr.setVisible("Pfam");
468     visible = fr.getDisplayedFeatureCols();
469     exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
470             visible);
471     /*
472      * note the order of feature types is uncontrolled - derives from
473      * FeaturesDisplayed.featuresDisplayed which is a HashSet
474      */
475     expected = "METAL\tcc9900\n"
476             + "Pfam\tff0000\n"
477             + "GAMMA-TURN\tff0000|00ffff|20.0|95.0|below|66.0\n"
478             + "\nSTARTGROUP\tuniprot\n"
479             + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
480             + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
481             + "<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"
482             + "ENDGROUP\tuniprot\n";
483     assertEquals(expected, exported);
484   }
485 }