apply version 2.7 copyright
[jalview.git] / src / jalview / datamodel / SeqCigar.java
index cc9eaa7..182ad7b 100644 (file)
@@ -1,24 +1,25 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
- * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
+ * Copyright (C) 2011 J Procter, AM Waterhouse, 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 file is part of Jalview.
  * 
- * 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.
+ * Jalview 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 3 of the License, or (at your option) any later version.
  * 
- * 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 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 Jalview.  If not, see <http://www.gnu.org/licenses/>.
  */
 package jalview.datamodel;
 
+import java.util.Enumeration;
 import java.util.Hashtable;
+import java.util.Vector;
 
 import jalview.analysis.*;
 import jalview.util.*;
@@ -69,8 +70,8 @@ public class SeqCigar extends CigarSimple
    */
   public String getSequenceString(char GapChar)
   {
-    return (length == 0) ? "" : (String) getSequenceAndDeletions(refseq
-            .getSequenceAsString(start, end), GapChar)[0];
+    return (length == 0) ? "" : (String) getSequenceAndDeletions(
+            refseq.getSequenceAsString(start, end), GapChar)[0];
   }
 
   /**
@@ -86,18 +87,17 @@ public class SeqCigar extends CigarSimple
     {
       return null;
     }
-    Object[] edit_result = getSequenceAndDeletions(refseq
-            .getSequenceAsString(start, end), GapChar);
+    Object[] edit_result = getSequenceAndDeletions(
+            refseq.getSequenceAsString(start, end), GapChar);
     if (edit_result == null)
     {
       throw new Error(
               "Implementation Error - unexpected null from getSequenceAndDeletions");
     }
     int bounds[] = (int[]) edit_result[1];
-    seq = new Sequence(refseq.getName(), (String) edit_result[0], refseq
-            .getStart()
-            + start + bounds[0], refseq.getStart() + start
-            + ((bounds[2] == 0) ? -1 : bounds[2]));
+    seq = new Sequence(refseq.getName(), (String) edit_result[0],
+            refseq.getStart() + start + bounds[0], refseq.getStart()
+                    + start + ((bounds[2] == 0) ? -1 : bounds[2]));
     seq.setDescription(refseq.getDescription());
     int sstart = seq.getStart(), send = seq.getEnd();
     // seq.checkValidRange(); probably not needed
@@ -124,14 +124,14 @@ public class SeqCigar extends CigarSimple
    * prepends any 'D' operations needed to get to the first residue of seq.
    * 
    * @param seq
-   *                SequenceI
+   *          SequenceI
    * @param initialDeletion
-   *                true to mark initial dataset sequence residues as deleted in
-   *                subsequence
+   *          true to mark initial dataset sequence residues as deleted in
+   *          subsequence
    * @param _s
-   *                index of first position in seq
+   *          index of first position in seq
    * @param _e
-   *                index after last position in (possibly gapped) seq
+   *          index after last position in (possibly gapped) seq
    * @return true if gaps are present in seq
    */
   private boolean _setSeq(SequenceI seq, boolean initialDeletion, int _s,
@@ -171,9 +171,8 @@ public class SeqCigar extends CigarSimple
       }
       else
       {
-        ds = new Sequence(seq.getName(), ungapped, seq.getStart(), seq
-                .getStart()
-                + ungapped.length() - 1);
+        ds = new Sequence(seq.getName(), ungapped, seq.getStart(),
+                seq.getStart() + ungapped.length() - 1);
         // JBPNote: this would be consistent but may not be useful
         // seq.setDatasetSequence(ds);
       }
@@ -225,11 +224,11 @@ public class SeqCigar extends CigarSimple
    * to the seq.getStart()'th residue of the dataset seq resolved from seq.
    * 
    * @param seq
-   *                SequenceI
+   *          SequenceI
    * @param operation
-   *                char[]
+   *          char[]
    * @param range
