JAL-2055 prototype alternate feature colouring based on
[jalview.git] / src / jalview / viewmodel / seqfeatures / FeatureRendererModel.java
index c3b6c1e..7e28c7f 100644 (file)
@@ -1,6 +1,27 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.viewmodel.seqfeatures;
 
 import jalview.api.AlignViewportI;
+import jalview.api.FeatureColourI;
 import jalview.api.FeaturesDisplayedI;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.SequenceFeature;
@@ -14,13 +35,12 @@ import java.beans.PropertyChangeListener;
 import java.beans.PropertyChangeSupport;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.Vector;
 import java.util.concurrent.ConcurrentHashMap;
 
 public abstract class FeatureRendererModel implements
@@ -38,6 +58,9 @@ public abstract class FeatureRendererModel implements
 
   protected Object currentColour;
 
+  /*
+   * feature types in ordering of rendering, where last means on top
+   */
   protected String[] renderOrder;
 
   protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
@@ -45,6 +68,19 @@ public abstract class FeatureRendererModel implements
 
   protected AlignmentViewport av;
 
+  /*
+   * map holds per feature type, {{min, max}, {min, max}} feature score
+   * values for positional and non-positional features respectively
+   */
+  private Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
+
+  /*
+   * List of feature types where getFeatureNumber() > 0 is found
+   * - a heuristic for 'features have an explicit ordering'
+   */
+  private List<String> ordinalFeatures = new ArrayList<String>();
+
+  @Override
   public AlignViewportI getViewport()
   {
     return av;
@@ -121,8 +157,7 @@ public abstract class FeatureRendererModel implements
     }
     if (!fdi.isRegistered(featureType))
     {
-      pushFeatureType(Arrays.asList(new String[]
-      { featureType }));
+      pushFeatureType(Arrays.asList(new String[] { featureType }));
     }
     fdi.setVisible(featureType);
   }
@@ -165,15 +200,13 @@ public abstract class FeatureRendererModel implements
     types.toArray(neworder);
     if (renderOrder != null)
     {
-      System.arraycopy(neworder,0,neworder,renderOrder.length,ts);
+      System.arraycopy(neworder, 0, neworder, renderOrder.length, ts);
       System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
     }
     renderOrder = neworder;
   }
 
-  protected Hashtable minmax = new Hashtable();
-
-  public Hashtable getMinMax()
+  public Map<String, float[][]> getMinMax()
   {
     return minmax;
   }
