Merge branch 'bug/JAL-2791exportFilteredFeature' into merge/JAL-2791
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 1 Mar 2019 15:35:54 +0000 (15:35 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 1 Mar 2019 15:35:54 +0000 (15:35 +0000)
src/jalview/api/FeatureColourI.java
src/jalview/api/FeatureRenderer.java
src/jalview/appletgui/AlignFrame.java
src/jalview/gui/AnnotationExporter.java
src/jalview/io/FeaturesFile.java
src/jalview/schemes/FeatureColour.java
src/jalview/viewmodel/seqfeatures/FeatureRendererModel.java
test/jalview/io/FeaturesFileTest.java
test/jalview/renderer/seqfeatures/FeatureRendererTest.java
test/jalview/schemes/FeatureColourTest.java

index 4dbb1bb..999104c 100644 (file)
@@ -192,4 +192,16 @@ public interface FeatureColourI
    * @return
    */
   void setAttributeName(String... name);
+
+  /**
+   * Answers true if colour has a threshold set, and the feature score (or other
+   * attribute selected for colouring) is outwith the threshold.
+   * <p>
+   * Answers false if not a graduated colour, or no threshold is set, or value
+   * is not outwith the threshold, or value is null or non-numeric.
+   * 
+   * @param sf
+   * @return
+   */
+  boolean isOutwithThreshold(SequenceFeature sf);
 }
index 868f196..404c497 100644 (file)
@@ -265,4 +265,19 @@ public interface FeatureRenderer
    * @return
    */
   Color getColour(SequenceFeature feature);
+
+  /**
+   * Answers true if feature would be shown, else false. A feature is shown if
+   * <ul>
+   * <li>its feature type is set to visible</li>
+   * <li>its feature group is either null, or set to visible</li>
+   * <li>it is not excluded by a colour threshold on score or other numeric
+   * attribute</li>
+   * <li>it is not excluded by a filter condition</li>
+   * </ul>
+   * 
+   * @param feature
+   * @return
+   */
+  boolean isVisible(SequenceFeature feature);
 }
index 5ad212e..85fb03c 100644 (file)
@@ -1447,14 +1447,12 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener,
     {
       features = formatter.printJalviewFormat(
               viewport.getAlignment().getSequencesArray(),
-              getDisplayedFeatureCols(), null, getDisplayedFeatureGroups(),
-              true);
+              alignPanel.getFeatureRenderer(), true);
     }
     else
     {
       features = formatter.printGffFormat(viewport.getAlignment()
-              .getSequencesArray(), getDisplayedFeatureCols(),
-              getDisplayedFeatureGroups(), true);
+              .getSequencesArray(), alignPanel.getFeatureRenderer(), true);
     }
 
     if (displayTextbox)
index 6fefbd0..fac531e 100644 (file)
  */
 package jalview.gui;
 
-import jalview.api.FeatureColourI;
+import jalview.api.FeatureRenderer;
 import jalview.bin.Cache;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.SequenceI;
-import jalview.datamodel.features.FeatureMatcherSetI;
 import jalview.io.AnnotationFile;
 import jalview.io.FeaturesFile;
 import jalview.io.JalviewFileChooser;
@@ -38,8 +37,6 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.FileWriter;
 import java.io.PrintWriter;
-import java.util.List;
-import java.util.Map;
 
 import javax.swing.BorderFactory;
 import javax.swing.ButtonGroup;
