JAL-2349 Abstract logic for colouring and computing over a range of contacts on a...
authorJim Procter <jprocter@issues.jalview.org>
Mon, 23 Jan 2017 14:37:56 +0000 (14:37 +0000)
committerJim Procter <jprocter@issues.jalview.org>
Mon, 23 Jan 2017 14:37:56 +0000 (14:37 +0000)
src/jalview/datamodel/ContactListI.java
src/jalview/datamodel/ContactListImpl.java [new file with mode: 0644]
src/jalview/datamodel/ContactListProviderI.java [new file with mode: 0644]
src/jalview/datamodel/ContactMatrix.java
src/jalview/datamodel/SeqDistanceContactMatrix.java

index 84fcb7c..72dbf2e 100644 (file)
@@ -1,31 +1,16 @@
 package jalview.datamodel;
 
-import java.awt.Color;
 
-public interface ContactListI
+public interface ContactListI extends ContactListProviderI
 {
 
-  int getColumnWidth();
-
-  int getContactHeight();
-
-  Color getColorForScore(int column);
-
   /**
-   * return colour representing contacts from i through to j for this site
+   * return bounds for range
    * 
    * @param from_column
    * @param to_column
-   * @return
-   */
-  Color getColorForRange(int from_column, int to_column);
-  
-  /**
-   * get a value representing contact at column for this site
-   * 
-   * @param column
-   * @return
+   * @return double[] { min, max,
    */
-  double getContactAt(int column);
+  ContactRange getRangeFor(int from_column, int to_column);
 
 }
diff --git a/src/jalview/datamodel/ContactListImpl.java b/src/jalview/datamodel/ContactListImpl.java
new file mode 100644 (file)
index 0000000..6eb4cdb
--- /dev/null
@@ -0,0 +1,92 @@
+package jalview.datamodel;
+
+/**
+ * helper class to compute min/max/mean for a range on a contact list
+ * 
+ * @author jprocter
+ *
+ */
+public class ContactListImpl implements ContactListI
+{
+  ContactListProviderI clist;
+
+
+  public static ContactListI newContactList(ContactListProviderI list)
+  {
+    return new ContactListImpl(list);
+  }
+
+  public ContactListImpl(ContactListProviderI list)
+  {
+    clist = list;
+  }
+
+  @Override
+  public double getContactAt(int column)
+  {
+    return clist.getContactAt(column);
+  }
+
+  @Override
+  public int getContactHeight()
+  {
+    return clist.getContactHeight();
+  }
+
+  @Override
+  public ContactRange getRangeFor(int from_column, int to_column)
+  {
+    if (clist instanceof ContactListI)
+    {
+      // clist may implement getRangeFor in a more efficient way, so use theirs
+      return ((ContactListI) clist).getRangeFor(from_column, to_column);
+    }
+    if (from_column < 0)
+    {
+      from_column = 0;
+    }
+    if (to_column > getContactHeight())
+    {
+      to_column = getContactHeight();
+    }
+    ContactRange cr = new ContactRange();
+    cr.setFrom_column(from_column);
+    cr.setTo_column(to_column);
+    double tot = 0;
+    for (int i = from_column; i < to_column; i++)
+    {
+      double contact = getContactAt(i);
+      tot += contact;
+      if (i == from_column)
+      {
+        cr.setMin(contact);
+        cr.setMax(contact);
+        cr.setMinPos(i);
+        cr.setMaxPos(i);
+      }
+      else
+      {
+        if (cr.getMax() < contact)
+        {
+          cr.setMax(contact);
+          cr.setMaxPos(i);
+        }
+        if (cr.getMin() < contact)
+        {
+          cr.setMin(contact);
+          cr.setMinPos(i);
+        }
+      }
+    }
+    if (tot > 0)
+    {
+      cr.setMean(tot / (to_column - from_column));
+    }
+    else
+    {
+      cr.setMean(tot);
+    }
+    return cr;
+  }
+
+}
diff --git a/src/jalview/datamodel/ContactListProviderI.java b/src/jalview/datamodel/ContactListProviderI.java
new file mode 100644 (file)
index 0000000..37c4e07
--- /dev/null
@@ -0,0 +1,22 @@
+package jalview.datamodel;
+
+public interface ContactListProviderI
+{
+
+  /**
+   * dimension of list where getContactAt(column<getContactHeight()) may return
+   * a value
+   * 
+   * @return
+   */
+  int getContactHeight();
+
+  /**
+   * get a value representing contact at column for this site
+   * 
+   * @param column
+   * @return Double.NaN or a contact strength for this site
+   */
+  double getContactAt(int column);
+
+}
index fac1dee..baf6e27 100644 (file)
@@ -2,7 +2,6 @@ package jalview.datamodel;
 
 import jalview.ws.params.InvalidArgumentException;
 
