JAL-2349 fix off by one when resolving columns for sequence associated contact matrix
[jalview.git] / src / jalview / datamodel / Alignment.java
index e1c87a9..517a6dd 100755 (executable)
@@ -1653,8 +1653,10 @@ public class Alignment implements AlignmentI, AutoCloseable
   public Iterable<AlignmentAnnotation> findAnnotations(SequenceI seq,
           String calcId, String label)
   {
-    return AlignmentAnnotation.findAnnotations(
-            Arrays.asList(getAlignmentAnnotation()), seq, calcId, label);
+    return annotations == null ? null
+            : AlignmentAnnotation.findAnnotations(
+                    Arrays.asList(getAlignmentAnnotation()), seq, calcId,
+                    label);
   }
 
   @Override
@@ -2045,9 +2047,47 @@ public class Alignment implements AlignmentI, AutoCloseable
   }
 
   @Override
+  public ContactMatrixI getContactMatrixFor(AlignmentAnnotation _aa)
+  {
+    ContactMatrixI cm = cmholder.getContactMatrixFor(_aa);
+    if (cm==null && _aa.groupRef!=null)
+    {
+      cm = _aa.groupRef.getContactMatrixFor(_aa);
+    }
+    if (cm==null && _aa.sequenceRef!=null)
+    {
+      cm = _aa.sequenceRef.getContactMatrixFor(_aa);
+      if (cm==null)
+      {
+        // TODO fix up this logic and unify with getContactListFor
+        cm = _aa.sequenceRef.getDatasetSequence().getContactMatrixFor(_aa);
+      }
+    }
+    return cm;
+  }
+
+  @Override
   public ContactListI getContactListFor(AlignmentAnnotation _aa, int column)
   {
-    return cmholder.getContactListFor(_aa, column);
+    ContactListI cl = cmholder.getContactListFor(_aa, column);
+    if (cl == null && _aa.groupRef != null)
+    {
+      cl = _aa.groupRef.getContactListFor(_aa, column);
+    }
+    if (cl == null && _aa.sequenceRef != null)
+    {
+      int spos = _aa.sequenceRef.findPosition(column);
+      if (spos >= _aa.sequenceRef.getStart()
+              && spos <= 1 + _aa.sequenceRef.getEnd())
+      {
+        cl = _aa.sequenceRef.getContactListFor(_aa, spos-_aa.sequenceRef.getStart());
+        if (cl == null && _aa.sequenceRef.getDatasetSequence() != null)
+        {
+          _aa.sequenceRef.getDatasetSequence().getContactListFor(_aa, spos-_aa.sequenceRef.getStart());
+        }
+      }
+    }
+    return cl;
   }
 
   @Override
@@ -2065,4 +2105,12 @@ public class Alignment implements AlignmentI, AutoCloseable
     addAnnotation(aa);
     return aa;
   }
+
+  @Override
+  public void addContactListFor(AlignmentAnnotation annotation,
+          ContactMatrixI cm)
+  {
+    cmholder.addContactListFor(annotation, cm);
+
+  }
 }