67098ae85d3fa493839b320f147eea34b66b7aa1
[jalview.git] / test / jalview / gui / AlignFrameTest.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.Assert.assertEquals;
24 import static org.testng.Assert.assertFalse;
25 import static org.testng.Assert.assertNotSame;
26 import static org.testng.Assert.assertSame;
27 import static org.testng.Assert.assertTrue;
28
29 import jalview.bin.Cache;
30 import jalview.bin.Jalview;
31 import jalview.datamodel.Alignment;
32 import jalview.datamodel.AlignmentI;
33 import jalview.datamodel.Sequence;
34 import jalview.datamodel.SequenceFeature;
35 import jalview.datamodel.SequenceGroup;
36 import jalview.datamodel.SequenceI;
37 import jalview.io.DataSourceType;
38 import jalview.io.FileLoader;
39 import jalview.io.Jalview2xmlTests;
40 import jalview.renderer.ResidueShaderI;
41 import jalview.schemes.BuriedColourScheme;
42 import jalview.schemes.HelixColourScheme;
43 import jalview.schemes.JalviewColourScheme;
44 import jalview.schemes.StrandColourScheme;
45 import jalview.schemes.TurnColourScheme;
46 import jalview.util.MessageManager;
47
48 import java.awt.Color;
49 import java.util.List;
50
51 import org.testng.annotations.AfterMethod;
52 import org.testng.annotations.BeforeClass;
53 import org.testng.annotations.BeforeMethod;
54 import org.testng.annotations.Test;
55
56 public class AlignFrameTest
57 {
58   AlignFrame af;
59
60   @BeforeClass(alwaysRun = true)
61   public void setUpJvOptionPane()
62   {
63     JvOptionPane.setInteractiveMode(false);
64     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
65   }
66
67   @Test(groups = "Functional")
68   public void testHideFeatureColumns()
69   {
70     SequenceI seq1 = new Sequence("Seq1", "ABCDEFGHIJ");
71     SequenceI seq2 = new Sequence("Seq2", "ABCDEFGHIJ");
72     seq1.addSequenceFeature(new SequenceFeature("Metal", "", 1, 5,
73             Float.NaN, null));
74     seq2.addSequenceFeature(new SequenceFeature("Metal", "", 6, 10,
75             Float.NaN, null));
76     seq1.addSequenceFeature(new SequenceFeature("Turn", "", 2, 4,
77             Float.NaN, null));
78     seq2.addSequenceFeature(new SequenceFeature("Turn", "", 7, 9,
79             Float.NaN, null));
80     AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
81     AlignFrame alignFrame = new AlignFrame(al, al.getWidth(), al.getHeight());
82
83     /*
84      * hiding a feature not present does nothing
85      */
86     assertFalse(alignFrame.hideFeatureColumns("exon", true));
87     assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
88     assertTrue(alignFrame.getViewport().getAlignment().getHiddenColumns()
89             .getHiddenColumnsCopy()
90             .isEmpty());
91     assertFalse(alignFrame.hideFeatureColumns("exon", false));
92     assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
93     assertTrue(alignFrame.getViewport().getAlignment().getHiddenColumns()
94             .getHiddenColumnsCopy()
95             .isEmpty());
96
97     /*
98      * hiding a feature in all columns does nothing
99      */
100     assertFalse(alignFrame.hideFeatureColumns("Metal", true));
101     assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
102     List<int[]> hidden = alignFrame.getViewport().getAlignment()
103             .getHiddenColumns()
104             .getHiddenColumnsCopy();
105     assertTrue(hidden.isEmpty());
106
107     /*
108      * hide a feature present in some columns
109      * sequence positions [2-4], [7-9] are column positions
110      * [1-3], [6-8] base zero
111      */
112     assertTrue(alignFrame.hideFeatureColumns("Turn", true));
113     hidden = alignFrame.getViewport().getAlignment().getHiddenColumns()
114             .getHiddenColumnsCopy();
115     assertEquals(hidden.size(), 2);
116     assertEquals(hidden.get(0)[0], 1);
117     assertEquals(hidden.get(0)[1], 3);
118     assertEquals(hidden.get(1)[0], 6);
119     assertEquals(hidden.get(1)[1], 8);
120   }
121
122   @BeforeClass(alwaysRun = true)
123   public static void setUpBeforeClass() throws Exception
124   {
125     /*
126      * use read-only test properties file
127      */
128     Cache.loadProperties("test/jalview/io/testProps.jvprops");
129     Jalview.main(new String[] { "-nonews" });
130   }
131
132   @AfterMethod(alwaysRun = true)
133   public void tearDown()
134   {
135     Desktop.instance.closeAll_actionPerformed(null);
136   }
137
138   /**
139    * configure (read-only) properties for test to ensure Consensus is computed
140    * for colour Above PID testing
141    */
142   @BeforeMethod(alwaysRun = true)
143   public void setUp()
144   {
145     Cache.loadProperties("test/jalview/io/testProps.jvprops");
146     Cache.applicationProperties.setProperty("SHOW_IDENTITY",
147             Boolean.TRUE.toString());
148     af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
149             DataSourceType.FILE);
150
151     /*
152      * wait for Consensus thread to complete
153      */
154     synchronized (this)
155     {
156       while (af.getViewport().getConsensusSeq() == null)
157       {
158         try
159         {
160           wait(50);
161         } catch (InterruptedException e)
162         {
163         }
164       }
165     }
166   }
167
168   /**
169    * Test that changing background (alignment) colour scheme
170    * <ul>
171    * <li>with Apply Colour to All Groups not selected, does not change group
172    * colours</li>
173    * <li>with Apply Colour to All Groups selected, does change group colours</li>
174    * <li>in neither case, changes alignment or group colour thresholds (PID or
175    * Conservation)</li>
176    * </ul>
177    */
178   @Test(groups = "Functional")
179   public void testChangeColour_background_groupsAndThresholds()
180   {
181     AlignViewport av = af.getViewport();
182     AlignmentI al = av.getAlignment();
183
184     /*
185      * Colour alignment by Buried Index
186      */
187     af.applyToAllGroups_actionPerformed(false);
188     af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
189     assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
190     assertFalse(av.getResidueShading().conservationApplied());
191     assertEquals(av.getResidueShading().getThreshold(), 0);
192
193     /*
194      * Apply Conservation 20%
195      */
196     af.conservationMenuItem_actionPerformed(true);
197     SliderPanel sp = SliderPanel.getSliderPanel();
198     assertEquals(sp.getTitle(), MessageManager.formatMessage(
199             "label.conservation_colour_increment",
200             new String[] { "Background" }));
201     assertTrue(sp.isForConservation());
202     sp.valueChanged(20);
203     assertTrue(av.getResidueShading().conservationApplied());
204     assertEquals(av.getResidueShading().getConservationInc(), 20);
205
206     /*
207      * Apply PID threshold 10% (conservation still applies as well)
208      */
209     af.abovePIDThreshold_actionPerformed(true);
210     sp = SliderPanel.getSliderPanel();
211     assertFalse(sp.isForConservation());
212     assertEquals(sp.getTitle(), MessageManager.formatMessage(
213             "label.percentage_identity_threshold",
214             new String[] { "Background" }));
215     sp.valueChanged(10);
216     assertEquals(av.getResidueShading().getThreshold(), 10);
217     assertTrue(av.getResidueShading().conservationApplied());
218     assertEquals(av.getResidueShading().getConservationInc(), 20);
219
220     /*
221      * create a group with Strand colouring, 30% Conservation
222      * and 40% PID threshold
223      */
224     SequenceGroup sg = new SequenceGroup();
225     sg.addSequence(al.getSequenceAt(0), false);
226     sg.setStartRes(15);
227     sg.setEndRes(25);
228     av.setSelectionGroup(sg);
229
230     /*
231      * apply 30% Conservation to group
232      */
233     PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
234     popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
235             .toString());
236     assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
237     assertEquals(al.getGroups().size(), 1);
238     assertSame(al.getGroups().get(0), sg);
239     popupMenu.conservationMenuItem_actionPerformed(true);
240     sp = SliderPanel.getSliderPanel();
241     assertTrue(sp.isForConservation());
242     assertEquals(sp.getTitle(), MessageManager.formatMessage(
243             "label.conservation_colour_increment",
244             new String[] { sg.getName() }));
245     sp.valueChanged(30);
246     assertTrue(sg.getGroupColourScheme().conservationApplied());
247     assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
248
249     /*
250      * apply 40% PID threshold to group
251      */
252     popupMenu.abovePIDColour_actionPerformed(true);
253     sp = SliderPanel.getSliderPanel();
254     assertFalse(sp.isForConservation());
255     assertEquals(sp.getTitle(), MessageManager.formatMessage(
256             "label.percentage_identity_threshold",
257             new String[] { sg.getName() }));
258     sp.valueChanged(40);
259     assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
260     // conservation threshold is unchanged:
261     assertTrue(sg.getGroupColourScheme().conservationApplied());
262     assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
263
264     /*
265      * change alignment colour - group colour, and all thresholds,
266      * should be unaffected
267      */
268     af.changeColour_actionPerformed(JalviewColourScheme.Turn.toString());
269     assertTrue(av.getGlobalColourScheme() instanceof TurnColourScheme);
270     assertTrue(av.getResidueShading().conservationApplied());
271     assertEquals(av.getResidueShading().getConservationInc(), 20);
272     assertEquals(av.getResidueShading().getThreshold(), 10);
273     assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
274     assertTrue(sg.getGroupColourScheme().conservationApplied());
275     assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
276     assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
277
278     /*
279      * Now change alignment colour with Apply Colour To All Groups
280      * - group colour should change, but not colour thresholds
281      */
282     af.applyToAllGroups_actionPerformed(true);
283     af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
284     assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
285     assertTrue(av.getResidueShading().conservationApplied());
286     assertEquals(av.getResidueShading().getConservationInc(), 20);
287     assertEquals(av.getResidueShading().getThreshold(), 10);
288     assertTrue(sg.getColourScheme() instanceof HelixColourScheme);
289     assertTrue(sg.getGroupColourScheme().conservationApplied());
290     assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
291     assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
292   }
293
294   /**
295    * Test residue colouring with various options
296    * <ol>
297    * <li>no PID or Conservation threshold</li>
298    * <li>colour by Conservation applied</li>
299    * <li>colour by Conservation removed</li>
300    * <li>colour above PID - various values</li>
301    * <li>colour above PID removed</li>
302    * <li>Above PID plus By Conservation combined</li>
303    * <li>remove Above PID to leave just By Conservation</li>
304    * <li>re-add Above PID</li>
305    * <li>remove By Conservation to leave just Above PID</li>
306    * <li>remove Above PID to leave original colours</li>
307    * </ol>
308    */
309   @Test(groups = "Functional")
310   public void testColourThresholdActions()
311   {
312     AlignViewport av = af.getViewport();
313     AlignmentI al = av.getAlignment();
314
315     /*
316      * Colour alignment by Helix Propensity, no thresholds
317      */
318     af.applyToAllGroups_actionPerformed(false);
319     af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
320     assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
321     assertFalse(av.getResidueShading().conservationApplied());
322     assertEquals(av.getResidueShading().getThreshold(), 0);
323
324     /*
325      * inspect the colour of 
326      * FER_CAPAN.9(I), column 14 (14 base 0)
327      * FER_CAPAN.10(SER), column 16 (15 base 0)
328      */
329     SequenceI ferCapan = al.findName("FER_CAPAN");
330     ResidueShaderI rs = av.getResidueShading();
331     Color c = rs.findColour('I', 14, ferCapan);
332     Color i_original = new Color(138, 117, 138);
333     assertEquals(c, i_original);
334     c = rs.findColour('S', 15, ferCapan);
335     Color s_original = new Color(54, 201, 54);
336     assertEquals(c, s_original);
337
338     /*
339      * colour by conservation with increment 10
340      */
341     af.conservationMenuItem_actionPerformed(true);
342     SliderPanel sp = SliderPanel.getSliderPanel();
343     assertTrue(sp.isForConservation());
344     assertEquals(sp.getValue(), 30); // initial slider setting
345     sp.valueChanged(10);
346     assertSame(rs, av.getResidueShading());
347     c = rs.findColour('I', 14, ferCapan);
348     Color i_faded = new Color(196, 186, 196);
349     assertEquals(c, i_faded);
350     c = rs.findColour('S', 15, ferCapan);
351     Color s_faded = new Color(144, 225, 144);
352     assertEquals(c, s_faded);
353
354     /*
355      * deselect By Conservation - colour should revert
356      */
357     af.conservationMenuItem_actionPerformed(false);
358     c = rs.findColour('S', 15, ferCapan);
359     assertEquals(c, s_original);
360
361     /*
362      * now Above PID, threshold = 0%
363      * should be no change
364      */
365     af.abovePIDThreshold_actionPerformed(true);
366     sp = SliderPanel.getSliderPanel();
367     assertFalse(sp.isForConservation());
368     assertEquals(sp.getValue(), 0); // initial slider setting
369     c = rs.findColour('I', 14, ferCapan);
370     assertEquals(c, i_original);
371     c = rs.findColour('S', 15, ferCapan);
372     assertEquals(c, s_original);
373
374     /*
375      * Above PID, threshold = 1%
376      * 15.I becomes White because no match to consensus (V)
377      * 16.S remains coloured as matches 66.66% consensus
378      */
379     sp.valueChanged(1);
380     c = rs.findColour('I', 14, ferCapan);
381     assertEquals(c, Color.white);
382     c = rs.findColour('S', 15, ferCapan);
383     assertEquals(c, s_original);
384
385     /*
386      * threshold 66% - no further change yet...
387      */
388     sp.valueChanged(66);
389     c = rs.findColour('I', 14, ferCapan);
390     assertEquals(c, Color.white);
391     c = rs.findColour('S', 15, ferCapan);
392     assertEquals(c, s_original);
393
394     /*
395      * threshold 67% - now both residues are white
396      */
397     sp.valueChanged(67);
398     c = rs.findColour('I', 14, ferCapan);
399     assertEquals(c, Color.white);
400     c = rs.findColour('S', 15, ferCapan);
401     assertEquals(c, Color.white);
402
403     /*
404      * deselect Above PID - colours should revert
405      */
406     af.abovePIDThreshold_actionPerformed(false);
407     c = rs.findColour('I', 14, ferCapan);
408     assertEquals(c, i_original);
409     c = rs.findColour('S', 15, ferCapan);
410     assertEquals(c, s_original);
411
412     /*
413      * Now combine Above 50% PID and By Conservation 10%
414      * 15.I is White because no match to consensus (V)
415      * 16.S is coloured but faded
416      */
417     af.abovePIDThreshold_actionPerformed(true);
418     sp = SliderPanel.getSliderPanel();
419     assertFalse(sp.isForConservation());
420     sp.valueChanged(50);
421     af.conservationMenuItem_actionPerformed(true);
422     sp = SliderPanel.getSliderPanel();
423     assertTrue(sp.isForConservation());
424     sp.valueChanged(10);
425     c = rs.findColour('I', 14, ferCapan);
426     assertEquals(c, Color.white);
427     c = rs.findColour('S', 15, ferCapan);
428     assertEquals(c, s_faded);
429
430     /*
431      * turn off Above PID - should just leave Conservation fading as before 
432      */
433     af.abovePIDThreshold_actionPerformed(false);
434     c = rs.findColour('I', 14, ferCapan);
435     assertEquals(c, i_faded);
436     c = rs.findColour('S', 15, ferCapan);
437     assertEquals(c, s_faded);
438
439     /*
440      * Now add Above 50% PID to conservation colouring
441      * - should give the same as PID followed by conservation (above)
442      */
443     af.abovePIDThreshold_actionPerformed(true);
444     SliderPanel.getSliderPanel().valueChanged(50);
445     c = rs.findColour('I', 14, ferCapan);
446     assertEquals(c, Color.white);
447     c = rs.findColour('S', 15, ferCapan);
448     assertEquals(c, s_faded);
449
450     /*
451      * turn off By Conservation
452      * should leave I white, S original (unfaded) colour
453      */
454     af.conservationMenuItem_actionPerformed(false);
455     c = rs.findColour('I', 14, ferCapan);
456     assertEquals(c, Color.white);
457     c = rs.findColour('S', 15, ferCapan);
458     assertEquals(c, s_original);
459
460     /*
461      * finally turn off Above PID to leave original colours
462      */
463     af.abovePIDThreshold_actionPerformed(false);
464     c = rs.findColour('I', 14, ferCapan);
465     assertEquals(c, i_original);
466     c = rs.findColour('S', 15, ferCapan);
467     assertEquals(c, s_original);
468   }
469
470   /**
471    * Verify that making a New View transfers alignment and group colour schemes,
472    * including any thresholds, to the new view. Because New View is performed by
473    * saving and reloading a 'project' file, this is similar to verifying a
474    * project save and reload.
475    * 
476    * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
477    */
478   @Test(groups = "Functional")
479   public void testNewView_colourThresholds()
480   {
481     AlignViewport av = af.getViewport();
482     AlignmentI al = av.getAlignment();
483
484     /*
485      * Colour alignment by Buried Index, Above 10% PID, By Conservation 20%
486      */
487     af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
488     assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
489     af.abovePIDThreshold_actionPerformed(true);
490     SliderPanel sp = SliderPanel.getSliderPanel();
491     assertFalse(sp.isForConservation());
492     sp.valueChanged(10);
493     af.conservationMenuItem_actionPerformed(true);
494     sp = SliderPanel.getSliderPanel();
495     assertTrue(sp.isForConservation());
496     sp.valueChanged(20);
497     ResidueShaderI rs = av.getResidueShading();
498     assertEquals(rs.getThreshold(), 10);
499     assertTrue(rs.conservationApplied());
500     assertEquals(rs.getConservationInc(), 20);
501
502     /*
503      * create a group with Strand colouring, 30% Conservation
504      * and 40% PID threshold
505      */
506     SequenceGroup sg = new SequenceGroup();
507     sg.addSequence(al.getSequenceAt(0), false);
508     sg.setStartRes(15);
509     sg.setEndRes(25);
510     av.setSelectionGroup(sg);
511     PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
512     popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
513             .toString());
514     assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
515     assertEquals(al.getGroups().size(), 1);
516     assertSame(al.getGroups().get(0), sg);
517     popupMenu.conservationMenuItem_actionPerformed(true);
518     sp = SliderPanel.getSliderPanel();
519     assertTrue(sp.isForConservation());
520     sp.valueChanged(30);
521     popupMenu.abovePIDColour_actionPerformed(true);
522     sp = SliderPanel.getSliderPanel();
523     assertFalse(sp.isForConservation());
524     sp.valueChanged(40);
525     rs = sg.getGroupColourScheme();
526     assertTrue(rs.conservationApplied());
527     assertEquals(rs.getConservationInc(), 30);
528     assertEquals(rs.getThreshold(), 40);
529
530     /*
531      * set slider panel focus to the background alignment
532      */
533     af.conservationMenuItem_actionPerformed(true);
534     sp = SliderPanel.getSliderPanel();
535     assertTrue(sp.isForConservation());
536     assertEquals(sp.getTitle(), MessageManager.formatMessage(
537             "label.conservation_colour_increment",
538             new String[] { "Background" }));
539
540     /*
541      * make a new View, verify alignment and group colour schemes
542      */
543     af.newView_actionPerformed(null);
544     assertEquals(af.alignPanel.getViewName(), "View 1");
545     AlignViewport av2 = af.getViewport();
546     assertNotSame(av, av2);
547     rs = av2.getResidueShading();
548     assertNotSame(av.getResidueShading(), rs);
549     assertEquals(rs.getThreshold(), 10);
550     assertTrue(rs.conservationApplied());
551     assertEquals(rs.getConservationInc(), 20);
552     assertEquals(av2.getAlignment().getGroups().size(), 1);
553     sg = av2.getAlignment().getGroups().get(0);
554     rs = sg.getGroupColourScheme();
555     assertTrue(rs.conservationApplied());
556     assertEquals(rs.getConservationInc(), 30);
557     assertEquals(rs.getThreshold(), 40);
558
559     /*
560      * check the Conservation SliderPanel (still open) is linked to 
561      * and updates the new view (JAL-2385)
562      */
563     sp = SliderPanel.getSliderPanel();
564     assertTrue(sp.isForConservation());
565     assertEquals(sp.getTitle(), MessageManager.formatMessage(
566             "label.conservation_colour_increment",
567             new String[] { "View 1" }));
568     sp.valueChanged(22);
569     assertEquals(av2.getResidueShading().getConservationInc(), 22);
570   }
571 }