remove p+3 < query.length
[jalview.git] / src / jalview / datamodel / Sequence.java
index ff7ec32..2303d5d 100755 (executable)
@@ -1,26 +1,26 @@
 /*
-* Jalview - A Sequence Alignment Editor and Viewer
-* 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
-* as published by the Free Software Foundation; either version 2
-* of the License, or (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program; if not, write to the Free Software
-* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
-*/
+ * Jalview - A Sequence Alignment Editor and Viewer
+ * Copyright (C) 2007 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
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ */
 package jalview.datamodel;
 
-
 import java.util.*;
 
+import jalview.analysis.*;
 
 /**
  * DOCUMENT ME!
@@ -28,11 +28,12 @@ import java.util.*;
  * @author $author$
  * @version $Revision$
  */
-public class Sequence implements SequenceI
+public class Sequence
+    implements SequenceI
 {
   SequenceI datasetSequence;
   String name;
-  private char [] sequence;
+  private char[] sequence;
   String description;
   int start;
   int end;
@@ -47,11 +48,6 @@ public class Sequence implements SequenceI
   /** DOCUMENT ME!! */
   public SequenceFeature[] sequenceFeatures;
 
-  /** This array holds hidden sequences
-   * of which this sequence is the representitive member of a group
-   */
-  SequenceGroup hiddenSequences;
-
   /**
    * Creates a new Sequence object.
    *
@@ -70,7 +66,7 @@ public class Sequence implements SequenceI
     checkValidRange();
   }
 
-  public Sequence(String name, char [] sequence, int start, int end)
+  public Sequence(String name, char[] sequence, int start, int end)
   {
     this.name = name;
     this.sequence = sequence;
@@ -105,7 +101,7 @@ public class Sequence implements SequenceI
       int endRes = 0;
       for (int j = 0; j < sequence.length; j++)
       {
-        if (!jalview.util.Comparison.isGap( sequence[j] ))
+        if (!jalview.util.Comparison.isGap(sequence[j]))
         {
           endRes++;
         }
@@ -145,7 +141,6 @@ public class Sequence implements SequenceI
     description = seq.getDescription();
   }
 
-
   /**
    * DOCUMENT ME!
    *
@@ -180,10 +175,12 @@ public class Sequence implements SequenceI
 
   public void deleteFeature(SequenceFeature sf)
   {
-    if(sequenceFeatures==null)
+    if (sequenceFeatures == null)
+    {
       return;
+    }
 
-    int index=0;
+    int index = 0;
     for (index = 0; index < sequenceFeatures.length; index++)
     {
       if (sequenceFeatures[index].equals(sf))
@@ -192,25 +189,28 @@ public class Sequence implements SequenceI
       }
     }
 
-
-    if(index==sequenceFeatures.length)
+    if (index == sequenceFeatures.length)
+    {
       return;
+    }
 
     int sfLength = sequenceFeatures.length;
-    if(sfLength<2)
+    if (sfLength < 2)
     {
       sequenceFeatures = null;
     }
     else
     {
-      SequenceFeature[] temp = new SequenceFeature[sfLength-1];
+      SequenceFeature[] temp = new SequenceFeature[sfLength - 1];
       System.arraycopy(sequenceFeatures, 0, temp, 0, index);
 
-      if(index<sfLength)
+      if (index < sfLength)
+      {
         System.arraycopy(sequenceFeatures,
                          index + 1,
                          temp,
-                         index, sequenceFeatures.length - index -1);
+                         index, sequenceFeatures.length - index - 1);
+      }
 
       sequenceFeatures = temp;
     }
@@ -229,7 +229,9 @@ public class Sequence implements SequenceI
   public void addPDBId(PDBEntry entry)
   {
     if (pdbIds == null)
+    {
       pdbIds = new Vector();
+    }
 
     pdbIds.addElement(entry);
   }
@@ -352,7 +354,6 @@ public class Sequence implements SequenceI
     checkValidRange();
   }
 
-
   public String getSequenceAsString()
   {
     return new String(sequence);
@@ -363,8 +364,7 @@ public class Sequence implements SequenceI
     return new String(getSequence(start, end));
   }
 
-
-  public char [] getSequence()
+  public char[] getSequence()
   {
     return sequence;
   }
@@ -377,7 +377,7 @@ public class Sequence implements SequenceI
    *
    * @return DOCUMENT ME!
    */
-  public char [] getSequence(int start, int end)
+  public char[] getSequence(int start, int end)
   {
     // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
     if (start >= sequence.length)
@@ -390,13 +390,12 @@ public class Sequence implements SequenceI
       end = sequence.length;
     }
 
-    char [] reply = new char[end-start];
-    System.arraycopy(sequence, start, reply, 0, end-start);
+    char[] reply = new char[end - start];
+    System.arraycopy(sequence, start, reply, 0, end - start);
 
     return reply;
   }
 
