Merge branch 'develop' into patch/JAL-3088_finalbracenotinrightplace
[jalview.git] / src / jalview / ws / datamodel / alphafold / MappableContactMatrix.java
index 9a01738..5f27a2c 100644 (file)
@@ -7,6 +7,7 @@ import java.util.BitSet;
 import jalview.datamodel.ContactListI;
 import jalview.datamodel.ContactListImpl;
 import jalview.datamodel.ContactListProviderI;
+import jalview.datamodel.ContactMatrixI;
 import jalview.datamodel.GroupSet;
 import jalview.datamodel.GroupSetI;
 import jalview.datamodel.Mapping;
@@ -17,42 +18,77 @@ import jalview.ws.datamodel.MappableContactMatrixI;
 public abstract class MappableContactMatrix<T extends MappableContactMatrix<T>>
         implements MappableContactMatrixI
 {
-  SequenceI refSeq = null;
-
-  MapList toSeq = null;
-
   /**
-   * the length that refSeq is expected to be (excluding gaps, of course)
+   * the matrix that is being mapped to
    */
-  int length;
+  protected ContactMatrixI mappedMatrix=null;
+  
+  public ContactListI getContactList(int column)
+  {
+    return mappedMatrix.getContactList(column);
+  }
+
+  public float getMin()
+  {
+    return mappedMatrix.getMin();
+  }
+
+  public float getMax()
+  {
+    return mappedMatrix.getMax();
+  }
+
+  public int getWidth()
+  {
+    return mappedMatrix.getWidth();
+  }
+
+  public int getHeight()
+  {
+    return mappedMatrix.getHeight();
+  }
 
   @Override
-  public boolean hasReferenceSeq()
+  public ContactMatrixI getMappedMatrix()
   {
-    return (refSeq != null);
+    return mappedMatrix;
   }
+  
+  @Override
+  public GroupSetI getGroupSet()
+  {
+    return mappedMatrix.getGroupSet();
+  };
 
   @Override
-  public SequenceI getReferenceSeq()
+  public void setGroupSet(GroupSet makeGroups)
   {
-    return refSeq;
+    mappedMatrix.setGroupSet(makeGroups);
   }
+  
+  /**
+   * the sequence and how it is mapped to the matrix
+   */
+
+  SequenceI refSeq = null;
+
+  MapList toSeq = null;
 
   /**
-   * container for groups - defined on matrix columns
+   * the length that refSeq is expected to be (excluding gaps, of course)
    */
-  GroupSet grps = new GroupSet();
+  int length;
 
   @Override
-  public GroupSetI getGroupSet()
+  public boolean hasReferenceSeq()
   {
-    return grps;
-  };
+    return (refSeq != null);
+  }
 
   @Override
-  public void setGroupSet(GroupSet makeGroups)
+  public SequenceI getReferenceSeq()
   {
-    grps = makeGroups;
+    return refSeq;
   }
 
   @Override
@@ -186,6 +222,7 @@ public abstract class MappableContactMatrix<T extends MappableContactMatrix<T>>
     return getMappedPositionsFor(localFrame, column, column);
   }
 
+  @Override
   public int[] getMappedPositionsFor(final SequenceI localFrame, int from,
           int to)
   {
@@ -193,21 +230,9 @@ public abstract class MappableContactMatrix<T extends MappableContactMatrix<T>>
     {
       throw new Error("Unimplemented when no local sequence given.");
     }
-    // return a ContactListI for column
-    // column is index into localFrame
-    // 1. map column to corresponding column in matrix
-
-    final int _lcolumn = localFrame.findPosition(from);
-    final int _rcolumn = (from == to) ? _lcolumn
-            : localFrame.findPosition(to);
-    if (toSeq == null)
-    {
-      // no mapping
-      return new int[] { _lcolumn, _rcolumn };
-    }
-
     SequenceI lf = localFrame, uf = refSeq;
 
+    // check that localFrame is derived from refSeq
     // just look for dataset sequences and check they are the same.
     // in future we could use DBRefMappings/whatever.
     while (lf.getDatasetSequence() != null
@@ -229,26 +254,45 @@ public abstract class MappableContactMatrix<T extends MappableContactMatrix<T>>
               + "' is not mappable for the given localFrame sequence. ("
               + localFrame.getName() + ")");
     }
+    
+    // now look up from-to matrix columns in toSeq frame
+    
+    if (toSeq == null)
+    {
+      // no mapping - so we assume 1:1
+      return new int[] { from, to };
+    }
+    // from-to are matrix columns
+    // first locate on reference sequence
 
-    int[] mappedPositions = toSeq.locateInFrom(_lcolumn, _rcolumn);
-    // TODO - trim to localFrame ?
-    // if (mappedPositions!=null) {
-    // int s=-1,e=-1;
-    // for (int p=0;p<mappedPositions.length;p++)
-    // {
-    // if (s==-1 && mappedPositions[p]>=localFrame.getStart())
-    // {
-    // s=p; // remember first position within local frame
-    // }
-    // if (e==-1 || mappedPositions[p]<=localFrame.getEnd())
-    // {
-    // // update end pointer
-    // e=p;
-    // // compute local map
-    // mappedPositions[p] = localFrame.findIndex(mappedPositions[p]);
-    // }
-    // }
-    // }
+    int[] mappedPositions = toSeq.locateInFrom(from, to);
+    if (mappedPositions==null)
+    {
+      return null;
+    }
+    
+    // and now map to localFrame
+    // from-to columns on the associated sequence should be
+    // i. restricted to positions in localFrame
+    // ii. 
+
+//    int s = -1, e = -1;
+//    for (int p = 0; p < mappedPositions.length; p++)
+//    {
+//      if (s == -1 && mappedPositions[p] >= localFrame.getStart())
+//      {
+//        s = p; // remember first position within local frame
+//      }
+//      if (e == -1 || mappedPositions[p] <= localFrame.getEnd())
+//      {
+//        // update end pointer
+//        e = p;
+//        // compute local map
+//        mappedPositions[p] = localFrame.findIndex(mappedPositions[p]);
+//      }
+//    }
+//    int[] _trimmed = new int[e - s + 1];
+//    return _trimmed;
     return mappedPositions;
   }
 
@@ -431,7 +475,7 @@ public abstract class MappableContactMatrix<T extends MappableContactMatrix<T>>
   }
 
   /**
-   * get a specific element of the contact matrix in its data-local coordinates
+   * get a specific element of the underlying contact matrix in its data-local coordinates
    * rather than the mapped frame. Implementations are allowed to throw
    * RunTimeExceptions if _column/i are out of bounds
    * 
@@ -439,6 +483,29 @@ public abstract class MappableContactMatrix<T extends MappableContactMatrix<T>>
    * @param i
    * @return
    */
-  protected abstract double getElementAt(int _column, int i);
+  public double getElementAt(int _column, int i) {
+    return mappedMatrix.getElementAt(_column, i);
+  }
+
+  @Override
+  public int hashCode()
+  {
+    return 7 * (refSeq != null ? refSeq.hashCode() : 0)
+            + 11 * (toSeq != null ? toSeq.hashCode() : 0)
+            + 13 * (mappedMatrix != null ? mappedMatrix.hashCode() : 0)
+            + length * 3;
+  }
 
+  @Override
+  public boolean equals(Object obj)
+  {
+    if (obj == null || !(obj.getClass().equals(getClass())))
+    {
+      return false;
+    }
+    T them = (T) obj;
+    return mappedMatrix == them.mappedMatrix && length == them.length
+            && refSeq == them.refSeq && toSeq.equals(them.toSeq);
+
+  }
 }