Merge branch 'alpha/JAL-3362_Jalview_212_alpha' into alpha/merge_212_JalviewJS_2112
[jalview.git] / src / jalview / datamodel / Alignment.java
index 3ae46b8..3d81e32 100755 (executable)
@@ -47,14 +47,12 @@ import java.util.Vector;
  * @author JimP
  * 
  */
-public class Alignment implements AlignmentI
+public class Alignment implements AlignmentI, AutoCloseable
 {
   private Alignment dataset;
 
   private List<SequenceI> sequences;
 
-  private SequenceI hmmConsensus;
-
   protected List<SequenceGroup> groups;
 
   protected char gapCharacter = '-';
@@ -197,6 +195,7 @@ public class Alignment implements AlignmentI
   {
     synchronized (sequences)
     {
+    
       if (i > -1 && i < sequences.size())
       {
         return sequences.get(i);
@@ -330,15 +329,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();
   }
 
   /**
@@ -617,11 +621,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)
         {
@@ -634,7 +639,7 @@ public class Alignment implements AlignmentI
         i = 0;
       }
     }
-    while (i < sequences.size())
+    while (i < nseq)
     {
       sq = getSequenceAt(i);
       sqname = sq.getName();
@@ -740,39 +745,21 @@ public class Alignment implements AlignmentI
   
     for (int i = 0; i < sequences.size(); i++)
     {
-      if (getSequenceAt(i).getLength() > maxLength)
-      {
-        maxLength = getSequenceAt(i).getLength();
-      }
+      maxLength = Math.max(maxLength, getSequenceAt(i).getLength());
     }
-  
     return maxLength;
   }
-  /*
+
   @Override
-  public int getWidth()
+  public int getVisibleWidth()
   {
-    final Wrapper temp = new Wrapper();
-  
-    forEachSequence(new Consumer<SequenceI>()
+    int w = getWidth();
+    if (hiddenCols != null)
     {
-      @Override
-      public void accept(SequenceI s)
-      {
-        if (s.getLength() > temp.inner)
-        {
-          temp.inner = s.getLength();
-        }
-      }
-    }, 0, sequences.size() - 1);
-  
-    return temp.inner;
+      w -= hiddenCols.getSize();
+    }
+    return w;
   }
-  
-  public static class Wrapper
-  {
-    public int inner;
-  }*/
 
   /**
    * DOCUMENT ME!
@@ -1234,7 +1221,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--)
@@ -1251,7 +1239,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();
@@ -1643,34 +1631,50 @@ public class Alignment implements AlignmentI
           String calcId, boolean autoCalc, SequenceI seqRef,
           SequenceGroup groupRef)
   {
-    if (annotations != null)
+    AlignmentAnnotation annot = annotations == null ? null
+            : AlignmentAnnotation.findFirstAnnotation(
+              Arrays.asList(getAlignmentAnnotation()), name, calcId,
+              autoCalc, seqRef, groupRef);
+
+    if (annot == null)
     {
-      for (AlignmentAnnotation annot : getAlignmentAnnotation())
+
+      annot = new AlignmentAnnotation(name, name, new Annotation[1], 0f, 0f,
+              AlignmentAnnotation.BAR_GRAPH);
+      annot.hasText = false;
+      if (calcId != null)
       {
-        if (annot.autoCalculated == autoCalc && (name.equals(annot.label))
-                && (calcId == null || annot.getCalcId().equals(calcId))
-                && annot.sequenceRef == seqRef
-                && annot.groupRef == groupRef)
-        {
-          return annot;
-        }
+        annot.setCalcId(calcId);
       }
+      annot.autoCalculated = autoCalc;
+      if (seqRef != null)
+      {
+        annot.setSequenceRef(seqRef);
+      }
+      annot.groupRef = groupRef;
+      addAnnotation(annot);
     }
-    AlignmentAnnotation annot = new AlignmentAnnotation(name, name,
-            new Annotation[1], 0f, 0f, AlignmentAnnotation.BAR_GRAPH);
-    annot.hasText = false;
-    if (calcId != null)
+    return annot;
+  }
+
+
+  @Override
+  public AlignmentAnnotation updateFromOrCopyAnnotation(
+          AlignmentAnnotation ala)
+  {
+    AlignmentAnnotation annot = AlignmentAnnotation.findFirstAnnotation(
+            Arrays.asList(getAlignmentAnnotation()), ala.label, ala.calcId,
+            ala.autoCalculated, ala.sequenceRef, ala.groupRef);
+    if (annot == null)
     {
-      annot.setCalcId(new String(calcId));
+      annot = new AlignmentAnnotation(ala);
+      addAnnotation(annot);
     }
-    annot.autoCalculated = autoCalc;
-    if (seqRef != null)
+    else
     {
-      annot.setSequenceRef(seqRef);
+      annot.updateAlignmentAnnotationFrom(ala);
     }
-    annot.groupRef = groupRef;
-    addAnnotation(annot);
-
+    validateAnnotation(annot);
     return annot;
   }
 
@@ -1950,21 +1954,12 @@ public class Alignment implements AlignmentI
   }
 
   @Override
-  public void setHiddenColumns(HiddenColumns cols)
+  public boolean setHiddenColumns(HiddenColumns cols)
   {
+    boolean changed = cols == null ? hiddenCols != null
+            : !cols.equals(hiddenCols);
     hiddenCols = cols;
-  }
-
-  @Override
-  public SequenceI getHmmConsensus()
-  {
-    return hmmConsensus;
-  }
-
-  @Override
-  public void setHmmConsensus(SequenceI hmmConsensus)
-  {
-    this.hmmConsensus = hmmConsensus;
+    return changed;
   }
 
   @Override
@@ -2078,4 +2073,19 @@ public class Alignment implements AlignmentI
       }
     }
   }
+
+  @Override
+  public List<SequenceI> getHmmSequences()
+  {
+    List<SequenceI> result = new ArrayList<>();
+    for (int i = 0; i < sequences.size(); i++)
+    {
+      SequenceI seq = sequences.get(i);
+      if (seq.hasHMMProfile())
+      {
+        result.add(seq);
+      }
+    }
+    return result;
+  }
 }