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 java.awt.Color;
31 import java.util.Iterator;
33 import org.testng.annotations.AfterMethod;
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.BeforeMethod;
36 import org.testng.annotations.Test;
38 import jalview.api.AlignViewportI;
39 import jalview.api.FeatureColourI;
40 import jalview.bin.Cache;
41 import jalview.bin.Jalview;
42 import jalview.datamodel.Alignment;
43 import jalview.datamodel.AlignmentI;
44 import jalview.datamodel.HiddenColumns;
45 import jalview.datamodel.Sequence;
46 import jalview.datamodel.SequenceFeature;
47 import jalview.datamodel.SequenceGroup;
48 import jalview.datamodel.SequenceI;
49 import jalview.io.DataSourceType;
50 import jalview.io.FileLoader;
51 import jalview.project.Jalview2xmlTests;
52 import jalview.renderer.ResidueShaderI;
53 import jalview.schemes.BuriedColourScheme;
54 import jalview.schemes.FeatureColour;
55 import jalview.schemes.HelixColourScheme;
56 import jalview.schemes.JalviewColourScheme;
57 import jalview.schemes.StrandColourScheme;
58 import jalview.schemes.TurnColourScheme;
59 import jalview.util.MessageManager;
60 import jalview.viewmodel.AlignmentViewport;
62 public class AlignFrameTest
66 @BeforeClass(alwaysRun = true)
67 public static void setUpBeforeClass() throws Exception
71 * use read-only test properties file
73 Cache.loadProperties("test/jalview/io/testProps.jvprops");
74 Jalview.main(new String[] { "-nonews" });
77 @AfterMethod(alwaysRun = true)
78 public void tearDown()
80 Desktop.getInstance().closeAll_actionPerformed(null);
84 * configure (read-only) properties for test to ensure Consensus is computed
85 * for colour Above PID testing
87 @BeforeMethod(alwaysRun = true)
90 Cache.loadProperties("test/jalview/io/testProps.jvprops");
91 Cache.setPropertyNoSave("SHOW_IDENTITY",
92 Boolean.TRUE.toString());
93 af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
97 * wait for Consensus thread to complete
104 } catch (InterruptedException x)
107 } while (af.getViewport().getCalcManager().isWorking());
110 public static void setUpJvOptionPane()
112 JvOptionPane.setInteractiveMode(false);
113 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
116 @Test(groups = "Functional")
117 public void testHideFeatureColumns()
119 SequenceI seq1 = new Sequence("Seq1", "ABCDEFGHIJ");
120 SequenceI seq2 = new Sequence("Seq2", "ABCDEFGHIJ");
121 seq1.addSequenceFeature(
122 new SequenceFeature("Metal", "", 1, 5, 0f, null));
123 seq2.addSequenceFeature(
124 new SequenceFeature("Metal", "", 6, 10, 10f, null));
125 seq1.addSequenceFeature(
126 new SequenceFeature("Turn", "", 2, 4, Float.NaN, null));
127 seq2.addSequenceFeature(
128 new SequenceFeature("Turn", "", 7, 9, Float.NaN, null));
129 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
130 AlignFrame alignFrame = new AlignFrame(al, al.getWidth(),
134 * make all features visible (select feature columns checks visibility)
136 alignFrame.getFeatureRenderer().findAllFeatures(true);
139 * hiding a feature not present does nothing
141 assertFalse(alignFrame.hideFeatureColumns("exon", true));
142 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
144 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
145 .getNumberOfRegions(), 0);
147 assertFalse(alignFrame.hideFeatureColumns("exon", false));
148 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
150 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
151 .getNumberOfRegions(), 0);
154 * hiding a feature in all columns does nothing
156 assertFalse(alignFrame.hideFeatureColumns("Metal", true));
157 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
159 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
160 .getNumberOfRegions(), 0);
163 * threshold Metal to hide features where score < 5
164 * seq1 feature in columns 1-5 is hidden
165 * seq2 feature in columns 6-10 is shown
167 FeatureColourI fc = new FeatureColour(null, Color.red, Color.blue, null,
169 fc.setAboveThreshold(true);
171 alignFrame.getFeatureRenderer().setColour("Metal", fc);
172 assertTrue(alignFrame.hideFeatureColumns("Metal", true));
173 HiddenColumns hidden = alignFrame.getViewport().getAlignment()
175 assertEquals(hidden.getNumberOfRegions(), 1);
176 Iterator<int[]> regions = hidden.iterator();
177 int[] next = regions.next();
178 assertEquals(next[0], 5);
179 assertEquals(next[1], 9);
182 * hide a feature present in some columns
183 * sequence positions [2-4], [7-9] are column positions
184 * [1-3], [6-8] base zero
186 alignFrame.getViewport().showAllHiddenColumns();
187 assertTrue(alignFrame.hideFeatureColumns("Turn", true));
188 regions = alignFrame.getViewport().getAlignment().getHiddenColumns()
190 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
191 .getNumberOfRegions(), 2);
192 next = regions.next();
193 assertEquals(next[0], 1);
194 assertEquals(next[1], 3);
195 next = regions.next();
196 assertEquals(next[0], 6);
197 assertEquals(next[1], 8);
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
207 * <li>in neither case, changes alignment or group colour thresholds (PID or
211 @Test(groups = "Functional")
212 public void testChangeColour_background_groupsAndThresholds()
214 AlignViewportI av = af.getViewport();
215 AlignmentI al = av.getAlignment();
218 * Colour alignment by Buried Index
220 af.applyToAllGroups_actionPerformed(false);
221 af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
222 assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
223 assertFalse(av.getResidueShading().conservationApplied());
224 assertEquals(av.getResidueShading().getThreshold(), 0);
227 * Apply Conservation 20%
229 af.conservationMenuItem_actionPerformed(true);
230 SliderPanel sp = SliderPanel.getSliderPanel();
231 assertEquals(sp.getTitle(), MessageManager.formatMessage(
232 "label.conservation_colour_increment", new String[]
234 assertTrue(sp.isForConservation());
236 assertTrue(av.getResidueShading().conservationApplied());
237 assertEquals(av.getResidueShading().getConservationInc(), 20);
240 * Apply PID threshold 10% (conservation still applies as well)
242 af.abovePIDThreshold_actionPerformed(true);
243 sp = SliderPanel.getSliderPanel();
244 assertFalse(sp.isForConservation());
245 assertEquals(sp.getTitle(), MessageManager.formatMessage(
246 "label.percentage_identity_threshold", new String[]
249 assertEquals(av.getResidueShading().getThreshold(), 10);
250 assertTrue(av.getResidueShading().conservationApplied());
251 assertEquals(av.getResidueShading().getConservationInc(), 20);
254 * create a group with Strand colouring, 30% Conservation
255 * and 40% PID threshold
257 SequenceGroup sg = new SequenceGroup();
258 sg.addSequence(al.getSequenceAt(0), false);
261 av.setSelectionGroup(sg);
264 * apply 30% Conservation to group
265 * (notice menu action applies to selection group even if mouse click
266 * is at a sequence not in the group)
268 PopupMenu popupMenu = new PopupMenu(af.alignPanel, al.getSequenceAt(2),
270 popupMenu.changeColour_actionPerformed(
271 JalviewColourScheme.Strand.toString());
272 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
273 assertEquals(al.getGroups().size(), 1);
274 assertSame(al.getGroups().get(0), sg);
275 popupMenu.conservationMenuItem_actionPerformed(true);
276 sp = SliderPanel.getSliderPanel();
277 assertTrue(sp.isForConservation());
278 assertEquals(sp.getTitle(), MessageManager.formatMessage(
279 "label.conservation_colour_increment", new String[]
282 assertTrue(sg.getGroupColourScheme().conservationApplied());
283 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
286 * apply 40% PID threshold to group
288 popupMenu.abovePIDColour_actionPerformed(true);
289 sp = SliderPanel.getSliderPanel();
290 assertFalse(sp.isForConservation());
291 assertEquals(sp.getTitle(), MessageManager.formatMessage(
292 "label.percentage_identity_threshold", new String[]
295 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
296 // conservation threshold is unchanged:
297 assertTrue(sg.getGroupColourScheme().conservationApplied());
298 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
301 * change alignment colour - group colour, and all thresholds,
302 * should be unaffected
304 af.changeColour_actionPerformed(JalviewColourScheme.Turn.toString());
305 assertTrue(av.getGlobalColourScheme() instanceof TurnColourScheme);
306 assertTrue(av.getResidueShading().conservationApplied());
307 assertEquals(av.getResidueShading().getConservationInc(), 20);
308 assertEquals(av.getResidueShading().getThreshold(), 10);
309 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
310 assertTrue(sg.getGroupColourScheme().conservationApplied());
311 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
312 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
315 * Now change alignment colour with Apply Colour To All Groups
316 * - group colour should change, but not colour thresholds
318 af.applyToAllGroups_actionPerformed(true);
319 af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
320 assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
321 assertTrue(av.getResidueShading().conservationApplied());
322 assertEquals(av.getResidueShading().getConservationInc(), 20);
323 assertEquals(av.getResidueShading().getThreshold(), 10);
324 assertTrue(sg.getColourScheme() instanceof HelixColourScheme);
325 assertTrue(sg.getGroupColourScheme().conservationApplied());
326 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
327 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
331 * Test residue colouring with various options
333 * <li>no PID or Conservation threshold</li>
334 * <li>colour by Conservation applied</li>
335 * <li>colour by Conservation removed</li>
336 * <li>colour above PID - various values</li>
337 * <li>colour above PID removed</li>
338 * <li>Above PID plus By Conservation combined</li>
339 * <li>remove Above PID to leave just By Conservation</li>
340 * <li>re-add Above PID</li>
341 * <li>remove By Conservation to leave just Above PID</li>
342 * <li>remove Above PID to leave original colours</li>
345 @Test(groups = "Functional")
346 public void testColourThresholdActions()
348 AlignViewportI av = af.getViewport();
349 AlignmentI al = av.getAlignment();
352 * Colour alignment by Helix Propensity, no thresholds
354 af.applyToAllGroups_actionPerformed(false);
355 af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
356 assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
357 assertFalse(av.getResidueShading().conservationApplied());
358 assertEquals(av.getResidueShading().getThreshold(), 0);
361 * inspect the colour of
362 * FER_CAPAN.9(I), column 15 (14 base 0)
363 * FER_CAPAN.10(SER), column 16 (15 base 0)
365 SequenceI ferCapan = al.findName("FER_CAPAN");
366 ResidueShaderI rs = av.getResidueShading();
367 Color c = rs.findColour('I', 14, ferCapan);
368 Color i_original = new Color(138, 117, 138);
369 assertEquals(c, i_original);
370 c = rs.findColour('S', 15, ferCapan);
371 Color s_original = new Color(54, 201, 54);
372 assertEquals(c, s_original);
375 * colour by conservation with increment 10
377 af.conservationMenuItem_actionPerformed(true);
378 SliderPanel sp = SliderPanel.getSliderPanel();
379 assertTrue(sp.isForConservation());
380 assertEquals(sp.getValue(), 30); // initial slider setting
381 c = rs.findColour('I', 14, ferCapan);
382 Color i_faded = new Color(255, 255, 255);
383 assertEquals(c, i_faded);
385 assertSame(rs, av.getResidueShading());
386 assertEquals(rs.getConservationInc(), 10);
387 c = rs.findColour('I', 14, ferCapan);
388 i_faded = new Color(196, 186, 196);
389 assertEquals(c, i_faded);
390 c = rs.findColour('S', 15, ferCapan);
391 Color s_faded = new Color(144, 225, 144);
392 assertEquals(c, s_faded);
395 * deselect By Conservation - colour should revert
397 af.conservationMenuItem_actionPerformed(false);
398 c = rs.findColour('S', 15, ferCapan);
399 assertEquals(c, s_original);
402 * now Above PID, threshold = 0%
403 * should be no change
405 af.abovePIDThreshold_actionPerformed(true);
406 sp = SliderPanel.getSliderPanel();
407 assertFalse(sp.isForConservation());
408 assertEquals(sp.getValue(), 0); // initial slider setting
409 c = rs.findColour('I', 14, ferCapan);
410 assertEquals(c, i_original);
411 c = rs.findColour('S', 15, ferCapan);
412 assertEquals(c, s_original);
415 * Above PID, threshold = 1%
416 * 15.I becomes White because no match to consensus (V)
417 * 16.S remains coloured as matches 66.66% consensus
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 66% - no further change yet...
429 c = rs.findColour('I', 14, ferCapan);
430 assertEquals(c, Color.white);
431 c = rs.findColour('S', 15, ferCapan);
432 assertEquals(c, s_original);
435 * threshold 67% - now both residues are white
438 c = rs.findColour('I', 14, ferCapan);
439 assertEquals(c, Color.white);
440 c = rs.findColour('S', 15, ferCapan);
441 assertEquals(c, Color.white);
444 * deselect Above PID - colours should revert
446 af.abovePIDThreshold_actionPerformed(false);
447 c = rs.findColour('I', 14, ferCapan);
448 assertEquals(c, i_original);
449 c = rs.findColour('S', 15, ferCapan);
450 assertEquals(c, s_original);
453 * Now combine Above 50% PID and By Conservation 10%
454 * 15.I is White because no match to consensus (V)
455 * 16.S is coloured but faded
457 af.abovePIDThreshold_actionPerformed(true);
458 sp = SliderPanel.getSliderPanel();
459 assertFalse(sp.isForConservation());
461 af.conservationMenuItem_actionPerformed(true);
462 sp = SliderPanel.getSliderPanel();
463 assertTrue(sp.isForConservation());
465 c = rs.findColour('I', 14, ferCapan);
466 assertEquals(c, Color.white);
467 c = rs.findColour('S', 15, ferCapan);
468 assertEquals(c, s_faded);
471 * turn off Above PID - should just leave Conservation fading as before
473 af.abovePIDThreshold_actionPerformed(false);
474 c = rs.findColour('I', 14, ferCapan);
475 assertEquals(c, i_faded);
476 c = rs.findColour('S', 15, ferCapan);
477 assertEquals(c, s_faded);
480 * Now add Above 50% PID to conservation colouring
481 * - should give the same as PID followed by conservation (above)
483 af.abovePIDThreshold_actionPerformed(true);
484 SliderPanel.getSliderPanel().valueChanged(50);
485 c = rs.findColour('I', 14, ferCapan);
486 assertEquals(c, Color.white);
487 c = rs.findColour('S', 15, ferCapan);
488 assertEquals(c, s_faded);
491 * turn off By Conservation
492 * should leave I white, S original (unfaded) colour
494 af.conservationMenuItem_actionPerformed(false);
495 c = rs.findColour('I', 14, ferCapan);
496 assertEquals(c, Color.white);
497 c = rs.findColour('S', 15, ferCapan);
498 assertEquals(c, s_original);
501 * finally turn off Above PID to leave original colours
503 af.abovePIDThreshold_actionPerformed(false);
504 c = rs.findColour('I', 14, ferCapan);
505 assertEquals(c, i_original);
506 c = rs.findColour('S', 15, ferCapan);
507 assertEquals(c, s_original);
511 * Verify that making a New View transfers alignment and group colour schemes,
512 * including any thresholds, to the new view. Because New View is performed by
513 * saving and reloading a 'project' file, this is similar to verifying a
514 * project save and reload.
516 * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
518 @Test(groups = "Functional")
519 public void testNewView_colourThresholds()
521 AlignViewportI av = af.getViewport();
522 AlignmentI al = av.getAlignment();
525 * Colour alignment by Buried Index, Above 10% PID, By Conservation 20%
527 af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
528 assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
529 af.abovePIDThreshold_actionPerformed(true);
530 SliderPanel sp = SliderPanel.getSliderPanel();
531 assertFalse(sp.isForConservation());
533 af.conservationMenuItem_actionPerformed(true);
534 sp = SliderPanel.getSliderPanel();
535 assertTrue(sp.isForConservation());
537 ResidueShaderI rs = av.getResidueShading();
538 assertEquals(rs.getThreshold(), 10);
539 assertTrue(rs.conservationApplied());
540 assertEquals(rs.getConservationInc(), 20);
543 * create a group with Strand colouring, 30% Conservation
544 * and 40% PID threshold
546 SequenceGroup sg = new SequenceGroup();
547 sg.addSequence(al.getSequenceAt(0), false);
550 av.setSelectionGroup(sg);
551 PopupMenu popupMenu = new PopupMenu(af.alignPanel, al.getSequenceAt(0),
553 popupMenu.changeColour_actionPerformed(
554 JalviewColourScheme.Strand.toString());
555 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
556 assertEquals(al.getGroups().size(), 1);
557 assertSame(al.getGroups().get(0), sg);
558 popupMenu.conservationMenuItem_actionPerformed(true);
559 sp = SliderPanel.getSliderPanel();
560 assertTrue(sp.isForConservation());
562 popupMenu.abovePIDColour_actionPerformed(true);
563 sp = SliderPanel.getSliderPanel();
564 assertFalse(sp.isForConservation());
566 rs = sg.getGroupColourScheme();
567 assertTrue(rs.conservationApplied());
568 assertEquals(rs.getConservationInc(), 30);
569 assertEquals(rs.getThreshold(), 40);
572 * set slider panel focus to the background alignment
574 af.conservationMenuItem_actionPerformed(true);
575 sp = SliderPanel.getSliderPanel();
576 assertTrue(sp.isForConservation());
577 assertEquals(sp.getTitle(), MessageManager.formatMessage(
578 "label.conservation_colour_increment", new String[]
582 * make a new View, verify alignment and group colour schemes
584 af.newView_actionPerformed(null);
585 assertEquals(af.alignPanel.getViewName(), "View 1");
586 AlignmentViewport av2 = af.getViewport();
587 assertNotSame(av, av2);
588 assertSame(av2, af.alignPanel.av);
589 rs = av2.getResidueShading();
590 assertNotSame(av.getResidueShading(), rs);
591 assertEquals(rs.getThreshold(), 10);
592 assertTrue(rs.conservationApplied(), rs.toString());
593 assertEquals(rs.getConservationInc(), 20);
594 assertEquals(av2.getAlignment().getGroups().size(), 1);
595 sg = av2.getAlignment().getGroups().get(0);
596 rs = sg.getGroupColourScheme();
597 assertTrue(rs.conservationApplied());
598 assertEquals(rs.getConservationInc(), 30);
599 assertEquals(rs.getThreshold(), 40);
602 * check the Conservation SliderPanel (still open) is linked to
603 * and updates the new view (JAL-2385)
605 sp = SliderPanel.getSliderPanel();
606 assertTrue(sp.isForConservation());
607 assertEquals(sp.getTitle(), MessageManager.formatMessage(
608 "label.conservation_colour_increment", new String[]
611 assertEquals(av2.getResidueShading().getConservationInc(), 22);
615 * Verify that making a New View preserves the dataset reference for the
616 * alignment. Otherwise, see a 'duplicate jar entry' reference when trying to
617 * save alignments with multiple views, and codon mappings will not be shared
618 * across all panels in a split frame.
620 * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
622 @Test(groups = "Functional")
623 public void testNewView_dsRefPreserved()
625 AlignViewport av = af.getViewport();
626 AlignmentI al = av.getAlignment();
627 AlignmentI original_ds = al.getDataset();
628 af.newView_actionPerformed(null);
629 assertNotEquals("New view didn't select the a new panel", av,
631 org.testng.Assert.assertEquals(original_ds,
632 af.getViewport().getAlignment().getDataset(),
633 "Dataset was not preserved in new view");