Sequence is char []
[jalview.git] / src / jalview / datamodel / Alignment.java
index 8c320f7..b5f31bc 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
@@ -70,11 +70,7 @@ public class Alignment implements AlignmentI
      * @param seqs SeqCigar[]
      */
     public Alignment(SeqCigar[] alseqs) {
-
-      SequenceI[] seqs = new SequenceI[alseqs.length];
-      for (int i=0; i<alseqs.length; i++) {
-        seqs[i] = alseqs[i].getSeq(this.gapCharacter);
-      }
+      SequenceI[] seqs = SeqCigar.createAlignmentSequences(alseqs, gapCharacter, new ColumnSelection(), null);
       initAlignment(seqs);
     }
     /**
@@ -83,7 +79,7 @@ public class Alignment implements AlignmentI
      * JBPNote - must also check that compactAlignment resolves to a set of SeqCigars - or construct them appropriately.
      * @param compactAlignment CigarArray
      */
-    public Alignment(CigarArray compactAlignment) {
+    public static AlignmentI createAlignment(CigarArray compactAlignment) {
       throw new Error("Alignment(CigarArray) not yet implemented");
       // this(compactAlignment.refCigars);
     }
@@ -135,14 +131,13 @@ public class Alignment implements AlignmentI
       {
         if(snew.getDatasetSequence()!=null)
         {
-          System.out.println(snew.getName());
           getDataset().addSequence(snew.getDatasetSequence());
         }
         else
         {
           Sequence ds = new Sequence(snew.getName(),
                                      AlignSeq.extractGaps("-. ",
-              snew.getSequence()),
+              snew.getSequenceAsString()),
                                      snew.getStart(),
                                      snew.getEnd());
 
@@ -150,8 +145,9 @@ public class Alignment implements AlignmentI
           getDataset().addSequence(ds);
         }
       }
-
       sequences.addElement(snew);
+
+      hiddenSequences.adjustHeightSequenceAdded();
     }
 
 
@@ -177,124 +173,6 @@ public class Alignment implements AlignmentI
         return groups;
     }
 
