Merge branch 'develop' into trialMerge
[jalview.git] / src / jalview / io / FeaturesFile.java
index 0b46197..d51da33 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();