JAL-3899 Refactor sequence de/uniquification.
[jalview.git] / test / jalview / analysis / SeqsetUtilsTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.analysis;
22
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.Sequence;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.gui.JvOptionPane;
29
30 import java.util.Hashtable;
31 import java.util.Map;
32
33 import org.testng.Assert;
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.Test;
36
37 /**
38  * @author jprocter
39  *
40  */
41 public class SeqsetUtilsTest
42 {
43
44   @BeforeClass(alwaysRun = true)
45   public void setUpJvOptionPane()
46   {
47     JvOptionPane.setInteractiveMode(false);
48     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
49   }
50
51
52   /**
53    * test for JAL-2046 bug - duplication of sequence features on reconstructed
54    * alignment
55    */
56   @Test(groups = { "Functional" })
57   public void testSeqFeatureAddition()
58   {
59     SequenceI[] sqset = new SequenceI[] {
60         new Sequence("Aseq1", "AREALSEQ"),
61         new Sequence("Aseq2", "AREALSEQ") };
62
63     AlignmentI al = new Alignment(sqset);
64     al.setDataset(null);
65     AlignmentI ds = al.getDataset();
66     SequenceFeature sf1 = new SequenceFeature("f1", "foo", 2, 3, "far");
67     SequenceFeature sf2 = new SequenceFeature("f2", "foo", 2, 3, "far");
68     ds.getSequenceAt(0).addSequenceFeature(sf1);
69     Map<String, SeqsetUtils.SequenceInfo> unq = SeqsetUtils.uniquify(sqset, true);
70     SequenceI[] sqset2 = new SequenceI[] {
71         new Sequence(sqset[0].getName(), sqset[0].getSequenceAsString()),
72         new Sequence(sqset[1].getName(), sqset[1].getSequenceAsString()) };
73     Assert.assertSame(sqset[0].getSequenceFeatures().get(0), sf1);
74     Assert.assertTrue(sqset2[0].getSequenceFeatures().isEmpty());
75     ds.getSequenceAt(0).addSequenceFeature(sf2);
76     Assert.assertEquals(sqset[0].getSequenceFeatures().size(), 2);
77     SeqsetUtils.deuniquify(unq, sqset2);
78     // explicitly test that original sequence features still exist because they
79     // are on the shared dataset sequence
80     Assert.assertEquals(sqset[0].getSequenceFeatures().size(), 2);
81     Assert.assertEquals(sqset2[0].getSequenceFeatures().size(), 2);
82     Assert.assertSame(sqset[0].getSequenceFeatures().get(0), sqset2[0]
83             .getSequenceFeatures().get(0));
84     Assert.assertSame(sqset[0].getSequenceFeatures().get(1), sqset2[0]
85             .getSequenceFeatures().get(1));
86   }
87 }