JAL-2292 squashed bug that breaks feature mapping for different cased residues
[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
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 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 Color(0x93b1d1));
71     assertEquals(colours.get("ASX-MOTIF").getColour(), new Color(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"
157             + "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").getColour(), 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 parsing a features file with GFF formatted content only
210    * 
211    * @throws Exception
212    */
213   @Test(groups = { "Functional" })
214   public void testParse_pureGff3() throws Exception
215   {
216     File f = new File("examples/uniref50.fa");
217     AlignmentI al = readAlignmentFile(f);
218     AlignFrame af = new AlignFrame(al, 500, 500);
219     Map<String, FeatureColourI> colours = af.getFeatureRenderer()
220             .getFeatureColours();
221     // GFF3 uses '=' separator for name/value pairs in colum 9
222     String gffData = "##gff-version 3\n"
223             + "FER_CAPAA\tuniprot\tMETAL\t39\t39\t0.0\t.\t.\t"
224             + "Note=Iron-sulfur (2Fe-2S);Note=another note;evidence=ECO:0000255|PROSITE-ProRule:PRU00465\n"
225             + "FER1_SOLLC\tuniprot\tPfam\t55\t130\t3.0\t.\t.\tID=$23";
226     FeaturesFile featuresFile = new FeaturesFile(gffData,
227             FormatAdapter.PASTE);
228     assertTrue("Failed to parse features file",
229             featuresFile.parse(al.getDataset(), colours, true));
230
231     // verify feature on FER_CAPAA
232     SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
233             .getSequenceFeatures();
234     assertEquals(1, sfs.length);
235     SequenceFeature sf = sfs[0];
236     // description parsed from Note attribute
237     assertEquals("Iron-sulfur (2Fe-2S),another note", sf.description);
238     assertEquals(39, sf.begin);
239     assertEquals(39, sf.end);
240     assertEquals("uniprot", sf.featureGroup);
241     assertEquals("METAL", sf.type);
242     assertEquals(
243             "Note=Iron-sulfur (2Fe-2S);Note=another note;evidence=ECO:0000255|PROSITE-ProRule:PRU00465",
244             sf.getValue("ATTRIBUTES"));
245
246     // verify feature on FER1_SOLLC1
247     sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
248     assertEquals(1, sfs.length);
249     sf = sfs[0];
250     // ID used for description if available
251     assertEquals("$23", sf.description);
252     assertEquals(55, sf.begin);
253     assertEquals(130, sf.end);
254     assertEquals("uniprot", sf.featureGroup);
255     assertEquals("Pfam", sf.type);
256     assertEquals(3f, sf.getScore(), 0.001f);
257   }
258
259   /**
260    * Test parsing a features file with Jalview format features (but no colour
261    * descriptors or startgroup to give the hint not to parse as GFF)
262    * 
263    * @throws Exception
264    */
265   @Test(groups = { "Functional" })
266   public void testParse_jalviewFeaturesOnly() throws Exception
267   {
268     File f = new File("examples/uniref50.fa");
269     AlignmentI al = readAlignmentFile(f);
270     AlignFrame af = new AlignFrame(al, 500, 500);
271     Map<String, FeatureColourI> colours = af.getFeatureRenderer()
272             .getFeatureColours();
273
274     /*
275      * one feature on FER_CAPAA and one on sequence 3 (index 2) FER1_SOLLC
276      */
277     String featureData = "Iron-sulfur (2Fe-2S)\tFER_CAPAA\t-1\t39\t39\tMETAL\n"
278             + "Iron-phosphorus (2Fe-P)\tID_NOT_SPECIFIED\t2\t86\t87\tMETALLIC\n";
279     FeaturesFile featuresFile = new FeaturesFile(featureData,
280             FormatAdapter.PASTE);
281     assertTrue("Failed to parse features file",
282             featuresFile.parse(al.getDataset(), colours, true));
283
284     // verify FER_CAPAA feature
285     SequenceFeature[] sfs = al.getSequenceAt(0).getDatasetSequence()
286             .getSequenceFeatures();
287     assertEquals(1, sfs.length);
288     SequenceFeature sf = sfs[0];
289     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
290     assertEquals(39, sf.begin);
291     assertEquals(39, sf.end);
292     assertEquals("METAL", sf.type);
293
294     // verify FER1_SOLLC feature
295     sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
296     assertEquals(1, sfs.length);
297     sf = sfs[0];
298     assertEquals("Iron-phosphorus (2Fe-P)", sf.description);
299     assertEquals(86, sf.begin);
300     assertEquals(87, sf.end);
301     assertEquals("METALLIC", sf.type);
302   }
303
304   private void checkDatasetfromSimpleGff3(AlignmentI dataset)
305   {
306     assertEquals("no sequences extracted from GFF3 file", 2,
307             dataset.getHeight());
308
309     SequenceI seq1 = dataset.findName("seq1");
310     SequenceI seq2 = dataset.findName("seq2");
311     assertNotNull(seq1);
312     assertNotNull(seq2);
313     assertFalse(
314             "Failed to replace dummy seq1 with real sequence",
315             seq1 instanceof SequenceDummy
316                     && ((SequenceDummy) seq1).isDummy());
317     assertFalse(
318             "Failed to replace dummy seq2 with real sequence",
319             seq2 instanceof SequenceDummy
320                     && ((SequenceDummy) seq2).isDummy());
321     String placeholderseq = new SequenceDummy("foo").getSequenceAsString();
322     assertFalse("dummy replacement buggy for seq1",
323             placeholderseq.equals(seq1.getSequenceAsString()));
324     assertFalse("dummy replacement buggy for seq2",
325             placeholderseq.equals(seq2.getSequenceAsString()));
326     assertNotNull("No features added to seq1", seq1.getSequenceFeatures());
327     assertEquals("Wrong number of features", 3,
328             seq1.getSequenceFeatures().length);
329     assertNull(seq2.getSequenceFeatures());
330     assertEquals(
331             "Wrong number of features",
332             0,
333             seq2.getSequenceFeatures() == null ? 0 : seq2
334                     .getSequenceFeatures().length);
335     assertTrue(
336             "Expected at least one CDNA/Protein mapping for seq1",
337             dataset.getCodonFrame(seq1) != null
338                     && dataset.getCodonFrame(seq1).size() > 0);
339
340   }
341
342   @Test(groups = { "Functional" })
343   public void readGff3File() throws IOException
344   {
345     FeaturesFile gffreader = new FeaturesFile(true, simpleGffFile,
346             FormatAdapter.FILE);
347     Alignment dataset = new Alignment(gffreader.getSeqsAsArray());
348     gffreader.addProperties(dataset);
349     checkDatasetfromSimpleGff3(dataset);
350   }
351
352   @Test(groups = { "Functional" })
353   public void simpleGff3FileClass() throws IOException
354   {
355     AlignmentI dataset = new Alignment(new SequenceI[] {});
356     FeaturesFile ffile = new FeaturesFile(simpleGffFile, FormatAdapter.FILE);
357
358     boolean parseResult = ffile.parse(dataset, null, false, false);
359     assertTrue("return result should be true", parseResult);
360     checkDatasetfromSimpleGff3(dataset);
361   }
362
363   @Test(groups = { "Functional" })
364   public void simpleGff3FileLoader() throws IOException
365   {
366     AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
367             simpleGffFile, FormatAdapter.FILE);
368     assertTrue(
369             "Didn't read the alignment into an alignframe from Gff3 File",
370             af != null);
371     checkDatasetfromSimpleGff3(af.getViewport().getAlignment());
372   }
373
374   @Test(groups = { "Functional" })
375   public void simpleGff3RelaxedIdMatching() throws IOException
376   {
377     AlignmentI dataset = new Alignment(new SequenceI[] {});
378     FeaturesFile ffile = new FeaturesFile(simpleGffFile, FormatAdapter.FILE);
379
380     boolean parseResult = ffile.parse(dataset, null, false, true);
381     assertTrue("return result (relaxedID matching) should be true",
382             parseResult);
383     checkDatasetfromSimpleGff3(dataset);
384   }
385
386   @Test(groups = { "Functional" })
387   public void testPrintJalviewFormat() throws Exception
388   {
389     File f = new File("examples/uniref50.fa");
390     AlignmentI al = readAlignmentFile(f);
391     AlignFrame af = new AlignFrame(al, 500, 500);
392     Map<String, FeatureColourI> colours = af.getFeatureRenderer()
393             .getFeatureColours();
394     String features = "METAL\tcc9900\n"
395             + "GAMMA-TURN\tred|0,255,255|20.0|95.0|below|66.0\n"
396             + "Pfam\tred\n"
397             + "STARTGROUP\tuniprot\n"
398             + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\n"
399             + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\n"
400             + "<html>Pfam domain<a href=\"http://pfam.xfam.org/family/PF00111\">Pfam_3_4</a></html>\tFER_CAPAA\t-1\t20\t20\tPfam\n"
401             + "ENDGROUP\tuniprot\n";
402     FeaturesFile featuresFile = new FeaturesFile(features,
403             FormatAdapter.PASTE);
404     featuresFile.parse(al.getDataset(), colours, false);
405
406     /*
407      * first with no features displayed
408      */
409     FeatureRenderer fr = af.alignPanel.getFeatureRenderer();
410     Map<String, FeatureColourI> visible = fr.getDisplayedFeatureCols();
411     String exported = featuresFile.printJalviewFormat(
412             al.getSequencesArray(), visible);
413     String expected = "No Features Visible";
414     assertEquals(expected, exported);
415
416     /*
417      * set METAL (in uniprot group) and GAMMA-TURN visible, but not Pfam
418      */
419     fr.setVisible("METAL");
420     fr.setVisible("GAMMA-TURN");
421     visible = fr.getDisplayedFeatureCols();
422     exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
423             visible);
424     expected = "METAL\tcc9900\n"
425             + "GAMMA-TURN\tff0000|00ffff|20.0|95.0|below|66.0\n"
426             + "\nSTARTGROUP\tuniprot\n"
427             + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
428             + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
429             + "ENDGROUP\tuniprot\n";
430     assertEquals(expected, exported);
431
432     /*
433      * now set Pfam visible
434      */
435     fr.setVisible("Pfam");
436     visible = fr.getDisplayedFeatureCols();
437     exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
438             visible);
439     /*
440      * note the order of feature types is uncontrolled - derives from
441      * FeaturesDisplayed.featuresDisplayed which is a HashSet
442      */
443     expected = "METAL\tcc9900\n"
444             + "Pfam\tff0000\n"
445             + "GAMMA-TURN\tff0000|00ffff|20.0|95.0|below|66.0\n"
446             + "\nSTARTGROUP\tuniprot\n"
447             + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
448             + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
449             + "<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"
450             + "ENDGROUP\tuniprot\n";
451     assertEquals(expected, exported);
452   }
453 }