891a203fc1ce6fd904075afc5b05099bd7114271
[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.getInstance()
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.getInstance()
153             .getStructureSelectionManager();
154     ssm.resetAll();
155
156     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
157             ">Seq1\nRSVQ\n", DataSourceType.PASTE);
158     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
159             ">Seq2\nDGEL\n", DataSourceType.PASTE);
160     SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
161     SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
162     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
163     SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
164     // need to be distinct
165     AlignedCodonFrame acf1 = new AlignedCodonFrame();
166     acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
167             new int[] { 1, 12 }, 1, 3));
168     AlignedCodonFrame acf2 = new AlignedCodonFrame();
169     acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
170             new int[] { 1, 12 }, 1, 3));
171     AlignedCodonFrame acf3 = new AlignedCodonFrame();
172     acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
173         12 }, 1, 1));
174
175     List<AlignedCodonFrame> mappings1 = new ArrayList<>();
176     mappings1.add(acf1);
177     af1.getViewport().getAlignment().setCodonFrames(mappings1);
178
179     List<AlignedCodonFrame> mappings2 = new ArrayList<>();
180     mappings2.add(acf2);
181     mappings2.add(acf3);
182     af2.getViewport().getAlignment().setCodonFrames(mappings2);
183
184     /*
185      * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
186      */
187
188     List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
189     assertEquals(0, ssmMappings.size());
190     ssm.registerMapping(acf1);
191     assertEquals(1, ssmMappings.size());
192     ssm.registerMapping(acf2);
193     assertEquals(2, ssmMappings.size());
194     ssm.registerMapping(acf3);
195     assertEquals(3, ssmMappings.size());
196
197     /*
198      * Closing AlignFrame2 should remove its mappings from
199      * StructureSelectionManager, since AlignFrame1 has no reference to them
200      */
201     af2.closeMenuItem_actionPerformed(true);
202     assertEquals(1, ssmMappings.size());
203     assertTrue(ssmMappings.contains(acf1));
204   }
205
206   /**
207    * Test that a mapping is not deregistered if another alignment holds a
208    * reference to it
209    */
210   @Test(groups = { "Functional" })
211   public void testDeregisterMapping_withReference()
212   {
213     Desktop d = Desktop.getInstance();
214     assertNotNull(d);
215     StructureSelectionManager ssm = Desktop.getInstance()
216             .getStructureSelectionManager();
217     ssm.resetAll();
218
219     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
220             ">Seq1\nRSVQ\n", DataSourceType.PASTE);
221     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
222             ">Seq2\nDGEL\n", DataSourceType.PASTE);
223     SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
224     SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
225     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
226     SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
227     // need to be distinct
228     AlignedCodonFrame acf1 = new AlignedCodonFrame();
229     acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
230             new int[] { 1, 12 }, 1, 3));
231     AlignedCodonFrame acf2 = new AlignedCodonFrame();
232     acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
233             new int[] { 1, 12 }, 1, 3));
234     AlignedCodonFrame acf3 = new AlignedCodonFrame();
235     acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
236         12 }, 1, 1));
237
238     List<AlignedCodonFrame> mappings1 = new ArrayList<>();
239     mappings1.add(acf1);
240     mappings1.add(acf2);
241     af1.getViewport().getAlignment().setCodonFrames(mappings1);
242
243     List<AlignedCodonFrame> mappings2 = new ArrayList<>();
244     mappings2.add(acf2);
245     mappings2.add(acf3);
246     af2.getViewport().getAlignment().setCodonFrames(mappings2);
247
248     /*
249      * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
250      */
251
252     List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
253     assertEquals(0, ssmMappings.size());
254     ssm.registerMapping(acf1);
255     assertEquals(1, ssmMappings.size());
256     ssm.registerMapping(acf2);
257     assertEquals(2, ssmMappings.size());
258     ssm.registerMapping(acf3);
259     assertEquals(3, ssmMappings.size());
260
261     /*
262      * Closing AlignFrame2 should remove mapping acf3 from
263      * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
264      * reference to it
265      */
266     af2.closeMenuItem_actionPerformed(true);
267     assertEquals(2, ssmMappings.size());
268     assertTrue(ssmMappings.contains(acf1));
269     assertTrue(ssmMappings.contains(acf2));
270     assertFalse(ssmMappings.contains(acf3));
271   }
272
273   /**
274    * Test for JAL-1306 - conservation thread should run even when only Quality
275    * (and not Conservation) is enabled in Preferences
276    */
277   @Test(groups = { "Functional" }, timeOut = 2000)
278   public void testUpdateConservation_qualityOnly()
279   {
280     Cache.setPropertyNoSave("SHOW_ANNOTATIONS",
281             Boolean.TRUE.toString());
282     Cache.setPropertyNoSave(Preferences.SHOW_QUALITY,
283             Boolean.TRUE.toString());
284     Cache.setPropertyNoSave("SHOW_CONSERVATION",
285             Boolean.FALSE.toString());
286     Cache.setPropertyNoSave("SHOW_OCCUPANCY",
287             Boolean.FALSE.toString());
288     Cache.setPropertyNoSave("SHOW_IDENTITY",
289             Boolean.FALSE.toString());
290     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
291             "examples/uniref50.fa", DataSourceType.FILE);
292
293     /*
294      * wait for Conservation thread to complete
295      */
296     AlignViewport viewport = af.getViewport();
297     synchronized (this)
298     {
299       // System.out.println("AVT consv: "
300       // + viewport.getAlignmentConservationAnnotation());
301       try
302       {
303         wait(250); // BH increased from 50 -- was getting Quality in column 1
304                    // is null
305       } catch (InterruptedException e)
306       {
307       }
308       // System.out.println("AVT consv: "
309       // + viewport.getAlignmentConservationAnnotation());
310       while (viewport.getAlignmentConservationAnnotation() != null)
311       {
312         try
313         {
314           wait(250); // BH increased from 50 -- was getting Quality in column 1
315                      // is null
316         } catch (InterruptedException e)
317         {
318         }
319       }
320     }
321     af.paintImmediately(af.getBounds());
322     AlignmentAnnotation[] anns = viewport.getAlignment()
323             .getAlignmentAnnotation();
324     assertNotNull("No annotations found", anns);
325     assertEquals("More than one annotation found", 1, anns.length);
326     assertTrue("Annotation is not Quality",
327             anns[0].description.startsWith("Alignment Quality"));
328     Annotation[] annotations = anns[0].annotations;
329     assertNotNull("Quality annotations are null", annotations);
330     assertNotNull("Quality in column 1 is null", annotations[0]);
331     assertTrue("No quality value in column 1", annotations[0].value > 10f);
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.setPropertyNoSave("DEFAULT_COLOUR_PROT", "None");
341     Cache.setPropertyNoSave("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     Assert.assertEquals(c, 1, "Expected to find one occupancy row.");
462   }
463
464   @Test(groups = { "Functional" })
465   public void testGetConsensusSeq()
466   {
467     /*
468      * A-C
469      * A-C
470      * A-D
471      * --D
472      * consensus expected to be A-C
473      */
474     String fasta = ">s1\nA-C\n>s2\nA-C\n>s3\nA-D\n>s4\n--D\n";
475     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(fasta,
476             DataSourceType.PASTE);
477     synchronized (this)
478     {
479     try
480     {
481         wait(50);
482     } catch (InterruptedException e)
483     {
484       // TODO Auto-generated catch block
485       e.printStackTrace();
486     }
487     }
488     // af.paintImmediately(af.getBounds());
489     AlignViewport testme = af.getViewport();
490     SequenceI cons = testme.getConsensusSeq();
491     // System.out.println("AVT " + cons.getSequenceAsString());
492     assertEquals("A-C", cons.getSequenceAsString());
493   }
494
495   @Test(groups = { "Functional" })
496   public void testHideRevealSequences()
497   {
498     ViewportRanges ranges = testee.getRanges();
499     assertEquals(3, al.getHeight());
500     assertEquals(0, ranges.getStartSeq());
501     assertEquals(2, ranges.getEndSeq());
502
503     /*
504      * hide first sequence
505      */
506     testee.hideSequence(new SequenceI[] { al.getSequenceAt(0) });
507     assertEquals(2, al.getHeight());
508     assertEquals(0, ranges.getStartSeq());
509     assertEquals(1, ranges.getEndSeq());
510
511     /*
512      * reveal hidden sequences above the first
513      */
514     testee.showSequence(0);
515     assertEquals(3, al.getHeight());
516     assertEquals(0, ranges.getStartSeq());
517     assertEquals(2, ranges.getEndSeq());
518
519     /*
520      * hide first and third sequences
521      */
522     testee.hideSequence(new SequenceI[] { al.getSequenceAt(0),
523         al.getSequenceAt(2) });
524     assertEquals(1, al.getHeight());
525     assertEquals(0, ranges.getStartSeq());
526     assertEquals(0, ranges.getEndSeq());
527
528     /*
529      * reveal all hidden sequences
530      */
531     testee.showAllHiddenSeqs();
532     assertEquals(3, al.getHeight());
533     assertEquals(0, ranges.getStartSeq());
534     assertEquals(2, ranges.getEndSeq());
535   }
536 }