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.FeatureColourI;
39 import jalview.bin.Cache;
40 import jalview.bin.Jalview;
41 import jalview.datamodel.Alignment;
42 import jalview.datamodel.AlignmentI;
43 import jalview.datamodel.HiddenColumns;
44 import jalview.datamodel.Sequence;
45 import jalview.datamodel.SequenceFeature;
46 import jalview.datamodel.SequenceGroup;
47 import jalview.datamodel.SequenceI;
48 import jalview.io.DataSourceType;
49 import jalview.io.FileLoader;
50 import jalview.project.Jalview2xmlTests;
51 import jalview.renderer.ResidueShaderI;
52 import jalview.schemes.BuriedColourScheme;
53 import jalview.schemes.FeatureColour;
54 import jalview.schemes.HelixColourScheme;
55 import jalview.schemes.JalviewColourScheme;
56 import jalview.schemes.StrandColourScheme;
57 import jalview.schemes.TurnColourScheme;
58 import jalview.util.MessageManager;
60 public class AlignFrameTest
64 @BeforeClass(alwaysRun = true)
65 public static void setUpBeforeClass() throws Exception
69 * use read-only test properties file
71 Cache.loadProperties("test/jalview/io/testProps.jvprops");
72 Jalview.main(new String[] { "-nonews" });
75 @AfterMethod(alwaysRun = true)
76 public void tearDown()
78 Desktop.instance.closeAll_actionPerformed(null);
82 * configure (read-only) properties for test to ensure Consensus is computed
83 * for colour Above PID testing
85 @BeforeMethod(alwaysRun = true)
88 Cache.loadProperties("test/jalview/io/testProps.jvprops");
89 Cache.applicationProperties.setProperty("SHOW_IDENTITY",
90 Boolean.TRUE.toString());
91 af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
95 * wait for Consensus thread to complete
102 } catch (InterruptedException x)
105 } while (af.getViewport().getCalcManager().isWorking());
108 public static void setUpJvOptionPane()
110 JvOptionPane.setInteractiveMode(false);
111 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
114 @Test(groups = "Functional")
115 public void testHideFeatureColumns()
117 SequenceI seq1 = new Sequence("Seq1", "ABCDEFGHIJ");
118 SequenceI seq2 = new Sequence("Seq2", "ABCDEFGHIJ");
119 seq1.addSequenceFeature(
120 new SequenceFeature("Metal", "", 1, 5, 0f, null));
121 seq2.addSequenceFeature(
122 new SequenceFeature("Metal", "", 6, 10, 10f, null));
123 seq1.addSequenceFeature(
124 new SequenceFeature("Turn", "", 2, 4, Float.NaN, null));
125 seq2.addSequenceFeature(
126 new SequenceFeature("Turn", "", 7, 9, Float.NaN, null));
127 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
128 AlignFrame alignFrame = new AlignFrame(al, al.getWidth(),
132 * make all features visible (select feature columns checks visibility)
134 alignFrame.getFeatureRenderer().findAllFeatures(true);
137 * hiding a feature not present does nothing
139 assertFalse(alignFrame.hideFeatureColumns("exon", true));
140 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
142 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
143 .getNumberOfRegions(), 0);
145 assertFalse(alignFrame.hideFeatureColumns("exon", false));
146 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
148 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
149 .getNumberOfRegions(), 0);
152 * hiding a feature in all columns does nothing
154 assertFalse(alignFrame.hideFeatureColumns("Metal", true));
155 assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
157 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
158 .getNumberOfRegions(), 0);
161 * threshold Metal to hide features where score < 5
162 * seq1 feature in columns 1-5 is hidden
163 * seq2 feature in columns 6-10 is shown
165 FeatureColourI fc = new FeatureColour(null, Color.red, Color.blue, null,
167 fc.setAboveThreshold(true);
169 alignFrame.getFeatureRenderer().setColour("Metal", fc);
170 assertTrue(alignFrame.hideFeatureColumns("Metal", true));
171 HiddenColumns hidden = alignFrame.getViewport().getAlignment()
173 assertEquals(hidden.getNumberOfRegions(), 1);
174 Iterator<int[]> regions = hidden.iterator();
175 int[] next = regions.next();
176 assertEquals(next[0], 5);
177 assertEquals(next[1], 9);
180 * hide a feature present in some columns
181 * sequence positions [2-4], [7-9] are column positions
182 * [1-3], [6-8] base zero
184 alignFrame.getViewport().showAllHiddenColumns();
185 assertTrue(alignFrame.hideFeatureColumns("Turn", true));
186 regions = alignFrame.getViewport().getAlignment().getHiddenColumns()
188 assertEquals(alignFrame.getViewport().getAlignment().getHiddenColumns()
189 .getNumberOfRegions(), 2);
190 next = regions.next();
191 assertEquals(next[0], 1);
192 assertEquals(next[1], 3);
193 next = regions.next();
194 assertEquals(next[0], 6);
195 assertEquals(next[1], 8);
199 * Test that changing background (alignment) colour scheme
201 * <li>with Apply Colour to All Groups not selected, does not change group
203 * <li>with Apply Colour to All Groups selected, does change group
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", new String[]
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", new String[]
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
263 * (notice menu action applies to selection group even if mouse click
264 * is at a sequence not in the group)
266 PopupMenu popupMenu = new PopupMenu(af.alignPanel, al.getSequenceAt(2),
268 popupMenu.changeColour_actionPerformed(
269 JalviewColourScheme.Strand.toString());
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", new String[]
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", new String[]
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 AlignViewport 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 15 (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
379 c = rs.findColour('I', 14, ferCapan);
380 Color i_faded = new Color(255, 255, 255);
381 assertEquals(c, i_faded);
383 assertSame(rs, av.getResidueShading());
384 assertEquals(rs.getConservationInc(), 10);
385 c = rs.findColour('I', 14, ferCapan);
386 i_faded = new Color(196, 186, 196);
387 assertEquals(c, i_faded);
388 c = rs.findColour('S', 15, ferCapan);
389 Color s_faded = new Color(144, 225, 144);
390 assertEquals(c, s_faded);
393 * deselect By Conservation - colour should revert
395 af.conservationMenuItem_actionPerformed(false);
396 c = rs.findColour('S', 15, ferCapan);
397 assertEquals(c, s_original);
400 * now Above PID, threshold = 0%
401 * should be no change
403 af.abovePIDThreshold_actionPerformed(true);
404 sp = SliderPanel.getSliderPanel();
405 assertFalse(sp.isForConservation());
406 assertEquals(sp.getValue(), 0); // initial slider setting
407 c = rs.findColour('I', 14, ferCapan);
408 assertEquals(c, i_original);
409 c = rs.findColour('S', 15, ferCapan);
410 assertEquals(c, s_original);
413 * Above PID, threshold = 1%
414 * 15.I becomes White because no match to consensus (V)
415 * 16.S remains coloured as matches 66.66% consensus
418 c = rs.findColour('I', 14, ferCapan);
419 assertEquals(c, Color.white);
420 c = rs.findColour('S', 15, ferCapan);
421 assertEquals(c, s_original);
424 * threshold 66% - no further change yet...
427 c = rs.findColour('I', 14, ferCapan);
428 assertEquals(c, Color.white);
429 c = rs.findColour('S', 15, ferCapan);
430 assertEquals(c, s_original);
433 * threshold 67% - now both residues are white
436 c = rs.findColour('I', 14, ferCapan);
437 assertEquals(c, Color.white);
438 c = rs.findColour('S', 15, ferCapan);
439 assertEquals(c, Color.white);
442 * deselect Above PID - colours should revert
444 af.abovePIDThreshold_actionPerformed(false);
445 c = rs.findColour('I', 14, ferCapan);
446 assertEquals(c, i_original);
447 c = rs.findColour('S', 15, ferCapan);
448 assertEquals(c, s_original);
451 * Now combine Above 50% PID and By Conservation 10%
452 * 15.I is White because no match to consensus (V)
453 * 16.S is coloured but faded
455 af.abovePIDThreshold_actionPerformed(true);
456 sp = SliderPanel.getSliderPanel();
457 assertFalse(sp.isForConservation());
459 af.conservationMenuItem_actionPerformed(true);
460 sp = SliderPanel.getSliderPanel();
461 assertTrue(sp.isForConservation());
463 c = rs.findColour('I', 14, ferCapan);
464 assertEquals(c, Color.white);
465 c = rs.findColour('S', 15, ferCapan);
466 assertEquals(c, s_faded);
469 * turn off Above PID - should just leave Conservation fading as before
471 af.abovePIDThreshold_actionPerformed(false);
472 c = rs.findColour('I', 14, ferCapan);
473 assertEquals(c, i_faded);
474 c = rs.findColour('S', 15, ferCapan);
475 assertEquals(c, s_faded);
478 * Now add Above 50% PID to conservation colouring
479 * - should give the same as PID followed by conservation (above)
481 af.abovePIDThreshold_actionPerformed(true);
482 SliderPanel.getSliderPanel().valueChanged(50);
483 c = rs.findColour('I', 14, ferCapan);
484 assertEquals(c, Color.white);
485 c = rs.findColour('S', 15, ferCapan);
486 assertEquals(c, s_faded);
489 * turn off By Conservation
490 * should leave I white, S original (unfaded) colour
492 af.conservationMenuItem_actionPerformed(false);
493 c = rs.findColour('I', 14, ferCapan);
494 assertEquals(c, Color.white);
495 c = rs.findColour('S', 15, ferCapan);
496 assertEquals(c, s_original);
499 * finally turn off Above PID to leave original colours
501 af.abovePIDThreshold_actionPerformed(false);
502 c = rs.findColour('I', 14, ferCapan);
503 assertEquals(c, i_original);
504 c = rs.findColour('S', 15, ferCapan);
505 assertEquals(c, s_original);
509 * Verify that making a New View transfers alignment and group colour schemes,
510 * including any thresholds, to the new view. Because New View is performed by
511 * saving and reloading a 'project' file, this is similar to verifying a
512 * project save and reload.
514 * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
516 @Test(groups = "Functional")
517 public void testNewView_colourThresholds()
519 AlignViewport av = af.getViewport();
520 AlignmentI al = av.getAlignment();
523 * Colour alignment by Buried Index, Above 10% PID, By Conservation 20%
525 af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
526 assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
527 af.abovePIDThreshold_actionPerformed(true);
528 SliderPanel sp = SliderPanel.getSliderPanel();
529 assertFalse(sp.isForConservation());
531 af.conservationMenuItem_actionPerformed(true);
532 sp = SliderPanel.getSliderPanel();
533 assertTrue(sp.isForConservation());
535 ResidueShaderI rs = av.getResidueShading();
536 assertEquals(rs.getThreshold(), 10);
537 assertTrue(rs.conservationApplied());
538 assertEquals(rs.getConservationInc(), 20);
541 * create a group with Strand colouring, 30% Conservation
542 * and 40% PID threshold
544 SequenceGroup sg = new SequenceGroup();
545 sg.addSequence(al.getSequenceAt(0), false);
548 av.setSelectionGroup(sg);
549 PopupMenu popupMenu = new PopupMenu(af.alignPanel, al.getSequenceAt(0),
551 popupMenu.changeColour_actionPerformed(
552 JalviewColourScheme.Strand.toString());
553 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
554 assertEquals(al.getGroups().size(), 1);
555 assertSame(al.getGroups().get(0), sg);
556 popupMenu.conservationMenuItem_actionPerformed(true);
557 sp = SliderPanel.getSliderPanel();
558 assertTrue(sp.isForConservation());
560 popupMenu.abovePIDColour_actionPerformed(true);
561 sp = SliderPanel.getSliderPanel();
562 assertFalse(sp.isForConservation());
564 rs = sg.getGroupColourScheme();
565 assertTrue(rs.conservationApplied());
566 assertEquals(rs.getConservationInc(), 30);
567 assertEquals(rs.getThreshold(), 40);
570 * set slider panel focus to the background alignment
572 af.conservationMenuItem_actionPerformed(true);
573 sp = SliderPanel.getSliderPanel();
574 assertTrue(sp.isForConservation());
575 assertEquals(sp.getTitle(), MessageManager.formatMessage(
576 "label.conservation_colour_increment", new String[]
580 * make a new View, verify alignment and group colour schemes
582 af.newView_actionPerformed(null);
583 assertEquals(af.alignPanel.getViewName(), "View 1");
584 AlignViewport av2 = af.getViewport();
585 assertNotSame(av, av2);
586 assertSame(av2, af.alignPanel.av);
587 rs = av2.getResidueShading();
588 assertNotSame(av.getResidueShading(), rs);
589 assertEquals(rs.getThreshold(), 10);
590 assertTrue(rs.conservationApplied(), rs.toString());
591 assertEquals(rs.getConservationInc(), 20);
592 assertEquals(av2.getAlignment().getGroups().size(), 1);
593 sg = av2.getAlignment().getGroups().get(0);
594 rs = sg.getGroupColourScheme();
595 assertTrue(rs.conservationApplied());
596 assertEquals(rs.getConservationInc(), 30);
597 assertEquals(rs.getThreshold(), 40);
600 * check the Conservation SliderPanel (still open) is linked to
601 * and updates the new view (JAL-2385)
603 sp = SliderPanel.getSliderPanel();
604 assertTrue(sp.isForConservation());
605 assertEquals(sp.getTitle(), MessageManager.formatMessage(
606 "label.conservation_colour_increment", new String[]
609 assertEquals(av2.getResidueShading().getConservationInc(), 22);
613 * Verify that making a New View preserves the dataset reference for the
614 * alignment. Otherwise, see a 'duplicate jar entry' reference when trying to
615 * save alignments with multiple views, and codon mappings will not be shared
616 * across all panels in a split frame.
618 * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
620 @Test(groups = "Functional")
621 public void testNewView_dsRefPreserved()
623 AlignViewport av = af.getViewport();
624 AlignmentI al = av.getAlignment();
625 AlignmentI original_ds = al.getDataset();
626 af.newView_actionPerformed(null);
627 assertNotEquals("New view didn't select the a new panel", av,
629 org.testng.Assert.assertEquals(original_ds,
630 af.getViewport().getAlignment().getDataset(),
631 "Dataset was not preserved in new view");