left and right justify method
authorjprocter <Jim Procter>
Fri, 19 Dec 2008 15:33:39 +0000 (15:33 +0000)
committerjprocter <Jim Procter>
Fri, 19 Dec 2008 15:33:39 +0000 (15:33 +0000)
src/jalview/datamodel/Alignment.java
src/jalview/datamodel/AlignmentI.java

index c74be73..64b79b7 100755 (executable)
@@ -794,6 +794,98 @@ public class Alignment implements AlignmentI
     }
     return modified;
   }
+  /**
+   * Justify the sequences to the left or right by deleting and inserting gaps before the initial residue or after the terminal residue
+   * @param right true if alignment padded to right, false to justify to left
+   * @return true if alignment was changed
+   */
+  public boolean justify(boolean right)
+  {
+    boolean modified = false;
+
+    // Remove excess gaps from the end of alignment
+    int maxLength = -1;
+    int ends[] = new int[sequences.size()*2];
+    SequenceI current;
+    for (int i = 0; i < sequences.size(); i++)
+    {
+      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());
+      boolean hitres=false;
+      for (int j = 0,rs=0,ssiz=current.getLength(); j<ssiz; j++)
+      {
+        if (!jalview.util.Comparison.isGap(current.getCharAt(j)))
+        {
+          if (!hitres)
+          {
+            ends[i*2] = j;
+            hitres=true;
+          } else {
+            ends[i*2+1] = j;
+            if (j-ends[i*2]>maxLength)
+            {
+              maxLength = j-ends[i*2];
+            }
+          }
+        }
+      }
+    }
+
+    maxLength++;
+    // now edit the flanking gaps to justify to either left or right
+    int cLength,extent,diff;
+    for (int i = 0; i < sequences.size(); i++)
+    {
+      current = getSequenceAt(i);
+      
+      cLength = 1+ends[i*2+1]-ends[i*2];
+      diff = maxLength-cLength; // number of gaps to indent
+      extent = current.getLength();
+      if (right)
+      {
+        // right justify
+        if (extent>ends[i*2+1])
+        {
+          current.deleteChars(ends[i*2+1]+1, extent);
+          modified = true;
+        }
+        if (ends[i*2]>diff)
+        {
+          current.deleteChars(0, ends[i*2]-diff);
+          modified = true;
+        } else {
+          if (ends[i*2]<diff)
+          {
+            current.insertCharAt(0, diff-ends[i*2],gapCharacter);
+            modified = true;
+          }
+        }
+      } else {
+        // left justify
+        if (ends[i*2]>0)
+        {
+          current.deleteChars(0, ends[i*2]);
+          modified = true;
+          ends[i*2+1]-=ends[i*2];
+          extent-=ends[i*2];
+        }
+        if (extent>maxLength)
+        {
+          current.deleteChars(maxLength+1, extent);
+          modified = true;
+        } else {
+          if (extent<maxLength)
+          {
+            current.insertCharAt(extent, maxLength-extent,gapCharacter);
+            modified = true;
+          }
+        }
+      }
+    }
+    return modified;
+  }
 
   public HiddenSequences getHiddenSequences()
   {
index 4de17e7..d9791f1 100755 (executable)
@@ -375,5 +375,10 @@ public interface AlignmentI
    * @param toappend - the alignment to be appended. 
    */
   public void append(AlignmentI toappend);
-
+  /**
+   * Justify the sequences to the left or right by deleting and inserting gaps before the initial residue or after the terminal residue
+   * @param right true if alignment padded to right, false to justify to left
+   * @return true if alignment was changed
+   */
+  public boolean justify(boolean right);
 }