JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / jalview / datamodel / AlignmentView.java
index 2d234fe..9db9f38 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -26,7 +26,6 @@ import jalview.util.ShiftList;
 import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Vector;
 
 /**
  * Transient object compactly representing a 'view' of an alignment - with
@@ -47,7 +46,7 @@ public class AlignmentView
    * one or more ScGroup objects, which are referenced by each seqCigar's group
    * membership
    */
-  private List<ScGroup> scGroups=null;
+  private List<ScGroup> scGroups = null;
 
   private boolean isNa = false;
 
@@ -69,13 +68,51 @@ public class AlignmentView
    */
   private class ScGroup
   {
-    public Vector seqs;
+    public List<SeqCigar> seqs;
 
     public SequenceGroup sg;
 
     ScGroup()
     {
-      seqs = new Vector();
+      seqs = new ArrayList<SeqCigar>();
+    }
+
+    /**
+     * @param seq
+     * @return true if seq was not a member before and was added to group
+     */
+    public boolean add(SeqCigar seq)
+    {
+      if (!seq.isMemberOf(this))
+      {
+        seqs.add(seq);
+        seq.setGroupMembership(this);
+        return true;
+      }
+      else
+      {
+        return false;
+      }
+    }
+
+    /**
+     * 
+     * @param seq
+     * @return true if seq was a member and was removed from group
+     */
+    public boolean remove(SeqCigar seq)
+    {
+      if (seq.removeGroupMembership(this))
+      {
+        seqs.remove(seq);
+        return true;
+      }
+      return false;
+    }
+
+    public int size()
+    {
+      return seqs.size();
     }
   }
 
@@ -83,7 +120,7 @@ public class AlignmentView
    * vector of selected seqCigars. This vector is also referenced by each
    * seqCigar contained in it.
    */
-  private Vector selected;
+  private ScGroup selected;
 
   /**
    * Construct an alignmentView from a live jalview alignment view. Note -
@@ -124,7 +161,7 @@ public class AlignmentView
     if (selection != null && selection.getSize() > 0)
     {
       List<SequenceI> sel = selection.getSequences(null);
-      this.selected = new Vector();
+      this.selected = new ScGroup();
       selseqs = selection
               .getSequencesInOrder(alignment, selectedRegionOnly);
     }
@@ -133,7 +170,7 @@ public class AlignmentView
       selseqs = alignment.getSequencesArray();
     }
 
-    List<List<SequenceI>> seqsets=new ArrayList<List<SequenceI>>();
+    List<List<SequenceI>> seqsets = new ArrayList<List<SequenceI>>();
     // get the alignment's group list and make a copy
     List<SequenceGroup> grps = new ArrayList<SequenceGroup>();
     List<SequenceGroup> gg = alignment.getGroups();
@@ -179,7 +216,9 @@ public class AlignmentView
         sgrps[g] = new ScGroup();
         sgrps[g].sg = new SequenceGroup(sg);
         addedgps[g] = false;
-        seqsets.set(g, sg.getSequences(null));
+        // can't set entry 0 in an empty list
+        // seqsets.set(g, sg.getSequences(null));
+        seqsets.add(sg.getSequences());
       }
       // seqsets now contains vectors (should be sets) for each group, so we can
       // track when we've done with the group
@@ -192,8 +231,7 @@ public class AlignmentView
         if (selection != null && selection.getSize() > 0
                 && !selectedRegionOnly)
         {
-          sequences[csi].setGroupMembership(selected);
-          selected.addElement(sequences[csi]);
+          selected.add(sequences[csi]);
         }
         if (seqsets != null)
         {
@@ -201,9 +239,8 @@ public class AlignmentView
           {
             if ((seqsets.get(sg)).contains(selseqs[i]))
             {
-              sequences[csi].setGroupMembership(sgrps[sg]);
               sgrps[sg].sg.deleteSequence(selseqs[i], false);
-              sgrps[sg].seqs.addElement(sequences[csi]);
+              sgrps[sg].add(sequences[csi]);
               if (!addedgps[sg])
               {
                 if (scGroups == null)
@@ -239,7 +276,8 @@ public class AlignmentView
   {
     if (!seqcigararray.isSeqCigarArray())
     {
-      throw new Error(MessageManager.getString("error.implementation_error_can_only_make_alignmnet_from_cigararray"));
+      throw new Error(
+              "Implementation Error - can only make an alignment view from a CigarArray of sequences.");
     }
     // contigs = seqcigararray.applyDeletions();
     contigs = seqcigararray.getDeletedRegions();
@@ -296,8 +334,7 @@ public class AlignmentView
   {
     ColumnSelection colsel = new ColumnSelection();
 
-    return new Object[]
-    {
+    return new Object[] {
         SeqCigar.createAlignmentSequences(sequences, gapCharacter, colsel,
                 contigs), colsel };
   }
@@ -463,6 +500,7 @@ public class AlignmentView
    * alignment.
    * 
    * @param c
+   *          gap character to use to recreate the alignment
    * @return
    */
   private SequenceI[] getVisibleSeqs(char c)