@@ -214,24 +211,18 @@ public class AnnotationExporter extends JPanel
   {
     String text;
     SequenceI[] sequences = ap.av.getAlignment().getSequencesArray();
-    Map<String, FeatureColourI> featureColours = ap.getFeatureRenderer()
-            .getDisplayedFeatureCols();
-    Map<String, FeatureMatcherSetI> featureFilters = ap.getFeatureRenderer()
-            .getFeatureFilters();
-    List<String> featureGroups = ap.getFeatureRenderer()
-            .getDisplayedFeatureGroups();
     boolean includeNonPositional = ap.av.isShowNPFeats();
 
     FeaturesFile formatter = new FeaturesFile();
+    final FeatureRenderer fr = ap.getFeatureRenderer();
     if (GFFFormat.isSelected())
     {
-      text = formatter.printGffFormat(sequences, featureColours,
-              featureGroups, includeNonPositional);
+      text = formatter.printGffFormat(sequences, fr, includeNonPositional);
     }
     else
     {
-      text = formatter.printJalviewFormat(sequences, featureColours,
-              featureFilters, featureGroups, includeNonPositional);
+      text = formatter.printJalviewFormat(sequences, fr,
+              includeNonPositional);
     }
     return text;
   }
index 169da5a..12ad0d4 100755 (executable)
@@ -24,6 +24,7 @@ import jalview.analysis.AlignmentUtils;
 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;
@@ -562,28 +563,27 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
   }
 
   /**
-   * 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();
+
+    if (!includeNonPositional
+            && (visibleColours == null || visibleColours.isEmpty()))
     {
       // no point continuing.
       return "No Features Visible";
@@ -594,9 +594,10 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
      */
     // 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(
@@ -604,50 +605,22 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
       }
     }
 
-    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);
-
-    /*
-     * sort groups alphabetically, and ensure that features with a
-     * null or empty group are output after those in named groups
-     */
-    List<String> sortedGroups = new ArrayList<>(visibleFeatureGroups);
-    sortedGroups.remove(null);
-    sortedGroups.remove("");
-    Collections.sort(sortedGroups);
-    sortedGroups.add(null);
-    sortedGroups.add("");
-
-    boolean foundSome = false;
-
-    /*
-     * first output any non-positional features
-     */
-    if (includeNonPositional)
-    {
-      for (int i = 0; i < sequences.length; i++)
-      {
-        String sequenceName = sequences[i].getName();
-        for (SequenceFeature feature : sequences[i].getFeatures()
-                .getNonPositionalFeatures())
-        {
-          foundSome = true;
-          out.append(formatJalviewFeature(sequenceName, feature));
-        }
-      }
-    }
+    outputFeatureFilters(out, visibleColours, featureFilters);
 
     /*
-     * positional features within groups
+     * output features within groups
      */
-    foundSome |= outputFeaturesByGroup(out, sortedGroups, types, sequences);
+    int count = outputFeaturesByGroup(out, fr, types, sequences,
+            includeNonPositional);
 
-    return foundSome ? out.toString() : "No Features Visible";
+    return count > 0 ? out.toString() : "No Features Visible";
   }
 
   /**
@@ -685,65 +658,104 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
     }
     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 the number of features written.
    * 
    * @param out
-   * @param groups
+   * @param fr
    * @param featureTypes
    * @param sequences
+   * @param includeNonPositional
    * @return
    */