-   *                int[]
+   *          int[]
    */
   public SeqCigar(SequenceI seq, char operation[], int range[])
   {
@@ -285,7 +284,7 @@ public class SeqCigar extends CigarSimple
    * add range matched residues to cigar string
    * 
    * @param range
-   *                int
+   *          int
    */
   public void addMatch(int range)
   {
@@ -297,16 +296,16 @@ public class SeqCigar extends CigarSimple
    * endpos column of seq.
    * 
    * @param cigar
-   *                CigarBase
+   *          CigarBase
    * @param seq
-   *                SequenceI
+   *          SequenceI
    * @param startpos
-   *                int
+   *          int
    * @param endpos
-   *                int
+   *          int
    * @param initialDeletions
-   *                if true then initial deletions will be added from start of
-   *                seq to startpos
+   *          if true then initial deletions will be added from start of seq to
+   *          startpos
    */
   protected static void addSequenceOps(CigarBase cigar, SequenceI seq,
           int startpos, int endpos, boolean initialDeletions)
@@ -376,7 +375,7 @@ public class SeqCigar extends CigarSimple
    * create a cigar string for given sequence
    * 
    * @param seq
-   *                SequenceI
+   *          SequenceI
    */
   public SeqCigar(SequenceI seq)
   {
@@ -394,11 +393,11 @@ public class SeqCigar extends CigarSimple
    * Create Cigar from a range of gaps and residues on a sequence object
    * 
    * @param seq
-   *                SequenceI
+   *          SequenceI
    * @param start
-   *                int - first column in range
+   *          int - first column in range
    * @param end
-   *                int - last column in range
+   *          int - last column in range
    */
   public SeqCigar(SequenceI seq, int start, int end)
   {
@@ -418,9 +417,9 @@ public class SeqCigar extends CigarSimple
    * will fix)
    * 
    * @param seq
-   *                SequenceI object resolvable to a dataset sequence
+   *          SequenceI object resolvable to a dataset sequence
    * @param cigarString
-   *                String
+   *          String
    * @return Cigar
    */
   public static SeqCigar parseCigar(SequenceI seq, String cigarString)
@@ -432,12 +431,16 @@ public class SeqCigar extends CigarSimple
   }
 
   /**
-   * createAlignment
+   * create an alignment from the given array of cigar sequences and gap
+   * character, and marking the given segments as visible in the given
+   * columselection.
    * 
    * @param alseqs
-   *                SeqCigar[]
    * @param gapCharacter
-   *                char
+   * @param colsel
+   *          - columnSelection where hidden regions are marked
+   * @param segments
+   *          - visible regions of alignment
    * @return SequenceI[]
    */
   public static SequenceI[] createAlignmentSequences(SeqCigar[] alseqs,
@@ -453,16 +456,16 @@ public class SeqCigar extends CigarSimple
               alseqs[i].start, alseqs[i].end);
       gs_regions[i] = alseqs[i].getSequenceAndDeletions(alseqs_string[i],
               gapCharacter); // gapped sequence, {start, start col, end.
-                              // endcol}, hidden regions {{start, end, col}})
+      // endcol}, hidden regions {{start, end, col}})
       if (gs_regions[i] == null)
       {
         throw new Error("Implementation error: " + i
                 + "'th sequence Cigar has no operations.");
       }
       g_seqs[i] = new StringBuffer((String) ((Object[]) gs_regions[i])[0]); // the
-                                                                            // visible
-                                                                            // gapped
-                                                                            // sequence
+      // visible
+      // gapped
+      // sequence
     }
     // Now account for insertions. (well - deletions)
     // this is complicated because we must keep track of shifted positions in
@@ -483,8 +486,8 @@ public class SeqCigar extends CigarSimple
             insert[s] = gapCharacter;
           }
           int inspos = shifts.shift(region[2]); // resolve insertion position in
