JAL-2778 Revert back to old synchronisation
[jalview.git] / src / jalview / datamodel / Alignment.java
index 5d91b36..8b74129 100755 (executable)
@@ -48,7 +48,7 @@ public class Alignment implements AlignmentI
 {
   private Alignment dataset;
 
-  protected List<SequenceI> sequences;
+  private List<SequenceI> sequences;
 
   protected List<SequenceGroup> groups;
 
@@ -143,9 +143,8 @@ public class Alignment implements AlignmentI
    */
   public static AlignmentI createAlignment(CigarArray compactAlignment)
   {
-    throw new Error(
-            MessageManager
-                    .getString("error.alignment_cigararray_not_implemented"));
+    throw new Error(MessageManager
+            .getString("error.alignment_cigararray_not_implemented"));
     // this(compactAlignment.refCigars);
   }
 
@@ -188,7 +187,6 @@ public class Alignment implements AlignmentI
     return AlignmentUtils.getSequencesByName(this);
   }
 
-
   @Override
   public SequenceI getSequenceAt(int i)
   {
@@ -199,6 +197,7 @@ public class Alignment implements AlignmentI
         return sequences.get(i);
       }
     }
+
     return null;
   }
 
@@ -475,7 +474,9 @@ public class Alignment implements AlignmentI
       return;
     }
     // remove annotation very quickly
-    AlignmentAnnotation[] t, todelete = new AlignmentAnnotation[annotations.length], tokeep = new AlignmentAnnotation[annotations.length];
+    AlignmentAnnotation[] t,
+            todelete = new AlignmentAnnotation[annotations.length],
+            tokeep = new AlignmentAnnotation[annotations.length];
     int i, p, k;
     if (gp == null)
     {
@@ -608,7 +609,7 @@ public class Alignment implements AlignmentI
       sqname = sq.getName();
       if (sqname.equals(token) // exact match
               || (b && // allow imperfect matches - case varies
-              (sqname.equalsIgnoreCase(token))))
+                      (sqname.equalsIgnoreCase(token))))
       {
         return getSequenceAt(i);
       }
@@ -689,7 +690,6 @@ public class Alignment implements AlignmentI
     return -1;
   }
 
-
   @Override
   public int getHeight()
   {
@@ -706,7 +706,7 @@ public class Alignment implements AlignmentI
   public int getWidth()
   {
     int maxLength = -1;
-
+  
     for (int i = 0; i < sequences.size(); i++)
     {
       if (getSequenceAt(i).getLength() > maxLength)
@@ -714,9 +714,34 @@ public class Alignment implements AlignmentI
         maxLength = getSequenceAt(i).getLength();
       }
     }
-
+  
     return maxLength;
   }