@@ -187,9 +220,8 @@ public abstract class FeatureRendererModel implements
    */
   protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
   {
-    float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
-    final byte[] r = new byte[]
-    { 0, (byte) 255 };
+    float[] mm = minmax.get(sequenceFeature.type)[0];
+    final byte[] r = new byte[] { 0, (byte) 255 };
     if (mm != null)
     {
       if (r[0] != 0 || mm[0] < 0.0)
@@ -248,6 +280,7 @@ public abstract class FeatureRendererModel implements
   {
     ArrayList<SequenceFeature> tmp = new ArrayList<SequenceFeature>();
     SequenceFeature[] features = sequence.getSequenceFeatures();
+
     if (features != null)
     {
       for (int i = 0; i < features.length; i++)
@@ -262,9 +295,11 @@ public abstract class FeatureRendererModel implements
         if (features[i].featureGroup != null
                 && featureGroups != null
                 && featureGroups.containsKey(features[i].featureGroup)
-                && !((Boolean) featureGroups.get(features[i].featureGroup))
+                && !featureGroups.get(features[i].featureGroup)
                         .booleanValue())
+        {
           continue;
+        }
 
         if ((features[i].getBegin() <= res)
                 && (features[i].getEnd() >= res))
@@ -280,7 +315,7 @@ public abstract class FeatureRendererModel implements
    * Searches alignment for all features and updates colours
    * 
    * @param newMadeVisible
-   *          if true newly added feature types will be rendered immediatly
+   *          if true newly added feature types will be rendered immediately
    *          TODO: check to see if this method should actually be proxied so
    *          repaint events can be propagated by the renderer code
    */
@@ -296,14 +331,15 @@ public abstract class FeatureRendererModel implements
     }
 
     findingFeatures = true;
+    ordinalFeatures.clear();
     if (av.getFeaturesDisplayed() == null)
     {
       av.setFeaturesDisplayed(new FeaturesDisplayed());
     }
     FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
 
-    ArrayList<String> allfeatures = new ArrayList<String>();
-    ArrayList<String> oldfeatures = new ArrayList<String>();
+    List<String> allfeatures = new ArrayList<String>();
+    List<String> oldfeatures = new ArrayList<String>();
     if (renderOrder != null)
     {
       for (int i = 0; i < renderOrder.length; i++)
@@ -316,27 +352,24 @@ public abstract class FeatureRendererModel implements
     }
     if (minmax == null)
     {
-      minmax = new Hashtable();
+      minmax = new Hashtable<String, float[][]>();
     }
     AlignmentI alignment = av.getAlignment();
-    for (int i = 0; i < alignment.getHeight(); i++)
+    for (SequenceI asq : alignment.getSequences())
     {
-      SequenceI asq = alignment.getSequenceAt(i);
-      SequenceI dasq = asq.getDatasetSequence();
-      SequenceFeature[] features = dasq != null ? dasq
-              .getSequenceFeatures() : asq.getSequenceFeatures();
+      SequenceFeature[] features = asq.getSequenceFeatures();
 
       if (features == null)
       {
         continue;
       }
 
-      int index = 0;
-      while (index < features.length)
+      for (SequenceFeature feature : features)
       {
-        if (!featuresDisplayed.isRegistered(features[index].getType()))
+        String type = feature.getType();
+        if (!featuresDisplayed.isRegistered(type))
         {
-          String fgrp = features[index].getFeatureGroup();
+          String fgrp = feature.getFeatureGroup();
           if (fgrp != null)
           {
             Boolean groupDisplayed = featureGroups.get(fgrp);
@@ -345,60 +378,62 @@ public abstract class FeatureRendererModel implements
               groupDisplayed = Boolean.valueOf(newMadeVisible);
               featureGroups.put(fgrp, groupDisplayed);
             }
-            if (!((Boolean) groupDisplayed).booleanValue())
+            if (!groupDisplayed.booleanValue())
             {
-              index++;
               continue;
             }
           }
-          if (!(features[index].begin == 0 && features[index].end == 0))
+          if (!(feature.begin == 0 && feature.end == 0))
           {
             // If beginning and end are 0, the feature is for the whole sequence
             // and we don't want to render the feature in the normal way
 
             if (newMadeVisible
-                    && !oldfeatures.contains(features[index].getType()))
+                    && !oldfeatures.contains(type))
             {
               // this is a new feature type on the alignment. Mark it for
               // display.
-              featuresDisplayed.setVisible(features[index].getType());
-              setOrder(features[index].getType(), 0);
+              featuresDisplayed.setVisible(type);
+              setOrder(type, 0);
             }
           }
         }
-        if (!allfeatures.contains(features[index].getType()))
+        if (!allfeatures.contains(type))
         {
-          allfeatures.add(features[index].getType());
+          allfeatures.add(type);
         }
-        if (features[index].score != Float.NaN)
+        float score = feature.score;
+        if (!Float.isNaN(score))
         {
-          int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
-          float[][] mm = (float[][]) minmax.get(features[index].getType());
+          int nonpos = feature.getBegin() >= 1 ? 0 : 1;
+          float[][] mm = minmax.get(type);
           if (mm == null)
           {
-            mm = new float[][]
-            { null, null };
-            minmax.put(features[index].getType(), mm);
+            mm = new float[][] { null, null };
+            minmax.put(type, mm);
           }
           if (mm[nonpos] == null)
           {
-            mm[nonpos] = new float[]
-            { features[index].score, features[index].score };
+            mm[nonpos] = new float[] { score, score };
 
           }
           else
           {
-            if (mm[nonpos][0] > features[index].score)
-            {
-              mm[nonpos][0] = features[index].score;
-            }
-            if (mm[nonpos][1] < features[index].score)
-            {
-              mm[nonpos][1] = features[index].score;
-            }
+            mm[nonpos][0] = Math.min(mm[nonpos][0], score);
+            mm[nonpos][1] = Math.max(mm[nonpos][1], score);
+          }
+        }
+
+        /*
+         * add to 'ordinal feature types' if it has featureNumber > 0
+         */
+        if (!ordinalFeatures.contains(type))
+        {
+          if (feature.getFeatureNumber() > 0)
+          {
+            ordinalFeatures.add(type);
           }
         }
-        index++;
       }
     }
     updateRenderOrder(allfeatures);
@@ -423,7 +458,8 @@ public abstract class FeatureRendererModel implements
     List<String> allfeatures = new ArrayList<String>(allFeatures);
     String[] oldRender = renderOrder;
     renderOrder = new String[allfeatures.size()];
-    Object mmrange, fc = null;
+    float[][] mmrange;
+    Object fc;
     boolean initOrders = (featureOrder == null);
     int opos = 0;
     if (oldRender != null && oldRender.length > 0)
@@ -434,13 +470,12 @@ public abstract class FeatureRendererModel implements
         {
           if (initOrders)
           {
-            setOrder(oldRender[j], (1 - (1 + (float) j)
-                    / (float) oldRender.length));
+            setOrder(oldRender[j], (1 - (1 + (float) j) / oldRender.length));
           }
           if (allfeatures.contains(oldRender[j]))
           {
-            renderOrder[opos++] = oldRender[j]; // existing features always
-            // appear below new features
+            renderOrder[opos++] = oldRender[j];
+            // existing features always appear below new features
             allfeatures.remove(oldRender[j]);
             if (minmax != null)
             {
@@ -452,8 +487,8 @@ public abstract class FeatureRendererModel implements
                         && ((GraduatedColor) fc).isAutoScale())
                 {
                   ((GraduatedColor) fc).updateBounds(
-                          ((float[][]) mmrange)[0][0],
-                          ((float[][]) mmrange)[0][1]);
+                          mmrange[0][0],
+                          mmrange[0][1]);
                 }
               }
             }
