JAL-3198 SequenceGroup 'bulk constructor' without duplicates check
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Sun, 24 Feb 2019 08:10:17 +0000 (08:10 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Sun, 24 Feb 2019 08:10:17 +0000 (08:10 +0000)
src/jalview/datamodel/SequenceGroup.java
src/jalview/gui/AlignFrame.java
src/jalview/gui/ScalePanel.java
test/jalview/datamodel/SequenceGroupTest.java

index fc8ac49..3bf7bc4 100755 (executable)
@@ -88,7 +88,7 @@ public class SequenceGroup implements AnnotatedCollectionI
   /**
    * group members
    */
-  private List<SequenceI> sequences = new ArrayList<>();
+  private List<SequenceI> sequences;
 
   /**
    * representative sequence for this group (if any)
@@ -162,6 +162,7 @@ public class SequenceGroup implements AnnotatedCollectionI
   {
     groupName = "JGroup:" + this.hashCode();
     cs = new ResidueShader();
+    sequences = new ArrayList<>();
   }
 
   /**
@@ -243,6 +244,17 @@ public class SequenceGroup implements AnnotatedCollectionI
     }
   }
 
+  /**
+   * Constructor that copies the given list of sequences
+   * 
+   * @param seqs
+   */
+  public SequenceGroup(List<SequenceI> seqs)
+  {
+    this();
+    this.sequences.addAll(seqs);
+  }
+
   public boolean isShowSequenceLogo()
   {
     return showSequenceLogo;
index 86b4422..936e714 100644 (file)
@@ -2398,12 +2398,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   @Override
   public void selectAllSequenceMenuItem_actionPerformed(ActionEvent e)
   {
-    SequenceGroup sg = new SequenceGroup();
-
-    for (int i = 0; i < viewport.getAlignment().getSequences().size(); i++)
-    {
-      sg.addSequence(viewport.getAlignment().getSequenceAt(i), false);
-    }
+    SequenceGroup sg = new SequenceGroup(
+            viewport.getAlignment().getSequences());
 
     sg.setEndRes(viewport.getAlignment().getWidth() - 1);
     viewport.setSelectionGroup(sg);
index 6abee08..5821b61 100755 (executable)
@@ -23,7 +23,6 @@ package jalview.gui;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.SequenceGroup;
-import jalview.datamodel.SequenceI;
 import jalview.renderer.ScaleRenderer;
 import jalview.renderer.ScaleRenderer.ScaleMark;
 import jalview.util.MessageManager;
@@ -237,15 +236,7 @@ public class ScalePanel extends JPanel
     }
 
     av.getColumnSelection().addElement(res);
-    SequenceGroup sg = new SequenceGroup();
-    // try to be as quick as possible
-    SequenceI[] iVec = av.getAlignment().getSequencesArray();
-    for (int i = 0; i < iVec.length; i++)
-    {
-      sg.addSequence(iVec[i], false);
-      iVec[i] = null;
-    }
-    iVec = null;
+    SequenceGroup sg = new SequenceGroup(av.getAlignment().getSequences());
     sg.setStartRes(res);
     sg.setEndRes(res);
 
index b0af5c8..8419d4c 100644 (file)
@@ -14,12 +14,14 @@ import jalview.schemes.NucleotideColourScheme;
 import jalview.schemes.PIDColourScheme;
 
 import java.awt.Color;
+import java.util.ArrayList;
 import java.util.Collections;
-
-import junit.extensions.PA;
+import java.util.List;
 
 import org.testng.annotations.Test;
 
+import junit.extensions.PA;
+
 public class SequenceGroupTest
 {
   @Test(groups={"Functional"})
@@ -301,4 +303,36 @@ public class SequenceGroupTest
      */
     assertNull(sg2.getContext());
   }
+
+  @Test(groups = { "Functional" })
+  public void testConstructor_list()
+  {
+    SequenceI s1 = new Sequence("abcde", "fg");
+    SequenceI s2 = new Sequence("foo", "bar");
+    List<SequenceI> seqs = new ArrayList<SequenceI>();
+    seqs.add(s1);
+    seqs.add(s2);
+    SequenceGroup sg = new SequenceGroup(seqs);
+
+    /*
+     * verify sg has a copy of the original list
+     */
+    List<SequenceI> sgList = sg.getSequences();
+    assertNotSame(sgList, seqs);
+    assertEquals(sgList, seqs);
+
+    /*
+     * add to sgList, original is unchanged
+     */
+    sg.addSequence(new Sequence("bar", "foo"), false);
+    assertEquals(sgList.size(), 3);
+    assertEquals(seqs.size(), 2);
+
+    /*
+     * delete from original, sgList is unchanged
+     */
+    seqs.remove(s1);
+    assertEquals(sgList.size(), 3);
+    assertEquals(seqs.size(), 1);
+  }
 }