@@ -470,13 +508,9 @@ public class AlignmentView
     SequenceI[] aln = new SequenceI[sequences.length];
     for (int i = 0, j = sequences.length; i < j; i++)
     {
-      aln[i] = sequences[i].getSeq('-');
-    }
-    // Remove hidden regions from sequence objects.
-    String seqs[] = getSequenceStrings('-');
-    for (int i = 0, j = aln.length; i < j; i++)
-    {
-      aln[i].setSequence(seqs[i]);
+      aln[i] = sequences[i].getSeq(c);
+      // Remove hidden regions from sequence
+      aln[i].setSequence(getASequenceString(c, i));
     }
     return aln;
   }
@@ -512,8 +546,38 @@ public class AlignmentView
   }
 
   /**
+   * build a string excluding hidden regions from a particular sequence in the
+   * view
+   * 
+   * @param c
+   * @param n
+   * @return
+   */
+  private String getASequenceString(char c, int n)
+  {
+    String sqn;
+    String fullseq = sequences[n].getSequenceString(c);
+    if (contigs != null)
+    {
+      sqn = "";
+      int p = 0;
+      for (int h = 0; h < contigs.length; h += 3)
+      {
+        sqn += fullseq.substring(p, contigs[h + 1]);
+        p = contigs[h + 1] + contigs[h + 2];
+      }
+      sqn += fullseq.substring(p);
+    }
+    else
+    {
+      sqn = fullseq;
+    }
+    return sqn;
+  }
+
+  /**
    * get an array of visible sequence strings for a view on an alignment using
-   * the given gap character
+   * the given gap character uses getASequenceString
    * 
    * @param c
    *          char
@@ -524,22 +588,7 @@ public class AlignmentView
     String[] seqs = new String[sequences.length];
     for (int n = 0; n < sequences.length; n++)
     {
-      String fullseq = sequences[n].getSequenceString(c);
-      if (contigs != null)
-      {
-        seqs[n] = "";
-        int p = 0;
-        for (int h = 0; h < contigs.length; h += 3)
-        {
-          seqs[n] += fullseq.substring(p, contigs[h + 1]);
-          p = contigs[h + 1] + contigs[h + 2];
-        }
-        seqs[n] += fullseq.substring(p);
-      }
-      else
-      {
-        seqs[n] = fullseq;
-      }
+      seqs[n] = getASequenceString(c, n);
     }
     return seqs;
   }
@@ -649,7 +698,9 @@ public class AlignmentView
   {
     if (sequences == null || width <= 0)
     {
-      throw new Error(MessageManager.getString("error.empty_view_cannot_be_updated"));
+      throw new Error(
+              MessageManager
+                      .getString("error.empty_view_cannot_be_updated"));
     }
     if (nvismsa == null)
     {
@@ -679,7 +730,17 @@ public class AlignmentView
               j++;
               if (mseq.length != sequences.length)
               {
-                throw new Error(MessageManager.formatMessage("error.mismatch_between_number_of_sequences_in_block", new String[]{Integer.valueOf(j).toString(),Integer.valueOf(mseq.length).toString(),Integer.valueOf(sequences.length).toString() }));
+                throw new Error(
+                        MessageManager
+                                .formatMessage(
+                                        "error.mismatch_between_number_of_sequences_in_block",
+                                        new String[] {
+                                            Integer.valueOf(j).toString(),
+                                            Integer.valueOf(mseq.length)
+                                                    .toString(),
+                                            Integer.valueOf(
+                                                    sequences.length)
+                                                    .toString() }));
               }
               swidth = mseq[0].getLength(); // JBPNote: could ensure padded
               // here.
@@ -832,25 +893,30 @@ public class AlignmentView
               else
               {
                 // place gaps.
-                throw new Error(MessageManager.getString("error.padding_not_yet_implemented"));
+                throw new Error(
+                        MessageManager
+                                .getString("error.padding_not_yet_implemented"));
               }
             }
           }
         }
       }
-      return new Object[]
-      { alignment, columnselection };
+      return new Object[] { alignment, columnselection };
     }
     else
     {
       if (nvismsa.length != 1)
       {
-        throw new Error(MessageManager.formatMessage("error.mismatch_between_visible_blocks_to_update_and_number_of_contigs_in_view", new String[]{Integer.valueOf(nvismsa.length).toString()}));
+        throw new Error(
+                MessageManager
+                        .formatMessage(
+                                "error.mismatch_between_visible_blocks_to_update_and_number_of_contigs_in_view",
+                                new String[] { Integer.valueOf(
+                                        nvismsa.length).toString() }));
       }
       if (nvismsa[0] != null)
       {
-        return new Object[]
-        { nvismsa[0], new ColumnSelection() };
+        return new Object[] { nvismsa[0], new ColumnSelection() };
       }
       else
       {
@@ -911,8 +977,7 @@ public class AlignmentView
     }
     else
     {
-      return new int[]
-      { 0, width };
+      return new int[] { 0, width };
     }
   }
 
@@ -1042,10 +1107,10 @@ public class AlignmentView
                 + sgr.sg.getEndRes());
         for (int s = 0; s < sgr.seqs.size(); s++)
         {
-          if (!((SeqCigar) sgr.seqs.elementAt(s)).isMemberOf(sgr))
+          // JBPnote this should be a unit test for ScGroup
+          if (!sgr.seqs.get(s).isMemberOf(sgr))
           {
-            os.println("** WARNING: sequence "
-                    + ((SeqCigar) sgr.seqs.elementAt(s)).toString()
+            os.println("** WARNING: sequence " + sgr.seqs.get(s).toString()
                     + " is not marked as member of group.");
           }
         }