-  private boolean outputFeaturesByGroup(StringBuilder out,
-          List<String> groups, String[] featureTypes, SequenceI[] sequences)
+  private int outputFeaturesByGroup(StringBuilder out,
+          FeatureRenderer fr, String[] featureTypes,
+          SequenceI[] sequences, boolean includeNonPositional)
   {
-    boolean foundSome = false;
-    for (String group : groups)
+    List<String> featureGroups = fr.getFeatureGroups();
+
+    /*
+     * sort groups alphabetically, and ensure that features with a
+     * null or empty group are output after those in named groups
+     */
+    List<String> sortedGroups = new ArrayList<>(featureGroups);
+    sortedGroups.remove(null);
+    sortedGroups.remove("");
+    Collections.sort(sortedGroups);
+    sortedGroups.add(null);
+    sortedGroups.add("");
+
+    int count = 0;
+    List<String> visibleGroups = fr.getDisplayedFeatureGroups();
+
+    /*
+     * loop over all groups (may be visible or not);
+     * non-positional features are output even if group is not visible
+     */
+    for (String group : sortedGroups)
     {
-      boolean isNamedGroup = (group != null && !"".equals(group));
-      if (isNamedGroup)
-      {
-        out.append(newline);
-        out.append(STARTGROUP).append(TAB);
-        out.append(group);
-        out.append(newline);
-      }
+      boolean firstInGroup = true;
+      boolean isNullGroup = group == null || "".equals(group);
 
-      /*
-       * output positional features within groups
-       */
       for (int i = 0; i < sequences.length; i++)
       {
         String sequenceName = sequences[i].getName();
         List<SequenceFeature> features = new ArrayList<>();
-        if (featureTypes.length > 0)
+
+        /*
+         * get any non-positional features in this group, if wanted
+         * (for any feature type, whether visible or not)
+         */
+        if (includeNonPositional)
+        {
+          features.addAll(sequences[i].getFeatures()
+                  .getFeaturesForGroup(false, group));
+        }
+
+        /*
+         * add positional features for visible feature types, but
+         * (for named groups) only if feature group is visible
+         */
+        if (featureTypes.length > 0
+                && (isNullGroup || visibleGroups.contains(group)))
         {
           features.addAll(sequences[i].getFeatures().getFeaturesForGroup(
                   true, group, featureTypes));
         }
 
-        for (SequenceFeature sequenceFeature : features)
+        for (SequenceFeature sf : features)
         {
-          foundSome = true;
-          out.append(formatJalviewFeature(sequenceName, sequenceFeature));
+          if (sf.isNonPositional() || fr.isVisible(sf))
+          {
+            count++;
+            if (firstInGroup)
+            {
+              out.append(newline);
+              if (!isNullGroup)
+              {
+                out.append(STARTGROUP).append(TAB).append(group)
+                        .append(newline);
+              }
+            }
+            firstInGroup = false;
+            out.append(formatJalviewFeature(sequenceName, sf));
+          }
         }
       }
 
-      if (isNamedGroup)
+      if (!isNullGroup && !firstInGroup)
       {
-        out.append(ENDGROUP).append(TAB);
-        out.append(group);
-        out.append(newline);
+        out.append(ENDGROUP).append(TAB).append(group).append(newline);
       }
     }
-    return foundSome;
+    return count;
   }
 
   /**
@@ -872,23 +884,23 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * @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)
     {
@@ -897,21 +909,23 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
       {
         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();
index 51e7645..f34478c 100644 (file)
@@ -689,9 +689,12 @@ public class FeatureColour implements FeatureColourI
 
   /**
    * Returns the colour for the given instance of the feature. This may be a
-   * simple colour, a colour generated from the feature description (if
-   * isColourByLabel()), or a colour derived from the feature score (if
-   * isGraduatedColour()).
+   * simple colour, a colour generated from the feature description or other
+   * attribute (if isColourByLabel()), or a colour derived from the feature
+   * score or other attribute (if isGraduatedColour()).
+   * <p>
+   * Answers null if feature score (or attribute) value lies outside a
+   * configured threshold.
    * 
    * @param feature
    * @return
@@ -890,4 +893,32 @@ public class FeatureColour implements FeatureColourI
     attributeName = name;
   }
 
+  @Override
+  public boolean isOutwithThreshold(SequenceFeature feature)
+  {
+    if (!isGraduatedColour())
+    {
+      return false;
+    }
+    float scr = feature.getScore();
+    if (attributeName != null)
+    {
+      try
+      {
+        String attVal = feature.getValueAsString(attributeName);
+        scr = Float.valueOf(attVal);
+      } catch (Throwable e)
+      {
+        scr = Float.NaN;
+      }
+    }
+    if (Float.isNaN(scr))
+    {
+      return false;
+    }
+
+    return ((isAboveThreshold() && scr <= threshold)
+            || (isBelowThreshold() && scr >= threshold));
+  }
+
 }
index 5db8751..4af6fde 100644 (file)
@@ -1156,4 +1156,32 @@ public abstract class FeatureRendererModel
     return filter == null ? true : filter.matches(sf);
   }
 
+  @Override
+  public boolean isVisible(SequenceFeature feature)
+  {
+    if (feature == null)
+    {
+      return false;
+    }
+    if (getFeaturesDisplayed() == null
+            || !getFeaturesDisplayed().isVisible(feature.getType()))
+    {
+      return false;
+    }
+    if (featureGroupNotShown(feature))
+    {
+      return false;
+    }
+    FeatureColourI fc = featureColours.get(feature.getType());
+    if (fc != null && fc.isOutwithThreshold(feature))
+    {
+      return false;
+    }
+    if (!featureMatchesFilters(feature))
+    {
+      return false;
+    }
+    return true;
+  }
+
 }
index 32ca841..3632cc7 100644 (file)
@@ -45,12 +45,12 @@ import jalview.gui.JvOptionPane;
 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;
@@ -475,24 +475,22 @@ public class FeaturesFileTest
      * 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
+     * include non-positional features, but still no positional features
      */
