2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.junit.Assert.assertNotEquals;
24 import static org.testng.Assert.assertEquals;
25 import static org.testng.Assert.assertFalse;
26 import static org.testng.Assert.assertNotSame;
27 import static org.testng.Assert.assertSame;
28 import static org.testng.Assert.assertTrue;
30 import jalview.api.FeatureColourI;
31 import jalview.bin.Cache;
32 import jalview.bin.Jalview;
33 import jalview.datamodel.Alignment;
34 import jalview.datamodel.AlignmentI;
35 import jalview.datamodel.HiddenColumns;
36 import jalview.datamodel.Sequence;
37 import jalview.datamodel.SequenceFeature;
38 import jalview.datamodel.SequenceGroup;
39 import jalview.datamodel.SequenceI;
40 import jalview.io.DataSourceType;
41 import jalview.io.FileLoader;
42 import jalview.project.Jalview2xmlTests;
43 import jalview.renderer.ResidueShaderI;
44 import jalview.schemes.BuriedColourScheme;
45 import jalview.schemes.FeatureColour;
46 import jalview.schemes.HelixColourScheme;
47 import jalview.schemes.JalviewColourScheme;
48 import jalview.schemes.StrandColourScheme;
49 import jalview.schemes.TurnColourScheme;
50 import jalview.util.MessageManager;
52 import java.awt.Color;
53 import java.util.Iterator;
55 import org.testng.annotations.AfterMethod;
56 import org.testng.annotations.BeforeClass;
57 import org.testng.annotations.BeforeMethod;
58 import org.testng.annotations.Test;
60 public class AlignFrameTest
64 @BeforeClass(alwaysRun = true)
65 public void setUpJvOptionPane()
67 JvOptionPane.setInteractiveMode(false);
68 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
70 // BH 2020.03.22 solved the assertEquals(c, i_faded) problem below
71 Jalview.setSynchronous(true);
74 @Test(groups = "Functional")
75 public void testHideFeatureColumns()
77 SequenceI seq1 = new Sequence("Seq1", "ABCDEFGHIJ");
78 SequenceI seq2 = new Sequence("Seq2", "ABCDEFGHIJ");
79 seq1.addSequenceFeature(new SequenceFeature("Metal", "", 1, 5, 0f, null));
80 seq2.addSequenceFeature(new SequenceFeature("Metal", "", 6, 10, 10f,
82 seq1.addSequenceFeature(new SequenceFeature("Turn", "", 2, 4,
84 seq2.addSequenceFeature(new SequenceFeature("Turn", "", 7, 9,
86 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
87 AlignFrame alignFrame = new AlignFrame(al, al.getWidth(),
91 * make all features visible (select feature columns checks visibility)
93 alignFrame.getFeatureRenderer().findAllFeatures(true);
96 * hiding a feature not present does nothing
98 assertFalse(alignFrame.hideFeatureColumns("exon", true));
99 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
101 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
102 .getNumberOfRegions(), 0);
104 assertFalse(alignFrame.hideFeatureColumns("exon", false));
105 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
107 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
108 .getNumberOfRegions(), 0);
111 * hiding a feature in all columns does nothing
113 assertFalse(alignFrame.hideFeatureColumns("Metal", true));
114 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
116 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
117 .getNumberOfRegions(), 0);
121 * threshold Metal to hide features where score < 5
122 * seq1 feature in columns 1-5 is hidden
123 * seq2 feature in columns 6-10 is shown
125 FeatureColourI fc = new FeatureColour(null, Color.red, Color.blue, null,
127 fc.setAboveThreshold(true);
129 alignFrame.getFeatureRenderer().setColour("Metal", fc);
130 assertTrue(alignFrame.hideFeatureColumns("Metal", true));
131 HiddenColumns hidden = alignFrame.getViewport().getAlignment().getHiddenColumns();
132 assertEquals(hidden.getNumberOfRegions(), 1);
133 Iterator<int[]> regions = hidden.iterator();
134 int[] next = regions.next();
135 assertEquals(next[0], 5);
136 assertEquals(next[1], 9);
139 * hide a feature present in some columns
140 * sequence positions [2-4], [7-9] are column positions
141 * [1-3], [6-8] base zero
143 alignFrame.getViewport().showAllHiddenColumns();
144 assertTrue(alignFrame.hideFeatureColumns("Turn", true));
145 regions = alignFrame.getViewport().getAlignment()
146 .getHiddenColumns().iterator();
147 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
148 .getNumberOfRegions(), 2);
149 next = regions.next();
150 assertEquals(next[0], 1);
151 assertEquals(next[1], 3);
152 next = regions.next();
153 assertEquals(next[0], 6);
154 assertEquals(next[1], 8);
157 @BeforeClass(alwaysRun = true)
158 public static void setUpBeforeClass() throws Exception
161 * use read-only test properties file
163 Cache.loadProperties("test/jalview/io/testProps.jvprops");
164 Jalview.main(new String[] { "-nonews" });
167 @AfterMethod(alwaysRun = true)
168 public void tearDown()
170 Desktop.getInstance().closeAll_actionPerformed(null);
174 * configure (read-only) properties for test to ensure Consensus is computed
175 * for colour Above PID testing
177 @BeforeMethod(alwaysRun = true)
180 Cache.loadProperties("test/jalview/io/testProps.jvprops");
181 Cache.getInstance().applicationProperties.setProperty("SHOW_IDENTITY",
182 Boolean.TRUE.toString());
183 af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
184 DataSourceType.FILE);
187 * wait for Consensus thread to complete
191 while (af.getViewport().getConsensusSeq() == null)
196 } catch (InterruptedException e)
204 * Test that changing background (alignment) colour scheme
206 * <li>with Apply Colour to All Groups not selected, does not change group
208 * <li>with Apply Colour to All Groups selected, does change group colours</li>
209 * <li>in neither case, changes alignment or group colour thresholds (PID or
213 @Test(groups = "Functional")
214 public void testChangeColour_background_groupsAndThresholds()
216 AlignViewport av = af.getViewport();
217 AlignmentI al = av.getAlignment();
220 * Colour alignment by Buried Index
222 af.applyToAllGroups_actionPerformed(false);
223 af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
224 assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
225 assertFalse(av.getResidueShading().conservationApplied());
226 assertEquals(av.getResidueShading().getThreshold(), 0);
229 * Apply Conservation 20%
231 af.conservationMenuItem_actionPerformed(true);
232 SliderPanel sp = SliderPanel.getSliderPanel();
233 assertEquals(sp.getTitle(), MessageManager.formatMessage(
234 "label.conservation_colour_increment",
235 new String[] { "Background" }));
236 assertTrue(sp.isForConservation());
238 assertTrue(av.getResidueShading().conservationApplied());
239 assertEquals(av.getResidueShading().getConservationInc(), 20);
242 * Apply PID threshold 10% (conservation still applies as well)
244 af.abovePIDThreshold_actionPerformed(true);
245 sp = SliderPanel.getSliderPanel();
246 assertFalse(sp.isForConservation());
247 assertEquals(sp.getTitle(), MessageManager.formatMessage(
248 "label.percentage_identity_threshold",
249 new String[] { "Background" }));
251 assertEquals(av.getResidueShading().getThreshold(), 10);
252 assertTrue(av.getResidueShading().conservationApplied());
253 assertEquals(av.getResidueShading().getConservationInc(), 20);
256 * create a group with Strand colouring, 30% Conservation
257 * and 40% PID threshold
259 SequenceGroup sg = new SequenceGroup();
260 sg.addSequence(al.getSequenceAt(0), false);
263 av.setSelectionGroup(sg);
266 * apply 30% Conservation to group
268 PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
269 popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
271 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
272 assertEquals(al.getGroups().size(), 1);
273 assertSame(al.getGroups().get(0), sg);
274 popupMenu.conservationMenuItem_actionPerformed(true);
275 sp = SliderPanel.getSliderPanel();
276 assertTrue(sp.isForConservation());
277 assertEquals(sp.getTitle(), MessageManager.formatMessage(
278 "label.conservation_colour_increment",
279 new String[] { sg.getName() }));
281 assertTrue(sg.getGroupColourScheme().conservationApplied());
282 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
285 * apply 40% PID threshold to group
287 popupMenu.abovePIDColour_actionPerformed(true);
288 sp = SliderPanel.getSliderPanel();
289 assertFalse(sp.isForConservation());
290 assertEquals(sp.getTitle(), MessageManager.formatMessage(
291 "label.percentage_identity_threshold",
292 new String[] { sg.getName() }));
294 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
295 // conservation threshold is unchanged:
296 assertTrue(sg.getGroupColourScheme().conservationApplied());
297 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
300 * change alignment colour - group colour, and all thresholds,
301 * should be unaffected
303 af.changeColour_actionPerformed(JalviewColourScheme.Turn.toString());
304 assertTrue(av.getGlobalColourScheme() instanceof TurnColourScheme);
305 assertTrue(av.getResidueShading().conservationApplied());
306 assertEquals(av.getResidueShading().getConservationInc(), 20);
307 assertEquals(av.getResidueShading().getThreshold(), 10);
308 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
309 assertTrue(sg.getGroupColourScheme().conservationApplied());
310 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
311 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
314 * Now change alignment colour with Apply Colour To All Groups
315 * - group colour should change, but not colour thresholds
317 af.applyToAllGroups_actionPerformed(true);
318 af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
319 assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
320 assertTrue(av.getResidueShading().conservationApplied());
321 assertEquals(av.getResidueShading().getConservationInc(), 20);
322 assertEquals(av.getResidueShading().getThreshold(), 10);
323 assertTrue(sg.getColourScheme() instanceof HelixColourScheme);
324 assertTrue(sg.getGroupColourScheme().conservationApplied());
325 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
326 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
330 * Test residue colouring with various options
332 * <li>no PID or Conservation threshold</li>
333 * <li>colour by Conservation applied</li>
334 * <li>colour by Conservation removed</li>
335 * <li>colour above PID - various values</li>
336 * <li>colour above PID removed</li>
337 * <li>Above PID plus By Conservation combined</li>
338 * <li>remove Above PID to leave just By Conservation</li>
339 * <li>re-add Above PID</li>
340 * <li>remove By Conservation to leave just Above PID</li>
341 * <li>remove Above PID to leave original colours</li>
344 @Test(groups = "Functional")
345 public void testColourThresholdActions()
347 AlignViewport av = af.getViewport();
348 AlignmentI al = av.getAlignment();
351 * Colour alignment by Helix Propensity, no thresholds
353 af.applyToAllGroups_actionPerformed(false);
354 af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
355 assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
356 assertFalse(av.getResidueShading().conservationApplied());
357 assertEquals(av.getResidueShading().getThreshold(), 0);
360 * inspect the colour of
361 * FER_CAPAN.9(I), column 14 (14 base 0)
362 * FER_CAPAN.10(SER), column 16 (15 base 0)
364 SequenceI ferCapan = al.findName("FER_CAPAN");
365 ResidueShaderI rs = av.getResidueShading();
366 Color c = rs.findColour('I', 14, ferCapan);
367 Color i_original = new Color(138, 117, 138);
368 assertEquals(c, i_original);
369 c = rs.findColour('S', 15, ferCapan);
370 Color s_original = new Color(54, 201, 54);
371 assertEquals(c, s_original);
374 * colour by conservation with increment 10
376 af.conservationMenuItem_actionPerformed(true);
377 SliderPanel sp = SliderPanel.getSliderPanel();
378 assertTrue(sp.isForConservation());
379 assertEquals(sp.getValue(), 30); // initial slider setting
381 assertSame(rs, av.getResidueShading());
382 c = rs.findColour('I', 14, ferCapan);
383 Color i_faded = new Color(196, 186, 196);
384 assertEquals(c, i_faded);
385 c = rs.findColour('S', 15, ferCapan);
386 Color s_faded = new Color(144, 225, 144);
387 assertEquals(c, s_faded);
390 * deselect By Conservation - colour should revert
392 af.conservationMenuItem_actionPerformed(false);
393 c = rs.findColour('S', 15, ferCapan);
394 assertEquals(c, s_original);
397 * now Above PID, threshold = 0%
398 * should be no change
400 af.abovePIDThreshold_actionPerformed(true);
401 sp = SliderPanel.getSliderPanel();
402 assertFalse(sp.isForConservation());
403 assertEquals(sp.getValue(), 0); // initial slider setting
404 c = rs.findColour('I', 14, ferCapan);
405 assertEquals(c, i_original);
406 c = rs.findColour('S', 15, ferCapan);
407 assertEquals(c, s_original);
410 * Above PID, threshold = 1%
411 * 15.I becomes White because no match to consensus (V)
412 * 16.S remains coloured as matches 66.66% consensus
415 c = rs.findColour('I', 14, ferCapan);
416 assertEquals(c, Color.white);
417 c = rs.findColour('S', 15, ferCapan);
418 assertEquals(c, s_original);
421 * threshold 66% - no further change yet...
424 c = rs.findColour('I', 14, ferCapan);
425 assertEquals(c, Color.white);
426 c = rs.findColour('S', 15, ferCapan);
427 assertEquals(c, s_original);
430 * threshold 67% - now both residues are white
433 c = rs.findColour('I', 14, ferCapan);
434 assertEquals(c, Color.white);
435 c = rs.findColour('S', 15, ferCapan);
436 assertEquals(c, Color.white);
439 * deselect Above PID - colours should revert
441 af.abovePIDThreshold_actionPerformed(false);
442 c = rs.findColour('I', 14, ferCapan);
443 assertEquals(c, i_original);
444 c = rs.findColour('S', 15, ferCapan);
445 assertEquals(c, s_original);
448 * Now combine Above 50% PID and By Conservation 10%
449 * 15.I is White because no match to consensus (V)
450 * 16.S is coloured but faded
452 af.abovePIDThreshold_actionPerformed(true);
453 sp = SliderPanel.getSliderPanel();
454 assertFalse(sp.isForConservation());
456 af.conservationMenuItem_actionPerformed(true);
457 sp = SliderPanel.getSliderPanel();
458 assertTrue(sp.isForConservation());
460 c = rs.findColour('I', 14, ferCapan);
461 assertEquals(c, Color.white);
462 c = rs.findColour('S', 15, ferCapan);
463 assertEquals(c, s_faded);
466 * turn off Above PID - should just leave Conservation fading as before
468 af.abovePIDThreshold_actionPerformed(false);
469 c = rs.findColour('I', 14, ferCapan);
470 assertEquals(c, i_faded);
471 c = rs.findColour('S', 15, ferCapan);
472 assertEquals(c, s_faded);
475 * Now add Above 50% PID to conservation colouring
476 * - should give the same as PID followed by conservation (above)
478 af.abovePIDThreshold_actionPerformed(true);
479 SliderPanel.getSliderPanel().valueChanged(50);
480 c = rs.findColour('I', 14, ferCapan);
481 assertEquals(c, Color.white);
482 c = rs.findColour('S', 15, ferCapan);
483 assertEquals(c, s_faded);
486 * turn off By Conservation
487 * should leave I white, S original (unfaded) colour
489 af.conservationMenuItem_actionPerformed(false);
490 c = rs.findColour('I', 14, ferCapan);
491 assertEquals(c, Color.white);
492 c = rs.findColour('S', 15, ferCapan);
493 assertEquals(c, s_original);
496 * finally turn off Above PID to leave original colours
498 af.abovePIDThreshold_actionPerformed(false);
499 c = rs.findColour('I', 14, ferCapan);
500 assertEquals(c, i_original);
501 c = rs.findColour('S', 15, ferCapan);
502 assertEquals(c, s_original);
506 * Verify that making a New View transfers alignment and group colour schemes,
507 * including any thresholds, to the new view. Because New View is performed by
508 * saving and reloading a 'project' file, this is similar to verifying a
509 * project save and reload.
511 * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
513 @Test(groups = "Functional")
514 public void testNewView_colourThresholds()
516 AlignViewport av = af.getViewport();
517 AlignmentI al = av.getAlignment();
520 * Colour alignment by Buried Index, Above 10% PID, By Conservation 20%
522 af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
523 assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
524 af.abovePIDThreshold_actionPerformed(true);
525 SliderPanel sp = SliderPanel.getSliderPanel();
526 assertFalse(sp.isForConservation());
528 af.conservationMenuItem_actionPerformed(true);
529 sp = SliderPanel.getSliderPanel();
530 assertTrue(sp.isForConservation());
532 ResidueShaderI rs = av.getResidueShading();
533 assertEquals(rs.getThreshold(), 10);
534 assertTrue(rs.conservationApplied());
535 assertEquals(rs.getConservationInc(), 20);
538 * create a group with Strand colouring, 30% Conservation
539 * and 40% PID threshold
541 SequenceGroup sg = new SequenceGroup();
542 sg.addSequence(al.getSequenceAt(0), false);
545 av.setSelectionGroup(sg);
546 PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
547 popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
549 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
550 assertEquals(al.getGroups().size(), 1);
551 assertSame(al.getGroups().get(0), sg);
552 popupMenu.conservationMenuItem_actionPerformed(true);
553 sp = SliderPanel.getSliderPanel();
554 assertTrue(sp.isForConservation());
556 popupMenu.abovePIDColour_actionPerformed(true);
557 sp = SliderPanel.getSliderPanel();
558 assertFalse(sp.isForConservation());
560 rs = sg.getGroupColourScheme();
561 assertTrue(rs.conservationApplied());
562 assertEquals(rs.getConservationInc(), 30);
563 assertEquals(rs.getThreshold(), 40);
566 * set slider panel focus to the background alignment
568 af.conservationMenuItem_actionPerformed(true);
569 sp = SliderPanel.getSliderPanel();
570 assertTrue(sp.isForConservation());
571 assertEquals(sp.getTitle(), MessageManager.formatMessage(
572 "label.conservation_colour_increment",
573 new String[] { "Background" }));
576 * make a new View, verify alignment and group colour schemes
578 af.newView_actionPerformed(null);
579 assertEquals(af.alignPanel.getViewName(), "View 1");
580 AlignViewport av2 = af.getViewport();
581 assertNotSame(av, av2);
582 assertSame(av2, af.alignPanel.av);
583 rs = av2.getResidueShading();
584 assertNotSame(av.getResidueShading(), rs);
585 assertEquals(rs.getThreshold(), 10);
586 assertTrue(rs.conservationApplied(), rs.toString());
587 assertEquals(rs.getConservationInc(), 20);
588 assertEquals(av2.getAlignment().getGroups().size(), 1);
589 sg = av2.getAlignment().getGroups().get(0);
590 rs = sg.getGroupColourScheme();
591 assertTrue(rs.conservationApplied());
592 assertEquals(rs.getConservationInc(), 30);
593 assertEquals(rs.getThreshold(), 40);
596 * check the Conservation SliderPanel (still open) is linked to
597 * and updates the new view (JAL-2385)
599 sp = SliderPanel.getSliderPanel();
600 assertTrue(sp.isForConservation());
601 assertEquals(sp.getTitle(), MessageManager.formatMessage(
602 "label.conservation_colour_increment",
603 new String[] { "View 1" }));
605 assertEquals(av2.getResidueShading().getConservationInc(), 22);
609 * Verify that making a New View preserves the dataset reference for the
610 * alignment. Otherwise, see a 'duplicate jar entry' reference when trying to
611 * save alignments with multiple views, and codon mappings will not be shared
612 * across all panels in a split frame.
614 * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
616 @Test(groups = "Functional")
617 public void testNewView_dsRefPreserved()
619 AlignViewport av = af.getViewport();
620 AlignmentI al = av.getAlignment();
621 AlignmentI original_ds = al.getDataset();
622 af.newView_actionPerformed(null);
623 assertNotEquals("New view didn't select the a new panel", av,
625 org.testng.Assert.assertEquals(original_ds,
626 af.getViewport().getAlignment().getDataset(),
627 "Dataset was not preserved in new view");