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);
71 @Test(groups = "Functional")
72 public void testHideFeatureColumns()
74 SequenceI seq1 = new Sequence("Seq1", "ABCDEFGHIJ");
75 SequenceI seq2 = new Sequence("Seq2", "ABCDEFGHIJ");
76 seq1.addSequenceFeature(new SequenceFeature("Metal", "", 1, 5, 0f, null));
77 seq2.addSequenceFeature(new SequenceFeature("Metal", "", 6, 10, 10f,
79 seq1.addSequenceFeature(new SequenceFeature("Turn", "", 2, 4,
81 seq2.addSequenceFeature(new SequenceFeature("Turn", "", 7, 9,
83 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
84 AlignFrame alignFrame = new AlignFrame(al, al.getWidth(),
88 * make all features visible (select feature columns checks visibility)
90 alignFrame.getFeatureRenderer().findAllFeatures(true);
93 * hiding a feature not present does nothing
95 assertFalse(alignFrame.hideFeatureColumns("exon", true));
96 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
98 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
99 .getNumberOfRegions(), 0);
101 assertFalse(alignFrame.hideFeatureColumns("exon", false));
102 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
104 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
105 .getNumberOfRegions(), 0);
108 * hiding a feature in all columns does nothing
110 assertFalse(alignFrame.hideFeatureColumns("Metal", true));
111 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
113 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
114 .getNumberOfRegions(), 0);
118 * threshold Metal to hide features where score < 5
119 * seq1 feature in columns 1-5 is hidden
120 * seq2 feature in columns 6-10 is shown
122 FeatureColourI fc = new FeatureColour(null, Color.red, Color.blue, null,
124 fc.setAboveThreshold(true);
126 alignFrame.getFeatureRenderer().setColour("Metal", fc);
127 assertTrue(alignFrame.hideFeatureColumns("Metal", true));
128 HiddenColumns hidden = alignFrame.getViewport().getAlignment().getHiddenColumns();
129 assertEquals(hidden.getNumberOfRegions(), 1);
130 Iterator<int[]> regions = hidden.iterator();
131 int[] next = regions.next();
132 assertEquals(next[0], 5);
133 assertEquals(next[1], 9);
136 * hide a feature present in some columns
137 * sequence positions [2-4], [7-9] are column positions
138 * [1-3], [6-8] base zero
140 alignFrame.getViewport().showAllHiddenColumns();
141 assertTrue(alignFrame.hideFeatureColumns("Turn", true));
142 regions = alignFrame.getViewport().getAlignment()
143 .getHiddenColumns().iterator();
144 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
145 .getNumberOfRegions(), 2);
146 next = regions.next();
147 assertEquals(next[0], 1);
148 assertEquals(next[1], 3);
149 next = regions.next();
150 assertEquals(next[0], 6);
151 assertEquals(next[1], 8);
154 @BeforeClass(alwaysRun = true)
155 public static void setUpBeforeClass() throws Exception
158 * use read-only test properties file
160 Cache.loadProperties("test/jalview/io/testProps.jvprops");
161 Jalview.main(new String[] { "-nonews" });
164 @AfterMethod(alwaysRun = true)
165 public void tearDown()
167 Desktop.getInstance().closeAll_actionPerformed(null);
171 * configure (read-only) properties for test to ensure Consensus is computed
172 * for colour Above PID testing
174 @BeforeMethod(alwaysRun = true)
177 Cache.loadProperties("test/jalview/io/testProps.jvprops");
178 Cache.getInstance().applicationProperties.setProperty("SHOW_IDENTITY",
179 Boolean.TRUE.toString());
180 af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
181 DataSourceType.FILE);
184 * wait for Consensus thread to complete
188 while (af.getViewport().getConsensusSeq() == null)
193 } catch (InterruptedException e)
201 * Test that changing background (alignment) colour scheme
203 * <li>with Apply Colour to All Groups not selected, does not change group
205 * <li>with Apply Colour to All Groups selected, does change group colours</li>
206 * <li>in neither case, changes alignment or group colour thresholds (PID or
210 @Test(groups = "Functional")
211 public void testChangeColour_background_groupsAndThresholds()
213 AlignViewport av = af.getViewport();
214 AlignmentI al = av.getAlignment();
217 * Colour alignment by Buried Index
219 af.applyToAllGroups_actionPerformed(false);
220 af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
221 assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
222 assertFalse(av.getResidueShading().conservationApplied());
223 assertEquals(av.getResidueShading().getThreshold(), 0);
226 * Apply Conservation 20%
228 af.conservationMenuItem_actionPerformed(true);
229 SliderPanel sp = SliderPanel.getSliderPanel();
230 assertEquals(sp.getTitle(), MessageManager.formatMessage(
231 "label.conservation_colour_increment",
232 new String[] { "Background" }));
233 assertTrue(sp.isForConservation());
235 assertTrue(av.getResidueShading().conservationApplied());
236 assertEquals(av.getResidueShading().getConservationInc(), 20);
239 * Apply PID threshold 10% (conservation still applies as well)
241 af.abovePIDThreshold_actionPerformed(true);
242 sp = SliderPanel.getSliderPanel();
243 assertFalse(sp.isForConservation());
244 assertEquals(sp.getTitle(), MessageManager.formatMessage(
245 "label.percentage_identity_threshold",
246 new String[] { "Background" }));
248 assertEquals(av.getResidueShading().getThreshold(), 10);
249 assertTrue(av.getResidueShading().conservationApplied());
250 assertEquals(av.getResidueShading().getConservationInc(), 20);
253 * create a group with Strand colouring, 30% Conservation
254 * and 40% PID threshold
256 SequenceGroup sg = new SequenceGroup();
257 sg.addSequence(al.getSequenceAt(0), false);
260 av.setSelectionGroup(sg);
263 * apply 30% Conservation to group
265 PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
266 popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
268 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
269 assertEquals(al.getGroups().size(), 1);
270 assertSame(al.getGroups().get(0), sg);
271 popupMenu.conservationMenuItem_actionPerformed(true);
272 sp = SliderPanel.getSliderPanel();
273 assertTrue(sp.isForConservation());
274 assertEquals(sp.getTitle(), MessageManager.formatMessage(
275 "label.conservation_colour_increment",
276 new String[] { sg.getName() }));
278 assertTrue(sg.getGroupColourScheme().conservationApplied());
279 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
282 * apply 40% PID threshold to group
284 popupMenu.abovePIDColour_actionPerformed(true);
285 sp = SliderPanel.getSliderPanel();
286 assertFalse(sp.isForConservation());
287 assertEquals(sp.getTitle(), MessageManager.formatMessage(
288 "label.percentage_identity_threshold",
289 new String[] { sg.getName() }));
291 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
292 // conservation threshold is unchanged:
293 assertTrue(sg.getGroupColourScheme().conservationApplied());
294 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
297 * change alignment colour - group colour, and all thresholds,
298 * should be unaffected
300 af.changeColour_actionPerformed(JalviewColourScheme.Turn.toString());
301 assertTrue(av.getGlobalColourScheme() instanceof TurnColourScheme);
302 assertTrue(av.getResidueShading().conservationApplied());
303 assertEquals(av.getResidueShading().getConservationInc(), 20);
304 assertEquals(av.getResidueShading().getThreshold(), 10);
305 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
306 assertTrue(sg.getGroupColourScheme().conservationApplied());
307 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
308 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
311 * Now change alignment colour with Apply Colour To All Groups
312 * - group colour should change, but not colour thresholds
314 af.applyToAllGroups_actionPerformed(true);
315 af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
316 assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
317 assertTrue(av.getResidueShading().conservationApplied());
318 assertEquals(av.getResidueShading().getConservationInc(), 20);
319 assertEquals(av.getResidueShading().getThreshold(), 10);
320 assertTrue(sg.getColourScheme() instanceof HelixColourScheme);
321 assertTrue(sg.getGroupColourScheme().conservationApplied());
322 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
323 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
327 * Test residue colouring with various options
329 * <li>no PID or Conservation threshold</li>
330 * <li>colour by Conservation applied</li>
331 * <li>colour by Conservation removed</li>
332 * <li>colour above PID - various values</li>
333 * <li>colour above PID removed</li>
334 * <li>Above PID plus By Conservation combined</li>
335 * <li>remove Above PID to leave just By Conservation</li>
336 * <li>re-add Above PID</li>
337 * <li>remove By Conservation to leave just Above PID</li>
338 * <li>remove Above PID to leave original colours</li>
341 @Test(groups = "Functional")
342 public void testColourThresholdActions()
344 AlignViewport av = af.getViewport();
345 AlignmentI al = av.getAlignment();
348 * Colour alignment by Helix Propensity, no thresholds
350 af.applyToAllGroups_actionPerformed(false);
351 af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
352 assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
353 assertFalse(av.getResidueShading().conservationApplied());
354 assertEquals(av.getResidueShading().getThreshold(), 0);
357 * inspect the colour of
358 * FER_CAPAN.9(I), column 14 (14 base 0)
359 * FER_CAPAN.10(SER), column 16 (15 base 0)
361 SequenceI ferCapan = al.findName("FER_CAPAN");
362 ResidueShaderI rs = av.getResidueShading();
363 Color c = rs.findColour('I', 14, ferCapan);
364 Color i_original = new Color(138, 117, 138);
365 assertEquals(c, i_original);
366 c = rs.findColour('S', 15, ferCapan);
367 Color s_original = new Color(54, 201, 54);
368 assertEquals(c, s_original);
371 * colour by conservation with increment 10
373 af.conservationMenuItem_actionPerformed(true);
374 SliderPanel sp = SliderPanel.getSliderPanel();
375 assertTrue(sp.isForConservation());
376 assertEquals(sp.getValue(), 30); // initial slider setting
378 assertSame(rs, av.getResidueShading());
379 c = rs.findColour('I', 14, ferCapan);
380 Color i_faded = new Color(196, 186, 196);
381 assertEquals(c, i_faded);
382 c = rs.findColour('S', 15, ferCapan);
383 Color s_faded = new Color(144, 225, 144);
384 assertEquals(c, s_faded);
387 * deselect By Conservation - colour should revert
389 af.conservationMenuItem_actionPerformed(false);
390 c = rs.findColour('S', 15, ferCapan);
391 assertEquals(c, s_original);
394 * now Above PID, threshold = 0%
395 * should be no change
397 af.abovePIDThreshold_actionPerformed(true);
398 sp = SliderPanel.getSliderPanel();
399 assertFalse(sp.isForConservation());
400 assertEquals(sp.getValue(), 0); // initial slider setting
401 c = rs.findColour('I', 14, ferCapan);
402 assertEquals(c, i_original);
403 c = rs.findColour('S', 15, ferCapan);
404 assertEquals(c, s_original);
407 * Above PID, threshold = 1%
408 * 15.I becomes White because no match to consensus (V)
409 * 16.S remains coloured as matches 66.66% consensus
412 c = rs.findColour('I', 14, ferCapan);
413 assertEquals(c, Color.white);
414 c = rs.findColour('S', 15, ferCapan);
415 assertEquals(c, s_original);
418 * threshold 66% - no further change yet...
421 c = rs.findColour('I', 14, ferCapan);
422 assertEquals(c, Color.white);
423 c = rs.findColour('S', 15, ferCapan);
424 assertEquals(c, s_original);
427 * threshold 67% - now both residues are white
430 c = rs.findColour('I', 14, ferCapan);
431 assertEquals(c, Color.white);
432 c = rs.findColour('S', 15, ferCapan);
433 assertEquals(c, Color.white);
436 * deselect Above PID - colours should revert
438 af.abovePIDThreshold_actionPerformed(false);
439 c = rs.findColour('I', 14, ferCapan);
440 assertEquals(c, i_original);
441 c = rs.findColour('S', 15, ferCapan);
442 assertEquals(c, s_original);
445 * Now combine Above 50% PID and By Conservation 10%
446 * 15.I is White because no match to consensus (V)
447 * 16.S is coloured but faded
449 af.abovePIDThreshold_actionPerformed(true);
450 sp = SliderPanel.getSliderPanel();
451 assertFalse(sp.isForConservation());
453 af.conservationMenuItem_actionPerformed(true);
454 sp = SliderPanel.getSliderPanel();
455 assertTrue(sp.isForConservation());
457 c = rs.findColour('I', 14, ferCapan);
458 assertEquals(c, Color.white);
459 c = rs.findColour('S', 15, ferCapan);
460 assertEquals(c, s_faded);
463 * turn off Above PID - should just leave Conservation fading as before
465 af.abovePIDThreshold_actionPerformed(false);
466 c = rs.findColour('I', 14, ferCapan);
467 assertEquals(c, i_faded);
468 c = rs.findColour('S', 15, ferCapan);
469 assertEquals(c, s_faded);
472 * Now add Above 50% PID to conservation colouring
473 * - should give the same as PID followed by conservation (above)
475 af.abovePIDThreshold_actionPerformed(true);
476 SliderPanel.getSliderPanel().valueChanged(50);
477 c = rs.findColour('I', 14, ferCapan);
478 assertEquals(c, Color.white);
479 c = rs.findColour('S', 15, ferCapan);
480 assertEquals(c, s_faded);
483 * turn off By Conservation
484 * should leave I white, S original (unfaded) colour
486 af.conservationMenuItem_actionPerformed(false);
487 c = rs.findColour('I', 14, ferCapan);
488 assertEquals(c, Color.white);
489 c = rs.findColour('S', 15, ferCapan);
490 assertEquals(c, s_original);
493 * finally turn off Above PID to leave original colours
495 af.abovePIDThreshold_actionPerformed(false);
496 c = rs.findColour('I', 14, ferCapan);
497 assertEquals(c, i_original);
498 c = rs.findColour('S', 15, ferCapan);
499 assertEquals(c, s_original);
503 * Verify that making a New View transfers alignment and group colour schemes,
504 * including any thresholds, to the new view. Because New View is performed by
505 * saving and reloading a 'project' file, this is similar to verifying a
506 * project save and reload.
508 * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
510 @Test(groups = "Functional")
511 public void testNewView_colourThresholds()
513 AlignViewport av = af.getViewport();
514 AlignmentI al = av.getAlignment();
517 * Colour alignment by Buried Index, Above 10% PID, By Conservation 20%
519 af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
520 assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
521 af.abovePIDThreshold_actionPerformed(true);
522 SliderPanel sp = SliderPanel.getSliderPanel();
523 assertFalse(sp.isForConservation());
525 af.conservationMenuItem_actionPerformed(true);
526 sp = SliderPanel.getSliderPanel();
527 assertTrue(sp.isForConservation());
529 ResidueShaderI rs = av.getResidueShading();
530 assertEquals(rs.getThreshold(), 10);
531 assertTrue(rs.conservationApplied());
532 assertEquals(rs.getConservationInc(), 20);
535 * create a group with Strand colouring, 30% Conservation
536 * and 40% PID threshold
538 SequenceGroup sg = new SequenceGroup();
539 sg.addSequence(al.getSequenceAt(0), false);
542 av.setSelectionGroup(sg);
543 PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
544 popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
546 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
547 assertEquals(al.getGroups().size(), 1);
548 assertSame(al.getGroups().get(0), sg);
549 popupMenu.conservationMenuItem_actionPerformed(true);
550 sp = SliderPanel.getSliderPanel();
551 assertTrue(sp.isForConservation());
553 popupMenu.abovePIDColour_actionPerformed(true);
554 sp = SliderPanel.getSliderPanel();
555 assertFalse(sp.isForConservation());
557 rs = sg.getGroupColourScheme();
558 assertTrue(rs.conservationApplied());
559 assertEquals(rs.getConservationInc(), 30);
560 assertEquals(rs.getThreshold(), 40);
563 * set slider panel focus to the background alignment
565 af.conservationMenuItem_actionPerformed(true);
566 sp = SliderPanel.getSliderPanel();
567 assertTrue(sp.isForConservation());
568 assertEquals(sp.getTitle(), MessageManager.formatMessage(
569 "label.conservation_colour_increment",
570 new String[] { "Background" }));
573 * make a new View, verify alignment and group colour schemes
575 af.newView_actionPerformed(null);
576 assertEquals(af.alignPanel.getViewName(), "View 1");
577 AlignViewport av2 = af.getViewport();
578 assertNotSame(av, av2);
579 assertSame(av2, af.alignPanel.av);
580 rs = av2.getResidueShading();
581 assertNotSame(av.getResidueShading(), rs);
582 assertEquals(rs.getThreshold(), 10);
583 assertTrue(rs.conservationApplied(), rs.toString());
584 assertEquals(rs.getConservationInc(), 20);
585 assertEquals(av2.getAlignment().getGroups().size(), 1);
586 sg = av2.getAlignment().getGroups().get(0);
587 rs = sg.getGroupColourScheme();
588 assertTrue(rs.conservationApplied());
589 assertEquals(rs.getConservationInc(), 30);
590 assertEquals(rs.getThreshold(), 40);
593 * check the Conservation SliderPanel (still open) is linked to
594 * and updates the new view (JAL-2385)
596 sp = SliderPanel.getSliderPanel();
597 assertTrue(sp.isForConservation());
598 assertEquals(sp.getTitle(), MessageManager.formatMessage(
599 "label.conservation_colour_increment",
600 new String[] { "View 1" }));
602 assertEquals(av2.getResidueShading().getConservationInc(), 22);
606 * Verify that making a New View preserves the dataset reference for the
607 * alignment. Otherwise, see a 'duplicate jar entry' reference when trying to
608 * save alignments with multiple views, and codon mappings will not be shared
609 * across all panels in a split frame.
611 * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
613 @Test(groups = "Functional")
614 public void testNewView_dsRefPreserved()
616 AlignViewport av = af.getViewport();
617 AlignmentI al = av.getAlignment();
618 AlignmentI original_ds = al.getDataset();
619 af.newView_actionPerformed(null);
620 assertNotEquals("New view didn't select the a new panel", av,
622 org.testng.Assert.assertEquals(original_ds,
623 af.getViewport().getAlignment().getDataset(),
624 "Dataset was not preserved in new view");