-    visibleGroups.add("uniprot");
-    exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
-            visible, null, visibleGroups, 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";
+    fr.setGroupVisibility("uniprot", true);
+    exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
+            true);
+    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);
 
     /*
@@ -500,9 +498,8 @@ public class FeaturesFileTest
      */
     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"
@@ -515,11 +512,10 @@ public class FeaturesFileTest
      * 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
+     * features are output within group, ordered by sequence and type
      */
     expected = "METAL\tcc9900\n"
             + "Pfam\tff0000\n"
@@ -529,9 +525,36 @@ public class FeaturesFileTest
             + "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"
             + "ENDGROUP\tuniprot\n"
-            // null / empty group features output after features in named
-            // groups:
+            // 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);
+    assertEquals(expected, exported);
+
+    /*
+     * include non-positional (overrides group not shown)
+     */
+    exported = featuresFile.printJalviewFormat(al.getSequencesArray(), fr,
+            true);
+    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);
   }
@@ -547,16 +570,14 @@ public class FeaturesFileTest
      * 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);
 
     /*
@@ -578,18 +599,31 @@ public class FeaturesFileTest
     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);
@@ -600,9 +634,8 @@ public class FeaturesFileTest
      */
     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);
@@ -610,9 +643,9 @@ public class FeaturesFileTest
     /*
      * 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";
@@ -622,9 +655,8 @@ public class FeaturesFileTest
      * 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"
@@ -704,7 +736,167 @@ public class FeaturesFileTest
     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);
+  }
 }
index a9e3754..a0fb498 100644 (file)
@@ -535,4 +535,71 @@ public class FeatureRendererTest
     csqData.put("Feature", "ENST01234");
     assertEquals(fr.getColour(sf2), expected);
   }
+
+  @Test(groups = "Functional")
+  public void testIsVisible()
+  {
+    String seqData = ">s1\nMLQGIFPRS\n";
+    AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(seqData,
+            DataSourceType.PASTE);
+    AlignViewportI av = af.getViewport();
+    FeatureRenderer fr = new FeatureRenderer(av);
+    SequenceI seq = av.getAlignment().getSequenceAt(0);
+    SequenceFeature sf = new SequenceFeature("METAL", "Desc", 10, 10, 1f,
+            "Group");
+    sf.setValue("AC", "11");
+    sf.setValue("CLIN_SIG", "Likely Pathogenic");
+    seq.addSequenceFeature(sf);
+
+    assertFalse(fr.isVisible(null));
+
+    /*
+     * initial state FeatureRenderer hasn't 'found' feature
+     * and so its feature type has not yet been set visible
+     */
+    assertFalse(fr.getDisplayedFeatureCols().containsKey("METAL"));
+    assertFalse(fr.isVisible(sf));
+
+    fr.findAllFeatures(true);
+    assertTrue(fr.isVisible(sf));
+
+    /*
+     * feature group not visible
+     */
+    fr.setGroupVisibility("Group", false);
+    assertFalse(fr.isVisible(sf));
+    fr.setGroupVisibility("Group", true);
+    assertTrue(fr.isVisible(sf));
+
+    /*
+     * feature score outwith colour threshold (score > 2)
+     */
+    FeatureColourI fc = new FeatureColour(Color.white, Color.black,
+            Color.white, 0, 10);
+    fc.setAboveThreshold(true);
+    fc.setThreshold(2f);
+    fr.setColour("METAL", fc);
+    assertFalse(fr.isVisible(sf)); // score 1 is not above threshold 2
+    fc.setBelowThreshold(true);
+    assertTrue(fr.isVisible(sf)); // score 1 is below threshold 2
+
+    /*
+     * colour with threshold on attribute AC (value is 11)
+     */
+    fc.setAttributeName("AC");
+    assertFalse(fr.isVisible(sf)); // value 11 is not below threshold 2
+    fc.setAboveThreshold(true);
+    assertTrue(fr.isVisible(sf)); // value 11 is above threshold 2
+
+    fc.setAttributeName("AF"); // attribute AF is absent in sf
+    assertTrue(fr.isVisible(sf)); // feature is not excluded by threshold
+
+    FeatureMatcherSetI filter = new FeatureMatcherSet();
+    filter.and(FeatureMatcher.byAttribute(Condition.Contains, "pathogenic",
+            "CLIN_SIG"));
+    fr.setFeatureFilter("METAL", filter);
+    assertTrue(fr.isVisible(sf)); // feature matches filter
+    filter.and(FeatureMatcher.byScore(Condition.LE, "0.4"));
+    assertFalse(fr.isVisible(sf)); // feature doesn't match filter
+  }
 }