-    /** Takes out columns consisting entirely of gaps (-,.," ")
-     */
-    public void removeGaps()
-    {
-        SequenceI[] seqs = getVisibleAndRepresentedSeqs();
-        int j, jSize = seqs.length;
-
-        int width = 0;
-        for (int i = 0; i < jSize; i++)
-        {
-          if (seqs[i].getLength() > width)
-          {
-            width = seqs[i].getLength();
-          }
-        }
-
-        int startCol = -1, endCol = -1;
-        boolean delete = true;
-        for (int i = 0; i < width; i++)
-        {
-            delete = true;
-
-            for (j = 0; j < jSize; j++)
-            {
-                if (seqs[j].getLength() > i)
-                {
-                    if (!jalview.util.Comparison.isGap(seqs[j].getCharAt(i)))
-                    {
-                        if(delete)
-                          endCol = i;
-
-                        delete = false;
-                        break;
-                    }
-                }
-            }
-
-            if(delete && startCol==-1)
-            {
-              startCol = i;
-            }
-
-
-            if (!delete && startCol > -1)
-            {
-              deleteColumns(seqs, startCol, endCol);
-              width -= (endCol - startCol);
-              i -= (endCol - startCol);
-              startCol = -1;
-              endCol = -1;
-            }
-        }
-
-        if (delete && startCol > -1)
-        {
-          deleteColumns(seqs, startCol, endCol);
-        }
-    }
-
-    /** Removes a range of columns (start to end inclusive).
-     *
-     * @param seqs Sequences to remove columns from
-     * @param start Start column in the alignment
-     * @param end End column in the alignment
-     */
-    public void deleteColumns(SequenceI [] seqs, int start, int end)
-    {
-      for(int i=0; i<seqs.length; i++)
-        seqs[i].deleteChars(start, end);
-    }
-
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param i DOCUMENT ME!
-     */
-    public void trimLeft(int i)
-    {
-        SequenceI[] seqs = getVisibleAndRepresentedSeqs();
-        int j, jSize = seqs.length;
-        for (j = 0; j < jSize; j++)
-        {
-            int newstart = seqs[j].findPosition(i);
-
-            if(i>seqs[j].getLength())
-            {
-              sequences.removeElement(seqs[j]);
-              j--;
-              jSize--;
-            }
-            else
-            {
-              seqs[j].setStart(newstart);
-              seqs[j].setSequence(seqs[j].getSequence().substring(i));
-            }
-        }
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param i DOCUMENT ME!
-     */
-    public void trimRight(int i)
-    {
-        SequenceI[] seqs = getVisibleAndRepresentedSeqs();
-        int j, jSize = seqs.length;
-        for (j = 0; j < jSize; j++)
-        {
-            int newend = seqs[j].findPosition(i);
-
-            seqs[j].setEnd(newend);
-            if(seqs[j].getLength()>i)
-              seqs[j].setSequence(seqs[j].getSequence().substring(0, i + 1));
-        }
-    }
-
     /**
      * DOCUMENT ME!
      *
@@ -302,13 +180,7 @@ public class Alignment implements AlignmentI
      */
     public void deleteSequence(SequenceI s)
     {
-        for (int i = 0; i < getHeight(); i++)
-        {
-            if (getSequenceAt(i) == s)
-            {
-                deleteSequence(i);
-            }
-        }
+      deleteSequence(findIndex(s));
     }
 
     /**
@@ -318,7 +190,11 @@ public class Alignment implements AlignmentI
      */
     public void deleteSequence(int i)
     {
+      if(i>-1 && i<getHeight())
+      {
         sequences.removeElementAt(i);
+        hiddenSequences.adjustHeightSequenceDeleted(i);
+      }
     }
 
 
@@ -383,7 +259,26 @@ public class Alignment implements AlignmentI
     {
         if (!groups.contains(sg))
         {
-            groups.addElement(sg);
+          if(hiddenSequences.getSize()>0)
+          {
+            //We're not going to make groups of
+            //Hidden sequences
+            int i, iSize = sg.getSize(false);
+            for (i = 0; i < iSize; i++)
+            {
+              if (!sequences.contains(sg.getSequenceAt(i)))
+              {
+                sg.deleteSequence(sg.getSequenceAt(i), false);
+                iSize--;
+                i--;
+              }
+            }
+
+            if (sg.getSize(true) < 1)
+              return;
+          }
+
+          groups.addElement(sg);
         }
     }
 
@@ -393,15 +288,6 @@ public class Alignment implements AlignmentI
     public void deleteAllGroups()
     {
         groups.removeAllElements();
-
-        int i = 0;
-
-        while (i < sequences.size())
-        {
-            SequenceI s = getSequenceAt(i);
-            s.setColor(java.awt.Color.white);
-            i++;
-        }
     }
 
     /**    */
@@ -431,6 +317,28 @@ public class Alignment implements AlignmentI
         return null;
     }
 
+    public SequenceI [] findSequenceMatch(String name)
+    {
+      Vector matches = new Vector();
+      int i = 0;
+
+      while (i < sequences.size())
+      {
+          if (getSequenceAt(i).getName().equals(name))
+          {
+              matches.addElement(getSequenceAt(i));
+          }
+          i++;
+      }
+
+      SequenceI [] result = new SequenceI[matches.size()];
+      for(i=0; i<result.length; i++)
+        result[i] = (SequenceI)matches.elementAt(i);
+
+      return result;
+
+    }
+
 
     /**    */
     public int findIndex(SequenceI s)
@@ -480,32 +388,6 @@ public class Alignment implements AlignmentI
         return maxLength;
     }
 