-import java.awt.Color;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -74,13 +73,7 @@ public class ContactMatrix implements ContactMatrixI
     }
   }
 
-  Color minColor = Color.white, maxColor = Color.magenta;
 
-  Color shadeFor(float value)
-  {
-    return jalview.util.ColorUtils.getGraduatedColour(value, 0,
-            Color.white, max, Color.magenta);
-  }
   @Override
   public ContactListI getContactList(final int column)
   {
@@ -88,41 +81,12 @@ public class ContactMatrix implements ContactMatrixI
     {
       return null;
     }
-    return new ContactListI()
+
+    return new ContactListImpl(new ContactListProviderI()
     {
       int p = column;
 
       @Override
-      public Color getColorForScore(int column)
-      {
-
-        return shadeFor((float) getContactAt(column));
-      }
-
-      @Override
-      public Color getColorForRange(int i, int j)
-      {
-        double contc;
-        double v = 0;
-        for (int r=i;r<j;r++)
-        {
-          contc = getContactAt(r);
-          if (contc != contc)
-          {
-            v += contc;
-          }
-        }
-        // average for moment - probably more interested in maxIntProj though
-        return shadeFor(((float) v) / (j - i + 1));
-      }
-
-      @Override
-      public int getColumnWidth()
-      {
-        return 1;
-      }
-
-      @Override
       public int getContactHeight()
       {
         return width;
@@ -159,7 +123,7 @@ public class ContactMatrix implements ContactMatrixI
         }
         return cl.doubleValue();
       }
-    };
+    });
   }
 
 }
index ee7de5e..721e5ca 100644 (file)
@@ -1,7 +1,5 @@
 package jalview.datamodel;
 
-import java.awt.Color;
-
 /**
  * Dummy contact matrix based on sequence distance
  * 
@@ -19,30 +17,29 @@ public class SeqDistanceContactMatrix implements ContactMatrixI
   @Override
   public ContactListI getContactList(final int column)
   {
-    if (column<0 || column >= width)
+    if (column < 0 || column >= width)
     {
       return null;
     }
-    return new ContactListI() {
+    return new ContactListImpl(new ContactListProviderI()
+    {
+
       int p = column;
-      @Override
-      public Color getColorForScore(int column)
-      {
-        return jalview.util.ColorUtils.getGraduatedColour(Math.abs(column-p), 0, Color.white, width, Color.magenta);
-      }
-      @Override
-      public Color getColorForRange(int from_column, int to_column)
-      {
-        return jalview.util.ColorUtils.getGraduatedColour(
-                Math.abs(to_column + from_column - 2 * p) / 2, 0, Color.white, width,
-                Color.magenta);
-      }
 
-      @Override
-      public int getColumnWidth()
-      {
-        return 1;
-      }
+      // @Override
+      // public Color getColorForScore(int column)
+      // {
+      // return jalview.util.ColorUtils.getGraduatedColour(Math.abs(column-p),
+      // 0, Color.white, width, Color.magenta);
+      // }
+      // @Override
+      // public Color getColorForRange(int from_column, int to_column)
+      // {
+      // return jalview.util.ColorUtils.getGraduatedColour(
+      // Math.abs(to_column + from_column - 2 * p) / 2, 0, Color.white, width,
+      // Color.magenta);
+      // }
+
       @Override
       public int getContactHeight()
       {
@@ -55,7 +52,7 @@ public class SeqDistanceContactMatrix implements ContactMatrixI
       {
         return Math.abs(column - p);
       }
-    };
+    });
   }
 
 }