JAL-2738 update spike branch with latest
[jalview.git] / src / jalview / datamodel / CigarArray.java
index 1a3d35d..1723f1d 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -20,7 +20,7 @@
  */
 package jalview.datamodel;
 
-import java.util.Vector;
+import java.util.List;
 
 public class CigarArray extends CigarBase
 {
@@ -68,7 +68,8 @@ public class CigarArray extends CigarBase
       for (int c = 0; c < cigars.length; c++)
       {
         refCigars[c] = cigars[c];
-        if (!((cigars[c] instanceof SeqCigar) || cigars[c] instanceof CigarCigar))
+        if (!((cigars[c] instanceof SeqCigar)
+                || cigars[c] instanceof CigarCigar))
         {
           seqcigararray = false;
         }
@@ -85,20 +86,19 @@ public class CigarArray extends CigarBase
    * @param columnSelection
    * @param selectionGroup
    */
-  public CigarArray(AlignmentI alignment, ColumnSelection columnSelection,
+  public CigarArray(AlignmentI alignment, HiddenColumns hidden,
           SequenceGroup selectionGroup)
   {
     this(constructSeqCigarArray(alignment, selectionGroup));
     constructFromAlignment(alignment,
-            columnSelection != null ? columnSelection.getHiddenColumns()
-                    : null, selectionGroup);
+            hidden != null ? hidden.getHiddenColumnsCopy() : null,
+            selectionGroup);
   }
 
   private static int[] _calcStartEndBounds(AlignmentI alignment,
           SequenceGroup selectionGroup)
   {
-    int[] startend = new int[]
-    { 0, 0, 0 };
+    int[] startend = new int[] { 0, 0, 0 };
     if (selectionGroup != null)
     {
       startend[0] = selectionGroup.getSize();
@@ -148,54 +148,52 @@ public class CigarArray extends CigarBase
    * internal constructor function - called by CigarArray(AlignmentI, ...);
    * 
    * @param alignment
-   * @param columnSelection
+   * @param list
    *          - vector of visible regions as returned from
    *          columnSelection.getHiddenColumns()
    * @param selectionGroup
    */
   private void constructFromAlignment(AlignmentI alignment,
-          Vector columnSelection, SequenceGroup selectionGroup)
+          List<int[]> list, SequenceGroup selectionGroup)
   {
     int[] _startend = _calcStartEndBounds(alignment, selectionGroup);
     int start = _startend[1], end = _startend[2];
     // now construct the CigarArray operations
-    if (columnSelection != null)
+    if (list != null)
     {
       int[] region;
       int hideStart, hideEnd;
       int last = start;
-      for (int j = 0; last < end & j < columnSelection.size(); j++)
+      for (int j = 0; last < end & j < list.size(); j++)
       {
-        region = (int[]) columnSelection.elementAt(j);
+        region = list.get(j);
         hideStart = region[0];
         hideEnd = region[1];
         // edit hidden regions to selection range
-        if (hideStart < last)
+
+        // just move on if hideEnd is before last
+        if (hideEnd < last)
         {
-          if (hideEnd > last)
-          {
-            hideStart = last;
-          }
-          else
-          {
-            continue;
-          }
+          continue;
         }
-
+        // exit if next region is after end
         if (hideStart > end)
         {
           break;
         }
 
-        if (hideEnd > end)
+        // truncate region at start if last falls in region
+        if ((hideStart < last) && (hideEnd >= last))
         {
-          hideEnd = end;
+          hideStart = last;
         }
 
-        if (hideStart > hideEnd)
+        // truncate region at end if end falls in region
+        if (hideEnd > end) // already checked that hideStart<=end
         {
-          break;
+          hideEnd = end;
         }
+
         /**
          * form operations...
          */
@@ -207,7 +205,7 @@ public class CigarArray extends CigarBase
         last = hideEnd + 1;
       }
       // Final match if necessary.
-      if (last < end)
+      if (last <= end)
       {
         addOperation(CigarArray.M, end - last + 1);
       }
@@ -219,7 +217,7 @@ public class CigarArray extends CigarBase
   }
 
   /**
-   * @see Cigar.getSequenceAndDeletions
+   * @see CigarBase.getSequenceAndDeletions
    * @param GapChar
    *          char
    * @return Object[][]
@@ -343,8 +341,11 @@ public class CigarArray extends CigarBase
           delpos = new java.util.Vector();
         }
         int delstart = cursor, delend = cursor + range[i] - 1; // inclusive
-        delpos.addElement(new int[]
-        { vcursor + offset, range[i] }); // index of right hand column after
+        delpos.addElement(new int[] { vcursor + offset, range[i] }); // index of
+                                                                     // right
+                                                                     // hand
+                                                                     // column
+                                                                     // after
         // hidden region boundary
         offset += range[i] - 1; // shift in visible column coordinates
         System.arraycopy(operation, i + 1, operation, i, length - i);