-    /**
-     * DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     */
-    public int getMaxIdLength()
-    {
-        int max = 0;
-        int i = 0;
-
-        while (i < sequences.size())
-        {
-            SequenceI seq = getSequenceAt(i);
-            String tmp = seq.getName() + "/" + seq.getStart() + "-" +
-                seq.getEnd();
-
-            if (tmp.length() > max)
-            {
-                max = tmp.length();
-            }
-
-            i++;
-        }
-
-        return max;
-    }
 
     /**
      * DOCUMENT ME!
@@ -519,9 +401,9 @@ public class Alignment implements AlignmentI
         for (int i = 0; i < sequences.size(); i++)
         {
             Sequence seq = (Sequence) sequences.elementAt(i);
-            seq.setSequence( seq.getSequence().replace('.', gc) );
-            seq.setSequence( seq.getSequence().replace('-', gc) );
-            seq.setSequence( seq.getSequence().replace(' ', gc) );
+            seq.setSequence( seq.getSequenceAsString().replace('.', gc) );
+            seq.setSequence( seq.getSequenceAsString().replace('-', gc) );
+            seq.setSequence( seq.getSequenceAsString().replace(' ', gc) );
         }
     }
 
@@ -535,15 +417,6 @@ public class Alignment implements AlignmentI
         return gapCharacter;
     }
 
-    /**
-     * DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     */
-    public Vector getAAFrequency()
-    {
-        return AAFrequency.calculate(sequences, 0, getWidth());
-    }
 
     /**
      * DOCUMENT ME!
@@ -579,6 +452,9 @@ public class Alignment implements AlignmentI
             aSize = annotations.length;
         }
 
+        if(aSize<1)
+          return;
+
         AlignmentAnnotation[] temp = new AlignmentAnnotation[aSize - 1];
 
         int tIndex = 0;
@@ -700,22 +576,26 @@ public class Alignment implements AlignmentI
         // Can only be done once, if dataset is not null
         // This will not be performed
         Sequence[] seqs = new Sequence[getHeight()];
+        SequenceI currentSeq;
         for (int i = 0; i < getHeight(); i++)
         {
-          if(getSequenceAt(i).getDatasetSequence()!=null)
+          currentSeq = getSequenceAt(i);
+          if(currentSeq.getDatasetSequence()!=null)
           {
-            seqs[i] = (Sequence)getSequenceAt(i).getDatasetSequence();
+            seqs[i] = (Sequence)currentSeq.getDatasetSequence();
           }
           else
           {
-            seqs[i] = new Sequence(getSequenceAt(i).getName(),
+            seqs[i] = new Sequence(currentSeq.getName(),
                                    AlignSeq.extractGaps(
                                        jalview.util.Comparison.GapChars,
-                                       getSequenceAt(i).getSequence()
+                                       currentSeq.getSequenceAsString()
                                    ),
-                                   getSequenceAt(i).getStart(),
-                                   getSequenceAt(i).getEnd());
-
+                                   currentSeq.getStart(),
+                                   currentSeq.getEnd());
+            seqs[i].sequenceFeatures = currentSeq.getSequenceFeatures();
+            seqs[i].setDescription(currentSeq.getDescription());
+            getSequenceAt(i).setSequenceFeatures(null);
             getSequenceAt(i).setDatasetSequence(seqs[i]);
           }
         }
@@ -733,7 +613,8 @@ public class Alignment implements AlignmentI
       return dataset;
     }
 
-    public boolean padGaps() {
+    public boolean padGaps()
+    {
       boolean modified=false;
 
       //Remove excess gaps from the end of alignment
@@ -756,14 +637,17 @@ public class Alignment implements AlignmentI
 
       maxLength++;
 
+      int cLength;
       for (int i = 0; i < sequences.size();
            i++)
       {
         current = getSequenceAt(i);
+        cLength = current.getLength();
 
-        if (current.getLength() < maxLength)
+        if (cLength < maxLength)
         {
-          current.insertCharAt(maxLength - 1, gapCharacter);
+          current.insertCharAt(cLength,
+                              maxLength-cLength, gapCharacter);
           modified=true;
         }
         else if(current.getLength() > maxLength)
@@ -811,9 +695,11 @@ public class Alignment implements AlignmentI
   {
     SeqCigar alseqs[] = new SeqCigar[sequences.size()];
     for (int i=0; i<sequences.size(); i++) {
-      alseqs[i] = new SeqCigar((SequenceI) sequences.get(i));
+      alseqs[i] = new SeqCigar((SequenceI) sequences.elementAt(i));
     }
-    return new CigarArray(alseqs);
+    CigarArray cal = new CigarArray(alseqs);
+    cal.addOperation(CigarArray.M, getWidth());
+    return cal;
   }
 
 }