JAL-2791 refactored Export Features to apply all visibility tests
[jalview.git] / src / jalview / io / FeaturesFile.java
index 169da5a..39ec10d 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,28 @@ 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();
+    List<String> visibleFeatureGroups = fr.getDisplayedFeatureGroups();
+
+    if (!includeNonPositional
+            && (visibleColours == null || visibleColours.isEmpty()))
     {
       // no point continuing.
       return "No Features Visible";
@@ -594,9 +595,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,13 +606,14 @@ 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);
+    outputFeatureFilters(out, visibleColours, featureFilters);
 
     /*
      * sort groups alphabetically, and ensure that features with a
@@ -645,7 +648,8 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
     /*
      * positional features within groups
      */
-    foundSome |= outputFeaturesByGroup(out, sortedGroups, types, sequences);
+    foundSome |= outputFeaturesByGroup(out, fr, sortedGroups, types,
+            sequences);
 
     return foundSome ? out.toString() : "No Features Visible";
   }
@@ -685,36 +689,33 @@ 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 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
@@ -731,16 +732,23 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
 
         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;
@@ -872,23 +880,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 +905,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();