From 0d9271962a4c5e720ec8284d579bda635b59b231 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Mon, 22 Aug 2016 12:55:36 +0100 Subject: [PATCH] JAL-2154 always check dataset sequence for sequence instance exists in dataset before adding it in AlignmentI.addSequence() --- src/jalview/datamodel/Alignment.java | 15 +++++++++------ test/jalview/datamodel/AlignmentTest.java | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index fd918fc..2f64759 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -226,18 +226,21 @@ public class Alignment implements AlignmentI { if (dataset != null) { + // maintain dataset integrity - if (snew.getDatasetSequence() != null) - { - getDataset().addSequence(snew.getDatasetSequence()); - } - else + SequenceI dsseq = snew.getDatasetSequence(); + if (dsseq == null) { // derive new sequence SequenceI adding = snew.deriveSequence(); - getDataset().addSequence(adding.getDatasetSequence()); snew = adding; + dsseq = snew.getDatasetSequence(); } + if (getDataset().findIndex(dsseq) == -1) + { + getDataset().addSequence(dsseq); + } + } if (sequences == null) { diff --git a/test/jalview/datamodel/AlignmentTest.java b/test/jalview/datamodel/AlignmentTest.java index 379a9c5..2863340 100644 --- a/test/jalview/datamodel/AlignmentTest.java +++ b/test/jalview/datamodel/AlignmentTest.java @@ -857,6 +857,27 @@ public class AlignmentTest } @Test(groups = "Functional") + public void testAddSequencePreserveDatasetIntegrity() + { + Sequence seq = new Sequence("testSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + Alignment align = new Alignment(new SequenceI[] { seq }); + align.createDatasetAlignment(); + AlignmentI ds = align.getDataset(); + SequenceI copy = new Sequence(seq); + copy.insertCharAt(3, 5, '-'); + align.addSequence(copy); + Assert.assertEquals(align.getDataset().getHeight(), 1, + "Dataset shouldn't have more than one sequence."); + + Sequence seq2 = new Sequence("newtestSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + align.addSequence(seq2); + Assert.assertEquals(align.getDataset().getHeight(), 2, + "Dataset should now have two sequences."); + + assertAlignmentDatasetRefs(align, + "addSequence broke dataset reference integrity"); + } + @Test(groups = "Functional") public void getVisibleStartAndEndIndexTest() { Sequence seq = new Sequence("testSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); -- 1.7.10.2