Merge branch 'develop' into feature/r2_11_2/JAL-3808_gff2_exonerate
[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.assertSame;
27 import static org.testng.AssertJUnit.assertTrue;
28 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
29
30 import java.awt.Color;
31 import java.io.File;
32 import java.io.IOException;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Map;
37
38 import org.testng.annotations.AfterClass;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Test;
41
42 import jalview.api.FeatureColourI;
43 import jalview.api.FeatureRenderer;
44 import jalview.datamodel.Alignment;
45 import jalview.datamodel.AlignmentI;
46 import jalview.datamodel.SequenceDummy;
47 import jalview.datamodel.SequenceFeature;
48 import jalview.datamodel.SequenceI;
49 import jalview.datamodel.features.FeatureMatcher;
50 import jalview.datamodel.features.FeatureMatcherI;
51 import jalview.datamodel.features.FeatureMatcherSet;
52 import jalview.datamodel.features.FeatureMatcherSetI;
53 import jalview.datamodel.features.SequenceFeatures;
54 import jalview.gui.AlignFrame;
55 import jalview.gui.Desktop;
56 import jalview.gui.JvOptionPane;
57 import jalview.schemes.FeatureColour;
58 import jalview.structure.StructureSelectionManager;
59 import jalview.util.matcher.Condition;
60 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
61 import jalview.viewmodel.seqfeatures.FeatureRendererModel.FeatureSettingsBean;
62 import junit.extensions.PA;
63
64 public class FeaturesFileTest
65 {
66   private static String simpleGffFile = "examples/testdata/simpleGff3.gff";
67
68   @AfterClass(alwaysRun = true)
69   public void tearDownAfterClass()
70   {
71     /*
72      * remove any sequence mappings created so they don't pollute other tests
73      */
74     StructureSelectionManager ssm = StructureSelectionManager
75             .getStructureSelectionManager(Desktop.instance);
76     ssm.resetAll();
77   }
78
79   @BeforeClass(alwaysRun = true)
80   public void setUpJvOptionPane()
81   {
82     JvOptionPane.setInteractiveMode(false);
83     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
84   }
85
86   @Test(groups = { "Functional" })
87   public void testParse() throws Exception
88   {
89     File f = new File("examples/uniref50.fa");
90     AlignmentI al = readAlignmentFile(f);
91     AlignFrame af = new AlignFrame(al, 500, 500);
92     Map<String, FeatureColourI> colours = af.getFeatureRenderer()
93             .getFeatureColours();
94     FeaturesFile featuresFile = new FeaturesFile(
95             "examples/exampleFeatures.txt", DataSourceType.FILE);
96     assertTrue("Test " + "Features file test"
97             + "\nFailed to parse features file.",
98             featuresFile.parse(al.getDataset(), colours, true));
99
100     /*
101      * Refetch the colour map from the FeatureRenderer (to confirm it has been
102      * updated - JAL-1904), and verify (some) feature group colours
103      */
104     colours = af.getFeatureRenderer().getFeatureColours();
105     assertEquals("27 feature group colours not found", 27, colours.size());
106     assertEquals(colours.get("Cath").getColour(), new Color(0x93b1d1));
107     assertEquals(colours.get("ASX-MOTIF").getColour(), new Color(0x6addbb));
108     FeatureColourI kdColour = colours.get("kdHydrophobicity");
109     assertTrue(kdColour.isGraduatedColour());
110     assertTrue(kdColour.isAboveThreshold());
111     assertEquals(-2f, kdColour.getThreshold());
112
113     /*
114      * verify (some) features on sequences
115      */
116     List<SequenceFeature> sfs = al.getSequenceAt(0).getDatasetSequence()
117             .getSequenceFeatures(); // FER_CAPAA
118     SequenceFeatures.sortFeatures(sfs, true);
119     assertEquals(8, sfs.size());
120
121     /*
122      * verify (in ascending start position order)
123      */
124     SequenceFeature sf = sfs.get(0);
125     assertEquals("Pfam family%LINK%", sf.description);
126     assertEquals(0, sf.begin);
127     assertEquals(0, sf.end);
128     assertEquals("uniprot", sf.featureGroup);
129     assertEquals("Pfam", sf.type);
130     assertEquals(1, sf.links.size());
131     assertEquals("Pfam family|http://pfam.xfam.org/family/PF00111",
132             sf.links.get(0));
133
134     sf = sfs.get(1);
135     assertEquals("Ferredoxin_fold Status: True Positive ", sf.description);
136     assertEquals(3, sf.begin);
137     assertEquals(93, sf.end);
138     assertEquals("uniprot", sf.featureGroup);
139     assertEquals("Cath", sf.type);
140
141     sf = sfs.get(2);
142     assertEquals("Fer2 Status: True Positive Pfam 8_8%LINK%",
143             sf.description);
144     assertEquals("Pfam 8_8|http://pfam.xfam.org/family/PF00111",
145             sf.links.get(0));
146     assertEquals(8, sf.begin);
147     assertEquals(83, sf.end);
148     assertEquals("uniprot", sf.featureGroup);
149     assertEquals("Pfam", sf.type);
150
151     sf = sfs.get(3);
152     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
153     assertEquals(39, sf.begin);
154     assertEquals(39, sf.end);
155     assertEquals("uniprot", sf.featureGroup);
156     assertEquals("METAL", sf.type);
157
158     sf = sfs.get(4);
159     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
160     assertEquals(44, sf.begin);
161     assertEquals(44, sf.end);
162     assertEquals("uniprot", sf.featureGroup);
163     assertEquals("METAL", sf.type);
164
165     sf = sfs.get(5);
166     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
167     assertEquals(47, sf.begin);
168     assertEquals(47, sf.end);
169     assertEquals("uniprot", sf.featureGroup);
170     assertEquals("METAL", sf.type);
171
172     sf = sfs.get(6);
173     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
174     assertEquals(77, sf.begin);
175     assertEquals(77, sf.end);
176     assertEquals("uniprot", sf.featureGroup);
177     assertEquals("METAL", sf.type);
178
179     sf = sfs.get(7);
180     assertEquals(
181             "High confidence server. Only hits with scores over 0.8 are reported. PHOSPHORYLATION (T) 89_8%LINK%",
182             sf.description);
183     assertEquals(
184             "PHOSPHORYLATION (T) 89_8|http://www.cbs.dtu.dk/cgi-bin/proview/webface-link?seqid=P83527&amp;service=NetPhos-2.0",
185             sf.links.get(0));
186     assertEquals(89, sf.begin);
187     assertEquals(89, sf.end);
188     assertEquals("netphos", sf.featureGroup);
189     assertEquals("PHOSPHORYLATION (T)", sf.type);
190   }
191
192   /**
193    * Test parsing a features file with a mix of Jalview and GFF formatted
194    * content
195    * 
196    * @throws Exception
197    */
198   @Test(groups = { "Functional" })
199   public void testParse_mixedJalviewGff() throws Exception
200   {
201     File f = new File("examples/uniref50.fa");
202     AlignmentI al = readAlignmentFile(f);
203     AlignFrame af = new AlignFrame(al, 500, 500);
204     Map<String, FeatureColourI> colours = af.getFeatureRenderer()
205             .getFeatureColours();
206     // GFF2 uses space as name/value separator in column 9
207     String gffData = "METAL\tcc9900\n"
208             + "GFF\n"
209             + "FER_CAPAA\tuniprot\tMETAL\t44\t45\t4.0\t.\t.\tNote Iron-sulfur; Note 2Fe-2S\n"
210             + "FER1_SOLLC\tuniprot\tPfam\t55\t130\t2.0\t.\t.";
211     FeaturesFile featuresFile = new FeaturesFile(gffData,
212             DataSourceType.PASTE);
213     assertTrue("Failed to parse features file",
214             featuresFile.parse(al.getDataset(), colours, true));
215
216     // verify colours read or synthesized
217     colours = af.getFeatureRenderer().getFeatureColours();
218     assertEquals("1 feature group colours not found", 1, colours.size());
219     assertEquals(colours.get("METAL").getColour(), new Color(0xcc9900));
220
221     // verify feature on FER_CAPAA
222     List<SequenceFeature> sfs = al.getSequenceAt(0).getDatasetSequence()
223             .getSequenceFeatures();
224     assertEquals(1, sfs.size());
225     SequenceFeature sf = sfs.get(0);
226     assertEquals("Iron-sulfur,2Fe-2S", sf.description);
227     assertEquals(44, sf.begin);
228     assertEquals(45, sf.end);
229     assertEquals("uniprot", sf.featureGroup);
230     assertEquals("METAL", sf.type);
231     assertEquals(4f, sf.getScore(), 0.001f);
232
233     // verify feature on FER1_SOLLC
234     sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
235     assertEquals(1, sfs.size());
236     sf = sfs.get(0);
237     assertEquals("uniprot", sf.description);
238     assertEquals(55, sf.begin);
239     assertEquals(130, sf.end);
240     assertEquals("uniprot", sf.featureGroup);
241     assertEquals("Pfam", sf.type);
242     assertEquals(2f, sf.getScore(), 0.001f);
243   }
244
245   @Test(groups = { "Functional" })
246   public void testImportGFF2ExonerateCDSAndCoding2Genome()
247           throws IOException
248   {
249     /*
250      * test assumes sequence 1 in imported alignment is a 
251      * transcript shorter and aligned to exons on locus (sequence 0)
252      * 
253      * exonerate script was - where mode was query or target
254      * exonerate --showvulgar false --showalignment false --show${mode}gff ... > test_${mode}.gff2
255      * echo '##FASTA' >> test_${mode}.gff2
256      * cat example_Locus.fa example_CDS.fa >> test_${mode}.gff2  
257      * [ then edit out stuff before gff-version-2 header and the end of exonerate lines after the gff dump ]
258      */
259     String[][] testFiles = new String[][] {
260         { "test_cdna2genome_showquerygff.gff2",
261             "test_cdna2genome_showtargetgff.gff2" },
262         { "test_coding2genome_showquerygff.gff2",
263             "test_coding2genome_showtargetgff.gff2" } };
264
265     for (String[] testfilepair : testFiles)
266     {
267       FormatAdapter fa = new FormatAdapter();
268       AlignmentI al = fa.readFile("examples/testdata/" + testfilepair[0],
269               DataSourceType.FILE, FileFormat.Features);
270       
271       assertEquals(2, al.getHeight());
272       // check there are gaps in sequence 1
273       assertTrue(al.getSequenceAt(1).getSequenceAsString().contains(""+al.getGapCharacter()));
274       assertTrue(al.isAligned());
275       
276       AlignmentI al2 = fa.readFile("examples/testdata/" + testfilepair[1],
277               DataSourceType.FILE, FileFormat.Features);
278       
279       assertEquals(2, al2.getHeight());
280       assertTrue(al2.isAligned());
281       // check sequence 1 is identical for alignment imported from both query and target gff
282       assertEquals(al.getSequenceAt(1).getSequenceAsString(),
283               al2.getSequenceAt(1).getSequenceAsString());
284     }
285   }
286   
287   public static AlignmentI readAlignmentFile(File f) throws IOException
288   {
289     System.out.println("Reading file: " + f);
290     String ff = f.getPath();
291     FormatAdapter rf = new FormatAdapter();
292
293     AlignmentI al = rf.readFile(ff, DataSourceType.FILE,
294             new IdentifyFile().identify(ff, DataSourceType.FILE));
295
296     al.setDataset(null); // creates dataset sequences
297     assertNotNull("Couldn't read supplied alignment data.", al);
298     return al;
299   }
300
301   /**
302    * Test parsing a features file with GFF formatted content only
303    * 
304    * @throws Exception
305    */
306   @Test(groups = { "Functional" })
307   public void testParse_pureGff3() throws Exception
308   {
309     File f = new File("examples/uniref50.fa");
310     AlignmentI al = readAlignmentFile(f);
311     AlignFrame af = new AlignFrame(al, 500, 500);
312     Map<String, FeatureColourI> colours = af.getFeatureRenderer()
313             .getFeatureColours();
314     // GFF3 uses '=' separator for name/value pairs in column 9
315     // comma (%2C) equals (%3D) or semi-colon (%3B) should be url-escaped in values
316     String gffData = "##gff-version 3\n"
317             + "FER_CAPAA\tuniprot\tMETAL\t39\t39\t0.0\t.\t.\t"
318             + "Note=Iron-sulfur (2Fe-2S);Note=another note,and another;evidence=ECO%3B0000255%2CPROSITE%3DProRule:PRU00465;"
319             + "CSQ=AF=21,POLYPHEN=benign,possibly_damaging,clin_sig=Benign%3Dgood\n"
320             + "FER1_SOLLC\tuniprot\tPfam\t55\t130\t3.0\t.\t.\tID=$23";
321     FeaturesFile featuresFile = new FeaturesFile(gffData,
322             DataSourceType.PASTE);
323     assertTrue("Failed to parse features file",
324             featuresFile.parse(al.getDataset(), colours, true));
325
326     // verify feature on FER_CAPAA
327     List<SequenceFeature> sfs = al.getSequenceAt(0).getDatasetSequence()
328             .getSequenceFeatures();
329     assertEquals(1, sfs.size());
330     SequenceFeature sf = sfs.get(0);
331     // description parsed from Note attribute
332     assertEquals("Iron-sulfur (2Fe-2S),another note,and another",
333             sf.description);
334     assertEquals(39, sf.begin);
335     assertEquals(39, sf.end);
336     assertEquals("uniprot", sf.featureGroup);
337     assertEquals("METAL", sf.type);
338     assertEquals(5, sf.otherDetails.size());
339     assertEquals("ECO;0000255,PROSITE=ProRule:PRU00465", // url decoded
340             sf.getValue("evidence"));
341     assertEquals("Iron-sulfur (2Fe-2S),another note,and another",
342             sf.getValue("Note"));
343     assertEquals("21", sf.getValueAsString("CSQ", "AF"));
344     assertEquals("benign,possibly_damaging",
345             sf.getValueAsString("CSQ", "POLYPHEN"));
346     assertEquals("Benign=good", sf.getValueAsString("CSQ", "clin_sig")); // url decoded
347     // todo change STRAND and !Phase into fields of SequenceFeature instead
348     assertEquals(".", sf.otherDetails.get("STRAND"));
349     assertEquals(0, sf.getStrand());
350     assertEquals(".", sf.getPhase());
351
352     // verify feature on FER1_SOLLC1
353     sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
354     assertEquals(1, sfs.size());
355     sf = sfs.get(0);
356     // ID used for description if available
357     assertEquals("$23", sf.description);
358     assertEquals(55, sf.begin);
359     assertEquals(130, sf.end);
360     assertEquals("uniprot", sf.featureGroup);
361     assertEquals("Pfam", sf.type);
362     assertEquals(3f, sf.getScore(), 0.001f);
363   }
364
365   /**
366    * Test parsing a features file with Jalview format features (but no colour
367    * descriptors or startgroup to give the hint not to parse as GFF)
368    * 
369    * @throws Exception
370    */
371   @Test(groups = { "Functional" })
372   public void testParse_jalviewFeaturesOnly() throws Exception
373   {
374     File f = new File("examples/uniref50.fa");
375     AlignmentI al = readAlignmentFile(f);
376     AlignFrame af = new AlignFrame(al, 500, 500);
377     Map<String, FeatureColourI> colours = af.getFeatureRenderer()
378             .getFeatureColours();
379
380     /*
381      * one feature on FER_CAPAA and one on sequence 3 (index 2) FER1_SOLLC
382      */
383     String featureData = "Iron-sulfur (2Fe-2S)\tFER_CAPAA\t-1\t39\t39\tMETAL\n"
384             + "Iron-phosphorus (2Fe-P)\tID_NOT_SPECIFIED\t2\t86\t87\tMETALLIC\n";
385     FeaturesFile featuresFile = new FeaturesFile(featureData,
386             DataSourceType.PASTE);
387     assertTrue("Failed to parse features file",
388             featuresFile.parse(al.getDataset(), colours, true));
389
390     // verify FER_CAPAA feature
391     List<SequenceFeature> sfs = al.getSequenceAt(0).getDatasetSequence()
392             .getSequenceFeatures();
393     assertEquals(1, sfs.size());
394     SequenceFeature sf = sfs.get(0);
395     assertEquals("Iron-sulfur (2Fe-2S)", sf.description);
396     assertEquals(39, sf.begin);
397     assertEquals(39, sf.end);
398     assertEquals("METAL", sf.type);
399
400     // verify FER1_SOLLC feature
401     sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
402     assertEquals(1, sfs.size());
403     sf = sfs.get(0);
404     assertEquals("Iron-phosphorus (2Fe-P)", sf.description);
405     assertEquals(86, sf.begin);
406     assertEquals(87, sf.end);
407     assertEquals("METALLIC", sf.type);
408   }
409
410   private void checkDatasetfromSimpleGff3(AlignmentI dataset)
411   {
412     assertEquals("no sequences extracted from GFF3 file", 2,
413             dataset.getHeight());
414
415     SequenceI seq1 = dataset.findName("seq1");
416     SequenceI seq2 = dataset.findName("seq2");
417     assertNotNull(seq1);
418     assertNotNull(seq2);
419     assertFalse(
420             "Failed to replace dummy seq1 with real sequence",
421             seq1 instanceof SequenceDummy
422                     && ((SequenceDummy) seq1).isDummy());
423     assertFalse(
424             "Failed to replace dummy seq2 with real sequence",
425             seq2 instanceof SequenceDummy
426                     && ((SequenceDummy) seq2).isDummy());
427     String placeholderseq = new SequenceDummy("foo").getSequenceAsString();
428     assertFalse("dummy replacement buggy for seq1",
429             placeholderseq.equals(seq1.getSequenceAsString()));
430     assertFalse("dummy replacement buggy for seq2",
431             placeholderseq.equals(seq2.getSequenceAsString()));
432     assertNotNull("No features added to seq1", seq1.getSequenceFeatures());
433     assertEquals("Wrong number of features", 3, seq1.getSequenceFeatures()
434             .size());
435     assertTrue(seq2.getSequenceFeatures().isEmpty());
436     assertEquals(
437             "Wrong number of features",
438             0,
439             seq2.getSequenceFeatures() == null ? 0 : seq2
440                     .getSequenceFeatures().size());
441     assertTrue(
442             "Expected at least one CDNA/Protein mapping for seq1",
443             dataset.getCodonFrame(seq1) != null
444                     && dataset.getCodonFrame(seq1).size() > 0);
445
446   }
447
448   @Test(groups = { "Functional" })
449   public void readGff3File() throws IOException
450   {
451     FeaturesFile gffreader = new FeaturesFile(true, simpleGffFile,
452             DataSourceType.FILE);
453     Alignment dataset = new Alignment(gffreader.getSeqsAsArray());
454     gffreader.addProperties(dataset);
455     checkDatasetfromSimpleGff3(dataset);
456   }
457
458   @Test(groups = { "Functional" })
459   public void simpleGff3FileClass() throws IOException
460   {
461     AlignmentI dataset = new Alignment(new SequenceI[] {});
462     FeaturesFile ffile = new FeaturesFile(simpleGffFile,
463             DataSourceType.FILE);
464   
465     boolean parseResult = ffile.parse(dataset, null, false, false);
466     assertTrue("return result should be true", parseResult);
467     checkDatasetfromSimpleGff3(dataset);
468   }
469
470   @Test(groups = { "Functional" })
471   public void simpleGff3FileLoader() throws IOException
472   {
473     AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
474             simpleGffFile, DataSourceType.FILE);
475     assertTrue(
476             "Didn't read the alignment into an alignframe from Gff3 File",
477             af != null);
478     checkDatasetfromSimpleGff3(af.getViewport().getAlignment());
479   }
480
481   @Test(groups = { "Functional" })
482   public void simpleGff3RelaxedIdMatching() throws IOException
483   {
484     AlignmentI dataset = new Alignment(new SequenceI[] {});
485     FeaturesFile ffile = new FeaturesFile(simpleGffFile,
486             DataSourceType.FILE);
487   
488     boolean parseResult = ffile.parse(dataset, null, false, true);
489     assertTrue("return result (relaxedID matching) should be true",
490             parseResult);
491     checkDatasetfromSimpleGff3(dataset);
492   }
493
494   @Test(groups = { "Functional" })
495   public void testPrintJalviewFormat() throws Exception
496   {
497     File f = new File("examples/uniref50.fa");
498     AlignmentI al = readAlignmentFile(f);
499     AlignFrame af = new AlignFrame(al, 500, 500);
500     Map<String, FeatureColourI> colours = af.getFeatureRenderer()
501             .getFeatureColours();
502     String features = "METAL\tcc9900\n"
503             + "GAMMA-TURN\tred|0,255,255|20.0|95.0|below|66.0\n"
504             + "Pfam\tred\n"
505             + "STARTGROUP\tuniprot\n"
506             + "Cath\tFER_CAPAA\t-1\t0\t0\tDomain\n" // non-positional feature
507             + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\n"
508             + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\n"
509             + "<html>Pfam domain<a href=\"http://pfam.xfam.org/family/PF00111\">Pfam_3_4</a></html>\tFER_CAPAA\t-1\t20\t20\tPfam\n"
510             + "ENDGROUP\tuniprot\n";
511     FeaturesFile featuresFile = new FeaturesFile(features,
512             DataSourceType.PASTE);
513     featuresFile.parse(al.getDataset(), colours, false);
514
515     /*
516      * add positional and non-positional features with null and
517      * empty feature group to check handled correctly
518      */
519     SequenceI seq = al.getSequenceAt(1); // FER_CAPAN
520     seq.addSequenceFeature(new SequenceFeature("Pfam", "desc1", 0, 0, 1.3f,
521             null));
522     seq.addSequenceFeature(new SequenceFeature("Pfam", "desc2", 4, 9,
523             Float.NaN, null));
524     seq = al.getSequenceAt(2); // FER1_SOLLC
525     seq.addSequenceFeature(new SequenceFeature("Pfam", "desc3", 0, 0,
526             Float.NaN, ""));
527     seq.addSequenceFeature(new SequenceFeature("Pfam", "desc4", 5, 8,
528             -2.6f, ""));
529
530     /*
531      * first with no features displayed, exclude non-positional features
532      */
533     FeatureRenderer fr = af.alignPanel.getFeatureRenderer();
534     String exported = featuresFile
535             .printJalviewFormat(al.getSequencesArray(), fr, false, false);
536     String expected = "No Features Visible";
537     assertEquals(expected, exported);
538
539     /*
540      * include non-positional features, but still no positional features
541      */
542     fr.setGroupVisibility("uniprot", true);
543     exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
544             true, false);
545     expected = "\nSTARTGROUP\tuniprot\n"
546             + "Cath\tFER_CAPAA\t-1\t0\t0\tDomain\t0.0\n"
547             + "ENDGROUP\tuniprot\n\n"
548             + "desc1\tFER_CAPAN\t-1\t0\t0\tPfam\t1.3\n\n"
549             + "desc3\tFER1_SOLLC\t-1\t0\t0\tPfam\n"; // NaN is not output
550     assertEquals(expected, exported);
551
552     /*
553      * set METAL (in uniprot group) and GAMMA-TURN visible, but not Pfam
554      */
555     fr.setVisible("METAL");
556     fr.setVisible("GAMMA-TURN");
557     exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
558             false, false);
559     expected = "METAL\tcc9900\n"
560             + "GAMMA-TURN\tscore|ff0000|00ffff|noValueMin|20.0|95.0|below|66.0\n"
561             + "\nSTARTGROUP\tuniprot\n"
562             + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
563             + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
564             + "ENDGROUP\tuniprot\n";
565     assertEquals(expected, exported);
566
567     /*
568      * now set Pfam visible
569      */
570     fr.setVisible("Pfam");
571     exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
572             false, false);
573     /*
574      * features are output within group, ordered by sequence and type
575      */
576     expected = "METAL\tcc9900\n"
577             + "Pfam\tff0000\n"
578             + "GAMMA-TURN\tscore|ff0000|00ffff|noValueMin|20.0|95.0|below|66.0\n"
579             + "\nSTARTGROUP\tuniprot\n"
580             + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
581             + "<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"
582             + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
583             + "ENDGROUP\tuniprot\n"
584             // null / empty group features are output after named groups
585             + "\ndesc2\tFER_CAPAN\t-1\t4\t9\tPfam\n"
586             + "\ndesc4\tFER1_SOLLC\t-1\t5\t8\tPfam\t-2.6\n";
587     assertEquals(expected, exported);
588
589     /*
590      * hide uniprot group
591      */
592     fr.setGroupVisibility("uniprot", false);
593     expected = "METAL\tcc9900\n" + "Pfam\tff0000\n"
594             + "GAMMA-TURN\tscore|ff0000|00ffff|noValueMin|20.0|95.0|below|66.0\n"
595             + "\ndesc2\tFER_CAPAN\t-1\t4\t9\tPfam\n"
596             + "\ndesc4\tFER1_SOLLC\t-1\t5\t8\tPfam\t-2.6\n";
597     exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
598             false, false);
599     assertEquals(expected, exported);
600
601     /*
602      * include non-positional (overrides group not shown)
603      */
604     exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
605             true, false);
606     expected = "METAL\tcc9900\n" + "Pfam\tff0000\n"
607             + "GAMMA-TURN\tscore|ff0000|00ffff|noValueMin|20.0|95.0|below|66.0\n"
608             + "\nSTARTGROUP\tuniprot\n"
609             + "Cath\tFER_CAPAA\t-1\t0\t0\tDomain\t0.0\n"
610             + "ENDGROUP\tuniprot\n"
611             + "\ndesc1\tFER_CAPAN\t-1\t0\t0\tPfam\t1.3\n"
612             + "desc2\tFER_CAPAN\t-1\t4\t9\tPfam\n"
613             + "\ndesc3\tFER1_SOLLC\t-1\t0\t0\tPfam\n"
614             + "desc4\tFER1_SOLLC\t-1\t5\t8\tPfam\t-2.6\n";
615     assertEquals(expected, exported);
616   }
617
618   @Test(groups = { "Functional" })
619   public void testPrintGffFormat() throws Exception
620   {
621     File f = new File("examples/uniref50.fa");
622     AlignmentI al = readAlignmentFile(f);
623     AlignFrame af = new AlignFrame(al, 500, 500);
624
625     /*
626      * no features
627      */
628     FeaturesFile featuresFile = new FeaturesFile();
629     FeatureRendererModel fr = (FeatureRendererModel) af.alignPanel
630             .getFeatureRenderer();
631     String exported = featuresFile.printGffFormat(al.getSequencesArray(),
632             fr, false, false);
633     String gffHeader = "##gff-version 2\n";
634     assertEquals(gffHeader, exported);
635     exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
636             true, false);
637     assertEquals(gffHeader, exported);
638
639     /*
640      * add some features
641      */
642     al.getSequenceAt(0).addSequenceFeature(
643             new SequenceFeature("Domain", "Cath", 0, 0, 0f, "Uniprot"));
644     al.getSequenceAt(0).addSequenceFeature(
645             new SequenceFeature("METAL", "Cath", 39, 39, 1.2f, null));
646     al.getSequenceAt(1)
647             .addSequenceFeature(
648                     new SequenceFeature("GAMMA-TURN", "Turn", 36, 38, 2.1f,
649                             "s3dm"));
650     SequenceFeature sf = new SequenceFeature("Pfam", "", 20, 20, 0f,
651             "Uniprot");
652     sf.setStrand("+");
653     sf.setPhase("2");
654     sf.setValue("x", "y");
655     sf.setValue("black", "white");
656     Map<String, String> csq = new HashMap<>();
657     csq.put("SIFT", "benign,mostly benign,cloudy, with meatballs");
658     csq.put("consequence", "missense_variant");
659     sf.setValue("CSQ", csq);
660     al.getSequenceAt(1).addSequenceFeature(sf);
661
662     /*
663      * 'discover' features then hide all feature types
664      */
665     fr.findAllFeatures(true);
666     FeatureSettingsBean[] data = new FeatureSettingsBean[4];
667     FeatureColourI fc = new FeatureColour(Color.PINK);
668     data[0] = new FeatureSettingsBean("Domain", fc, null, false);
669     data[1] = new FeatureSettingsBean("METAL", fc, null, false);
670     data[2] = new FeatureSettingsBean("GAMMA-TURN", fc, null, false);
671     data[3] = new FeatureSettingsBean("Pfam", fc, null, false);
672     fr.setFeaturePriority(data);
673
674     /*
675      * with no features displayed, exclude non-positional features
676      */
677     exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
678             false, false);
679     assertEquals(gffHeader, exported);
680
681     /*
682      * include non-positional features
683      */
684     fr.setGroupVisibility("Uniprot", true);
685     fr.setGroupVisibility("s3dm", false);
686     exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
687             true, false);
688     String expected = gffHeader
689             + "FER_CAPAA\tUniprot\tDomain\t0\t0\t0.0\t.\t.\n";
690     assertEquals(expected, exported);
691
692     /*
693      * set METAL (in uniprot group) and GAMMA-TURN visible, but not Pfam
694      * only Uniprot group visible here...
695      */
696     fr.setVisible("METAL");
697     fr.setVisible("GAMMA-TURN");
698     exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
699             false, false);
700     // METAL feature has null group: description used for column 2
701     expected = gffHeader + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\n";
702     assertEquals(expected, exported);
703
704     /*
705      * set s3dm group visible
706      */
707     fr.setGroupVisibility("s3dm", true);
708     exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
709             false, false);
710     // METAL feature has null group: description used for column 2
711     expected = gffHeader + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\n"
712             + "FER_CAPAN\ts3dm\tGAMMA-TURN\t36\t38\t2.1\t.\t.\n";
713     assertEquals(expected, exported);
714
715     /*
716      * now set Pfam visible
717      */
718     fr.setVisible("Pfam");
719     exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
720             false, false);
721     // Pfam feature columns include strand(+), phase(2), attributes
722     expected = gffHeader
723             + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\n"
724             // CSQ output as CSQ=att1=value1,att2=value2
725             // note all commas are encoded here which is wrong - it should be
726             // SIFT=benign,mostly benign,cloudy%2C with meatballs
727             + "FER_CAPAN\tUniprot\tPfam\t20\t20\t0.0\t+\t2\tx=y;black=white;"
728             + "CSQ=SIFT=benign%2Cmostly benign%2Ccloudy%2C with meatballs,consequence=missense_variant\n"
729             + "FER_CAPAN\ts3dm\tGAMMA-TURN\t36\t38\t2.1\t.\t.\n";
730     assertEquals(expected, exported);
731   }
732
733   /**
734    * Test for parsing of feature filters as represented in a Jalview features
735    * file
736    * 
737    * @throws Exception
738    */
739   @Test(groups = { "Functional" })
740   public void testParseFilters() throws Exception
741   {
742     Map<String, FeatureMatcherSetI> filters = new HashMap<>();
743     String text = "sequence_variant\tCSQ:PolyPhen NotContains 'damaging'\n"
744             + "missense_variant\t(label contains foobar) and (Score lt 1.3)";
745     FeaturesFile featuresFile = new FeaturesFile(text,
746             DataSourceType.PASTE);
747     featuresFile.parseFilters(filters);
748     assertEquals(filters.size(), 2);
749
750     FeatureMatcherSetI fm = filters.get("sequence_variant");
751     assertNotNull(fm);
752     Iterator<FeatureMatcherI> matchers = fm.getMatchers().iterator();
753     FeatureMatcherI matcher = matchers.next();
754     assertFalse(matchers.hasNext());
755     String[] attributes = matcher.getAttribute();
756     assertArrayEquals(attributes, new String[] { "CSQ", "PolyPhen" });
757     assertSame(matcher.getMatcher().getCondition(), Condition.NotContains);
758     assertEquals(matcher.getMatcher().getPattern(), "damaging");
759
760     fm = filters.get("missense_variant");
761     assertNotNull(fm);
762     matchers = fm.getMatchers().iterator();
763     matcher = matchers.next();
764     assertTrue(matcher.isByLabel());
765     assertSame(matcher.getMatcher().getCondition(), Condition.Contains);
766     assertEquals(matcher.getMatcher().getPattern(), "foobar");
767     matcher = matchers.next();
768     assertTrue(matcher.isByScore());
769     assertSame(matcher.getMatcher().getCondition(), Condition.LT);
770     assertEquals(matcher.getMatcher().getPattern(), "1.3");
771     assertEquals(PA.getValue(matcher.getMatcher(), "floatValue"), 1.3f);
772
773     assertFalse(matchers.hasNext());
774   }
775
776   @Test(groups = { "Functional" })
777   public void testOutputFeatureFilters()
778   {
779     FeaturesFile ff = new FeaturesFile();
780     StringBuilder sb = new StringBuilder();
781     Map<String, FeatureColourI> visible = new HashMap<>();
782     visible.put("pfam", new FeatureColour(Color.red));
783     Map<String, FeatureMatcherSetI> featureFilters = new HashMap<>();
784
785     // with no filters, nothing is output
786     ff.outputFeatureFilters(sb, visible, featureFilters);
787     assertEquals("", sb.toString());
788
789     // with filter for not visible features only, nothing is output
790     FeatureMatcherSet filter = new FeatureMatcherSet();
791     filter.and(FeatureMatcher.byLabel(Condition.Present, null));
792     featureFilters.put("foobar", filter);
793     ff.outputFeatureFilters(sb, visible, featureFilters);
794     assertEquals("", sb.toString());
795
796     // with filters for visible feature types
797     FeatureMatcherSet filter2 = new FeatureMatcherSet();
798     filter2.and(FeatureMatcher.byAttribute(Condition.Present, null, "CSQ",
799             "PolyPhen"));
800     filter2.and(FeatureMatcher.byScore(Condition.LE, "-2.4"));
801     featureFilters.put("pfam", filter2);
802     visible.put("foobar", new FeatureColour(Color.blue));
803     ff.outputFeatureFilters(sb, visible, featureFilters);
804     String expected = "\nSTARTFILTERS\nfoobar\tLabel Present\npfam\t(CSQ:PolyPhen Present) AND (Score LE -2.4)\nENDFILTERS\n";
805     assertEquals(expected, sb.toString());
806   }
807
808   /**
809    * Output as GFF should not include features which are not visible due to
810    * colour threshold or feature filter settings
811    * 
812    * @throws Exception
813    */
814   @Test(groups = { "Functional" })
815   public void testPrintGffFormat_withFilters() throws Exception
816   {
817     File f = new File("examples/uniref50.fa");
818     AlignmentI al = readAlignmentFile(f);
819     AlignFrame af = new AlignFrame(al, 500, 500);
820     SequenceFeature sf1 = new SequenceFeature("METAL", "Cath", 39, 39, 1.2f,
821             null);
822     sf1.setValue("clin_sig", "Likely Pathogenic");
823     sf1.setValue("AF", "24");
824     al.getSequenceAt(0).addSequenceFeature(sf1);
825     SequenceFeature sf2 = new SequenceFeature("METAL", "Cath", 41, 41, 0.6f,
826             null);
827     sf2.setValue("clin_sig", "Benign");
828     sf2.setValue("AF", "46");
829     al.getSequenceAt(0).addSequenceFeature(sf2);
830   
831     FeaturesFile featuresFile = new FeaturesFile();
832     FeatureRenderer fr = af.alignPanel.getFeatureRenderer();
833     final String gffHeader = "##gff-version 2\n";
834
835     fr.setVisible("METAL");
836     fr.setColour("METAL", new FeatureColour(Color.PINK));
837     String exported = featuresFile.printGffFormat(al.getSequencesArray(),
838             fr, false, false);
839     String expected = gffHeader
840             + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\tclin_sig=Likely Pathogenic;AF=24\n"
841             + "FER_CAPAA\tCath\tMETAL\t41\t41\t0.6\t.\t.\tclin_sig=Benign;AF=46\n";
842     assertEquals(expected, exported);
843
844     /*
845      * now threshold to Score > 1.1 - should exclude sf2
846      */
847     FeatureColourI fc = new FeatureColour(null, Color.white, Color.BLACK,
848             Color.white, 0f, 2f);
849     fc.setAboveThreshold(true);
850     fc.setThreshold(1.1f);
851     fr.setColour("METAL", fc);
852     exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
853             false, false);
854     expected = gffHeader
855             + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\tclin_sig=Likely Pathogenic;AF=24\n";
856     assertEquals(expected, exported);
857
858     /*
859      * remove threshold and check sf2 is exported
860      */
861     fc.setAboveThreshold(false);
862     exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
863             false, false);
864     expected = gffHeader
865             + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\tclin_sig=Likely Pathogenic;AF=24\n"
866             + "FER_CAPAA\tCath\tMETAL\t41\t41\t0.6\t.\t.\tclin_sig=Benign;AF=46\n";
867     assertEquals(expected, exported);
868
869     /*
870      * filter on (clin_sig contains Benign) - should include sf2 and exclude sf1
871      */
872     FeatureMatcherSetI filter = new FeatureMatcherSet();
873     filter.and(FeatureMatcher.byAttribute(Condition.Contains, "benign",
874             "clin_sig"));
875     fr.setFeatureFilter("METAL", filter);
876     exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
877             false, false);
878     expected = gffHeader
879             + "FER_CAPAA\tCath\tMETAL\t41\t41\t0.6\t.\t.\tclin_sig=Benign;AF=46\n";
880     assertEquals(expected, exported);
881   }
882
883   /**
884    * Output as Jalview should not include features which are not visible due to
885    * colour threshold or feature filter settings
886    * 
887    * @throws Exception
888    */
889   @Test(groups = { "Functional" })
890   public void testPrintJalviewFormat_withFilters() throws Exception
891   {
892     File f = new File("examples/uniref50.fa");
893     AlignmentI al = readAlignmentFile(f);
894     AlignFrame af = new AlignFrame(al, 500, 500);
895     SequenceFeature sf1 = new SequenceFeature("METAL", "Cath", 39, 39, 1.2f,
896             "grp1");
897     sf1.setValue("clin_sig", "Likely Pathogenic");
898     sf1.setValue("AF", "24");
899     al.getSequenceAt(0).addSequenceFeature(sf1);
900     SequenceFeature sf2 = new SequenceFeature("METAL", "Cath", 41, 41, 0.6f,
901             "grp2");
902     sf2.setValue("clin_sig", "Benign");
903     sf2.setValue("AF", "46");
904     al.getSequenceAt(0).addSequenceFeature(sf2);
905   
906     FeaturesFile featuresFile = new FeaturesFile();
907     FeatureRenderer fr = af.alignPanel.getFeatureRenderer();
908     fr.findAllFeatures(true);
909   
910     fr.setVisible("METAL");
911     fr.setColour("METAL", new FeatureColour(Color.PINK));
912     String exported = featuresFile.printJalviewFormat(
913             al.getSequencesArray(),
914             fr, false, false);
915     String expected = "METAL\tffafaf\n\nSTARTGROUP\tgrp1\n"
916             + "Cath\tFER_CAPAA\t-1\t39\t39\tMETAL\t1.2\n"
917             + "ENDGROUP\tgrp1\n\nSTARTGROUP\tgrp2\n"
918             + "Cath\tFER_CAPAA\t-1\t41\t41\tMETAL\t0.6\n"
919             + "ENDGROUP\tgrp2\n";
920     assertEquals(expected, exported);
921   
922     /*
923      * now threshold to Score > 1.1 - should exclude sf2
924      * (and there should be no empty STARTGROUP/ENDGROUP output)
925      */
926     FeatureColourI fc = new FeatureColour(null, Color.white, Color.BLACK,
927             Color.white, 0f, 2f);
928     fc.setAboveThreshold(true);
929     fc.setThreshold(1.1f);
930     fr.setColour("METAL", fc);
931     exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
932             false, false);
933     expected = "METAL\tscore|ffffff|000000|noValueMin|abso|0.0|2.0|above|1.1\n\n"
934             + "STARTGROUP\tgrp1\n"
935             + "Cath\tFER_CAPAA\t-1\t39\t39\tMETAL\t1.2\n"
936             + "ENDGROUP\tgrp1\n";
937     assertEquals(expected, exported);
938   
939     /*
940      * remove threshold and check sf2 is exported
941      */
942     fc.setAboveThreshold(false);
943     exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
944             false, false);
945     expected = "METAL\tscore|ffffff|000000|noValueMin|abso|0.0|2.0|none\n\n"
946             + "STARTGROUP\tgrp1\n"
947             + "Cath\tFER_CAPAA\t-1\t39\t39\tMETAL\t1.2\n"
948             + "ENDGROUP\tgrp1\n\nSTARTGROUP\tgrp2\n"
949             + "Cath\tFER_CAPAA\t-1\t41\t41\tMETAL\t0.6\n"
950             + "ENDGROUP\tgrp2\n";
951     assertEquals(expected, exported);
952   
953     /*
954      * filter on (clin_sig contains Benign) - should include sf2 and exclude sf1
955      */
956     FeatureMatcherSetI filter = new FeatureMatcherSet();
957     filter.and(FeatureMatcher.byAttribute(Condition.Contains, "benign",
958             "clin_sig"));
959     fr.setFeatureFilter("METAL", filter);
960     exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
961             false, false);
962     expected = "FER_CAPAA\tCath\tMETAL\t41\t41\t0.6\t.\t.\n";
963     expected = "METAL\tscore|ffffff|000000|noValueMin|abso|0.0|2.0|none\n\n"
964             + "STARTFILTERS\nMETAL\tclin_sig Contains benign\nENDFILTERS\n\n"
965             + "STARTGROUP\tgrp2\n"
966             + "Cath\tFER_CAPAA\t-1\t41\t41\tMETAL\t0.6\n"
967             + "ENDGROUP\tgrp2\n";
968     assertEquals(expected, exported);
969   }
970 }