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