JAL-2405 set initial selection group alignment context
[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.bin.Cache;
30 import jalview.bin.Jalview;
31 import jalview.datamodel.AlignedCodonFrame;
32 import jalview.datamodel.Alignment;
33 import jalview.datamodel.AlignmentAnnotation;
34 import jalview.datamodel.AlignmentI;
35 import jalview.datamodel.Annotation;
36 import jalview.datamodel.PDBEntry;
37 import jalview.datamodel.PDBEntry.Type;
38 import jalview.datamodel.SearchResults;
39 import jalview.datamodel.SearchResultsI;
40 import jalview.datamodel.Sequence;
41 import jalview.datamodel.SequenceGroup;
42 import jalview.datamodel.SequenceI;
43 import jalview.io.DataSourceType;
44 import jalview.io.FileLoader;
45 import jalview.schemes.ColourSchemeI;
46 import jalview.schemes.PIDColourScheme;
47 import jalview.structure.StructureSelectionManager;
48 import jalview.util.MapList;
49
50 import java.util.ArrayList;
51 import java.util.List;
52
53 import org.testng.annotations.BeforeClass;
54 import org.testng.annotations.BeforeMethod;
55 import org.testng.annotations.Test;
56
57 public class AlignViewportTest
58 {
59
60   @BeforeClass(alwaysRun = true)
61   public void setUpJvOptionPane()
62   {
63     JvOptionPane.setInteractiveMode(false);
64     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
65   }
66
67   AlignmentI al;
68
69   AlignViewport testee;
70
71   @BeforeClass(alwaysRun = true)
72   public static void setUpBeforeClass() throws Exception
73   {
74     Jalview.main(new String[] { "-nonews", "-props",
75         "test/jalview/testProps.jvprops" });
76   }
77
78   @BeforeMethod(alwaysRun = true)
79   public void setUp()
80   {
81     SequenceI seq1 = new Sequence("Seq1", "ABC");
82     SequenceI seq2 = new Sequence("Seq2", "ABC");
83     SequenceI seq3 = new Sequence("Seq3", "ABC");
84     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3 };
85     al = new Alignment(seqs);
86     al.setDataset(null);
87     testee = new AlignViewport(al);
88   }
89
90   @Test(groups = { "Functional" })
91   public void testCollateForPdb()
92   {
93     // JBP: What behaviour is this supposed to test ?
94     /*
95      * Set up sequence pdb ids
96      */
97     PDBEntry pdb1 = new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb");
98     PDBEntry pdb2 = new PDBEntry("2ABC", "C", Type.PDB, "2ABC.pdb");
99     PDBEntry pdb3 = new PDBEntry("3ABC", "D", Type.PDB, "3ABC.pdb");
100
101     /*
102      * seq1 and seq3 refer to 1abcB, seq2 to 2abcC, none to 3abcD
103      */
104     al.getSequenceAt(0).getDatasetSequence()
105             .addPDBId(new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
106     al.getSequenceAt(2).getDatasetSequence()
107             .addPDBId(new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
108     al.getSequenceAt(1).getDatasetSequence()
109             .addPDBId(new PDBEntry("2ABC", "C", Type.PDB, "2ABC.pdb"));
110     /*
111      * Add a second chain PDB xref to Seq2 - should not result in a duplicate in
112      * the results
113      */
114     al.getSequenceAt(1).getDatasetSequence()
115             .addPDBId(new PDBEntry("2ABC", "D", Type.PDB, "2ABC.pdb"));
116     /*
117      * Seq3 refers to 3abc - this does not match 3ABC (as the code stands)
118      */
119     al.getSequenceAt(2).getDatasetSequence()
120             .addPDBId(new PDBEntry("3abc", "D", Type.PDB, "3ABC.pdb"));
121
122     /*
123      * run method under test
124      */
125     SequenceI[][] seqs = testee.collateForPDB(new PDBEntry[] { pdb1, pdb2,
126         pdb3 });
127
128     // seq1 and seq3 refer to PDBEntry[0]
129     assertEquals(2, seqs[0].length);
130     assertSame(al.getSequenceAt(0), seqs[0][0]);
131     assertSame(al.getSequenceAt(2), seqs[0][1]);
132
133     // seq2 refers to PDBEntry[1]
134     assertEquals(1, seqs[1].length);
135     assertSame(al.getSequenceAt(1), seqs[1][0]);
136
137     // no sequence refers to PDBEntry[2]
138     assertEquals(0, seqs[2].length);
139   }
140
141   /**
142    * Test that a mapping is not deregistered when a second view is closed but
143    * the first still holds a reference to the mapping
144    */
145   @Test(groups = { "Functional" })
146   public void testDeregisterMapping_onCloseView()
147   {
148     /*
149      * alignment with reference to mappings
150      */
151     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
152             ">Seq1\nCAGT\n", DataSourceType.PASTE);
153
154     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
155     AlignedCodonFrame acf1 = new AlignedCodonFrame();
156     acf1.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 1, 4 },
157             1, 1));
158     AlignedCodonFrame acf2 = new AlignedCodonFrame();
159     acf2.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 4, 1 },
160             1, 1));
161
162     List<AlignedCodonFrame> mappings = new ArrayList<AlignedCodonFrame>();
163     mappings.add(acf1);
164     mappings.add(acf2);
165     af1.getViewport().getAlignment().setCodonFrames(mappings);
166     af1.newView_actionPerformed(null);
167
168     /*
169      * Verify that creating the alignment for the new View has registered the
170      * mappings
171      */
172     StructureSelectionManager ssm = StructureSelectionManager
173             .getStructureSelectionManager(Desktop.instance);
174     assertEquals(2, ssm.getSequenceMappings().size());
175     assertTrue(ssm.getSequenceMappings().contains(acf1));
176     assertTrue(ssm.getSequenceMappings().contains(acf2));
177
178     /*
179      * Close the second view. Verify that mappings are not removed as the first
180      * view still holds a reference to them.
181      */
182     af1.closeMenuItem_actionPerformed(false);
183     assertEquals(2, ssm.getSequenceMappings().size());
184     assertTrue(ssm.getSequenceMappings().contains(acf1));
185     assertTrue(ssm.getSequenceMappings().contains(acf2));
186   }
187
188   /**
189    * Test that a mapping is deregistered if no alignment holds a reference to it
190    */
191   @Test(groups = { "Functional" })
192   public void testDeregisterMapping_withNoReference()
193   {
194     Desktop d = Desktop.instance;
195     assertNotNull(d);
196     StructureSelectionManager ssm = StructureSelectionManager
197             .getStructureSelectionManager(Desktop.instance);
198     ssm.resetAll();
199
200     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
201             ">Seq1\nRSVQ\n", DataSourceType.PASTE);
202     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
203             ">Seq2\nDGEL\n", DataSourceType.PASTE);
204     SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
205     SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
206     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
207     SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
208     // need to be distinct
209     AlignedCodonFrame acf1 = new AlignedCodonFrame();
210     acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
211             new int[] { 1, 12 }, 1, 3));
212     AlignedCodonFrame acf2 = new AlignedCodonFrame();
213     acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
214             new int[] { 1, 12 }, 1, 3));
215     AlignedCodonFrame acf3 = new AlignedCodonFrame();
216     acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
217         12 }, 1, 1));
218
219     List<AlignedCodonFrame> mappings1 = new ArrayList<AlignedCodonFrame>();
220     mappings1.add(acf1);
221     af1.getViewport().getAlignment().setCodonFrames(mappings1);
222
223     List<AlignedCodonFrame> mappings2 = new ArrayList<AlignedCodonFrame>();
224     mappings2.add(acf2);
225     mappings2.add(acf3);
226     af2.getViewport().getAlignment().setCodonFrames(mappings2);
227
228     /*
229      * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
230      */
231
232     List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
233     assertEquals(0, ssmMappings.size());
234     ssm.registerMapping(acf1);
235     assertEquals(1, ssmMappings.size());
236     ssm.registerMapping(acf2);
237     assertEquals(2, ssmMappings.size());
238     ssm.registerMapping(acf3);
239     assertEquals(3, ssmMappings.size());
240
241     /*
242      * Closing AlignFrame2 should remove its mappings from
243      * StructureSelectionManager, since AlignFrame1 has no reference to them
244      */
245     af2.closeMenuItem_actionPerformed(true);
246     assertEquals(1, ssmMappings.size());
247     assertTrue(ssmMappings.contains(acf1));
248   }
249
250   /**
251    * Test that a mapping is not deregistered if another alignment holds a
252    * reference to it
253    */
254   @Test(groups = { "Functional" })
255   public void testDeregisterMapping_withReference()
256   {
257     Desktop d = Desktop.instance;
258     assertNotNull(d);
259     StructureSelectionManager ssm = StructureSelectionManager
260             .getStructureSelectionManager(Desktop.instance);
261     ssm.resetAll();
262
263     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
264             ">Seq1\nRSVQ\n", DataSourceType.PASTE);
265     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
266             ">Seq2\nDGEL\n", DataSourceType.PASTE);
267     SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
268     SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
269     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
270     SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
271     // need to be distinct
272     AlignedCodonFrame acf1 = new AlignedCodonFrame();
273     acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
274             new int[] { 1, 12 }, 1, 3));
275     AlignedCodonFrame acf2 = new AlignedCodonFrame();
276     acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
277             new int[] { 1, 12 }, 1, 3));
278     AlignedCodonFrame acf3 = new AlignedCodonFrame();
279     acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
280         12 }, 1, 1));
281
282     List<AlignedCodonFrame> mappings1 = new ArrayList<AlignedCodonFrame>();
283     mappings1.add(acf1);
284     mappings1.add(acf2);
285     af1.getViewport().getAlignment().setCodonFrames(mappings1);
286
287     List<AlignedCodonFrame> mappings2 = new ArrayList<AlignedCodonFrame>();
288     mappings2.add(acf2);
289     mappings2.add(acf3);
290     af2.getViewport().getAlignment().setCodonFrames(mappings2);
291
292     /*
293      * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
294      */
295
296     List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
297     assertEquals(0, ssmMappings.size());
298     ssm.registerMapping(acf1);
299     assertEquals(1, ssmMappings.size());
300     ssm.registerMapping(acf2);
301     assertEquals(2, ssmMappings.size());
302     ssm.registerMapping(acf3);
303     assertEquals(3, ssmMappings.size());
304
305     /*
306      * Closing AlignFrame2 should remove mapping acf3 from
307      * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
308      * reference to it
309      */
310     af2.closeMenuItem_actionPerformed(true);
311     assertEquals(2, ssmMappings.size());
312     assertTrue(ssmMappings.contains(acf1));
313     assertTrue(ssmMappings.contains(acf2));
314     assertFalse(ssmMappings.contains(acf3));
315   }
316
317   /**
318    * Test for JAL-1306 - conservation thread should run even when only Quality
319    * (and not Conservation) is enabled in Preferences
320    */
321   @Test(groups = { "Functional" })
322   public void testUpdateConservation_qualityOnly()
323   {
324     Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS",
325             Boolean.TRUE.toString());
326     Cache.applicationProperties.setProperty("SHOW_QUALITY",
327             Boolean.TRUE.toString());
328     Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
329             Boolean.FALSE.toString());
330     Cache.applicationProperties.setProperty("SHOW_IDENTITY",
331             Boolean.FALSE.toString());
332     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
333             "examples/uniref50.fa", DataSourceType.FILE);
334     AlignmentAnnotation[] anns = af.viewport.getAlignment()
335             .getAlignmentAnnotation();
336     assertNotNull("No annotations found", anns);
337     assertEquals("More than one annotation found", 1, anns.length);
338     assertTrue("Annotation is not Quality",
339             anns[0].description.startsWith("Alignment Quality"));
340     Annotation[] annotations = anns[0].annotations;
341     assertNotNull("Quality annotations are null", annotations);
342     assertNotNull("Quality in column 1 is null", annotations[0]);
343     assertTrue("No quality value in column 1", annotations[0].value > 10f);
344   }
345
346   @Test(groups = { "Functional" })
347   public void testSetGlobalColourScheme()
348   {
349     /*
350      * test for JAL-2283: don't inadvertently turn on colour by conservation
351      */
352     Cache.applicationProperties.setProperty("DEFAULT_COLOUR_PROT", "None");
353     Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
354             Boolean.TRUE.toString());
355     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
356             "examples/uniref50.fa", DataSourceType.FILE);
357     ColourSchemeI cs = new PIDColourScheme();
358     af.getViewport().setGlobalColourScheme(cs);
359     assertFalse(af.getViewport().getResidueShading()
360             .conservationApplied());
361   }
362
363   @Test(groups = { "Functional" })
364   public void testSetGetHasSearchResults()
365   {
366     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
367             "examples/uniref50.fa", DataSourceType.FILE);
368     SearchResultsI sr = new SearchResults();
369     SequenceI s1 = af.getViewport().getAlignment().getSequenceAt(0);
370
371     // create arbitrary range on first sequence
372     sr.addResult(s1, s1.getStart() + 10, s1.getStart() + 15);
373
374     // test set
375     af.getViewport().setSearchResults(sr);
376     // has -> true
377     assertTrue(af.getViewport().hasSearchResults());
378     // get == original
379     assertEquals(sr, af.getViewport().getSearchResults());
380
381     // set(null) results in has -> false
382
383     af.getViewport().setSearchResults(null);
384     assertFalse(af.getViewport().hasSearchResults());
385   }
386
387   /**
388    * Verify that setting the selection group has the side-effect of setting the
389    * context on the group, unless it already has one
390    */
391   @Test(groups = { "Functional" })
392   public void testSetSelectionGroup()
393   {
394     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
395             "examples/uniref50.fa", DataSourceType.FILE);
396     AlignViewport av = af.getViewport();
397     SequenceGroup sg1 = new SequenceGroup();
398     SequenceGroup sg2 = new SequenceGroup();
399
400     av.setSelectionGroup(sg1);
401     assertSame(sg1.getContext(), av.getAlignment()); // context set
402
403     sg2.setContext(sg1);
404     av.setSelectionGroup(sg2);
405     assertSame(sg2.getContext(), sg1); // unchanged
406   }
407 }