+ File f = new File("examples/uniref50.fa");
+ AlignmentI al = readAlignmentFile(f);
+ AlignFrame af = new AlignFrame(al, 500, 500);
+ Map<String, FeatureColourI> colours = af.getFeatureRenderer()
+ .getFeatureColours();
+ String features = "METAL\tcc9900\n"
+ + "GAMMA-TURN\tred|0,255,255|20.0|95.0|below|66.0\n"
+ + "Pfam\tred\n" + "STARTGROUP\tuniprot\n"
+ + "Cath\tFER_CAPAA\t-1\t0\t0\tDomain\n" // non-positional feature
+ + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\n"
+ + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\n"
+ + "<html>Pfam domain<a href=\"http://pfam.xfam.org/family/PF00111\">Pfam_3_4</a></html>\tFER_CAPAA\t-1\t20\t20\tPfam\n"
+ + "ENDGROUP\tuniprot\n";
+ FeaturesFile featuresFile = new FeaturesFile(features,
+ DataSourceType.PASTE);
+ featuresFile.parse(al.getDataset(), colours, false);
+
+ /*
+ * add positional and non-positional features with null and
+ * empty feature group to check handled correctly
+ */
+ SequenceI seq = al.getSequenceAt(1); // FER_CAPAN
+ seq.addSequenceFeature(
+ new SequenceFeature("Pfam", "desc1", 0, 0, 1.3f, null));
+ seq.addSequenceFeature(
+ new SequenceFeature("Pfam", "desc2", 4, 9, Float.NaN, null));
+ seq = al.getSequenceAt(2); // FER1_SOLLC
+ seq.addSequenceFeature(
+ new SequenceFeature("Pfam", "desc3", 0, 0, Float.NaN, ""));
+ seq.addSequenceFeature(
+ new SequenceFeature("Pfam", "desc4", 5, 8, -2.6f, ""));
+
+ /*
+ * first with no features displayed, exclude non-positional features
+ */
+ FeatureRenderer fr = af.alignPanel.getFeatureRenderer();
+ String exported = featuresFile
+ .printJalviewFormat(al.getSequencesArray(), fr, false, false);
+ String expected = "No Features Visible";
+ assertEquals(expected, exported);
+
+ /*
+ * include non-positional features, but still no positional features
+ */
+ fr.setGroupVisibility("uniprot", true);
+ exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
+ true, false);
+ expected = "\nSTARTGROUP\tuniprot\n"
+ + "Cath\tFER_CAPAA\t-1\t0\t0\tDomain\t0.0\n"
+ + "ENDGROUP\tuniprot\n\n"
+ + "desc1\tFER_CAPAN\t-1\t0\t0\tPfam\t1.3\n\n"
+ + "desc3\tFER1_SOLLC\t-1\t0\t0\tPfam\n"; // NaN is not output
+ assertEquals(expected, exported);
+
+ /*
+ * set METAL (in uniprot group) and GAMMA-TURN visible, but not Pfam
+ */
+ fr.setVisible("METAL");
+ fr.setVisible("GAMMA-TURN");
+ exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
+ false, false);
+ expected = "METAL\tcc9900\n"
+ + "GAMMA-TURN\tscore|ff0000|00ffff|noValueMin|20.0|95.0|below|66.0\n"
+ + "\nSTARTGROUP\tuniprot\n"
+ + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
+ + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
+ + "ENDGROUP\tuniprot\n";
+ assertEquals(expected, exported);
+
+ /*
+ * now set Pfam visible
+ */
+ fr.setVisible("Pfam");
+ exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
+ false, false);
+ /*
+ * features are output within group, ordered by sequence and type
+ */
+ expected = "METAL\tcc9900\n" + "Pfam\tff0000\n"
+ + "GAMMA-TURN\tscore|ff0000|00ffff|noValueMin|20.0|95.0|below|66.0\n"
+ + "\nSTARTGROUP\tuniprot\n"
+ + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
+ + "<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"
+ + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
+ + "ENDGROUP\tuniprot\n"
+ // null / empty group features are output after named groups
+ + "\ndesc2\tFER_CAPAN\t-1\t4\t9\tPfam\n"
+ + "\ndesc4\tFER1_SOLLC\t-1\t5\t8\tPfam\t-2.6\n";
+ assertEquals(expected, exported);
+
+ /*
+ * hide uniprot group
+ */
+ fr.setGroupVisibility("uniprot", false);
+ expected = "METAL\tcc9900\n" + "Pfam\tff0000\n"
+ + "GAMMA-TURN\tscore|ff0000|00ffff|noValueMin|20.0|95.0|below|66.0\n"
+ + "\ndesc2\tFER_CAPAN\t-1\t4\t9\tPfam\n"
+ + "\ndesc4\tFER1_SOLLC\t-1\t5\t8\tPfam\t-2.6\n";
+ exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
+ false, false);
+ assertEquals(expected, exported);
+
+ /*
+ * include non-positional (overrides group not shown)
+ */
+ exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
+ true, false);
+ expected = "METAL\tcc9900\n" + "Pfam\tff0000\n"
+ + "GAMMA-TURN\tscore|ff0000|00ffff|noValueMin|20.0|95.0|below|66.0\n"
+ + "\nSTARTGROUP\tuniprot\n"
+ + "Cath\tFER_CAPAA\t-1\t0\t0\tDomain\t0.0\n"
+ + "ENDGROUP\tuniprot\n"
+ + "\ndesc1\tFER_CAPAN\t-1\t0\t0\tPfam\t1.3\n"
+ + "desc2\tFER_CAPAN\t-1\t4\t9\tPfam\n"
+ + "\ndesc3\tFER1_SOLLC\t-1\t0\t0\tPfam\n"
+ + "desc4\tFER1_SOLLC\t-1\t5\t8\tPfam\t-2.6\n";
+ assertEquals(expected, exported);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testPrintGffFormat() throws Exception
+ {
+ File f = new File("examples/uniref50.fa");
+ AlignmentI al = readAlignmentFile(f);
+ AlignFrame af = new AlignFrame(al, 500, 500);
+
+ /*
+ * no features
+ */
+ FeaturesFile featuresFile = new FeaturesFile();
+ FeatureRendererModel fr = (FeatureRendererModel) af.alignPanel
+ .getFeatureRenderer();
+ String exported = featuresFile.printGffFormat(al.getSequencesArray(),
+ fr, false, false);
+ String gffHeader = "##gff-version 2\n";
+ assertEquals(gffHeader, exported);
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr, true,
+ false);
+ assertEquals(gffHeader, exported);
+
+ /*
+ * add some features
+ */
+ al.getSequenceAt(0).addSequenceFeature(
+ new SequenceFeature("Domain", "Cath", 0, 0, 0f, "Uniprot"));
+ al.getSequenceAt(0).addSequenceFeature(
+ new SequenceFeature("METAL", "Cath", 39, 39, 1.2f, null));
+ al.getSequenceAt(1).addSequenceFeature(new SequenceFeature("GAMMA-TURN",
+ "Turn", 36, 38, 2.1f, "s3dm"));
+ SequenceFeature sf = new SequenceFeature("Pfam", "", 20, 20, 0f,
+ "Uniprot");
+ sf.setStrand("+");
+ sf.setPhase("2");
+ sf.setValue("x", "y");
+ sf.setValue("black", "white");
+ Map<String, String> csq = new HashMap<>();
+ csq.put("SIFT", "benign,mostly benign,cloudy, with meatballs");
+ csq.put("consequence", "missense_variant");
+ sf.setValue("CSQ", csq);
+ al.getSequenceAt(1).addSequenceFeature(sf);
+
+ /*
+ * 'discover' features then hide all feature types
+ */
+ fr.findAllFeatures(true);
+ FeatureSettingsBean[] data = new FeatureSettingsBean[4];
+ FeatureColourI fc = new FeatureColour(Color.PINK);
+ data[0] = new FeatureSettingsBean("Domain", fc, null, false);
+ data[1] = new FeatureSettingsBean("METAL", fc, null, false);
+ data[2] = new FeatureSettingsBean("GAMMA-TURN", fc, null, false);
+ data[3] = new FeatureSettingsBean("Pfam", fc, null, false);
+ fr.setFeaturePriority(data);
+
+ /*
+ * with no features displayed, exclude non-positional features
+ */
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ false, false);
+ assertEquals(gffHeader, exported);
+
+ /*
+ * include non-positional features
+ */
+ fr.setGroupVisibility("Uniprot", true);
+ fr.setGroupVisibility("s3dm", false);
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr, true,
+ false);
+ String expected = gffHeader
+ + "FER_CAPAA\tUniprot\tDomain\t0\t0\t0.0\t.\t.\n";
+ assertEquals(expected, exported);
+
+ /*
+ * set METAL (in uniprot group) and GAMMA-TURN visible, but not Pfam
+ * only Uniprot group visible here...
+ */
+ fr.setVisible("METAL");
+ fr.setVisible("GAMMA-TURN");
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ false, false);
+ // METAL feature has null group: description used for column 2
+ expected = gffHeader + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\n";
+ assertEquals(expected, exported);
+
+ /*
+ * set s3dm group visible
+ */
+ fr.setGroupVisibility("s3dm", true);
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ false, false);
+ // METAL feature has null group: description used for column 2
+ expected = gffHeader + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\n"
+ + "FER_CAPAN\ts3dm\tGAMMA-TURN\t36\t38\t2.1\t.\t.\n";
+ assertEquals(expected, exported);
+
+ /*
+ * now set Pfam visible
+ */
+ fr.setVisible("Pfam");
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ false, false);
+ // Pfam feature columns include strand(+), phase(2), attributes
+ expected = gffHeader + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\n"
+ // CSQ output as CSQ=att1=value1,att2=value2
+ // note all commas are encoded here which is wrong - it should be
+ // SIFT=benign,mostly benign,cloudy%2C with meatballs
+ + "FER_CAPAN\tUniprot\tPfam\t20\t20\t0.0\t+\t2\tx=y;black=white;"
+ + "CSQ=SIFT=benign%2Cmostly benign%2Ccloudy%2C with meatballs,consequence=missense_variant\n"
+ + "FER_CAPAN\ts3dm\tGAMMA-TURN\t36\t38\t2.1\t.\t.\n";
+ assertEquals(expected, exported);
+ }
+
+ /**
+ * Test for parsing of feature filters as represented in a Jalview features
+ * file
+ *
+ * @throws Exception
+ */
+ @Test(groups = { "Functional" })
+ public void testParseFilters() throws Exception
+ {
+ Map<String, FeatureMatcherSetI> filters = new HashMap<>();
+ String text = "sequence_variant\tCSQ:PolyPhen NotContains 'damaging'\n"
+ + "missense_variant\t(label contains foobar) and (Score lt 1.3)";
+ FeaturesFile featuresFile = new FeaturesFile(text,
+ DataSourceType.PASTE);
+ featuresFile.parseFilters(filters);
+ assertEquals(filters.size(), 2);
+
+ FeatureMatcherSetI fm = filters.get("sequence_variant");
+ assertNotNull(fm);
+ Iterator<FeatureMatcherI> matchers = fm.getMatchers().iterator();
+ FeatureMatcherI matcher = matchers.next();
+ assertFalse(matchers.hasNext());
+ String[] attributes = matcher.getAttribute();
+ assertArrayEquals(attributes, new String[] { "CSQ", "PolyPhen" });
+ assertSame(matcher.getMatcher().getCondition(), Condition.NotContains);
+ assertEquals(matcher.getMatcher().getPattern(), "damaging");
+
+ fm = filters.get("missense_variant");
+ assertNotNull(fm);
+ matchers = fm.getMatchers().iterator();
+ matcher = matchers.next();
+ assertTrue(matcher.isByLabel());
+ assertSame(matcher.getMatcher().getCondition(), Condition.Contains);
+ assertEquals(matcher.getMatcher().getPattern(), "foobar");
+ matcher = matchers.next();
+ assertTrue(matcher.isByScore());
+ assertSame(matcher.getMatcher().getCondition(), Condition.LT);
+ assertEquals(matcher.getMatcher().getPattern(), "1.3");
+ assertEquals(PA.getValue(matcher.getMatcher(), "floatValue"), 1.3f);
+
+ assertFalse(matchers.hasNext());
+ }
+
+ @Test(groups = { "Functional" })
+ public void testOutputFeatureFilters()
+ {
+ FeaturesFile ff = new FeaturesFile();
+ StringBuilder sb = new StringBuilder();
+ Map<String, FeatureColourI> visible = new HashMap<>();
+ visible.put("pfam", new FeatureColour(Color.red));
+ Map<String, FeatureMatcherSetI> featureFilters = new HashMap<>();
+
+ // with no filters, nothing is output
+ ff.outputFeatureFilters(sb, visible, featureFilters);
+ assertEquals("", sb.toString());
+
+ // with filter for not visible features only, nothing is output
+ FeatureMatcherSet filter = new FeatureMatcherSet();
+ filter.and(FeatureMatcher.byLabel(Condition.Present, null));
+ featureFilters.put("foobar", filter);
+ ff.outputFeatureFilters(sb, visible, featureFilters);
+ assertEquals("", sb.toString());
+
+ // with filters for visible feature types
+ FeatureMatcherSet filter2 = new FeatureMatcherSet();
+ filter2.and(FeatureMatcher.byAttribute(Condition.Present, null, "CSQ",
+ "PolyPhen"));
+ filter2.and(FeatureMatcher.byScore(Condition.LE, "-2.4"));
+ featureFilters.put("pfam", filter2);
+ visible.put("foobar", new FeatureColour(Color.blue));
+ ff.outputFeatureFilters(sb, visible, featureFilters);
+ String expected = "\nSTARTFILTERS\nfoobar\tLabel Present\npfam\t(CSQ:PolyPhen Present) AND (Score LE -2.4)\nENDFILTERS\n";
+ assertEquals(expected, sb.toString());
+ }
+
+ /**
+ * Output as GFF should not include features which are not visible due to
+ * colour threshold or feature filter settings
+ *
+ * @throws Exception
+ */
+ @Test(groups = { "Functional" })
+ public void testPrintGffFormat_withFilters() throws Exception
+ {
+ File f = new File("examples/uniref50.fa");
+ AlignmentI al = readAlignmentFile(f);
+ AlignFrame af = new AlignFrame(al, 500, 500);
+ SequenceFeature sf1 = new SequenceFeature("METAL", "Cath", 39, 39, 1.2f,
+ null);
+ sf1.setValue("clin_sig", "Likely Pathogenic");
+ sf1.setValue("AF", "24");
+ al.getSequenceAt(0).addSequenceFeature(sf1);
+ SequenceFeature sf2 = new SequenceFeature("METAL", "Cath", 41, 41, 0.6f,
+ null);
+ sf2.setValue("clin_sig", "Benign");
+ sf2.setValue("AF", "46");
+ al.getSequenceAt(0).addSequenceFeature(sf2);
+
+ FeaturesFile featuresFile = new FeaturesFile();
+ FeatureRenderer fr = af.alignPanel.getFeatureRenderer();
+ final String gffHeader = "##gff-version 2\n";
+
+ fr.setVisible("METAL");
+ fr.setColour("METAL", new FeatureColour(Color.PINK));
+ String exported = featuresFile.printGffFormat(al.getSequencesArray(),
+ fr, false, false);
+ String expected = gffHeader
+ + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\tclin_sig=Likely Pathogenic;AF=24\n"
+ + "FER_CAPAA\tCath\tMETAL\t41\t41\t0.6\t.\t.\tclin_sig=Benign;AF=46\n";
+ assertEquals(expected, exported);
+
+ /*
+ * now threshold to Score > 1.1 - should exclude sf2
+ */
+ FeatureColourI fc = new FeatureColour(null, Color.white, Color.BLACK,
+ Color.white, 0f, 2f);
+ fc.setAboveThreshold(true);
+ fc.setThreshold(1.1f);
+ fr.setColour("METAL", fc);
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ false, false);
+ expected = gffHeader
+ + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\tclin_sig=Likely Pathogenic;AF=24\n";
+ assertEquals(expected, exported);
+
+ /*
+ * remove threshold and check sf2 is exported
+ */
+ fc.setAboveThreshold(false);
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ false, false);
+ expected = gffHeader
+ + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\tclin_sig=Likely Pathogenic;AF=24\n"
+ + "FER_CAPAA\tCath\tMETAL\t41\t41\t0.6\t.\t.\tclin_sig=Benign;AF=46\n";
+ assertEquals(expected, exported);
+
+ /*
+ * filter on (clin_sig contains Benign) - should include sf2 and exclude sf1
+ */
+ FeatureMatcherSetI filter = new FeatureMatcherSet();
+ filter.and(FeatureMatcher.byAttribute(Condition.Contains, "benign",
+ "clin_sig"));
+ fr.setFeatureFilter("METAL", filter);
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ false, false);
+ expected = gffHeader
+ + "FER_CAPAA\tCath\tMETAL\t41\t41\t0.6\t.\t.\tclin_sig=Benign;AF=46\n";
+ assertEquals(expected, exported);
+ }
+
+ /**
+ * Output as Jalview should not include features which are not visible due to
+ * colour threshold or feature filter settings
+ *
+ * @throws Exception
+ */
+ @Test(groups = { "Functional" })
+ public void testPrintJalviewFormat_withFilters() throws Exception
+ {
+ File f = new File("examples/uniref50.fa");
+ AlignmentI al = readAlignmentFile(f);
+ AlignFrame af = new AlignFrame(al, 500, 500);
+ SequenceFeature sf1 = new SequenceFeature("METAL", "Cath", 39, 39, 1.2f,
+ "grp1");
+ sf1.setValue("clin_sig", "Likely Pathogenic");
+ sf1.setValue("AF", "24");
+ al.getSequenceAt(0).addSequenceFeature(sf1);
+ SequenceFeature sf2 = new SequenceFeature("METAL", "Cath", 41, 41, 0.6f,
+ "grp2");
+ sf2.setValue("clin_sig", "Benign");
+ sf2.setValue("AF", "46");
+ al.getSequenceAt(0).addSequenceFeature(sf2);
+
+ FeaturesFile featuresFile = new FeaturesFile();
+ FeatureRenderer fr = af.alignPanel.getFeatureRenderer();
+ fr.findAllFeatures(true);
+
+ fr.setVisible("METAL");
+ fr.setColour("METAL", new FeatureColour(Color.PINK));
+ String exported = featuresFile
+ .printJalviewFormat(al.getSequencesArray(), fr, false, false);
+ String expected = "METAL\tffafaf\n\nSTARTGROUP\tgrp1\n"
+ + "Cath\tFER_CAPAA\t-1\t39\t39\tMETAL\t1.2\n"
+ + "ENDGROUP\tgrp1\n\nSTARTGROUP\tgrp2\n"
+ + "Cath\tFER_CAPAA\t-1\t41\t41\tMETAL\t0.6\n"
+ + "ENDGROUP\tgrp2\n";
+ assertEquals(expected, exported);
+
+ /*
+ * now threshold to Score > 1.1 - should exclude sf2
+ * (and there should be no empty STARTGROUP/ENDGROUP output)
+ */
+ FeatureColourI fc = new FeatureColour(null, Color.white, Color.BLACK,
+ Color.white, 0f, 2f);
+ fc.setAboveThreshold(true);
+ fc.setThreshold(1.1f);
+ fr.setColour("METAL", fc);
+ exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
+ false, false);
+ expected = "METAL\tscore|ffffff|000000|noValueMin|abso|0.0|2.0|above|1.1\n\n"
+ + "STARTGROUP\tgrp1\n"
+ + "Cath\tFER_CAPAA\t-1\t39\t39\tMETAL\t1.2\n"
+ + "ENDGROUP\tgrp1\n";
+ assertEquals(expected, exported);
+
+ /*
+ * remove threshold and check sf2 is exported
+ */
+ fc.setAboveThreshold(false);
+ exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
+ false, false);
+ expected = "METAL\tscore|ffffff|000000|noValueMin|abso|0.0|2.0|none\n\n"
+ + "STARTGROUP\tgrp1\n"
+ + "Cath\tFER_CAPAA\t-1\t39\t39\tMETAL\t1.2\n"
+ + "ENDGROUP\tgrp1\n\nSTARTGROUP\tgrp2\n"
+ + "Cath\tFER_CAPAA\t-1\t41\t41\tMETAL\t0.6\n"
+ + "ENDGROUP\tgrp2\n";
+ assertEquals(expected, exported);
+
+ /*
+ * filter on (clin_sig contains Benign) - should include sf2 and exclude sf1
+ */
+ FeatureMatcherSetI filter = new FeatureMatcherSet();
+ filter.and(FeatureMatcher.byAttribute(Condition.Contains, "benign",
+ "clin_sig"));
+ fr.setFeatureFilter("METAL", filter);
+ exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
+ false, false);
+ expected = "FER_CAPAA\tCath\tMETAL\t41\t41\t0.6\t.\t.\n";
+ expected = "METAL\tscore|ffffff|000000|noValueMin|abso|0.0|2.0|none\n\n"
+ + "STARTFILTERS\nMETAL\tclin_sig Contains benign\nENDFILTERS\n\n"
+ + "STARTGROUP\tgrp2\n"
+ + "Cath\tFER_CAPAA\t-1\t41\t41\tMETAL\t0.6\n"
+ + "ENDGROUP\tgrp2\n";
+ assertEquals(expected, exported);