Use AAFrequency final keys
[jalview.git] / src / jalview / datamodel / Sequence.java
index 0225c88..fc52190 100755 (executable)
@@ -1,6 +1,6 @@
 /*
 * Jalview - A Sequence Alignment Editor and Viewer
-* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+* Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
@@ -455,13 +455,12 @@ public class Sequence implements SequenceI
     }
 
     /**
-     * DOCUMENT ME!
+     * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
      *
-     * @return DOCUMENT ME!
+     * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
      */
     public int[] gapMap()
     {
-        // Returns an int array giving the position of each residue in the sequence in the alignment
         String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.GapChars, sequence);
         int[] map = new int[seq.length()];
         int j = 0;
@@ -718,4 +717,61 @@ public class Sequence implements SequenceI
     return getSubSequence(start, getLength());
   }
 
+  public int removeGaps() {
+    if (sequence!=null)
+      return removeGaps(0, getLength());
+    return 0;
+  }
+
+  public int removeGaps(int start, int end) {
+    int jSize = getLength();
+    int oSize=jSize;
+    if (jSize<=start)
+      return 0;
+    if (end>jSize)
+      end = jSize;
+
+    // Removing a range is much quicker than removing gaps
+    // one by one for long sequences
+    int j = start;
+    int rangeStart=-1, rangeEnd=-1;
+
+    do
+    {
+      if (jalview.util.Comparison.isGap(getCharAt(j)))
+      {
+        if(rangeStart==-1)
+         {
+           rangeStart = j;
+           rangeEnd = j+1;
+         }
+         else
+         {
+           rangeEnd++;
+         }
+         j++;
+      }
+      else
+      {
+        if(rangeStart>-1)
+        {
+          deleteChars(rangeStart, rangeEnd);
+          j-=rangeEnd-rangeStart;
+          jSize-=rangeEnd-rangeStart;
+          rangeStart = -1;
+          rangeEnd = -1;
+        }
+        else
+          j++;
+      }
+    }
+    while (j < end && j < jSize);
+    if(rangeStart>-1)
+    {
+     deleteChars(rangeStart, rangeEnd);
+     jSize-=rangeEnd-rangeStart;
+    }
+    return oSize-jSize; // number of deleted characters.
+  }
+
 }