+  /*
+  @Override
+  public int getWidth()
+  {
+    final Wrapper temp = new Wrapper();
+  
+    forEachSequence(new Consumer<SequenceI>()
+    {
+      @Override
+      public void accept(SequenceI s)
+      {
+        if (s.getLength() > temp.inner)
+        {
+          temp.inner = s.getLength();
+        }
+      }
+    }, 0, sequences.size() - 1);
+  
+    return temp.inner;
+  }
+  
+  public static class Wrapper
+  {
+    public int inner;
+  }*/
 
   /**
    * DOCUMENT ME!
@@ -1067,21 +1092,18 @@ public class Alignment implements AlignmentI
         currentSeq = currentSeq.createDatasetSequence();
       }
     }
-    if (seqs.contains(currentSeq))
-    {
-      return;
-    }
+
     List<SequenceI> toProcess = new ArrayList<>();
     toProcess.add(currentSeq);
     while (toProcess.size() > 0)
     {
       // use a queue ?
       SequenceI curDs = toProcess.remove(0);
-      if (seqs.contains(curDs))
+
+      if (!seqs.add(curDs))
       {
         continue;
       }
-      seqs.add(curDs);
       // iterate over database references, making sure we add forward referenced
       // sequences
       if (curDs.getDBRefs() != null)
@@ -1099,10 +1121,9 @@ public class Alignment implements AlignmentI
             }
             if (dbr.getMap().getTo().getDatasetSequence() != null)
             {
-              throw new Error(
-                      "Implementation error: Map.getTo() for dbref " + dbr
-                              + " from " + curDs.getName()
-                              + " is not a dataset sequence.");
+              throw new Error("Implementation error: Map.getTo() for dbref "
+                      + dbr + " from " + curDs.getName()
+                      + " is not a dataset sequence.");
             }
             // we recurse to add all forward references to dataset sequences via
             // DBRefs/etc
@@ -1239,8 +1260,8 @@ public class Alignment implements AlignmentI
       current = getSequenceAt(i);
       // This should really be a sequence method
       ends[i * 2] = current.findIndex(current.getStart());
-      ends[i * 2 + 1] = current.findIndex(current.getStart()
-              + current.getLength());
+      ends[i * 2 + 1] = current
+              .findIndex(current.getStart() + current.getLength());
       boolean hitres = false;
       for (int j = 0, rs = 0, ssiz = current.getLength(); j < ssiz; j++)
       {
@@ -1476,13 +1497,15 @@ public class Alignment implements AlignmentI
   {
     // TODO JAL-1270 needs test coverage
     // currently tested for use in jalview.gui.SequenceFetcher
-    boolean samegap = toappend.getGapCharacter() == getGapCharacter();
     char oldc = toappend.getGapCharacter();
+    boolean samegap = oldc == getGapCharacter();
     boolean hashidden = toappend.getHiddenSequences() != null
             && toappend.getHiddenSequences().hiddenSequences != null;
     // get all sequences including any hidden ones
-    List<SequenceI> sqs = (hashidden) ? toappend.getHiddenSequences()
-            .getFullAlignment().getSequences() : toappend.getSequences();
+    List<SequenceI> sqs = (hashidden)
+            ? toappend.getHiddenSequences().getFullAlignment()
+                    .getSequences()
+            : toappend.getSequences();
     if (sqs != null)
     {
       // avoid self append deadlock by
@@ -1493,14 +1516,7 @@ public class Alignment implements AlignmentI
         {
           if (!samegap)
           {
-            char[] oldseq = addedsq.getSequence();
-            for (int c = 0; c < oldseq.length; c++)
-            {
-              if (oldseq[c] == oldc)
-              {
-                oldseq[c] = gapCharacter;
-              }
-            }
+            addedsq.replace(oldc, gapCharacter);
           }
           toappendsq.add(addedsq);
         }
@@ -1564,8 +1580,8 @@ public class Alignment implements AlignmentI
             if (ourval instanceof String)
             {
               // append strings
-              this.setProperty(k, ((String) ourval) + "; "
-                      + ((String) toapprop));
+              this.setProperty(k,
+                      ((String) ourval) + "; " + ((String) toapprop));
             }
             else
             {
@@ -1633,9 +1649,8 @@ public class Alignment implements AlignmentI
     {
       for (AlignmentAnnotation a : alignmentAnnotation)
       {
-        if (a.getCalcId() == calcId
-                || (a.getCalcId() != null && calcId != null && a
-                        .getCalcId().equals(calcId)))
+        if (a.getCalcId() == calcId || (a.getCalcId() != null
+                && calcId != null && a.getCalcId().equals(calcId)))
         {
           aa.add(a);
         }
@@ -1651,11 +1666,12 @@ public class Alignment implements AlignmentI
     ArrayList<AlignmentAnnotation> aa = new ArrayList<>();
     for (AlignmentAnnotation ann : getAlignmentAnnotation())
     {
-      if ((calcId == null || (ann.getCalcId() != null && ann.getCalcId()
-              .equals(calcId)))
-              && (seq == null || (ann.sequenceRef != null && ann.sequenceRef == seq))
-              && (label == null || (ann.label != null && ann.label
-                      .equals(label))))
+      if ((calcId == null || (ann.getCalcId() != null
+              && ann.getCalcId().equals(calcId)))
+              && (seq == null || (ann.sequenceRef != null
+                      && ann.sequenceRef == seq))
+              && (label == null
+                      || (ann.label != null && ann.label.equals(label))))
       {
         aa.add(ann);
       }
@@ -1919,42 +1935,6 @@ public class Alignment implements AlignmentI
   }
 
   @Override
-  public int[] getVisibleStartAndEndIndex(List<int[]> hiddenCols)
-  {
-    int[] alignmentStartEnd = new int[] { 0, getWidth() - 1 };
-    int startPos = alignmentStartEnd[0];
-    int endPos = alignmentStartEnd[1];
-
-    int[] lowestRange = new int[] { -1, -1 };
-    int[] higestRange = new int[] { -1, -1 };
-
-    for (int[] hiddenCol : hiddenCols)
-    {
-      lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
-      higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
-    }
-
-    if (lowestRange[0] == -1 && lowestRange[1] == -1)
-    {
-      startPos = alignmentStartEnd[0];
-    }
-    else
-    {
-      startPos = lowestRange[1] + 1;
-    }
-
-    if (higestRange[0] == -1 && higestRange[1] == -1)
-    {
-      endPos = alignmentStartEnd[1];
-    }
-    else
-    {
-      endPos = higestRange[0] - 1;
-    }
-    return new int[] { startPos, endPos };
-  }
-
-  @Override
   public void setHiddenColumns(HiddenColumns cols)
   {
     hiddenCols = cols;