JAL-3184 JAL-2843 restore saved feature filters correctly
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 14 Jan 2019 12:14:59 +0000 (12:14 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 14 Jan 2019 12:14:59 +0000 (12:14 +0000)
src/jalview/gui/Jalview2XML.java
src/jalview/gui/Jalview2XML_V1.java
src/jalview/viewmodel/seqfeatures/FeatureRendererSettings.java

index 9285754..c8e887b 100644 (file)
@@ -52,7 +52,6 @@ import jalview.schemabinding.version2.Annotation;
 import jalview.schemabinding.version2.AnnotationColours;
 import jalview.schemabinding.version2.AnnotationElement;
 import jalview.schemabinding.version2.CalcIdParam;
-import jalview.schemabinding.version2.Colour;
 import jalview.schemabinding.version2.CompoundMatcher;
 import jalview.schemabinding.version2.DBRef;
 import jalview.schemabinding.version2.Features;
@@ -4639,125 +4638,9 @@ public class Jalview2XML
       af.viewport.setShowGroupConservation(false);
     }
 
-    // recover feature settings
     if (jms.getFeatureSettings() != null)
     {
-      FeatureRenderer fr = af.alignPanel.getSeqPanel().seqCanvas
-              .getFeatureRenderer();
-      FeaturesDisplayed fdi;
-      af.viewport.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
-      String[] renderOrder = new String[jms.getFeatureSettings()
-              .getSettingCount()];
-      Map<String, FeatureColourI> featureColours = new Hashtable<>();
-      Map<String, Float> featureOrder = new Hashtable<>();
-
-      for (int fs = 0; fs < jms.getFeatureSettings()
-              .getSettingCount(); fs++)
-      {
-        Setting setting = jms.getFeatureSettings().getSetting(fs);
-        String featureType = setting.getType();
-
-        /*
-         * restore feature filters (if any)
-         */
-        MatcherSet filters = setting.getMatcherSet();
-        if (filters != null)
-        {
-          FeatureMatcherSetI filter = Jalview2XML
-                  .unmarshalFilter(featureType, filters);
-          if (!filter.isEmpty())
-          {
-            fr.setFeatureFilter(featureType, filter);
-          }
-        }
-
-        /*
-         * restore feature colour scheme
-         */
-        Color maxColour = new Color(setting.getColour());
-        if (setting.hasMincolour())
-        {
-          /*
-           * minColour is always set unless a simple colour
-           * (including for colour by label though it doesn't use it)
-           */
-          Color minColour = new Color(setting.getMincolour());
-          Color noValueColour = minColour;
-          NoValueColour noColour = setting.getNoValueColour();
-          if (noColour == NoValueColour.NONE)
-          {
-            noValueColour = null;
-          }
-          else if (noColour == NoValueColour.MAX)
-          {
-            noValueColour = maxColour;
-          }
-          float min = setting.hasMin() ? setting.getMin() : 0f;
-          float max = setting.hasMin() ? setting.getMax() : 1f;
-          FeatureColourI gc = new FeatureColour(minColour, maxColour,
-                  noValueColour, min, max);
-          if (setting.getAttributeNameCount() > 0)
-          {
-            gc.setAttributeName(setting.getAttributeName());
-          }
-          if (setting.hasThreshold())
-          {
-            gc.setThreshold(setting.getThreshold());
-            int threshstate = setting.getThreshstate();
-            // -1 = None, 0 = Below, 1 = Above threshold
-            if (threshstate == 0)
-            {
-              gc.setBelowThreshold(true);
-            }
-            else if (threshstate == 1)
-            {
-              gc.setAboveThreshold(true);
-            }
-          }
-          gc.setAutoScaled(true); // default
-          if (setting.hasAutoScale())
-          {
-            gc.setAutoScaled(setting.getAutoScale());
-          }
-          if (setting.hasColourByLabel())
-          {
-            gc.setColourByLabel(setting.getColourByLabel());
-          }
-          // and put in the feature colour table.
-          featureColours.put(featureType, gc);
-        }
-        else
-        {
-          featureColours.put(featureType,
-                  new FeatureColour(maxColour));
-        }
-        renderOrder[fs] = featureType;
-        if (setting.hasOrder())
-        {
-          featureOrder.put(featureType, setting.getOrder());
-        }
-        else
-        {
-          featureOrder.put(featureType, new Float(
-                  fs / jms.getFeatureSettings().getSettingCount()));
-        }
-        if (setting.getDisplay())
-        {
-          fdi.setVisible(featureType);
-        }
-      }
-      Map<String, Boolean> fgtable = new Hashtable<>();
-      for (int gs = 0; gs < jms.getFeatureSettings().getGroupCount(); gs++)
-      {
-        Group grp = jms.getFeatureSettings().getGroup(gs);
-        fgtable.put(grp.getName(), new Boolean(grp.getDisplay()));
-      }
-      // FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
-      // fgtable, featureColours, jms.getFeatureSettings().hasTransparency() ?
-      // jms.getFeatureSettings().getTransparency() : 0.0, featureOrder);
-      FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
-              fgtable, featureColours, 1.0f, featureOrder);
-      fr.transferSettings(frs);
+      loadFeatureSettings(jms, af);
     }
 
     if (view.getHiddenColumnsCount() > 0)
