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.io.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(Color.red, Color.blue, 0f, 10f);
123 fc.setAboveThreshold(true);
125 alignFrame.getFeatureRenderer().setColour("Metal", fc);
126 assertTrue(alignFrame.hideFeatureColumns("Metal", true));
127 HiddenColumns hidden = alignFrame.getViewport().getAlignment().getHiddenColumns();
128 assertEquals(hidden.getNumberOfRegions(), 1);
129 Iterator<int[]> regions = hidden.iterator();
130 int[] next = regions.next();
131 assertEquals(next[0], 5);
132 assertEquals(next[1], 9);
135 * hide a feature present in some columns
136 * sequence positions [2-4], [7-9] are column positions
137 * [1-3], [6-8] base zero
139 alignFrame.getViewport().showAllHiddenColumns();
140 assertTrue(alignFrame.hideFeatureColumns("Turn", true));
141 regions = alignFrame.getViewport().getAlignment()
142 .getHiddenColumns().iterator();
143 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
144 .getNumberOfRegions(), 2);
145 next = regions.next();
146 assertEquals(next[0], 1);
147 assertEquals(next[1], 3);
148 next = regions.next();
149 assertEquals(next[0], 6);
150 assertEquals(next[1], 8);
153 @BeforeClass(alwaysRun = true)
154 public static void setUpBeforeClass() throws Exception
157 * use read-only test properties file
159 Cache.loadProperties("test/jalview/io/testProps.jvprops");
160 Jalview.main(new String[] { "-nonews" });
163 @AfterMethod(alwaysRun = true)
164 public void tearDown()
166 Desktop.instance.closeAll_actionPerformed(null);
170 * configure (read-only) properties for test to ensure Consensus is computed
171 * for colour Above PID testing
173 @BeforeMethod(alwaysRun = true)
176 Cache.loadProperties("test/jalview/io/testProps.jvprops");
177 Cache.applicationProperties.setProperty("SHOW_IDENTITY",
178 Boolean.TRUE.toString());
179 af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
180 DataSourceType.FILE);
183 * wait for Consensus thread to complete
187 while (af.getViewport().getConsensusSeq() == null)
192 } catch (InterruptedException e)
200 * Test that changing background (alignment) colour scheme
202 * <li>with Apply Colour to All Groups not selected, does not change group
204 * <li>with Apply Colour to All Groups selected, does change group colours</li>
205 * <li>in neither case, changes alignment or group colour thresholds (PID or
209 @Test(groups = "Functional")
210 public void testChangeColour_background_groupsAndThresholds()
212 AlignViewport av = af.getViewport();
213 AlignmentI al = av.getAlignment();
216 * Colour alignment by Buried Index
218 af.applyToAllGroups_actionPerformed(false);
219 af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
220 assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
221 assertFalse(av.getResidueShading().conservationApplied());
222 assertEquals(av.getResidueShading().getThreshold(), 0);
225 * Apply Conservation 20%
227 af.conservationMenuItem_actionPerformed(true);
228 SliderPanel sp = SliderPanel.getSliderPanel();
229 assertEquals(sp.getTitle(), MessageManager.formatMessage(
230 "label.conservation_colour_increment",
231 new String[] { "Background" }));
232 assertTrue(sp.isForConservation());
234 assertTrue(av.getResidueShading().conservationApplied());
235 assertEquals(av.getResidueShading().getConservationInc(), 20);
238 * Apply PID threshold 10% (conservation still applies as well)
240 af.abovePIDThreshold_actionPerformed(true);
241 sp = SliderPanel.getSliderPanel();
242 assertFalse(sp.isForConservation());
243 assertEquals(sp.getTitle(), MessageManager.formatMessage(
244 "label.percentage_identity_threshold",
245 new String[] { "Background" }));
247 assertEquals(av.getResidueShading().getThreshold(), 10);
248 assertTrue(av.getResidueShading().conservationApplied());
249 assertEquals(av.getResidueShading().getConservationInc(), 20);
252 * create a group with Strand colouring, 30% Conservation
253 * and 40% PID threshold
255 SequenceGroup sg = new SequenceGroup();
256 sg.addSequence(al.getSequenceAt(0), false);
259 av.setSelectionGroup(sg);
262 * apply 30% Conservation to group
264 PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
265 popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
267 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
268 assertEquals(al.getGroups().size(), 1);
269 assertSame(al.getGroups().get(0), sg);
270 popupMenu.conservationMenuItem_actionPerformed(true);
271 sp = SliderPanel.getSliderPanel();
272 assertTrue(sp.isForConservation());
273 assertEquals(sp.getTitle(), MessageManager.formatMessage(
274 "label.conservation_colour_increment",
275 new String[] { sg.getName() }));
277 assertTrue(sg.getGroupColourScheme().conservationApplied());
278 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
281 * apply 40% PID threshold to group
283 popupMenu.abovePIDColour_actionPerformed(true);
284 sp = SliderPanel.getSliderPanel();
285 assertFalse(sp.isForConservation());
286 assertEquals(sp.getTitle(), MessageManager.formatMessage(
287 "label.percentage_identity_threshold",
288 new String[] { sg.getName() }));
290 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
291 // conservation threshold is unchanged:
292 assertTrue(sg.getGroupColourScheme().conservationApplied());
293 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
296 * change alignment colour - group colour, and all thresholds,
297 * should be unaffected
299 af.changeColour_actionPerformed(JalviewColourScheme.Turn.toString());
300 assertTrue(av.getGlobalColourScheme() instanceof TurnColourScheme);
301 assertTrue(av.getResidueShading().conservationApplied());
302 assertEquals(av.getResidueShading().getConservationInc(), 20);
303 assertEquals(av.getResidueShading().getThreshold(), 10);
304 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
305 assertTrue(sg.getGroupColourScheme().conservationApplied());
306 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
307 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
310 * Now change alignment colour with Apply Colour To All Groups
311 * - group colour should change, but not colour thresholds
313 af.applyToAllGroups_actionPerformed(true);
314 af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
315 assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
316 assertTrue(av.getResidueShading().conservationApplied());
317 assertEquals(av.getResidueShading().getConservationInc(), 20);
318 assertEquals(av.getResidueShading().getThreshold(), 10);
319 assertTrue(sg.getColourScheme() instanceof HelixColourScheme);
320 assertTrue(sg.getGroupColourScheme().conservationApplied());
321 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
322 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
326 * Test residue colouring with various options
328 * <li>no PID or Conservation threshold</li>
329 * <li>colour by Conservation applied</li>
330 * <li>colour by Conservation removed</li>
331 * <li>colour above PID - various values</li>
332 * <li>colour above PID removed</li>
333 * <li>Above PID plus By Conservation combined</li>
334 * <li>remove Above PID to leave just By Conservation</li>
335 * <li>re-add Above PID</li>
336 * <li>remove By Conservation to leave just Above PID</li>
337 * <li>remove Above PID to leave original colours</li>
340 @Test(groups = "Functional")
341 public void testColourThresholdActions()
343 AlignViewport av = af.getViewport();
344 AlignmentI al = av.getAlignment();
347 * Colour alignment by Helix Propensity, no thresholds
349 af.applyToAllGroups_actionPerformed(false);
350 af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
351 assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
352 assertFalse(av.getResidueShading().conservationApplied());
353 assertEquals(av.getResidueShading().getThreshold(), 0);
356 * inspect the colour of
357 * FER_CAPAN.9(I), column 14 (14 base 0)
358 * FER_CAPAN.10(SER), column 16 (15 base 0)
360 SequenceI ferCapan = al.findName("FER_CAPAN");
361 ResidueShaderI rs = av.getResidueShading();
362 Color c = rs.findColour('I', 14, ferCapan);
363 Color i_original = new Color(138, 117, 138);
364 assertEquals(c, i_original);
365 c = rs.findColour('S', 15, ferCapan);
366 Color s_original = new Color(54, 201, 54);
367 assertEquals(c, s_original);
370 * colour by conservation with increment 10
372 af.conservationMenuItem_actionPerformed(true);
373 SliderPanel sp = SliderPanel.getSliderPanel();
374 assertTrue(sp.isForConservation());
375 assertEquals(sp.getValue(), 30); // initial slider setting
377 assertSame(rs, av.getResidueShading());
378 c = rs.findColour('I', 14, ferCapan);
379 Color i_faded = new Color(196, 186, 196);
380 assertEquals(c, i_faded);
381 c = rs.findColour('S', 15, ferCapan);
382 Color s_faded = new Color(144, 225, 144);
383 assertEquals(c, s_faded);
386 * deselect By Conservation - colour should revert
388 af.conservationMenuItem_actionPerformed(false);
389 c = rs.findColour('S', 15, ferCapan);
390 assertEquals(c, s_original);
393 * now Above PID, threshold = 0%
394 * should be no change
396 af.abovePIDThreshold_actionPerformed(true);
397 sp = SliderPanel.getSliderPanel();
398 assertFalse(sp.isForConservation());
399 assertEquals(sp.getValue(), 0); // initial slider setting
400 c = rs.findColour('I', 14, ferCapan);
401 assertEquals(c, i_original);
402 c = rs.findColour('S', 15, ferCapan);
403 assertEquals(c, s_original);
406 * Above PID, threshold = 1%
407 * 15.I becomes White because no match to consensus (V)
408 * 16.S remains coloured as matches 66.66% consensus
411 c = rs.findColour('I', 14, ferCapan);
412 assertEquals(c, Color.white);
413 c = rs.findColour('S', 15, ferCapan);
414 assertEquals(c, s_original);
417 * threshold 66% - no further change yet...
420 c = rs.findColour('I', 14, ferCapan);
421 assertEquals(c, Color.white);
422 c = rs.findColour('S', 15, ferCapan);
423 assertEquals(c, s_original);
426 * threshold 67% - now both residues are white
429 c = rs.findColour('I', 14, ferCapan);
430 assertEquals(c, Color.white);
431 c = rs.findColour('S', 15, ferCapan);
432 assertEquals(c, Color.white);
435 * deselect Above PID - colours should revert
437 af.abovePIDThreshold_actionPerformed(false);
438 c = rs.findColour('I', 14, ferCapan);
439 assertEquals(c, i_original);
440 c = rs.findColour('S', 15, ferCapan);
441 assertEquals(c, s_original);
444 * Now combine Above 50% PID and By Conservation 10%
445 * 15.I is White because no match to consensus (V)
446 * 16.S is coloured but faded
448 af.abovePIDThreshold_actionPerformed(true);
449 sp = SliderPanel.getSliderPanel();
450 assertFalse(sp.isForConservation());
452 af.conservationMenuItem_actionPerformed(true);
453 sp = SliderPanel.getSliderPanel();
454 assertTrue(sp.isForConservation());
456 c = rs.findColour('I', 14, ferCapan);
457 assertEquals(c, Color.white);
458 c = rs.findColour('S', 15, ferCapan);
459 assertEquals(c, s_faded);
462 * turn off Above PID - should just leave Conservation fading as before
464 af.abovePIDThreshold_actionPerformed(false);
465 c = rs.findColour('I', 14, ferCapan);
466 assertEquals(c, i_faded);
467 c = rs.findColour('S', 15, ferCapan);
468 assertEquals(c, s_faded);
471 * Now add Above 50% PID to conservation colouring
472 * - should give the same as PID followed by conservation (above)
474 af.abovePIDThreshold_actionPerformed(true);
475 SliderPanel.getSliderPanel().valueChanged(50);
476 c = rs.findColour('I', 14, ferCapan);
477 assertEquals(c, Color.white);
478 c = rs.findColour('S', 15, ferCapan);
479 assertEquals(c, s_faded);
482 * turn off By Conservation
483 * should leave I white, S original (unfaded) colour
485 af.conservationMenuItem_actionPerformed(false);
486 c = rs.findColour('I', 14, ferCapan);
487 assertEquals(c, Color.white);
488 c = rs.findColour('S', 15, ferCapan);
489 assertEquals(c, s_original);
492 * finally turn off Above PID to leave original colours
494 af.abovePIDThreshold_actionPerformed(false);
495 c = rs.findColour('I', 14, ferCapan);
496 assertEquals(c, i_original);
497 c = rs.findColour('S', 15, ferCapan);
498 assertEquals(c, s_original);
502 * Verify that making a New View transfers alignment and group colour schemes,
503 * including any thresholds, to the new view. Because New View is performed by
504 * saving and reloading a 'project' file, this is similar to verifying a
505 * project save and reload.
507 * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
509 @Test(groups = "Functional")
510 public void testNewView_colourThresholds()
512 AlignViewport av = af.getViewport();
513 AlignmentI al = av.getAlignment();
516 * Colour alignment by Buried Index, Above 10% PID, By Conservation 20%
518 af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
519 assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
520 af.abovePIDThreshold_actionPerformed(true);
521 SliderPanel sp = SliderPanel.getSliderPanel();
522 assertFalse(sp.isForConservation());
524 af.conservationMenuItem_actionPerformed(true);
525 sp = SliderPanel.getSliderPanel();
526 assertTrue(sp.isForConservation());
528 ResidueShaderI rs = av.getResidueShading();
529 assertEquals(rs.getThreshold(), 10);
530 assertTrue(rs.conservationApplied());
531 assertEquals(rs.getConservationInc(), 20);
534 * create a group with Strand colouring, 30% Conservation
535 * and 40% PID threshold
537 SequenceGroup sg = new SequenceGroup();
538 sg.addSequence(al.getSequenceAt(0), false);
541 av.setSelectionGroup(sg);
542 PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
543 popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
545 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
546 assertEquals(al.getGroups().size(), 1);
547 assertSame(al.getGroups().get(0), sg);
548 popupMenu.conservationMenuItem_actionPerformed(true);
549 sp = SliderPanel.getSliderPanel();
550 assertTrue(sp.isForConservation());
552 popupMenu.abovePIDColour_actionPerformed(true);
553 sp = SliderPanel.getSliderPanel();
554 assertFalse(sp.isForConservation());
556 rs = sg.getGroupColourScheme();
557 assertTrue(rs.conservationApplied());
558 assertEquals(rs.getConservationInc(), 30);
559 assertEquals(rs.getThreshold(), 40);
562 * set slider panel focus to the background alignment
564 af.conservationMenuItem_actionPerformed(true);
565 sp = SliderPanel.getSliderPanel();
566 assertTrue(sp.isForConservation());
567 assertEquals(sp.getTitle(), MessageManager.formatMessage(
568 "label.conservation_colour_increment",
569 new String[] { "Background" }));
572 * make a new View, verify alignment and group colour schemes
574 af.newView_actionPerformed(null);
575 assertEquals(af.alignPanel.getViewName(), "View 1");
576 AlignViewport av2 = af.getViewport();
577 assertNotSame(av, av2);
578 assertSame(av2, af.alignPanel.av);
579 rs = av2.getResidueShading();
580 assertNotSame(av.getResidueShading(), rs);
581 assertEquals(rs.getThreshold(), 10);
582 assertTrue(rs.conservationApplied(), rs.toString());
583 assertEquals(rs.getConservationInc(), 20);
584 assertEquals(av2.getAlignment().getGroups().size(), 1);
585 sg = av2.getAlignment().getGroups().get(0);
586 rs = sg.getGroupColourScheme();
587 assertTrue(rs.conservationApplied());
588 assertEquals(rs.getConservationInc(), 30);
589 assertEquals(rs.getThreshold(), 40);
592 * check the Conservation SliderPanel (still open) is linked to
593 * and updates the new view (JAL-2385)
595 sp = SliderPanel.getSliderPanel();
596 assertTrue(sp.isForConservation());
597 assertEquals(sp.getTitle(), MessageManager.formatMessage(
598 "label.conservation_colour_increment",
599 new String[] { "View 1" }));
601 assertEquals(av2.getResidueShading().getConservationInc(), 22);
605 * Verify that making a New View preserves the dataset reference for the
606 * alignment. Otherwise, see a 'duplicate jar entry' reference when trying to
607 * save alignments with multiple views, and codon mappings will not be shared
608 * across all panels in a split frame.
610 * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
612 @Test(groups = "Functional")
613 public void testNewView_dsRefPreserved()
615 AlignViewport av = af.getViewport();
616 AlignmentI al = av.getAlignment();
617 AlignmentI original_ds = al.getDataset();
618 af.newView_actionPerformed(null);
619 assertNotEquals("New view didn't select the a new panel", av,
621 org.testng.Assert.assertEquals(original_ds,
622 af.getViewport().getAlignment().getDataset(),
623 "Dataset was not preserved in new view");