import jalview.analysis.SequenceIdMatcher;
import jalview.api.AlignViewportI;
import jalview.api.FeatureColourI;
+import jalview.api.FeatureRenderer;
import jalview.api.FeaturesSourceI;
import jalview.datamodel.AlignedCodonFrame;
import jalview.datamodel.Alignment;
}
/**
- * Returns contents of a Jalview format features file, for visible features, as
- * filtered by type and group. Features with a null group are displayed if their
- * feature type is visible. Non-positional features may optionally be included
- * (with no check on type or group).
+ * Returns contents of a Jalview format features file, for visible features,
+ * as filtered by type and group. Features with a null group are displayed if
+ * their feature type is visible. Non-positional features may optionally be
+ * included (with no check on type or group).
*
* @param sequences
- * source of features
- * @param visible
- * map of colour for each visible feature type
- * @param featureFilters
- * @param visibleFeatureGroups
+ * @param fr
* @param includeNonPositional
* if true, include non-positional features (regardless of group or
* type)
* @return
*/
public String printJalviewFormat(SequenceI[] sequences,
- Map<String, FeatureColourI> visible,
- Map<String, FeatureMatcherSetI> featureFilters,
- List<String> visibleFeatureGroups, boolean includeNonPositional)
+ FeatureRenderer fr, boolean includeNonPositional)
{
- if (!includeNonPositional && (visible == null || visible.isEmpty()))
+ Map<String, FeatureColourI> visibleColours = fr
+ .getDisplayedFeatureCols();
+ Map<String, FeatureMatcherSetI> featureFilters = fr.getFeatureFilters();
+ List<String> visibleFeatureGroups = fr.getDisplayedFeatureGroups();
+
+ if (!includeNonPositional
+ && (visibleColours == null || visibleColours.isEmpty()))
{
// no point continuing.
return "No Features Visible";
*/
// TODO: decide if feature links should also be written here ?
StringBuilder out = new StringBuilder(256);
- if (visible != null)
+ if (visibleColours != null)
{
- for (Entry<String, FeatureColourI> featureColour : visible.entrySet())
+ for (Entry<String, FeatureColourI> featureColour : visibleColours
+ .entrySet())
{
FeatureColourI colour = featureColour.getValue();
out.append(colour.toJalviewFormat(featureColour.getKey())).append(
}
}
- String[] types = visible == null ? new String[0] : visible.keySet()
- .toArray(new String[visible.keySet().size()]);
+ String[] types = visibleColours == null ? new String[0]
+ : visibleColours.keySet()
+ .toArray(new String[visibleColours.keySet().size()]);
/*
* feature filters if any
*/
- outputFeatureFilters(out, visible, featureFilters);
+ outputFeatureFilters(out, visibleColours, featureFilters);
/*
* sort groups alphabetically, and ensure that features with a
/*
* positional features within groups
*/
- foundSome |= outputFeaturesByGroup(out, sortedGroups, types, sequences);
+ foundSome |= outputFeaturesByGroup(out, fr, sortedGroups, types,
+ sequences);
return foundSome ? out.toString() : "No Features Visible";
}
}
if (!first)
{
- out.append(ENDFILTERS).append(newline).append(newline);
+ out.append(ENDFILTERS).append(newline);
}
}
/**
- * Appends output of sequence features within feature groups to the output
- * buffer. Groups other than the null or empty group are sandwiched by
- * STARTGROUP and ENDGROUP lines.
+ * Appends output of visible sequence features within feature groups to the
+ * output buffer. Groups other than the null or empty group are sandwiched by
+ * STARTGROUP and ENDGROUP lines. Answers true if at least one feature was
+ * written, else false.
*
* @param out
+ * @param fr
* @param groups
* @param featureTypes
* @param sequences
* @return
*/
private boolean outputFeaturesByGroup(StringBuilder out,
- List<String> groups, String[] featureTypes, SequenceI[] sequences)
+ FeatureRenderer fr, List<String> groups, String[] featureTypes,
+ SequenceI[] sequences)
{
boolean foundSome = false;
for (String group : groups)
{
+ boolean firstInGroup = true;
boolean isNamedGroup = (group != null && !"".equals(group));
- if (isNamedGroup)
- {
- out.append(newline);
- out.append(STARTGROUP).append(TAB);
- out.append(group);
- out.append(newline);
- }
/*
* output positional features within groups
for (SequenceFeature sequenceFeature : features)
{
- foundSome = true;
- out.append(formatJalviewFeature(sequenceName, sequenceFeature));
+ if (fr.isVisible(sequenceFeature))
+ {
+ foundSome = true;
+ if (firstInGroup && isNamedGroup)
+ {
+ out.append(newline).append(STARTGROUP).append(TAB)
+ .append(group).append(newline);
+ }
+ firstInGroup = false;
+ out.append(formatJalviewFeature(sequenceName, sequenceFeature));
+ }
}
}
- if (isNamedGroup)
+ if (isNamedGroup && !firstInGroup)
{
- out.append(ENDGROUP).append(TAB);
- out.append(group);
- out.append(newline);
+ out.append(ENDGROUP).append(TAB).append(group).append(newline);
}
}
return foundSome;
* @return
*/
public String printGffFormat(SequenceI[] sequences,
- Map<String, FeatureColourI> visible,
- List<String> visibleFeatureGroups,
- boolean includeNonPositionalFeatures)
+ FeatureRenderer fr, boolean includeNonPositionalFeatures)
{
+ Map<String, FeatureColourI> visibleColours = fr.getDisplayedFeatureCols();
+
StringBuilder out = new StringBuilder(256);
out.append(String.format("%s %d\n", GFF_VERSION, gffVersion == 0 ? 2 : gffVersion));
if (!includeNonPositionalFeatures
- && (visible == null || visible.isEmpty()))
+ && (visibleColours == null || visibleColours.isEmpty()))
{
return out.toString();
}
- String[] types = visible == null ? new String[0] : visible.keySet()
- .toArray(
- new String[visible.keySet().size()]);
+ String[] types = visibleColours == null ? new String[0]
+ : visibleColours.keySet()
+ .toArray(new String[visibleColours.keySet().size()]);
for (SequenceI seq : sequences)
{
{
features.addAll(seq.getFeatures().getNonPositionalFeatures());
}
- if (visible != null && !visible.isEmpty())
+ if (visibleColours != null && !visibleColours.isEmpty())
{
features.addAll(seq.getFeatures().getPositionalFeatures(types));
}
for (SequenceFeature sf : features)
{
- String source = sf.featureGroup;
- if (!sf.isNonPositional() && source != null
- && !visibleFeatureGroups.contains(source))
+ if (!sf.isNonPositional() && !fr.isVisible(sf))
{
- // group is not visible
+ /*
+ * feature hidden by group visibility, colour threshold,
+ * or feature filter condition
+ */
continue;
}
+ String source = sf.featureGroup;
if (source == null)
{
source = sf.getDescription();
import jalview.schemes.FeatureColour;
import jalview.structure.StructureSelectionManager;
import jalview.util.matcher.Condition;
+import jalview.viewmodel.seqfeatures.FeatureRendererModel;
+import jalview.viewmodel.seqfeatures.FeatureRendererModel.FeatureSettingsBean;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
* first with no features displayed, exclude non-positional features
*/
FeatureRenderer fr = af.alignPanel.getFeatureRenderer();
- Map<String, FeatureColourI> visible = fr.getDisplayedFeatureCols();
- List<String> visibleGroups = new ArrayList<>(
- Arrays.asList(new String[] {}));
- String exported = featuresFile.printJalviewFormat(
- al.getSequencesArray(), visible, null, visibleGroups, false);
+ String exported = featuresFile
+ .printJalviewFormat(al.getSequencesArray(), fr, false);
String expected = "No Features Visible";
assertEquals(expected, exported);
/*
* include non-positional features
*/
- visibleGroups.add("uniprot");
- exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
- visible, null, visibleGroups, true);
+ fr.setGroupVisibility("uniprot", true);
+ exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
+ true);
expected = "Cath\tFER_CAPAA\t-1\t0\t0\tDomain\t0.0\n"
+ "desc1\tFER_CAPAN\t-1\t0\t0\tPfam\t1.3\n"
- + "desc3\tFER1_SOLLC\t-1\t0\t0\tPfam\n" // NaN is not output
- + "\nSTARTGROUP\tuniprot\nENDGROUP\tuniprot\n";
+ + "desc3\tFER1_SOLLC\t-1\t0\t0\tPfam\n"; // NaN is not output
assertEquals(expected, exported);
/*
*/
fr.setVisible("METAL");
fr.setVisible("GAMMA-TURN");
- visible = fr.getDisplayedFeatureCols();
- exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
- visible, null, visibleGroups, false);
+ exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
+ false);
expected = "METAL\tcc9900\n"
+ "GAMMA-TURN\tscore|ff0000|00ffff|noValueMin|20.0|95.0|below|66.0\n"
+ "\nSTARTGROUP\tuniprot\n"
* now set Pfam visible
*/
fr.setVisible("Pfam");
- visible = fr.getDisplayedFeatureCols();
- exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
- visible, null, visibleGroups, false);
+ exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
+ false);
/*
* features are output within group, ordered by sequence and by type
*/
* no features
*/
FeaturesFile featuresFile = new FeaturesFile();
- FeatureRenderer fr = af.alignPanel.getFeatureRenderer();
- Map<String, FeatureColourI> visible = new HashMap<>();
- List<String> visibleGroups = new ArrayList<>(
- Arrays.asList(new String[] {}));
+ FeatureRendererModel fr = (FeatureRendererModel) af.alignPanel
+ .getFeatureRenderer();
String exported = featuresFile.printGffFormat(al.getSequencesArray(),
- visible, visibleGroups, false);
+ fr, false);
String gffHeader = "##gff-version 2\n";
assertEquals(gffHeader, exported);
- exported = featuresFile.printGffFormat(al.getSequencesArray(), visible,
- visibleGroups, true);
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ true);
assertEquals(gffHeader, exported);
/*
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(), visible,
- visibleGroups, false);
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ false);
assertEquals(gffHeader, exported);
/*
* include non-positional features
*/
- visibleGroups.add("Uniprot");
- exported = featuresFile.printGffFormat(al.getSequencesArray(), visible,
- visibleGroups, true);
+ fr.setGroupVisibility("Uniprot", true);
+ fr.setGroupVisibility("s3dm", false);
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ true);
String expected = gffHeader
+ "FER_CAPAA\tUniprot\tDomain\t0\t0\t0.0\t.\t.\n";
assertEquals(expected, exported);
*/
fr.setVisible("METAL");
fr.setVisible("GAMMA-TURN");
- visible = fr.getDisplayedFeatureCols();
- exported = featuresFile.printGffFormat(al.getSequencesArray(), visible,
- visibleGroups, false);
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ 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
*/
- visibleGroups.add("s3dm");
- exported = featuresFile.printGffFormat(al.getSequencesArray(), visible,
- visibleGroups, false);
+ fr.setGroupVisibility("s3dm", true);
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ 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";
* now set Pfam visible
*/
fr.setVisible("Pfam");
- visible = fr.getDisplayedFeatureCols();
- exported = featuresFile.printGffFormat(al.getSequencesArray(), visible,
- visibleGroups, false);
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ false);
// Pfam feature columns include strand(+), phase(2), attributes
expected = gffHeader
+ "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\n"
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\n";
+ 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);
+ String expected = gffHeader
+ + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\n"
+ + "FER_CAPAA\tCath\tMETAL\t41\t41\t0.6\t.\t.\n";
+ assertEquals(expected, exported);
+
+ /*
+ * now threshold to Score > 1.1 - should exclude sf2
+ */
+ FeatureColourI fc = new FeatureColour(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);
+ expected = gffHeader + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\n";
+ assertEquals(expected, exported);
+
+ /*
+ * remove threshold and check sf2 is exported
+ */
+ fc.setAboveThreshold(false);
+ exported = featuresFile.printGffFormat(al.getSequencesArray(), fr,
+ false);
+ expected = gffHeader + "FER_CAPAA\tCath\tMETAL\t39\t39\t1.2\t.\t.\n"
+ + "FER_CAPAA\tCath\tMETAL\t41\t41\t0.6\t.\t.\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);
+ expected = gffHeader + "FER_CAPAA\tCath\tMETAL\t41\t41\t0.6\t.\t.\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);
+ 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(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);
+ 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);
+ 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);
+ 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);
+ }
}