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