JAL-4343 fix insufficient-caffeine error - add whats new for 2.11.3.1
[jalview.git] / src / jalview / datamodel / ContactMatrix.java
index 8434b4a..b8593d8 100644 (file)
@@ -1,15 +1,33 @@
+/*
+ * 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.datamodel;
 
-import java.awt.Color;
 import java.util.ArrayList;
-import java.util.BitSet;
-import java.util.HashMap;
 import java.util.List;
 import java.util.StringTokenizer;
 
 import jalview.bin.Console;
 
-public abstract class ContactMatrix implements ContactMatrixI
+public abstract class ContactMatrix extends GroupSetHolder
+        implements ContactMatrixI
 {
   /**
    * are contacts reflexive ?
@@ -105,26 +123,7 @@ public abstract class ContactMatrix implements ContactMatrixI
       @Override
       public double getContactAt(int column)
       {
-        List<Float> clist;
-        Float cl = null;
-        if (symmetric)
-        {
-          if (p < column)
-          {
-            clist = contacts.get(p);
-            cl = clist.get(column);
-          }
-          else
-          {
-            clist = contacts.get(column);
-            cl = clist.get(p);
-          }
-        }
-        else
-        {
-          clist = contacts.get(p);
-          cl = clist.get(column);
-        }
+        Float cl = getFloatElementAt(column, p);
         if (cl == null)
         {
           // return 0 not NaN ?
@@ -135,30 +134,53 @@ public abstract class ContactMatrix implements ContactMatrixI
     });
   }
 
-  @Override
-  public float getMin()
+  private Float getFloatElementAt(int column, int p)
   {
-    return min;
+
+    List<Float> clist;
+    Float cl = null;
+    if (symmetric)
+    {
+      if (p < column)
+      {
+        clist = contacts.get(p);
+        cl = clist.get(column);
+      }
+      else
+      {
+        clist = contacts.get(column);
+        cl = clist.get(p);
+      }
+    }
+    else
+    {
+      clist = contacts.get(p);
+      cl = clist.get(column);
+    }
+    return cl;
   }
 
   @Override
-  public float getMax()
+  public double getElementAt(int column, int row)
   {
-    return max;
+    Float cl = getFloatElementAt(column, row);
+    if (cl != null)
+    {
+      return cl;
+    }
+    throw (new RuntimeException("Out of Bounds " + column + "," + row));
   }
 
   @Override
-  public boolean hasReferenceSeq()
+  public float getMin()
   {
-    // TODO Auto-generated method stub
-    return false;
+    return min;
   }
 
   @Override
-  public SequenceI getReferenceSeq()
+  public float getMax()
   {
-    // TODO Auto-generated method stub
-    return null;
+    return max;
   }
 
   @Override
@@ -173,76 +195,29 @@ public abstract class ContactMatrix implements ContactMatrixI
     return "Contact Matrix";
   }
 
-  List<BitSet> groups = null;
-
-  @Override
-  public void updateGroups(List<BitSet> colGroups)
-  {
-    groups = colGroups;
-    colorMap = new HashMap<>();
-  }
-
-  @Override
-  public boolean hasGroups()
-  {
-    return groups != null && groups.size() > 0;
-  }
-
-  @Override
-  public List<BitSet> getGroups()
-  {
-    return groups;
-  }
-
-  @Override
-  public BitSet getGroupsFor(int column)
-  {
-    for (BitSet gp : groups)
-    {
-      if (gp.get(column))
-      {
-        return gp;
-      }
-    }
-    return ContactMatrixI.super.getGroupsFor(column);
-  }
-
-  HashMap<BitSet, Color> colorMap = new HashMap<>();
-
-  @Override
-  public Color getColourForGroup(BitSet bs)
-  {
-    if (bs == null)
-    {
-      return Color.white;
-    }
-    Color groupCol = colorMap.get(bs);
-    if (groupCol == null)
-    {
-      return Color.white;
-    }
-    return groupCol;
-  }
-
-  @Override
-  public void setColorForGroup(BitSet bs, Color color)
-  {
-    colorMap.put(bs, color);
-  }
-
   public static String contactToFloatString(ContactMatrixI cm)
   {
     StringBuilder sb = new StringBuilder();
     for (int c = 0; c < cm.getWidth(); c++)
     {
       ContactListI cl = cm.getContactList(c);
+      long lastsb = -1;
       if (cl != null)
       {
         for (int h = 0; h <= cl.getContactHeight(); h++)
         {
           if (sb.length() > 0)
           {
-            sb.append('\t');
+            if (sb.length() - lastsb > 320)
+            {
+              // newline
+              sb.append('\n');
+              lastsb = sb.length();
+            }
+            else
+            {
+              sb.append('\t');
+            }
           }
           sb.append(cl.getContactAt(h));
         }
@@ -255,9 +230,8 @@ public abstract class ContactMatrix implements ContactMatrixI
           int rows)
   {
     float[][] vals = new float[cols][rows];
-    StringTokenizer tabsep = new StringTokenizer(values, "" + '\t');
+    StringTokenizer tabsep = new StringTokenizer(values, "" + '\t' + '\n');
     int c = 0, r = 0;
-
     while (tabsep.hasMoreTokens())
     {
       double elem = Double.valueOf(tabsep.nextToken());
@@ -269,7 +243,6 @@ public abstract class ContactMatrix implements ContactMatrixI
       }
       if (c >= vals.length)
       {
-
         break;
       }
     }
@@ -278,7 +251,6 @@ public abstract class ContactMatrix implements ContactMatrixI
       Console.warn(
               "Ignoring additional elements for Float string to contact matrix parsing.");
     }
-
     return vals;
   }
 }