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