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