-
   /**
    * make a new Sequence object from start to end (including gaps) over this seqeunce
    * @param start int
@@ -406,16 +405,27 @@ public class Sequence implements SequenceI
   public SequenceI getSubSequence(int start, int end)
   {
     if (start < 0)
+    {
       start = 0;
-    char [] seq = getSequence(start, end);
+    }
+    char[] seq = getSequence(start, end);
     if (seq.length == 0)
+    {
       return null;
+    }
     int nstart = findPosition(start);
     int nend = findPosition(end) - 1;
     // JBPNote - this is an incomplete copy.
     SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
     nseq.setDescription(description);
-    nseq.setDatasetSequence(getDatasetSequence());
+    if (datasetSequence != null)
+    {
+      nseq.setDatasetSequence(datasetSequence);
+    }
+    else
+    {
+      nseq.setDatasetSequence(this);
+    }
     return nseq;
   }
 
@@ -505,7 +515,7 @@ public class Sequence implements SequenceI
     int seqlen = sequence.length;
     while ( (j < i) && (j < seqlen))
     {
-      if (!jalview.util.Comparison.isGap( sequence[j] ))
+      if (!jalview.util.Comparison.isGap(sequence[j]))
       {
         pos++;
       }
@@ -555,21 +565,42 @@ public class Sequence implements SequenceI
       return;
     }
 
-    char [] tmp;
+    char[] tmp;
 
     if (j >= sequence.length)
     {
       tmp = new char[i];
-      System.arraycopy(sequence,0,tmp,0,i);
+      System.arraycopy(sequence, 0, tmp, 0, i);
     }
     else
     {
-      tmp = new char[sequence.length-j+i];
-      System.arraycopy(sequence,0,tmp,0,i);
-      System.arraycopy(sequence,j,tmp,i,sequence.length-j);
+      tmp = new char[sequence.length - j + i];
+      System.arraycopy(sequence, 0, tmp, 0, i);
+      System.arraycopy(sequence, j, tmp, i, sequence.length - j);
+    }
+
+    if (this.datasetSequence != null)
+    {
+      for (int s = i; s < j; s++)
+      {
+        if (jalview.schemes.ResidueProperties.aaIndex[sequence[s]] != 23)
+        {
+
+          Sequence ds = new Sequence(name,
+                                     AlignSeq.extractGaps(
+                                         jalview.util.Comparison.GapChars,
+                                         this.getSequenceAsString()
+                                     ),
+                                     start,
+                                     end);
+          ds.setDescription(description);
+        }
+        break;
+      }
     }
 
     sequence = tmp;
+
   }
 
   /**
@@ -581,7 +612,7 @@ public class Sequence implements SequenceI
    */
   public void insertCharAt(int i, int length, char c)
   {
-    char [] tmp = new char[sequence.length+length];
+    char[] tmp = new char[sequence.length + length];
 
     if (i >= sequence.length)
     {
@@ -589,21 +620,20 @@ public class Sequence implements SequenceI
       i = sequence.length;
     }
     else
-   {
+    {
       System.arraycopy(sequence, 0, tmp, 0, i);
-   }
-
+    }
 
     int index = i;
     while (length > 0)
     {
-      tmp[ index++ ] = c;
+      tmp[index++] = c;
       length--;
     }
 
     if (i < sequence.length)
     {
-      System.arraycopy(sequence, i, tmp, index, sequence.length-i );
+      System.arraycopy(sequence, i, tmp, index, sequence.length - i);
     }
 
     sequence = tmp;
@@ -637,17 +667,21 @@ public class Sequence implements SequenceI
   public void addDBRef(DBRefEntry entry)
   {
     if (dbrefs == null)
+    {
       dbrefs = new DBRefEntry[0];
+    }
 
     int i, iSize = dbrefs.length;
 
-    for(i=0; i<iSize; i++)
-      if(dbrefs[i].getAccessionId().equals(entry.getAccessionId())
-      && dbrefs[i].getSource().equals(entry.getSource())
-      && dbrefs[i].getVersion().equals(entry.getVersion()))
+    for (i = 0; i < iSize; i++)
+    {
+      if (dbrefs[i].getAccessionId().equals(entry.getAccessionId())
+          && dbrefs[i].getSource().equals(entry.getSource())
+          && dbrefs[i].getVersion().equals(entry.getVersion()))
       {
         return;
       }
+    }
 
     DBRefEntry[] temp = new DBRefEntry[iSize + 1];
     System.arraycopy(dbrefs, 0, temp, 0, iSize);
@@ -669,11 +703,15 @@ public class Sequence implements SequenceI
   public AlignmentAnnotation[] getAnnotation()
   {
     if (annotation == null)
+    {
       return null;
+    }
 
     AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
     for (int r = 0; r < ret.length; r++)
+    {
       ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
+    }
 
     return ret;
   }
@@ -681,33 +719,53 @@ public class Sequence implements SequenceI
   public void addAlignmentAnnotation(AlignmentAnnotation annotation)
   {
     if (this.annotation == null)
+    {
       this.annotation = new Vector();
+    }
 
     this.annotation.addElement(annotation);
   }
 
-  public SequenceGroup getHiddenSequences()
-  {
-    return hiddenSequences;
-  }
-
-  public void addHiddenSequence(SequenceI seq)
+  /**
+   * test if this is a valid candidate for another
+   * sequence's dataset sequence.
+   *
+   */
+  private boolean isValidDatasetSequence()
   {
-    if (hiddenSequences == null)
+    if (datasetSequence != null)
     {
-      hiddenSequences = new SequenceGroup();
+      return false;
+    }
+    for (int i = 0; i < sequence.length; i++)
+    {
+      if (jalview.util.Comparison.isGap(sequence[i]))
+      {
+        return false;
+      }
     }
-    hiddenSequences.addSequence(seq, false);
+    return true;
   }
 
-  public void showHiddenSequence(SequenceI seq)
+  /* (non-Javadoc)
+   * @see jalview.datamodel.SequenceI#deriveSequence()
+   */
+  public SequenceI deriveSequence()
   {
-    hiddenSequences.deleteSequence(seq, false);
-    if (hiddenSequences.getSize(false) < 1)
+    SequenceI seq = new Sequence(name, sequence, start, end);
+    seq.setDescription(description);
+    if (datasetSequence != null)
     {
-      hiddenSequences = null;
+      seq.setDatasetSequence(datasetSequence);
     }
+    else
+    {
+      if (isValidDatasetSequence())
+      {
+        seq.setDatasetSequence(this);
+      }
+    }
+    return seq;
   }
-}
-
 
+}