Formatting
[jalview.git] / src / jalview / datamodel / CigarBase.java
index a36bf76..d44d599 100644 (file)
@@ -1,6 +1,6 @@
 /*\r
  * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
  *\r
  * This program is free software; you can redistribute it and/or\r
  * modify it under the terms of the GNU General Public License\r
@@ -18,7 +18,7 @@
  */\r
 package jalview.datamodel;\r
 \r
-import java.util.Vector;\r
+import java.util.*;\r
 \r
 public abstract class CigarBase\r
 {\r
@@ -58,18 +58,21 @@ public abstract class CigarBase
    */\r
   public Object[] getSequenceAndDeletions(String reference, char GapChar)\r
   {\r
-    int rlength=0;\r
+    int rlength = 0;\r
     int[][] deletions = new int[length][];\r
     int[][] trunc_deletions = null;\r
     StringBuffer sq = new StringBuffer();\r
-    int cursor = 0, alcursor=0,start = 0, startpos=0, end = 0, endpos=0, delcount = -1;\r
+    int cursor = 0, alcursor = 0, start = 0, startpos = 0, end = 0, endpos = 0,\r
+        delcount = -1;\r
     boolean consecutive_del = false;\r
     if (length == 0)\r
     {\r
       return null;\r
     }\r
-    if (reference!=null)\r
-      rlength=reference.length();\r
+    if (reference != null)\r
+    {\r
+      rlength = reference.length();\r
+    }\r
     boolean modstart = true;\r
     for (int i = 0; i < length; i++)\r
     {\r
@@ -99,22 +102,26 @@ public abstract class CigarBase
           if (modstart)\r
           {\r
             start = cursor;\r
-            startpos=alcursor;\r
+            startpos = alcursor;\r
             modstart = false;\r
           }\r
-          if (reference!=null) {\r
-            int sbend = cursor+range[i];\r
-            if (sbend>rlength) {\r
+          if (reference != null)\r
+          {\r
+            int sbend = cursor + range[i];\r
+            if (sbend > rlength)\r
+            {\r
               sq.append(reference.substring(cursor, rlength));\r
               while (sbend-- >= rlength)\r
               {\r
                 sq.append(GapChar);\r
               }\r
-            } else {\r
+            }\r
+            else\r
+            {\r
               sq.append(reference.substring(cursor, sbend));\r
             }\r
           }\r
-          alcursor+=range[i];\r
+          alcursor += range[i];\r
           cursor += range[i];\r
           end = cursor - 1;\r
           endpos = alcursor;\r
@@ -131,29 +138,40 @@ public abstract class CigarBase
     deletions = null;\r
     return new Object[]\r
         {\r
-        ((reference!=null) ? sq.toString() : null),\r
-        new int[] {\r
+        ( (reference != null) ? sq.toString() : null),\r
+        new int[]\r
+        {\r
         start, startpos, end, endpos}, trunc_deletions};\r
   }\r
-  protected void compact_operations() {\r
-    int i=1;\r
-    if (operation==null)\r
+\r
+  protected void compact_operations()\r
+  {\r
+    int i = 1;\r
+    if (operation == null)\r
+    {\r
       return;\r
+    }\r
     char last = operation[0];\r
-    while (i<length) {\r
-      if (last==operation[i]) {\r
-        range[i-1]+=range[i];\r
-        int r = length-i;\r
-        if (r>0) {\r
+    while (i < length)\r
+    {\r
+      if (last == operation[i])\r
+      {\r
+        range[i - 1] += range[i];\r
+        int r = length - i;\r
+        if (r > 0)\r
+        {\r
           System.arraycopy(range, i + 1, range, i, r);\r
           System.arraycopy(operation, i + 1, operation, i, r);\r
         }\r
         length--;\r
-      } else {\r
+      }\r
+      else\r
+      {\r
         last = operation[i++];\r
       }\r
     }\r
   }\r
+\r
   /**\r
    * turn a cigar string into a series of operation range pairs\r
    * @param cigarString String\r
@@ -235,8 +253,10 @@ public abstract class CigarBase
     {\r
       throw new Error("Implementation error. Invalid operation string.");\r
     }\r
-    if (range<=0)\r
+    if (range <= 0)\r
+    {\r
       throw new Error("Invalid range string (must be non-zero positive number)");\r
+    }\r
     int lngth = 0;\r
     if (operation == null)\r
     {\r
@@ -254,10 +274,14 @@ public abstract class CigarBase
       System.arraycopy(rng, 0, this.range, 0, length);\r
       rng = null;\r
     }\r
-    if ((length>0) && (operation[length-1]==op))\r
+    if ( (length > 0) && (operation[length - 1] == op))\r
+    {\r
       length--; // modify existing operation.\r
+    }\r
     else\r
-      this.range[length]=0; // reset range\r
+    {\r
+      this.range[length] = 0; // reset range\r
+    }\r
     this.operation[length] = op;\r
     this.range[length++] += range;\r
   }\r
@@ -284,9 +308,9 @@ public abstract class CigarBase
    * @param pos int -1, 0-length of visible region, or greater to append new ops (with insertions in between)\r
    * @param op char\r
    * @param range int\r
-  public void addOperationAt(int pos, char op, int range)\r
-  {\r
-    int cursor = -1; // mark the position for the current operation being edited.\r
+     public void addOperationAt(int pos, char op, int range)\r
+     {\r
+   int cursor = -1; // mark the position for the current operation being edited.\r
     int o = 0;\r
     boolean last_d = false; // previous op was a deletion.\r
     if (pos < -1)\r
@@ -373,7 +397,7 @@ public abstract class CigarBase
               }\r
           break;\r
         default:\r
-          throw new Error("Implementation Error: Unknown operation in addOperation!");\r
+   throw new Error("Implementation Error: Unknown operation in addOperation!");\r
       }\r
       // finally, add remaining ops.\r
       while (e_o<e_length) {\r
@@ -381,95 +405,113 @@ public abstract class CigarBase
         e_o++;\r
       }\r
     }\r
-  }\r
-**/\r
+     }\r
+   **/\r
   /**\r
    * Mark residues from start to end (inclusive) as deleted from the alignment, and removes any insertions.\r
    * @param start int\r
    * @param end int\r
    * @return deleted int - number of symbols marked as deleted\r
    */\r
-  public int deleteRange(int start, int end) {\r
-    int deleted=0;\r
-    if (length==0) {\r
+  public int deleteRange(int start, int end)\r
+  {\r
+    int deleted = 0;\r
+    if (length == 0)\r
+    {\r
       // nothing to do here\r
       return deleted;\r
     }\r
-    if (start<0 || start>end)\r
+    if (start < 0 || start > end)\r
+    {\r
       throw new Error("Implementation Error: deleteRange out of bounds: start must be non-negative and less than end.");\r
+    }\r
     // find beginning\r
     int cursor = 0; // mark the position for the current operation being edited.\r
-    int rlength=1+end-start; // number of positions to delete\r
-    int oldlen=length;\r
+    int rlength = 1 + end - start; // number of positions to delete\r
+    int oldlen = length;\r
     int o = 0;\r
-    boolean editing=false;\r
+    boolean editing = false;\r
     char[] oldops = operation;\r
     int[] oldrange = range;\r
-    length=0;\r
+    length = 0;\r
     operation = null;\r
     range = null;\r
     compact_operations();\r
-    while (o<oldlen && cursor<=end && rlength>0) {\r
-      if (oldops[o] == D) {\r
+    while (o < oldlen && cursor <= end && rlength > 0)\r
+    {\r
+      if (oldops[o] == D)\r
+      {\r
         // absorbed into new deleted region.\r
         addDeleted(oldrange[o++]);\r
         continue;\r
       }\r
 \r
       int remain = oldrange[o]; // number of op characters left to edit\r
-      if (!editing) {\r
+      if (!editing)\r
+      {\r
         if ( (cursor + remain) <= start)\r
         {\r
-          addOperation(oldops[o],oldrange[o]);\r
-          cursor+=oldrange[o++];\r
+          addOperation(oldops[o], oldrange[o]);\r
+          cursor += oldrange[o++];\r
           continue; // next operation\r
         }\r
-        editing=true;\r
+        editing = true;\r
         // add operations before hidden region\r
-        if (start-cursor>0) {\r
-          addOperation(oldops[o], start- cursor);\r
+        if (start - cursor > 0)\r
+        {\r
+          addOperation(oldops[o], start - cursor);\r
           remain -= start - cursor;\r
         }\r
       }\r
       // start inserting new ops\r
-      if (o<oldlen && editing && rlength>0 && remain>0) {\r
-        switch (oldops[o]) {\r
+      if (o < oldlen && editing && rlength > 0 && remain > 0)\r
+      {\r
+        switch (oldops[o])\r
+        {\r
           case M:\r
-            if (rlength>remain) {\r
+            if (rlength > remain)\r
+            {\r
               addDeleted(remain);\r
-              deleted+=remain;\r
-            } else {\r
-              deleted+=rlength;\r
+              deleted += remain;\r
+            }\r
+            else\r
+            {\r
+              deleted += rlength;\r
               addDeleted(rlength);\r
-              if (remain-rlength>0)\r
-                this.addOperation(M,remain-rlength); // add remaining back.\r
-              rlength=0;\r
-              remain=0;\r
+              if (remain - rlength > 0)\r
+              {\r
+                this.addOperation(M, remain - rlength); // add remaining back.\r
+              }\r
+              rlength = 0;\r
+              remain = 0;\r
             }\r
             break;\r
           case I:\r
-            if (remain-rlength>0) {\r
+            if (remain - rlength > 0)\r
+            {\r
               // only remove some gaps\r
-              addInsertion(remain-rlength);\r
-              rlength=0;\r
+              addInsertion(remain - rlength);\r
+              rlength = 0;\r
             }\r
             break;\r
           case D:\r
             throw new Error("Implementation error."); // do nothing;\r
           default:\r
-            throw new Error("Implementation Error! Unknown operation '"+oldops[o]+"'");\r
+            throw new Error("Implementation Error! Unknown operation '" +\r
+                            oldops[o] + "'");\r
         }\r
-        rlength-=remain;\r
+        rlength -= remain;\r
         remain = oldrange[++o]; // number of op characters left to edit\r
       }\r
     }\r
     // add remaining\r
-    while (o<oldlen) {\r
-      addOperation(oldops[o],oldrange[o++]);\r
+    while (o < oldlen)\r
+    {\r
+      addOperation(oldops[o], oldrange[o++]);\r
     }\r
     //if (cursor<(start+1)) {\r
-      // ran out of ops - nothing to do here ?\r
-     // addInsertion(start-cursor);\r
+    // ran out of ops - nothing to do here ?\r
+    // addInsertion(start-cursor);\r
     //}\r
     return deleted;\r
   }\r
@@ -481,7 +523,7 @@ public abstract class CigarBase
    */\r
   public boolean hasDeletedRegions()\r
   {\r
-    for (int i = 0; i<length ; i++)\r
+    for (int i = 0; i < length; i++)\r
     {\r
       if (operation[i] == D)\r
       {\r
@@ -490,38 +532,49 @@ public abstract class CigarBase
     }\r
     return false;\r
   }\r
+\r
   /**\r
    * enumerate the ranges on seq that are marked as deleted in this cigar\r
    * @return int[] { vis_start, sym_start, length }\r
    */\r
-  public int[] getDeletedRegions() {\r
-    if (length==0)\r
+  public int[] getDeletedRegions()\r
+  {\r
+    if (length == 0)\r
+    {\r
       return null;\r
+    }\r
     Vector dr = new Vector();\r
-    int cursor=0, vcursor=0;\r
-    for (int i=0;i<length;i++) {\r
-      switch (operation[i]) {\r
+    int cursor = 0, vcursor = 0;\r
+    for (int i = 0; i < length; i++)\r
+    {\r
+      switch (operation[i])\r
+      {\r
         case M:\r
-          cursor+=range[i];\r
+          cursor += range[i];\r
         case I:\r
-          vcursor+=range[i];\r
+          vcursor += range[i];\r
           break;\r
         case D:\r
-          dr.addElement(new int[] { vcursor, cursor, range[i]});\r
-          cursor+=range[i];\r
+          dr.addElement(new int[]\r
+                        {vcursor, cursor, range[i]});\r
+          cursor += range[i];\r
       }\r
     }\r
-    if (dr.size()==0)\r
+    if (dr.size() == 0)\r
+    {\r
       return null;\r
-    int[] delregions = new int[dr.size()*3];\r
-    for (int i=0,l=dr.size(); i<l; i++) {\r
+    }\r
+    int[] delregions = new int[dr.size() * 3];\r
+    for (int i = 0, l = dr.size(); i < l; i++)\r
+    {\r
       int[] reg = (int[]) dr.elementAt(i);\r
-      delregions[i*3] = reg[0];\r
-      delregions[i*3+1] = reg[1];\r
-      delregions[i*3+2] = reg[2];\r
+      delregions[i * 3] = reg[0];\r
+      delregions[i * 3 + 1] = reg[1];\r
+      delregions[i * 3 + 2] = reg[2];\r
     }\r
     return delregions;\r
   }\r
+\r
   /**\r
    * sum of ranges in cigar string\r
    * @return int number of residues hidden, matched, or gaps inserted into sequence\r
@@ -558,6 +611,7 @@ public abstract class CigarBase
     }\r
     return w;\r
   }\r
+\r
   /**\r
    * mark a range of inserted residues\r
    * @param range int\r
@@ -582,8 +636,8 @@ public abstract class CigarBase
    * @param start alignment column\r
    * @param end alignment column\r
    * @return boolean true if residues were marked as deleted.\r
-  public boolean deleteRange(int start, int end)\r
-  {\r
+     public boolean deleteRange(int start, int end)\r
+     {\r
     boolean deleted = false;\r
     int op = 0, prevop = -1, firstm = -1,\r
         lastm = -1, postop = -1;\r
@@ -633,8 +687,8 @@ public abstract class CigarBase
       addDeleted(end - start + 1);\r
     }\r
     return deleted;\r
-  }\r
-*/\r
+     }\r
+   */\r
   /**\r
    * Return an ENSEMBL style cigar string where D may indicates excluded parts of seq\r
    * @return String of form ([0-9]+[IMD])+\r