@@ -484,8 +519,8 @@ public abstract class FeatureRendererModel implements
           if (fc != null && fc instanceof GraduatedColor
                   && ((GraduatedColor) fc).isAutoScale())
           {
-            ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
-                    ((float[][]) mmrange)[0][1]);
+            ((GraduatedColor) fc).updateBounds(mmrange[0][0],
+                    mmrange[0][1]);
           }
         }
       }
@@ -522,6 +557,7 @@ public abstract class FeatureRendererModel implements
    * @param featureType
    * @return java.awt.Color or GraduatedColor
    */
+  @Override
   public Object getFeatureStyle(String featureType)
   {
     Object fc = featureColours.get(featureType);
@@ -602,6 +638,7 @@ public abstract class FeatureRendererModel implements
     return av.getFeaturesDisplayed().isVisible(type);
   }
 
+  @Override
   public void setColour(String featureType, Object col)
   {
     // overwrite
@@ -610,9 +647,18 @@ public abstract class FeatureRendererModel implements
     // Object c = featureColours.get(featureType);
     // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
     // !((GraduatedColor)c).getMaxColor().equals(_col)))
+    if (col instanceof FeatureColourI)
     {
-      featureColours.put(featureType, col);
+      if (((FeatureColourI) col).isGraduatedColour())
+      {
+        col = new GraduatedColor((FeatureColourI) col);
+      }
+      else
+      {
+        col = ((FeatureColourI) col).getColour();
+      }
     }
+      featureColours.put(featureType, col);
   }
 
   public void setTransparency(float value)
@@ -666,9 +712,9 @@ public abstract class FeatureRendererModel implements
   }
 
   @Override
-  public Map getFeatureColours()
+  public Map<String, Object> getFeatureColours()
   {
-    return new ConcurrentHashMap<>(featureColours);
+    return featureColours;
   }
 
   /**
@@ -676,21 +722,32 @@ public abstract class FeatureRendererModel implements
    * 
    * @param data
    *          { String(Type), Colour(Type), Boolean(Displayed) }
+   * @return true if any visible features have been reordered, else false
    */
-  public void setFeaturePriority(Object[][] data)
+  public boolean setFeaturePriority(Object[][] data)
   {
-    setFeaturePriority(data, true);
+    return setFeaturePriority(data, true);
   }
 
   /**
+   * Sets the priority order for features
    * 
    * @param data
    *          { String(Type), Colour(Type), Boolean(Displayed) }
    * @param visibleNew
    *          when true current featureDisplay list will be cleared
+   * @return true if any visible features have been reordered or recoloured,
+   *         else false (i.e. no need to repaint)
    */
-  public void setFeaturePriority(Object[][] data, boolean visibleNew)
+  public boolean setFeaturePriority(Object[][] data, boolean visibleNew)
   {
+    /*
+     * note visible feature ordering and colours before update
+     */
+    List<String> visibleFeatures = getDisplayedFeatureTypes();
+    Map<String, Object> visibleColours = new HashMap<String, Object>(
+            getFeatureColours());
+
     FeaturesDisplayedI av_featuresdisplayed = null;
     if (visibleNew)
     {
@@ -709,10 +766,10 @@ public abstract class FeatureRendererModel implements
     }
     if (data == null)
     {
-      return;
+      return false;
     }
     // The feature table will display high priority
-    // features at the top, but theses are the ones
+    // features at the top, but these are the ones
     // we need to render last, so invert the data
     renderOrder = new String[data.length];
 
@@ -732,6 +789,30 @@ public abstract class FeatureRendererModel implements
       }
     }
 
