JAL-3253 jalview.bin.Instance handles all singleton instances -
[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.assertNotSame;
27 import static org.testng.AssertJUnit.assertSame;
28 import static org.testng.AssertJUnit.assertTrue;
29
30 import jalview.bin.Cache;
31 import jalview.bin.Instance;
32 import jalview.bin.Jalview;
33 import jalview.datamodel.AlignedCodonFrame;
34 import jalview.datamodel.Alignment;
35 import jalview.datamodel.AlignmentAnnotation;
36 import jalview.datamodel.AlignmentI;
37 import jalview.datamodel.Annotation;
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.ClustalxColourScheme;
46 import jalview.schemes.ColourSchemeI;
47 import jalview.schemes.PIDColourScheme;
48 import jalview.structure.StructureSelectionManager;
49 import jalview.util.MapList;
50 import jalview.viewmodel.ViewportRanges;
51
52 import java.util.ArrayList;
53 import java.util.List;
54
55 import org.testng.Assert;
56 import org.testng.annotations.BeforeClass;
57 import org.testng.annotations.BeforeMethod;
58 import org.testng.annotations.Test;
59
60 public class AlignViewportTest
61 {
62
63   @BeforeClass(alwaysRun = true)
64   public void setUpJvOptionPane()
65   {
66     JvOptionPane.setInteractiveMode(false);
67     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
68   }
69
70   AlignmentI al;
71
72   AlignViewport testee;
73
74   @BeforeClass(alwaysRun = true)
75   public static void setUpBeforeClass() throws Exception
76   {
77     Jalview.main(new String[] { "-nonews", "-props",
78         "test/jalview/testProps.jvprops" });
79
80     /*
81      * remove any sequence mappings left lying around by other tests
82      */
83     StructureSelectionManager ssm = StructureSelectionManager
84             .getStructureSelectionManager(Instance.getDesktop());
85     ssm.resetAll();
86   }
87
88   @BeforeMethod(alwaysRun = true)
89   public void setUp()
90   {
91     SequenceI seq1 = new Sequence("Seq1", "ABC");
92     SequenceI seq2 = new Sequence("Seq2", "ABC");
93     SequenceI seq3 = new Sequence("Seq3", "ABC");
94     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3 };
95     al = new Alignment(seqs);
96     al.setDataset(null);
97     testee = new AlignViewport(al);
98   }
99
100   /**
101    * Test that a mapping is not deregistered when a second view is closed but
102    * the first still holds a reference to the mapping
103    */
104   @Test(groups = { "Functional" })
105   public void testDeregisterMapping_onCloseView()
106   {
107     /*
108      * alignment with reference to mappings
109      */
110     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
111             ">Seq1\nCAGT\n", DataSourceType.PASTE);
112
113     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
114     AlignedCodonFrame acf1 = new AlignedCodonFrame();
115     acf1.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 1, 4 },
116             1, 1));
117     AlignedCodonFrame acf2 = new AlignedCodonFrame();
118     acf2.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 4, 1 },
119             1, 1));
120
121     List<AlignedCodonFrame> mappings = new ArrayList<>();
122     mappings.add(acf1);
123     mappings.add(acf2);
124     af1.getViewport().getAlignment().setCodonFrames(mappings);
125     af1.newView_actionPerformed(null);
126
127     /*
128      * Verify that creating the alignment for the new View has registered the
129      * mappings
130      */
131     StructureSelectionManager ssm = StructureSelectionManager
132             .getStructureSelectionManager(Instance.getDesktop());
133     List<AlignedCodonFrame> sequenceMappings = ssm.getSequenceMappings();
134     assertEquals(2, sequenceMappings.size());
135     assertTrue(sequenceMappings.contains(acf1));
136     assertTrue(sequenceMappings.contains(acf2));
137
138     /*
139      * Close the second view. Verify that mappings are not removed as the first
140      * view still holds a reference to them.
141      */
142     af1.closeMenuItem_actionPerformed(false);
143     assertEquals(2, sequenceMappings.size());
144     assertTrue(sequenceMappings.contains(acf1));
145     assertTrue(sequenceMappings.contains(acf2));
146   }
147
148   /**
149    * Test that a mapping is deregistered if no alignment holds a reference to it
150    */
151   @Test(groups = { "Functional" })
152   public void testDeregisterMapping_withNoReference()
153   {
154     Desktop d = Instance.getDesktop();
155     assertNotNull(d);
156     StructureSelectionManager ssm = StructureSelectionManager
157             .getStructureSelectionManager(Instance.getDesktop());
158     ssm.resetAll();
159
160     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
161             ">Seq1\nRSVQ\n", DataSourceType.PASTE);
162     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
163             ">Seq2\nDGEL\n", DataSourceType.PASTE);
164     SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
165     SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
166     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
167     SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
168     // need to be distinct
169     AlignedCodonFrame acf1 = new AlignedCodonFrame();
170     acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
171             new int[] { 1, 12 }, 1, 3));
172     AlignedCodonFrame acf2 = new AlignedCodonFrame();
173     acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
174             new int[] { 1, 12 }, 1, 3));
175     AlignedCodonFrame acf3 = new AlignedCodonFrame();
176     acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
177         12 }, 1, 1));
178
179     List<AlignedCodonFrame> mappings1 = new ArrayList<>();
180     mappings1.add(acf1);
181     af1.getViewport().getAlignment().setCodonFrames(mappings1);
182
183     List<AlignedCodonFrame> mappings2 = new ArrayList<>();
184     mappings2.add(acf2);
185     mappings2.add(acf3);
186     af2.getViewport().getAlignment().setCodonFrames(mappings2);
187
188     /*
189      * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
190      */
191
192     List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
193     assertEquals(0, ssmMappings.size());
194     ssm.registerMapping(acf1);
195     assertEquals(1, ssmMappings.size());
196     ssm.registerMapping(acf2);
197     assertEquals(2, ssmMappings.size());
198     ssm.registerMapping(acf3);
199     assertEquals(3, ssmMappings.size());
200
201     /*
202      * Closing AlignFrame2 should remove its mappings from
203      * StructureSelectionManager, since AlignFrame1 has no reference to them
204      */
205     af2.closeMenuItem_actionPerformed(true);
206     assertEquals(1, ssmMappings.size());
207     assertTrue(ssmMappings.contains(acf1));
208   }
209
210   /**
211    * Test that a mapping is not deregistered if another alignment holds a
212    * reference to it
213    */
214   @Test(groups = { "Functional" })
215   public void testDeregisterMapping_withReference()
216   {
217     Desktop d = Instance.getDesktop();
218     assertNotNull(d);
219     StructureSelectionManager ssm = StructureSelectionManager
220             .getStructureSelectionManager(Instance.getDesktop());
221     ssm.resetAll();
222
223     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
224             ">Seq1\nRSVQ\n", DataSourceType.PASTE);
225     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
226             ">Seq2\nDGEL\n", DataSourceType.PASTE);
227     SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
228     SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
229     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
230     SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
231     // need to be distinct
232     AlignedCodonFrame acf1 = new AlignedCodonFrame();
233     acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
234             new int[] { 1, 12 }, 1, 3));
235     AlignedCodonFrame acf2 = new AlignedCodonFrame();
236     acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
237             new int[] { 1, 12 }, 1, 3));
238     AlignedCodonFrame acf3 = new AlignedCodonFrame();
239     acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
240         12 }, 1, 1));
241
242     List<AlignedCodonFrame> mappings1 = new ArrayList<>();
243     mappings1.add(acf1);
244     mappings1.add(acf2);
245     af1.getViewport().getAlignment().setCodonFrames(mappings1);
246
247     List<AlignedCodonFrame> mappings2 = new ArrayList<>();
248     mappings2.add(acf2);
249     mappings2.add(acf3);
250     af2.getViewport().getAlignment().setCodonFrames(mappings2);
251
252     /*
253      * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
254      */
255
256     List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
257     assertEquals(0, ssmMappings.size());
258     ssm.registerMapping(acf1);
259     assertEquals(1, ssmMappings.size());
260     ssm.registerMapping(acf2);
261     assertEquals(2, ssmMappings.size());
262     ssm.registerMapping(acf3);
263     assertEquals(3, ssmMappings.size());
264
265     /*
266      * Closing AlignFrame2 should remove mapping acf3 from
267      * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
268      * reference to it
269      */
270     af2.closeMenuItem_actionPerformed(true);
271     assertEquals(2, ssmMappings.size());
272     assertTrue(ssmMappings.contains(acf1));
273     assertTrue(ssmMappings.contains(acf2));
274     assertFalse(ssmMappings.contains(acf3));
275   }
276
277   /**
278    * Test for JAL-1306 - conservation thread should run even when only Quality
279    * (and not Conservation) is enabled in Preferences
280    */
281   @Test(groups = { "Functional" }, timeOut=2000)
282   public void testUpdateConservation_qualityOnly()
283   {
284     Cache.setPropertyNoSave("SHOW_ANNOTATIONS",
285             Boolean.TRUE.toString());
286     Cache.setPropertyNoSave("SHOW_QUALITY",
287             Boolean.TRUE.toString());
288     Cache.setPropertyNoSave("SHOW_CONSERVATION",
289             Boolean.FALSE.toString());
290     Cache.setPropertyNoSave("SHOW_OCCUPANCY",
291             Boolean.FALSE.toString());
292     Cache.setPropertyNoSave("SHOW_IDENTITY",
293             Boolean.FALSE.toString());
294     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
295             "examples/uniref50.fa", DataSourceType.FILE);
296
297     /*
298      * wait for Conservation thread to complete
299      */
300     AlignViewport viewport = af.getViewport();
301     synchronized (this)
302     {
303       while (viewport.getAlignmentConservationAnnotation() != null)
304       {
305         try
306         {
307           wait(50);
308         } catch (InterruptedException e)
309         {
310         }
311       }
312     }
313     AlignmentAnnotation[] anns = viewport.getAlignment()
314             .getAlignmentAnnotation();
315     assertNotNull("No annotations found", anns);
316     assertEquals("More than one annotation found", 1, anns.length);
317     assertTrue("Annotation is not Quality",
318             anns[0].description.startsWith("Alignment Quality"));
319     Annotation[] annotations = anns[0].annotations;
320     assertNotNull("Quality annotations are null", annotations);
321     assertNotNull("Quality in column 1 is null", annotations[0]);
322     assertTrue("No quality value in column 1", annotations[0].value > 10f);
323   }
324
325   @Test(groups = { "Functional" })
326   public void testSetGlobalColourScheme()
327   {
328     /*
329      * test for JAL-2283: don't inadvertently turn on colour by conservation
330      */
331     Cache.setPropertyNoSave("DEFAULT_COLOUR_PROT", "None");
332     Cache.setPropertyNoSave("SHOW_CONSERVATION",
333             Boolean.TRUE.toString());
334     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
335             "examples/uniref50.fa", DataSourceType.FILE);
336     ColourSchemeI cs = new PIDColourScheme();
337     AlignViewport viewport = af.getViewport();
338     viewport.setGlobalColourScheme(cs);
339     assertFalse(viewport.getResidueShading()
340             .conservationApplied());
341
342     /*
343      * JAL-3201 groups have their own ColourSchemeI instances
344      */
345     AlignmentI aln = viewport.getAlignment();
346     SequenceGroup sg1 = new SequenceGroup();
347     sg1.addSequence(aln.getSequenceAt(0), false);
348     sg1.addSequence(aln.getSequenceAt(2), false);
349     SequenceGroup sg2 = new SequenceGroup();
350     sg2.addSequence(aln.getSequenceAt(1), false);
351     sg2.addSequence(aln.getSequenceAt(3), false);
352     aln.addGroup(sg1);
353     aln.addGroup(sg2);
354     viewport.setColourAppliesToAllGroups(true);
355     viewport.setGlobalColourScheme(new ClustalxColourScheme());
356     ColourSchemeI cs0 = viewport.getGlobalColourScheme();
357     ColourSchemeI cs1 = sg1.getColourScheme();
358     ColourSchemeI cs2 = sg2.getColourScheme();
359     assertTrue(cs0 instanceof ClustalxColourScheme);
360     assertTrue(cs1 instanceof ClustalxColourScheme);
361     assertTrue(cs2 instanceof ClustalxColourScheme);
362     assertNotSame(cs0, cs1);
363     assertNotSame(cs0, cs2);
364     assertNotSame(cs1, cs2);
365   }
366
367   @Test(groups = { "Functional" })
368   public void testSetGetHasSearchResults()
369   {
370     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
371             "examples/uniref50.fa", DataSourceType.FILE);
372     SearchResultsI sr = new SearchResults();
373     SequenceI s1 = af.getViewport().getAlignment().getSequenceAt(0);
374
375     // create arbitrary range on first sequence
376     sr.addResult(s1, s1.getStart() + 10, s1.getStart() + 15);
377
378     // test set
379     af.getViewport().setSearchResults(sr);
380     // has -> true
381     assertTrue(af.getViewport().hasSearchResults());
382     // get == original
383     assertEquals(sr, af.getViewport().getSearchResults());
384
385     // set(null) results in has -> false
386
387     af.getViewport().setSearchResults(null);
388     assertFalse(af.getViewport().hasSearchResults());
389   }
390
391   /**
392    * Verify that setting the selection group has the side-effect of setting the
393    * context on the group, unless it already has one, but does not change
394    * whether the group is defined or not.
395    */
396   @Test(groups = { "Functional" })
397   public void testSetSelectionGroup()
398   {
399     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
400             "examples/uniref50.fa", DataSourceType.FILE);
401     AlignViewport av = af.getViewport();
402     SequenceGroup sg1 = new SequenceGroup();
403     SequenceGroup sg2 = new SequenceGroup();
404     SequenceGroup sg3 = new SequenceGroup();
405
406     av.setSelectionGroup(sg1);
407     assertSame(sg1.getContext(), av.getAlignment()); // context set
408     assertFalse(sg1.isDefined()); // group not defined
409
410     sg2.setContext(sg1, false);
411     av.setSelectionGroup(sg2);
412     assertFalse(sg2.isDefined()); // unchanged
413     assertSame(sg2.getContext(), sg1); // unchanged
414
415     // create a defined group
416     sg3.setContext(av.getAlignment(), true);
417     av.setSelectionGroup(sg3);
418     assertTrue(sg3.isDefined()); // unchanged
419   }
420   /**
421    * Verify that setting/clearing SHOW_OCCUPANCY preference adds or omits occupancy row from viewport
422    */
423   @Test(groups = { "Functional" })
424   public void testShowOrDontShowOccupancy()
425   {
426     // disable occupancy
427     jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.FALSE.toString());
428     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
429             "examples/uniref50.fa", DataSourceType.FILE);
430     AlignViewport av = af.getViewport();
431     Assert.assertNull(av.getAlignmentGapAnnotation(), "Preference did not disable occupancy row.");
432     int c = 0;
433     for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
434             null, "Occupancy"))
435     {
436       c++;
437     }
438     Assert.assertEquals(c, 0, "Expected zero occupancy rows.");
439     
440     // enable occupancy
441     jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.TRUE.toString());
442     af = new FileLoader().LoadFileWaitTillLoaded(
443             "examples/uniref50.fa", DataSourceType.FILE);
444     av = af.getViewport();
445     Assert.assertNotNull(av.getAlignmentGapAnnotation(), "Preference did not enable occupancy row.");
446     c = 0;
447     for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
448             null, av.getAlignmentGapAnnotation().label))
449     {
450       c++;
451     }
452     ;
453     Assert.assertEquals(c, 1, "Expected to find one occupancy row.");
454   }
455
456   @Test(groups = { "Functional" })
457   public void testGetConsensusSeq()
458   {
459     /*
460      * A-C
461      * A-C
462      * A-D
463      * --D
464      * consensus expected to be A-C
465      */
466     String fasta = ">s1\nA-C\n>s2\nA-C\n>s3\nA-D\n>s4\n--D\n";
467     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(fasta,
468             DataSourceType.PASTE);
469     AlignViewport testme = af.getViewport();
470     SequenceI cons = testme.getConsensusSeq();
471     assertEquals("A-C", cons.getSequenceAsString());
472   }
473
474   @Test(groups = { "Functional" })
475   public void testHideRevealSequences()
476   {
477     ViewportRanges ranges = testee.getRanges();
478     assertEquals(3, al.getHeight());
479     assertEquals(0, ranges.getStartSeq());
480     assertEquals(2, ranges.getEndSeq());
481
482     /*
483      * hide first sequence
484      */
485     testee.hideSequence(new SequenceI[] { al.getSequenceAt(0) });
486     assertEquals(2, al.getHeight());
487     assertEquals(0, ranges.getStartSeq());
488     assertEquals(1, ranges.getEndSeq());
489
490     /*
491      * reveal hidden sequences above the first
492      */
493     testee.showSequence(0);
494     assertEquals(3, al.getHeight());
495     assertEquals(0, ranges.getStartSeq());
496     assertEquals(2, ranges.getEndSeq());
497
498     /*
499      * hide first and third sequences
500      */
501     testee.hideSequence(new SequenceI[] { al.getSequenceAt(0),
502         al.getSequenceAt(2) });
503     assertEquals(1, al.getHeight());
504     assertEquals(0, ranges.getStartSeq());
505     assertEquals(0, ranges.getEndSeq());
506
507     /*
508      * reveal all hidden sequences
509      */
510     testee.showAllHiddenSeqs();
511     assertEquals(3, al.getHeight());
512     assertEquals(0, ranges.getStartSeq());
513     assertEquals(2, ranges.getEndSeq());
514   }
515 }