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.AlignViewportI;
31 import jalview.api.FeatureColourI;
32 import jalview.bin.Cache;
33 import jalview.bin.Jalview;
34 import jalview.datamodel.Alignment;
35 import jalview.datamodel.AlignmentI;
36 import jalview.datamodel.HiddenColumns;
37 import jalview.datamodel.Sequence;
38 import jalview.datamodel.SequenceFeature;
39 import jalview.datamodel.SequenceGroup;
40 import jalview.datamodel.SequenceI;
41 import jalview.io.DataSourceType;
42 import jalview.io.FileLoader;
43 import jalview.project.Jalview2xmlTests;
44 import jalview.renderer.ResidueShaderI;
45 import jalview.schemes.BuriedColourScheme;
46 import jalview.schemes.FeatureColour;
47 import jalview.schemes.HelixColourScheme;
48 import jalview.schemes.JalviewColourScheme;
49 import jalview.schemes.StrandColourScheme;
50 import jalview.schemes.TurnColourScheme;
51 import jalview.util.MessageManager;
52 import jalview.viewmodel.AlignmentViewport;
54 import java.awt.Color;
55 import java.util.Iterator;
57 import org.testng.annotations.AfterMethod;
58 import org.testng.annotations.BeforeClass;
59 import org.testng.annotations.BeforeMethod;
60 import org.testng.annotations.Test;
62 public class AlignFrameTest
66 @BeforeClass(alwaysRun = true)
67 public void setUpJvOptionPane()
69 JvOptionPane.setInteractiveMode(false);
70 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
73 @Test(groups = "Functional")
74 public void testHideFeatureColumns()
76 SequenceI seq1 = new Sequence("Seq1", "ABCDEFGHIJ");
77 SequenceI seq2 = new Sequence("Seq2", "ABCDEFGHIJ");
78 seq1.addSequenceFeature(new SequenceFeature("Metal", "", 1, 5, 0f, null));
79 seq2.addSequenceFeature(new SequenceFeature("Metal", "", 6, 10, 10f,
81 seq1.addSequenceFeature(new SequenceFeature("Turn", "", 2, 4,
83 seq2.addSequenceFeature(new SequenceFeature("Turn", "", 7, 9,
85 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
86 AlignFrame alignFrame = new AlignFrame(al, al.getWidth(),
90 * make all features visible (select feature columns checks visibility)
92 alignFrame.getFeatureRenderer().findAllFeatures(true);
95 * hiding a feature not present does nothing
97 assertFalse(alignFrame.hideFeatureColumns("exon", true));
98 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
100 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
101 .getNumberOfRegions(), 0);
103 assertFalse(alignFrame.hideFeatureColumns("exon", false));
104 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
106 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
107 .getNumberOfRegions(), 0);
110 * hiding a feature in all columns does nothing
112 assertFalse(alignFrame.hideFeatureColumns("Metal", true));
113 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
115 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
116 .getNumberOfRegions(), 0);
120 * threshold Metal to hide features where score < 5
121 * seq1 feature in columns 1-5 is hidden
122 * seq2 feature in columns 6-10 is shown
124 FeatureColourI fc = new FeatureColour(null, Color.red, Color.blue, null,
126 fc.setAboveThreshold(true);
128 alignFrame.getFeatureRenderer().setColour("Metal", fc);
129 assertTrue(alignFrame.hideFeatureColumns("Metal", true));
130 HiddenColumns hidden = alignFrame.getViewport().getAlignment().getHiddenColumns();
131 assertEquals(hidden.getNumberOfRegions(), 1);
132 Iterator<int[]> regions = hidden.iterator();
133 int[] next = regions.next();
134 assertEquals(next[0], 5);
135 assertEquals(next[1], 9);
138 * hide a feature present in some columns
139 * sequence positions [2-4], [7-9] are column positions
140 * [1-3], [6-8] base zero
142 alignFrame.getViewport().showAllHiddenColumns();
143 assertTrue(alignFrame.hideFeatureColumns("Turn", true));
144 regions = alignFrame.getViewport().getAlignment()
145 .getHiddenColumns().iterator();
146 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
147 .getNumberOfRegions(), 2);
148 next = regions.next();
149 assertEquals(next[0], 1);
150 assertEquals(next[1], 3);
151 next = regions.next();
152 assertEquals(next[0], 6);
153 assertEquals(next[1], 8);
156 @BeforeClass(alwaysRun = true)
157 public static void setUpBeforeClass() throws Exception
160 * use read-only test properties file
162 Cache.loadProperties("test/jalview/io/testProps.jvprops");
163 Jalview.main(new String[] { "-nonews" });
166 @AfterMethod(alwaysRun = true)
167 public void tearDown()
169 Desktop.instance.closeAll_actionPerformed(null);
173 * configure (read-only) properties for test to ensure Consensus is computed
174 * for colour Above PID testing
176 @BeforeMethod(alwaysRun = true)
179 Cache.loadProperties("test/jalview/io/testProps.jvprops");
180 Cache.applicationProperties.setProperty("SHOW_IDENTITY",
181 Boolean.TRUE.toString());
182 af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
183 DataSourceType.FILE);
186 * wait for Consensus thread to complete
190 while (af.getViewport().getConsensusSeq() == null)
195 } catch (InterruptedException e)
203 * Test that changing background (alignment) colour scheme
205 * <li>with Apply Colour to All Groups not selected, does not change group
207 * <li>with Apply Colour to All Groups selected, does change group colours</li>
208 * <li>in neither case, changes alignment or group colour thresholds (PID or
212 @Test(groups = "Functional")
213 public void testChangeColour_background_groupsAndThresholds()
215 AlignViewportI av = af.getViewport();
216 AlignmentI al = av.getAlignment();
219 * Colour alignment by Buried Index
221 af.applyToAllGroups_actionPerformed(false);
222 af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
223 assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
224 assertFalse(av.getResidueShading().conservationApplied());
225 assertEquals(av.getResidueShading().getThreshold(), 0);
228 * Apply Conservation 20%
230 af.conservationMenuItem_actionPerformed(true);
231 SliderPanel sp = SliderPanel.getSliderPanel();
232 assertEquals(sp.getTitle(), MessageManager.formatMessage(
233 "label.conservation_colour_increment",
234 new String[] { "Background" }));
235 assertTrue(sp.isForConservation());
237 assertTrue(av.getResidueShading().conservationApplied());
238 assertEquals(av.getResidueShading().getConservationInc(), 20);
241 * Apply PID threshold 10% (conservation still applies as well)
243 af.abovePIDThreshold_actionPerformed(true);
244 sp = SliderPanel.getSliderPanel();
245 assertFalse(sp.isForConservation());
246 assertEquals(sp.getTitle(), MessageManager.formatMessage(
247 "label.percentage_identity_threshold",
248 new String[] { "Background" }));
250 assertEquals(av.getResidueShading().getThreshold(), 10);
251 assertTrue(av.getResidueShading().conservationApplied());
252 assertEquals(av.getResidueShading().getConservationInc(), 20);
255 * create a group with Strand colouring, 30% Conservation
256 * and 40% PID threshold
258 SequenceGroup sg = new SequenceGroup();
259 sg.addSequence(al.getSequenceAt(0), false);
262 av.setSelectionGroup(sg);
265 * apply 30% Conservation to group
267 PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
268 popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
270 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
271 assertEquals(al.getGroups().size(), 1);
272 assertSame(al.getGroups().get(0), sg);
273 popupMenu.conservationMenuItem_actionPerformed(true);
274 sp = SliderPanel.getSliderPanel();
275 assertTrue(sp.isForConservation());
276 assertEquals(sp.getTitle(), MessageManager.formatMessage(
277 "label.conservation_colour_increment",
278 new String[] { sg.getName() }));
280 assertTrue(sg.getGroupColourScheme().conservationApplied());
281 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
284 * apply 40% PID threshold to group
286 popupMenu.abovePIDColour_actionPerformed(true);
287 sp = SliderPanel.getSliderPanel();
288 assertFalse(sp.isForConservation());
289 assertEquals(sp.getTitle(), MessageManager.formatMessage(
290 "label.percentage_identity_threshold",
291 new String[] { sg.getName() }));
293 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
294 // conservation threshold is unchanged:
295 assertTrue(sg.getGroupColourScheme().conservationApplied());
296 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
299 * change alignment colour - group colour, and all thresholds,
300 * should be unaffected
302 af.changeColour_actionPerformed(JalviewColourScheme.Turn.toString());
303 assertTrue(av.getGlobalColourScheme() instanceof TurnColourScheme);
304 assertTrue(av.getResidueShading().conservationApplied());
305 assertEquals(av.getResidueShading().getConservationInc(), 20);
306 assertEquals(av.getResidueShading().getThreshold(), 10);
307 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
308 assertTrue(sg.getGroupColourScheme().conservationApplied());
309 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
310 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
313 * Now change alignment colour with Apply Colour To All Groups
314 * - group colour should change, but not colour thresholds
316 af.applyToAllGroups_actionPerformed(true);
317 af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
318 assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
319 assertTrue(av.getResidueShading().conservationApplied());
320 assertEquals(av.getResidueShading().getConservationInc(), 20);
321 assertEquals(av.getResidueShading().getThreshold(), 10);
322 assertTrue(sg.getColourScheme() instanceof HelixColourScheme);
323 assertTrue(sg.getGroupColourScheme().conservationApplied());
324 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
325 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
329 * Test residue colouring with various options
331 * <li>no PID or Conservation threshold</li>
332 * <li>colour by Conservation applied</li>
333 * <li>colour by Conservation removed</li>
334 * <li>colour above PID - various values</li>
335 * <li>colour above PID removed</li>
336 * <li>Above PID plus By Conservation combined</li>
337 * <li>remove Above PID to leave just By Conservation</li>
338 * <li>re-add Above PID</li>
339 * <li>remove By Conservation to leave just Above PID</li>
340 * <li>remove Above PID to leave original colours</li>
343 @Test(groups = "Functional")
344 public void testColourThresholdActions()
346 AlignViewportI av = af.getViewport();
347 AlignmentI al = av.getAlignment();
350 * Colour alignment by Helix Propensity, no thresholds
352 af.applyToAllGroups_actionPerformed(false);
353 af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
354 assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
355 assertFalse(av.getResidueShading().conservationApplied());
356 assertEquals(av.getResidueShading().getThreshold(), 0);
359 * inspect the colour of
360 * FER_CAPAN.9(I), column 14 (14 base 0)
361 * FER_CAPAN.10(SER), column 16 (15 base 0)
363 SequenceI ferCapan = al.findName("FER_CAPAN");
364 ResidueShaderI rs = av.getResidueShading();
365 Color c = rs.findColour('I', 14, ferCapan);
366 Color i_original = new Color(138, 117, 138);
367 assertEquals(c, i_original);
368 c = rs.findColour('S', 15, ferCapan);
369 Color s_original = new Color(54, 201, 54);
370 assertEquals(c, s_original);
373 * colour by conservation with increment 10
375 af.conservationMenuItem_actionPerformed(true);
376 SliderPanel sp = SliderPanel.getSliderPanel();
377 assertTrue(sp.isForConservation());
378 assertEquals(sp.getValue(), 30); // initial slider setting
380 assertSame(rs, av.getResidueShading());
381 c = rs.findColour('I', 14, ferCapan);
382 Color i_faded = new Color(196, 186, 196);
383 assertEquals(c, i_faded);
384 c = rs.findColour('S', 15, ferCapan);
385 Color s_faded = new Color(144, 225, 144);
386 assertEquals(c, s_faded);
389 * deselect By Conservation - colour should revert
391 af.conservationMenuItem_actionPerformed(false);
392 c = rs.findColour('S', 15, ferCapan);
393 assertEquals(c, s_original);
396 * now Above PID, threshold = 0%
397 * should be no change
399 af.abovePIDThreshold_actionPerformed(true);
400 sp = SliderPanel.getSliderPanel();
401 assertFalse(sp.isForConservation());
402 assertEquals(sp.getValue(), 0); // initial slider setting
403 c = rs.findColour('I', 14, ferCapan);
404 assertEquals(c, i_original);
405 c = rs.findColour('S', 15, ferCapan);
406 assertEquals(c, s_original);
409 * Above PID, threshold = 1%
410 * 15.I becomes White because no match to consensus (V)
411 * 16.S remains coloured as matches 66.66% consensus
414 c = rs.findColour('I', 14, ferCapan);
415 assertEquals(c, Color.white);
416 c = rs.findColour('S', 15, ferCapan);
417 assertEquals(c, s_original);
420 * threshold 66% - no further change yet...
423 c = rs.findColour('I', 14, ferCapan);
424 assertEquals(c, Color.white);
425 c = rs.findColour('S', 15, ferCapan);
426 assertEquals(c, s_original);
429 * threshold 67% - now both residues are white
432 c = rs.findColour('I', 14, ferCapan);
433 assertEquals(c, Color.white);
434 c = rs.findColour('S', 15, ferCapan);
435 assertEquals(c, Color.white);
438 * deselect Above PID - colours should revert
440 af.abovePIDThreshold_actionPerformed(false);
441 c = rs.findColour('I', 14, ferCapan);
442 assertEquals(c, i_original);
443 c = rs.findColour('S', 15, ferCapan);
444 assertEquals(c, s_original);
447 * Now combine Above 50% PID and By Conservation 10%
448 * 15.I is White because no match to consensus (V)
449 * 16.S is coloured but faded
451 af.abovePIDThreshold_actionPerformed(true);
452 sp = SliderPanel.getSliderPanel();
453 assertFalse(sp.isForConservation());
455 af.conservationMenuItem_actionPerformed(true);
456 sp = SliderPanel.getSliderPanel();
457 assertTrue(sp.isForConservation());
459 c = rs.findColour('I', 14, ferCapan);
460 assertEquals(c, Color.white);
461 c = rs.findColour('S', 15, ferCapan);
462 assertEquals(c, s_faded);
465 * turn off Above PID - should just leave Conservation fading as before
467 af.abovePIDThreshold_actionPerformed(false);
468 c = rs.findColour('I', 14, ferCapan);
469 assertEquals(c, i_faded);
470 c = rs.findColour('S', 15, ferCapan);
471 assertEquals(c, s_faded);
474 * Now add Above 50% PID to conservation colouring
475 * - should give the same as PID followed by conservation (above)
477 af.abovePIDThreshold_actionPerformed(true);
478 SliderPanel.getSliderPanel().valueChanged(50);
479 c = rs.findColour('I', 14, ferCapan);
480 assertEquals(c, Color.white);
481 c = rs.findColour('S', 15, ferCapan);
482 assertEquals(c, s_faded);
485 * turn off By Conservation
486 * should leave I white, S original (unfaded) colour
488 af.conservationMenuItem_actionPerformed(false);
489 c = rs.findColour('I', 14, ferCapan);
490 assertEquals(c, Color.white);
491 c = rs.findColour('S', 15, ferCapan);
492 assertEquals(c, s_original);
495 * finally turn off Above PID to leave original colours
497 af.abovePIDThreshold_actionPerformed(false);
498 c = rs.findColour('I', 14, ferCapan);
499 assertEquals(c, i_original);
500 c = rs.findColour('S', 15, ferCapan);
501 assertEquals(c, s_original);
505 * Verify that making a New View transfers alignment and group colour schemes,
506 * including any thresholds, to the new view. Because New View is performed by
507 * saving and reloading a 'project' file, this is similar to verifying a
508 * project save and reload.
510 * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
512 @Test(groups = "Functional")
513 public void testNewView_colourThresholds()
515 AlignViewportI av = af.getViewport();
516 AlignmentI al = av.getAlignment();
519 * Colour alignment by Buried Index, Above 10% PID, By Conservation 20%
521 af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
522 assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
523 af.abovePIDThreshold_actionPerformed(true);
524 SliderPanel sp = SliderPanel.getSliderPanel();
525 assertFalse(sp.isForConservation());
527 af.conservationMenuItem_actionPerformed(true);
528 sp = SliderPanel.getSliderPanel();
529 assertTrue(sp.isForConservation());
531 ResidueShaderI rs = av.getResidueShading();
532 assertEquals(rs.getThreshold(), 10);
533 assertTrue(rs.conservationApplied());
534 assertEquals(rs.getConservationInc(), 20);
537 * create a group with Strand colouring, 30% Conservation
538 * and 40% PID threshold
540 SequenceGroup sg = new SequenceGroup();
541 sg.addSequence(al.getSequenceAt(0), false);
544 av.setSelectionGroup(sg);
545 PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
546 popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
548 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
549 assertEquals(al.getGroups().size(), 1);
550 assertSame(al.getGroups().get(0), sg);
551 popupMenu.conservationMenuItem_actionPerformed(true);
552 sp = SliderPanel.getSliderPanel();
553 assertTrue(sp.isForConservation());
555 popupMenu.abovePIDColour_actionPerformed(true);
556 sp = SliderPanel.getSliderPanel();
557 assertFalse(sp.isForConservation());
559 rs = sg.getGroupColourScheme();
560 assertTrue(rs.conservationApplied());
561 assertEquals(rs.getConservationInc(), 30);
562 assertEquals(rs.getThreshold(), 40);
565 * set slider panel focus to the background alignment
567 af.conservationMenuItem_actionPerformed(true);
568 sp = SliderPanel.getSliderPanel();
569 assertTrue(sp.isForConservation());
570 assertEquals(sp.getTitle(), MessageManager.formatMessage(
571 "label.conservation_colour_increment",
572 new String[] { "Background" }));
575 * make a new View, verify alignment and group colour schemes
577 af.newView_actionPerformed(null);
578 assertEquals(af.alignPanel.getViewName(), "View 1");
579 AlignmentViewport av2 = af.getViewport();
580 assertNotSame(av, av2);
581 assertSame(av2, af.alignPanel.av);
582 rs = av2.getResidueShading();
583 assertNotSame(av.getResidueShading(), rs);
584 assertEquals(rs.getThreshold(), 10);
585 assertTrue(rs.conservationApplied(), rs.toString());
586 assertEquals(rs.getConservationInc(), 20);
587 assertEquals(av2.getAlignment().getGroups().size(), 1);
588 sg = av2.getAlignment().getGroups().get(0);
589 rs = sg.getGroupColourScheme();
590 assertTrue(rs.conservationApplied());
591 assertEquals(rs.getConservationInc(), 30);
592 assertEquals(rs.getThreshold(), 40);
595 * check the Conservation SliderPanel (still open) is linked to
596 * and updates the new view (JAL-2385)
598 sp = SliderPanel.getSliderPanel();
599 assertTrue(sp.isForConservation());
600 assertEquals(sp.getTitle(), MessageManager.formatMessage(
601 "label.conservation_colour_increment",
602 new String[] { "View 1" }));
604 assertEquals(av2.getResidueShading().getConservationInc(), 22);
608 * Verify that making a New View preserves the dataset reference for the
609 * alignment. Otherwise, see a 'duplicate jar entry' reference when trying to
610 * save alignments with multiple views, and codon mappings will not be shared
611 * across all panels in a split frame.
613 * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
615 @Test(groups = "Functional")
616 public void testNewView_dsRefPreserved()
618 AlignViewport av = af.getViewport();
619 AlignmentI al = av.getAlignment();
620 AlignmentI original_ds = al.getDataset();
621 af.newView_actionPerformed(null);
622 assertNotEquals("New view didn't select the a new panel", av,
624 org.testng.Assert.assertEquals(original_ds,
625 af.getViewport().getAlignment().getDataset(),
626 "Dataset was not preserved in new view");