+    /*
+     * get the new visible ordering and return true if it has changed
+     * order or any colour has changed
+     */
+    List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
+    if (!visibleFeatures.equals(reorderedVisibleFeatures))
+    {
+      /*
+       * the list of ordered visible features has changed
+       */
+      return true;
+    }
+
+    /*
+     * return true if any feature colour has changed
+     */
+    for (String feature : visibleFeatures)
+    {
+      if (visibleColours.get(feature) != getFeatureStyle(feature))
+      {
+        return true;
+      }
+    }
+    return false;
   }
 
   /**
@@ -752,7 +833,7 @@ public abstract class FeatureRendererModel implements
     changeSupport.removePropertyChangeListener(listener);
   }
 
-  public Set getAllFeatureColours()
+  public Set<String> getAllFeatureColours()
   {
     return featureColours.keySet();
   }
@@ -771,8 +852,7 @@ public abstract class FeatureRendererModel implements
   {
     if (renderOrder == null)
     {
-      return Arrays.asList(new String[]
-      {});
+      return Arrays.asList(new String[] {});
     }
     return Arrays.asList(renderOrder);
   }
@@ -799,7 +879,7 @@ public abstract class FeatureRendererModel implements
     }
     if (featureGroups.containsKey(group))
     {
-      return ((Boolean) featureGroups.get(group)).booleanValue();
+      return featureGroups.get(group).booleanValue();
     }
     if (newGroupsVisible)
     {
@@ -825,7 +905,7 @@ public abstract class FeatureRendererModel implements
 
       for (Object grp : featureGroups.keySet())
       {
-        Boolean state = (Boolean) featureGroups.get(grp);
+        Boolean state = featureGroups.get(grp);
         if (state.booleanValue() == visible)
         {
           gp.add(grp);
@@ -854,7 +934,7 @@ public abstract class FeatureRendererModel implements
         featureGroups.put(gst, new Boolean(visible));
         if (st != null)
         {
-          rdrw = rdrw || (visible != ((Boolean) st).booleanValue());
+          rdrw = rdrw || (visible != st.booleanValue());
         }
       }
       if (rdrw)
@@ -888,39 +968,39 @@ public abstract class FeatureRendererModel implements
     return av.getFeaturesDisplayed();
   }
 
+  /**
+   * Returns a (possibly empty) list of visible feature types, in render order
+   * (last is on top)
+   */
   @Override
-  public String[] getDisplayedFeatureTypes()
+  public List<String> getDisplayedFeatureTypes()
   {
-    String[] typ = null;
-    typ = getRenderOrder().toArray(new String[0]);
+    List<String> typ = getRenderOrder();
+    List<String> displayed = new ArrayList<String>();
     FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
     if (feature_disp != null)
     {
       synchronized (feature_disp)
       {
-        for (int i = 0; i < typ.length; i++)
+        for (String type : typ)
         {
-          if (feature_disp.isVisible(typ[i]))
+          if (feature_disp.isVisible(type))
           {
-            typ[i] = null;
+            displayed.add(type);
           }
         }
       }
     }
-    return typ;
+    return displayed;
   }
 
   @Override
-  public String[] getDisplayedFeatureGroups()
+  public List<String> getDisplayedFeatureGroups()
   {
-    String[] gps = null;
-    ArrayList<String> _gps = new ArrayList<String>();
-    Iterator en = getFeatureGroups().iterator();
-    int g = 0;
+    List<String> _gps = new ArrayList<String>();
     boolean valid = false;
-    while (en.hasNext())
+    for (String gp : getFeatureGroups())
     {
-      String gp = (String) en.next();
       if (checkGroupVisibility(gp, false))
       {
         valid = true;
@@ -932,11 +1012,16 @@ public abstract class FeatureRendererModel implements
       }
       else
       {
-        gps = new String[_gps.size()];
-        _gps.toArray(gps);
+        // gps = new String[_gps.size()];
+        // _gps.toArray(gps);
       }
     }
-    return gps;
+    return _gps;
   }
 
+  @Override
+  public boolean isOrdinal(String featureType)
+  {
+    return ordinalFeatures.contains(featureType);
+  }
 }