@@ -4812,6 +4695,160 @@ public class Jalview2XML
   }
 
   /**
+   * Loads feature settings data from project XML and stores
+   * <ul>
+   * <li>feature type visibility in the Viewport</li>
+   * <li>other data in the FeatureRenderer:
+   * <ul>
+   * <li>feature type render order</li>
+   * <li>feature type colours</li>
+   * <li>feature type filters</li>
+   * <li>feature groups and their visibility</li>
+   * </ul>
+   * </li>
+   * </ul>
+   * 
+   * @param jms
+   * @param af
+   */
+  static void loadFeatureSettings(JalviewModelSequence jms, AlignFrame af)
+  {
+    FeatureRenderer fr = af.getFeatureRenderer();
+    FeaturesDisplayed fdi = new FeaturesDisplayed();
+    af.viewport.setFeaturesDisplayed(fdi);
+    String[] renderOrder = new String[jms.getFeatureSettings()
+            .getSettingCount()];
+    Map<String, FeatureColourI> featureColours = new Hashtable<>();
+    Map<String, Float> featureOrder = new Hashtable<>();
+    Map<String, FeatureMatcherSetI> filters = new HashMap<>();
+
+    for (int fs = 0; fs < jms.getFeatureSettings()
+            .getSettingCount(); fs++)
+    {
+      Setting setting = jms.getFeatureSettings().getSetting(fs);
+      String featureType = setting.getType();
+
+      /*
+       * restore feature filters (if any)
+       */
+      MatcherSet matchers = setting.getMatcherSet();
+      if (matchers != null)
+      {
+        FeatureMatcherSetI filter = Jalview2XML
+                .unmarshalFilter(featureType, matchers);
+        if (!filter.isEmpty())
+        {
+          filters.put(featureType, filter);
+        }
+      }
+
+      /*
+       * restore feature colour scheme
+       */
+      FeatureColourI featureColour = loadFeatureColour(setting);
+      featureColours.put(featureType, featureColour);
+
+      renderOrder[fs] = featureType;
+      if (setting.hasOrder())
+      {
+        featureOrder.put(featureType, setting.getOrder());
+      }
+      else
+      {
+        featureOrder.put(featureType, new Float(
+                fs / jms.getFeatureSettings().getSettingCount()));
+      }
+      if (setting.getDisplay())
+      {
+        fdi.setVisible(featureType);
+      }
+    }
+    Map<String, Boolean> fgtable = new Hashtable<>();
+    for (int gs = 0; gs < jms.getFeatureSettings().getGroupCount(); gs++)
+    {
+      Group grp = jms.getFeatureSettings().getGroup(gs);
+      fgtable.put(grp.getName(), new Boolean(grp.getDisplay()));
+    }
+
+    /*
+     * todo: save transparency in project (JAL-1147)
+     */
+    float transparency = 1.0f;
+    FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
+            fgtable, featureColours, transparency, featureOrder, filters);
+    fr.transferSettings(frs);
+  }
+
+  /**
+   * Constructs a FeatureColour from the data saved for one FeatureSetting
+   * <code>setting</code> element
+   * 
+   * @param setting
+   * @return
+   */
+  static FeatureColourI loadFeatureColour(Setting setting)
+  {
+    FeatureColourI featureColour;
+    Color maxColour = new Color(setting.getColour());
+    if (setting.hasMincolour())
+    {
+      /*
+       * minColour is always set unless a simple colour
+       * (including for colour by label though it doesn't use it)
+       */
+      Color minColour = new Color(setting.getMincolour());
+      Color noValueColour = minColour;
+      NoValueColour noColour = setting.getNoValueColour();
+      if (noColour == NoValueColour.NONE)
+      {
+        noValueColour = null;
+      }
+      else if (noColour == NoValueColour.MAX)
+      {
+        noValueColour = maxColour;
+      }
+      float min = setting.hasMin() ? setting.getMin() : 0f;
+      float max = setting.hasMin() ? setting.getMax() : 1f;
+      FeatureColourI gc = new FeatureColour(minColour, maxColour,
+              noValueColour, min, max);
+      if (setting.getAttributeNameCount() > 0)
+      {
+        gc.setAttributeName(setting.getAttributeName());
+      }
+      if (setting.hasThreshold())
+      {
+        gc.setThreshold(setting.getThreshold());
+        int threshstate = setting.getThreshstate();
+        // -1 = None, 0 = Below, 1 = Above threshold
+        if (threshstate == 0)
+        {
+          gc.setBelowThreshold(true);
+        }
+        else if (threshstate == 1)
+        {
+          gc.setAboveThreshold(true);
+        }
+      }
+      gc.setAutoScaled(true); // default
+      if (setting.hasAutoScale())
+      {
+        gc.setAutoScaled(setting.getAutoScale());
+      }
+      if (setting.hasColourByLabel())
+      {
+        gc.setColourByLabel(setting.getColourByLabel());
+      }
+      featureColour = gc;
+    }
+    else
+    {
+      featureColour = new FeatureColour(maxColour);
+    }
+
+    return featureColour;
+  }
+
+  /**
    * Reads saved data to restore Colour by Annotation settings
    * 
    * @param viewAnnColour
index 331e738..d2ba95b 100755 (executable)
@@ -21,6 +21,7 @@
 package jalview.gui;
 
 import jalview.analysis.Conservation;
+import jalview.api.FeatureColourI;
 import jalview.binding.Annotation;
 import jalview.binding.AnnotationElement;
 import jalview.binding.Features;
@@ -40,6 +41,7 @@ import jalview.datamodel.SequenceFeature;
 import jalview.io.FileFormat;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ColourSchemeProperty;
+import jalview.schemes.FeatureColour;
 import jalview.structure.StructureSelectionManager;
 import jalview.util.MessageManager;
 import jalview.util.jarInputStreamProvider;
@@ -428,8 +430,8 @@ public class Jalview2XML_V1
 
     if (jms.getFeatureSettings() != null)
     {
-      Hashtable featuresDisplayed = new Hashtable();
-      Hashtable featureColours = new Hashtable();
+      Hashtable<String, Integer> featuresDisplayed = new Hashtable<>();
+      Hashtable<String, FeatureColourI> featureColours = new Hashtable<>();
       String[] renderOrder = new String[jms.getFeatureSettings()
               .getSettingCount()];
       for (int fs = 0; fs < jms.getFeatureSettings()
@@ -438,7 +440,7 @@ public class Jalview2XML_V1
         Setting setting = jms.getFeatureSettings().getSetting(fs);
 
         featureColours.put(setting.getType(),
-                new java.awt.Color(setting.getColour()));
+                new FeatureColour(new java.awt.Color(setting.getColour())));
 
         renderOrder[fs] = setting.getType();
 
@@ -449,7 +451,8 @@ public class Jalview2XML_V1
         }
       }
       FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
-              new Hashtable(), featureColours, 1.0f, null);
+              new Hashtable<String, Boolean>(), featureColours, 1.0f, null,
+              null);
       af.alignPanel.getSeqPanel().seqCanvas.getFeatureRenderer()
               .transferSettings(frs);
     }
index 02b7f35..cd6f51b 100644 (file)
@@ -41,7 +41,7 @@ import java.util.concurrent.ConcurrentHashMap;
  * </ul>
  * Note that feature type visibility settings are not held here.
  */
-public class FeatureRendererSettings implements Cloneable
+public class FeatureRendererSettings
 {
   String[] renderOrder;
 
@@ -64,10 +64,21 @@ public class FeatureRendererSettings implements Cloneable
 
   Map<String, Float> featureOrder;
 
+  /**
+   * Constructor
+   * 
+   * @param renderOrder
+   * @param featureGroups
+   * @param featureColours
+   * @param transparency
+   * @param featureOrder
+   * @param filters
+   */
   public FeatureRendererSettings(String[] renderOrder,
           Map<String, Boolean> featureGroups,
           Map<String, FeatureColourI> featureColours, float transparency,
-          Map<String, Float> featureOrder)
+          Map<String, Float> featureOrder,
+          Map<String, FeatureMatcherSetI> filters)
   {
     super();
     this.renderOrder = Arrays.copyOf(renderOrder, renderOrder.length);
@@ -77,6 +88,7 @@ public class FeatureRendererSettings implements Cloneable
             featureColours);
     this.transparency = transparency;
     this.featureOrder = new ConcurrentHashMap<>(featureOrder);
+    featureFilters = filters;
   }
 
   /**