0a53f10b002e64892f8493c9e49782eef7ae7172
[jalview.git] / test / jalview / gui / AlignViewportTest.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.gui;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertSame;
27 import static org.testng.AssertJUnit.assertTrue;
28
29 import jalview.datamodel.AlignedCodonFrame;
30 import jalview.datamodel.Alignment;
31 import jalview.datamodel.AlignmentI;
32 import jalview.datamodel.PDBEntry;
33 import jalview.datamodel.PDBEntry.Type;
34 import jalview.datamodel.Sequence;
35 import jalview.datamodel.SequenceI;
36 import jalview.io.FileLoader;
37 import jalview.io.FormatAdapter;
38 import jalview.structure.StructureSelectionManager;
39
40 import java.util.LinkedHashSet;
41 import java.util.Set;
42
43 import org.testng.annotations.BeforeClass;
44 import org.testng.annotations.BeforeMethod;
45 import org.testng.annotations.Test;
46
47 public class AlignViewportTest
48 {
49
50   AlignmentI al;
51
52   AlignViewport testee;
53
54   @BeforeClass(alwaysRun = true)
55   public static void setUpBeforeClass() throws Exception
56   {
57     jalview.bin.Jalview.main(new String[] { "-props",
58         "test/jalview/testProps.jvprops" });
59   }
60
61   @BeforeMethod(alwaysRun = true)
62   public void setUp()
63   {
64     SequenceI seq1 = new Sequence("Seq1", "ABC");
65     SequenceI seq2 = new Sequence("Seq2", "ABC");
66     SequenceI seq3 = new Sequence("Seq3", "ABC");
67     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3 };
68     al = new Alignment(seqs);
69     al.setDataset(null);
70     testee = new AlignViewport(al);
71   }
72
73   @Test(groups = { "Functional" })
74   public void testCollateForPdb()
75   {
76     /*
77      * Set up sequence pdb ids
78      */
79     PDBEntry pdb1 = new PDBEntry("1ABC", "A", Type.PDB, "1ABC.pdb");
80     PDBEntry pdb2 = new PDBEntry("2ABC", "A", Type.PDB, "2ABC.pdb");
81     PDBEntry pdb3 = new PDBEntry("3ABC", "A", Type.PDB, "3ABC.pdb");
82
83     /*
84      * seq1 and seq3 refer to 1ABC, seq2 to 2ABC, none to 3ABC
85      */
86     al.getSequenceAt(0).getDatasetSequence()
87             .addPDBId(new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
88     al.getSequenceAt(2).getDatasetSequence()
89             .addPDBId(new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
90     al.getSequenceAt(1).getDatasetSequence()
91             .addPDBId(new PDBEntry("2ABC", "C", Type.PDB, "2ABC.pdb"));
92     /*
93      * Add a second chain PDB xref to Seq2 - should not result in a duplicate in
94      * the results
95      */
96     al.getSequenceAt(1).getDatasetSequence()
97             .addPDBId(new PDBEntry("2ABC", "D", Type.PDB, "2ABC.pdb"));
98     /*
99      * Seq3 refers to 3abc - this does not match 3ABC (as the code stands)
100      */
101     al.getSequenceAt(2).getDatasetSequence()
102             .addPDBId(new PDBEntry("3abc", "D", Type.PDB, "3ABC.pdb"));
103
104     /*
105      * run method under test
106      */
107     SequenceI[][] seqs = testee.collateForPDB(new PDBEntry[] { pdb1, pdb2,
108         pdb3 });
109
110     // seq1 and seq3 refer to PDBEntry[0]
111     assertEquals(2, seqs[0].length);
112     assertSame(al.getSequenceAt(0), seqs[0][0]);
113     assertSame(al.getSequenceAt(2), seqs[0][1]);
114
115     // seq2 refers to PDBEntry[1]
116     assertEquals(1, seqs[1].length);
117     assertSame(al.getSequenceAt(1), seqs[1][0]);
118
119     // no sequence refers to PDBEntry[2]
120     assertEquals(0, seqs[2].length);
121   }
122
123   /**
124    * Test that a mapping is not deregistered when a second view is closed but
125    * the first still holds a reference to the mapping
126    */
127   @Test(groups = { "Functional" })
128   public void testDeregisterMapping_onCloseView()
129   {
130     /*
131      * alignment with reference to mappings
132      */
133     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
134             ">Seq1\nCAGT\n", FormatAdapter.PASTE);
135
136     AlignedCodonFrame acf1 = new AlignedCodonFrame();
137     AlignedCodonFrame acf2 = new AlignedCodonFrame();
138
139     Set<AlignedCodonFrame> mappings = new LinkedHashSet<AlignedCodonFrame>();
140     mappings.add(acf1);
141     mappings.add(acf2);
142     af1.getViewport().getAlignment().setCodonFrames(mappings);
143     af1.newView_actionPerformed(null);
144
145     /*
146      * Verify that creating the alignment for the new View has registered the
147      * mappings
148      */
149     StructureSelectionManager ssm = StructureSelectionManager
150             .getStructureSelectionManager(Desktop.instance);
151     assertEquals(2, ssm.seqmappings.size());
152     assertTrue(ssm.seqmappings.contains(acf1));
153     assertTrue(ssm.seqmappings.contains(acf2));
154
155     /*
156      * Close the second view. Verify that mappings are not removed as the first
157      * view still holds a reference to them.
158      */
159     af1.closeMenuItem_actionPerformed(false);
160     assertEquals(2, ssm.seqmappings.size());
161     assertTrue(ssm.seqmappings.contains(acf1));
162     assertTrue(ssm.seqmappings.contains(acf2));
163   }
164
165   /**
166    * Test that a mapping is deregistered if no alignment holds a reference to it
167    */
168   @Test(groups = { "Functional" })
169   public void testDeregisterMapping_withNoReference()
170   {
171     Desktop d = Desktop.instance;
172     assertNotNull(d);
173     StructureSelectionManager ssm = StructureSelectionManager
174             .getStructureSelectionManager(Desktop.instance);
175     ssm.resetAll();
176
177     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
178             ">Seq1\nRSVQ\n", FormatAdapter.PASTE);
179     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
180             ">Seq2\nDGEL\n", FormatAdapter.PASTE);
181
182     AlignedCodonFrame acf1 = new AlignedCodonFrame();
183     AlignedCodonFrame acf2 = new AlignedCodonFrame();
184     AlignedCodonFrame acf3 = new AlignedCodonFrame();
185
186     Set<AlignedCodonFrame> mappings1 = new LinkedHashSet<AlignedCodonFrame>();
187     mappings1.add(acf1);
188     af1.getViewport().getAlignment().setCodonFrames(mappings1);
189
190     Set<AlignedCodonFrame> mappings2 = new LinkedHashSet<AlignedCodonFrame>();
191     mappings2.add(acf2);
192     mappings2.add(acf3);
193     af2.getViewport().getAlignment().setCodonFrames(mappings2);
194
195     /*
196      * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
197      */
198
199     Set<AlignedCodonFrame> ssmMappings = ssm.seqmappings;
200     assertEquals(0, ssmMappings.size());
201     ssm.registerMapping(acf1);
202     assertEquals(1, ssmMappings.size());
203     ssm.registerMapping(acf2);
204     assertEquals(2, ssmMappings.size());
205     ssm.registerMapping(acf3);
206     assertEquals(3, ssmMappings.size());
207
208     /*
209      * Closing AlignFrame2 should remove its mappings from
210      * StructureSelectionManager, since AlignFrame1 has no reference to them
211      */
212     af2.closeMenuItem_actionPerformed(true);
213     assertEquals(1, ssmMappings.size());
214     assertTrue(ssmMappings.contains(acf1));
215   }
216
217   /**
218    * Test that a mapping is not deregistered if another alignment holds a
219    * reference to it
220    */
221   @Test(groups = { "Functional" })
222   public void testDeregisterMapping_withReference()
223   {
224     Desktop d = Desktop.instance;
225     assertNotNull(d);
226     StructureSelectionManager ssm = StructureSelectionManager
227             .getStructureSelectionManager(Desktop.instance);
228     ssm.resetAll();
229
230     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
231             ">Seq1\nRSVQ\n", FormatAdapter.PASTE);
232     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
233             ">Seq2\nDGEL\n", FormatAdapter.PASTE);
234
235     AlignedCodonFrame acf1 = new AlignedCodonFrame();
236     AlignedCodonFrame acf2 = new AlignedCodonFrame();
237     AlignedCodonFrame acf3 = new AlignedCodonFrame();
238
239     Set<AlignedCodonFrame> mappings1 = new LinkedHashSet<AlignedCodonFrame>();
240     mappings1.add(acf1);
241     mappings1.add(acf2);
242     af1.getViewport().getAlignment().setCodonFrames(mappings1);
243
244     Set<AlignedCodonFrame> mappings2 = new LinkedHashSet<AlignedCodonFrame>();
245     mappings2.add(acf2);
246     mappings2.add(acf3);
247     af2.getViewport().getAlignment().setCodonFrames(mappings2);
248
249     /*
250      * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
251      */
252
253     Set<AlignedCodonFrame> ssmMappings = ssm.seqmappings;
254     assertEquals(0, ssmMappings.size());
255     ssm.registerMapping(acf1);
256     assertEquals(1, ssmMappings.size());
257     ssm.registerMapping(acf2);
258     assertEquals(2, ssmMappings.size());
259     ssm.registerMapping(acf3);
260     assertEquals(3, ssmMappings.size());
261
262     /*
263      * Closing AlignFrame2 should remove mapping acf3 from
264      * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
265      * reference to it
266      */
267     af2.closeMenuItem_actionPerformed(true);
268     assertEquals(2, ssmMappings.size());
269     assertTrue(ssmMappings.contains(acf1));
270     assertTrue(ssmMappings.contains(acf2));
271     assertFalse(ssmMappings.contains(acf3));
272   }
273 }