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