-                                                // current alignment frame of
-                                                // reference
+          // current alignment frame of
+          // reference
           for (int s = 0; s < alseqs.length; s++)
           {
             if (s != i)
@@ -495,20 +498,20 @@ public class SeqCigar extends CigarSimple
                 for (int l = inspos - g_seqs[s].length(); l > 0; l--)
                 {
                   g_seqs[s].append(gapCharacter); // to debug - use a diffferent
-                                                  // gap character here
+                  // gap character here
                 }
               }
               g_seqs[s].insert(inspos, insert);
             }
             else
             {
-              g_seqs[s].insert(inspos, alseqs_string[i].substring(
-                      region[0], region[1] + 1));
+              g_seqs[s].insert(inspos,
+                      alseqs_string[i].substring(region[0], region[1] + 1));
             }
           }
           shifts.addShift(region[2], insert.length); // update shift in
-                                                      // alignment frame of
-                                                      // reference
+          // alignment frame of
+          // reference
           if (segments == null)
           {
             // add a hidden column for this deletion
@@ -521,10 +524,9 @@ public class SeqCigar extends CigarSimple
     {
       int[] bounds = ((int[]) ((Object[]) gs_regions[i])[1]);
       SequenceI ref = alseqs[i].getRefSeq();
-      seqs[i] = new Sequence(ref.getName(), g_seqs[i].toString(), ref
-              .getStart()
-              + alseqs[i].start + bounds[0], ref.getStart()
-              + alseqs[i].start + (bounds[2] == 0 ? -1 : bounds[2]));
+      seqs[i] = new Sequence(ref.getName(), g_seqs[i].toString(),
+              ref.getStart() + alseqs[i].start + bounds[0], ref.getStart()
+                      + alseqs[i].start + (bounds[2] == 0 ? -1 : bounds[2]));
       seqs[i].setDatasetSequence(ref);
       seqs[i].setDescription(ref.getDescription());
     }
@@ -547,9 +549,9 @@ public class SeqCigar extends CigarSimple
   /**
    * 
    * @param seq
-   *                Sequence
+   *          Sequence
    * @param ex_cs_gapped
-   *                String
+   *          String
    * @return String
    */
   public static String testCigar_string(Sequence seq, String ex_cs_gapped)
@@ -692,4 +694,77 @@ public class SeqCigar extends CigarSimple
     // System.err.println("Subseqgaped\n------ktryas---dtqwrtsasll----dddptyipqqwa----slchvhryas---dtqwrtsasll--qwa----slchvh\n"+ssgapedseq+"\n"+sub_se_gp.getCigarstring());
   }
 
+  /**
+   * references to entities that this sequence cigar is associated with.
+   */
+  private Hashtable selGroups = null;
+
+  public void setGroupMembership(Object group)
+  {
+    if (selGroups == null)
+    {
+      selGroups = new Hashtable();
+    }
+    selGroups.put(group, new int[0]);
+  }
+
+  /**
+   * Test for and if present remove association to group.
+   * 
+   * @param group
+   * @return true if group was associated and it was removed
+   */
+  public boolean removeGroupMembership(Object group)
+  {
+    if (selGroups != null && selGroups.containsKey(group))
+    {
+      selGroups.remove(group);
+      return true;
+    }
+    return false;
+  }
+
+  /**
+   * forget all associations for this sequence.
+   */
+  public void clearMemberships()
+  {
+    if (selGroups != null)
+    {
+      selGroups.clear();
+    }
+    selGroups = null;
+  }
+
+  /**
+   * 
+   * @return null or array of all associated entities
+   */
+  public Object[] getAllMemberships()
+  {
+    if (selGroups == null)
+    {
+      return null;
+    }
+    Object[] mmbs = new Object[selGroups.size()];
+    Enumeration en = selGroups.keys();
+    for (int i = 0; en.hasMoreElements(); i++)
+    {
+      mmbs[i] = en.nextElement();
+    }
+    return mmbs;
+  }
+
+  /**
+   * Test for group membership
+   * 
+   * @param sgr
+   *          - a selection group or some other object that may be associated
+   *          with seqCigar
+   * @return true if sgr is associated with this seqCigar
+   */
+  public boolean isMemberOf(Object sgr)
+  {
+    return (selGroups != null) && selGroups.get(sgr) != null;
+  }
 }