index a96caec..6ccce85 100644 (file)
@@ -712,4 +712,44 @@ public class FeatureColourTest
     Color expected = new Color(70, 120, 170);
     assertEquals(expected, fc.getColor(sf));
   }
+
+  @Test(groups = { "Functional" })
+  public void testIsOutwithThreshold()
+  {
+    FeatureColourI fc = new FeatureColour(Color.red);
+    SequenceFeature sf = new SequenceFeature("METAL", "desc", 10, 12, 1.2f, "grp");
+    assertFalse(fc.isOutwithThreshold(null));
+    assertFalse(fc.isOutwithThreshold(sf));
+
+    fc = new FeatureColour(Color.white, Color.black, Color.green, 0f, 10f);
+    assertFalse(fc.isOutwithThreshold(sf)); // no threshold
+
+    fc.setAboveThreshold(true);
+    fc.setThreshold(1f);
+    assertFalse(fc.isOutwithThreshold(sf)); // feature score 1.2 is above 1
+
+    fc.setThreshold(2f);
+    assertTrue(fc.isOutwithThreshold(sf)); // feature score 1.2 is not above 2
+
+    fc.setBelowThreshold(true);
+    assertFalse(fc.isOutwithThreshold(sf)); // feature score 1.2 is below 2
+
+    fc.setThreshold(1f);
+    assertTrue(fc.isOutwithThreshold(sf)); // feature score 1.2 is not below 1
+
+    /*
+     * with attribute value threshold
+     */
+    fc.setAttributeName("AC");
+    assertFalse(fc.isOutwithThreshold(sf)); // missing attribute AC is ignored
+
+    sf.setValue("AC", "-1");
+    assertFalse(fc.isOutwithThreshold(sf)); // value -1 is below 1
+
+    sf.setValue("AC", "1");
+    assertTrue(fc.isOutwithThreshold(sf)); // value 1 is not below 1
+
+    sf.setValue("AC", "junk");
+    assertFalse(fc.isOutwithThreshold(sf)); // bad value is ignored
+  }
 }