JAL-4027 getter for contact matrices on the Alignment.
[jalview.git] / src / jalview / datamodel / Alignment.java
index ac00fa2..0aa8424 100755 (executable)
  */
 package jalview.datamodel;
 
-import jalview.analysis.AlignmentUtils;
-import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
-import jalview.io.FastaFile;
-import jalview.util.Comparison;
-import jalview.util.LinkedIdentityHashSet;
-import jalview.util.MessageManager;
-
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.BitSet;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Iterator;
@@ -40,6 +35,13 @@ import java.util.Map;
 import java.util.Set;
 import java.util.Vector;
 
+import jalview.analysis.AlignmentUtils;
+import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
+import jalview.io.FastaFile;
+import jalview.util.Comparison;
+import jalview.util.LinkedIdentityHashSet;
+import jalview.util.MessageManager;
+
 /**
  * Data structure to hold and manipulate a multiple sequence alignment
  */
@@ -47,7 +49,7 @@ import java.util.Vector;
  * @author JimP
  * 
  */
-public class Alignment implements AlignmentI
+public class Alignment implements AlignmentI, AutoCloseable
 {
   private Alignment dataset;
 
@@ -195,6 +197,7 @@ public class Alignment implements AlignmentI
   {
     synchronized (sequences)
     {
+
       if (i > -1 && i < sequences.size())
       {
         return sequences.get(i);
@@ -302,15 +305,20 @@ public class Alignment implements AlignmentI
   }
 
   @Override
-  public void finalize() throws Throwable
+  public void close()
   {
     if (getDataset() != null)
     {
-      getDataset().removeAlignmentRef();
+      try
+      {
+        getDataset().removeAlignmentRef();
+      } catch (Throwable e)
+      {
+        e.printStackTrace();
+      }
     }
 
     nullReferences();
-    super.finalize();
   }
 
   /**
@@ -589,11 +597,12 @@ public class Alignment implements AlignmentI
     int i = 0;
     SequenceI sq = null;
     String sqname = null;
+    int nseq = sequences.size();
     if (startAfter != null)
     {
       // try to find the sequence in the alignment
       boolean matched = false;
-      while (i < sequences.size())
+      while (i < nseq)
       {
         if (getSequenceAt(i++) == startAfter)
         {
@@ -606,7 +615,7 @@ public class Alignment implements AlignmentI
         i = 0;
       }
     }
-    while (i < sequences.size())
+    while (i < nseq)
     {
       sq = getSequenceAt(i);
       sqname = sq.getName();
@@ -709,7 +718,7 @@ public class Alignment implements AlignmentI
   public int getWidth()
   {
     int maxLength = -1;
-  
+
     for (int i = 0; i < sequences.size(); i++)
     {
       maxLength = Math.max(maxLength, getSequenceAt(i).getLength());
@@ -1188,7 +1197,8 @@ public class Alignment implements AlignmentI
     int maxLength = -1;
 
     SequenceI current;
-    for (int i = 0; i < sequences.size(); i++)
+    int nseq = sequences.size();
+    for (int i = 0; i < nseq; i++)
     {
       current = getSequenceAt(i);
       for (int j = current.getLength(); j > maxLength; j--)
@@ -1205,7 +1215,7 @@ public class Alignment implements AlignmentI
     maxLength++;
 
     int cLength;
-    for (int i = 0; i < sequences.size(); i++)
+    for (int i = 0; i < nseq; i++)
     {
       current = getSequenceAt(i);
       cLength = current.getLength();
@@ -2024,4 +2034,53 @@ public class Alignment implements AlignmentI
     }
   }
 
+  Map<Object, ContactMatrixI> contactmaps = new HashMap<>();
+
+  @Override
+  public Collection<ContactMatrixI> getContactMaps()
+  {
+    if (contactmaps != null && contactmaps.size() > 0)
+    {
+      return contactmaps.values();
+    }
+    return Collections.EMPTY_LIST;
+  }
+
+  @Override
+  public ContactListI getContactListFor(AlignmentAnnotation _aa, int column)
+  {
+    ContactMatrixI cm = contactmaps.get(_aa.annotationId);
+    if (cm == null)
+    {
+      return null;
+    }
+    return cm.getContactList(column);
+  }
+
+  @Override
+  public AlignmentAnnotation addContactList(ContactMatrixI cm)
+  {
+    Annotation _aa[] = new Annotation[getWidth()];
+    Annotation dummy = new Annotation(0.0f);
+    for (int i = 0; i < _aa.length; _aa[i++] = dummy)
+    {
+      ;
+    }
+    AlignmentAnnotation aa = new AlignmentAnnotation("Contact Matrix",
+            "Contact Matrix", _aa);
+    aa.graph = AlignmentAnnotation.CUSTOMRENDERER;
+    aa.graphMin = cm.getMin();
+    aa.graphMax = cm.getMax();
+    aa.editable = false;
+    // aa.autoCalculated = true;
+    contactmaps.put(aa.annotationId, cm);
+    // TODO: contact matrices could be intra or inter - more than one refseq
+    // possible!
+    if (cm.hasReferenceSeq())
+    {
+      aa.setSequenceRef(cm.getReferenceSeq());
+    }
+    addAnnotation(aa);
+    return aa;
+  }
 }