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.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertNotSame;
27 import static org.testng.AssertJUnit.assertSame;
28 import static org.testng.AssertJUnit.assertTrue;
30 import java.util.ArrayList;
31 import java.util.List;
33 import org.testng.Assert;
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.BeforeMethod;
36 import org.testng.annotations.Test;
38 import jalview.bin.Cache;
39 import jalview.bin.Jalview;
40 import jalview.datamodel.AlignedCodonFrame;
41 import jalview.datamodel.Alignment;
42 import jalview.datamodel.AlignmentAnnotation;
43 import jalview.datamodel.AlignmentI;
44 import jalview.datamodel.Annotation;
45 import jalview.datamodel.SearchResults;
46 import jalview.datamodel.SearchResultsI;
47 import jalview.datamodel.Sequence;
48 import jalview.datamodel.SequenceGroup;
49 import jalview.datamodel.SequenceI;
50 import jalview.io.DataSourceType;
51 import jalview.io.FileLoader;
52 import jalview.schemes.ClustalxColourScheme;
53 import jalview.schemes.ColourSchemeI;
54 import jalview.schemes.PIDColourScheme;
55 import jalview.structure.StructureSelectionManager;
56 import jalview.util.MapList;
57 import jalview.viewmodel.ViewportRanges;
59 public class AlignViewportTest
62 @BeforeClass(alwaysRun = true)
63 public void setUpJvOptionPane()
65 JvOptionPane.setInteractiveMode(false);
66 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
73 @BeforeClass(alwaysRun = true)
74 public static void setUpBeforeClass() throws Exception
78 { "-nonews", "-props", "test/jalview/testProps.jvprops" });
81 * remove any sequence mappings left lying around by other tests
83 StructureSelectionManager ssm = StructureSelectionManager
84 .getStructureSelectionManager(Desktop.instance);
88 @BeforeMethod(alwaysRun = true)
91 SequenceI seq1 = new Sequence("Seq1", "ABC");
92 SequenceI seq2 = new Sequence("Seq2", "ABC");
93 SequenceI seq3 = new Sequence("Seq3", "ABC");
94 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3 };
95 al = new Alignment(seqs);
97 testee = new AlignViewport(al);
101 * Test that a mapping is not deregistered when a second view is closed but
102 * the first still holds a reference to the mapping
104 @Test(groups = { "Functional" })
105 public void testDeregisterMapping_onCloseView()
108 * alignment with reference to mappings
110 AlignFrame af1 = new FileLoader()
111 .LoadFileWaitTillLoaded(">Seq1\nCAGT\n", DataSourceType.PASTE);
113 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
114 AlignedCodonFrame acf1 = new AlignedCodonFrame();
116 new MapList(new int[]
117 { 1, 4 }, new int[] { 1, 4 }, 1, 1));
118 AlignedCodonFrame acf2 = new AlignedCodonFrame();
120 new MapList(new int[]
121 { 1, 4 }, new int[] { 4, 1 }, 1, 1));
123 List<AlignedCodonFrame> mappings = new ArrayList<>();
126 af1.getViewport().getAlignment().setCodonFrames(mappings);
127 af1.newView_actionPerformed(null);
130 * Verify that creating the alignment for the new View has registered the
133 StructureSelectionManager ssm = StructureSelectionManager
134 .getStructureSelectionManager(Desktop.instance);
135 List<AlignedCodonFrame> sequenceMappings = ssm.getSequenceMappings();
136 assertEquals(2, sequenceMappings.size());
137 assertTrue(sequenceMappings.contains(acf1));
138 assertTrue(sequenceMappings.contains(acf2));
141 * Close the second view. Verify that mappings are not removed as the first
142 * view still holds a reference to them.
144 af1.closeMenuItem_actionPerformed(false);
145 assertEquals(2, sequenceMappings.size());
146 assertTrue(sequenceMappings.contains(acf1));
147 assertTrue(sequenceMappings.contains(acf2));
151 * Test that a mapping is deregistered if no alignment holds a reference to it
153 @Test(groups = { "Functional" })
154 public void testDeregisterMapping_withNoReference()
156 Desktop d = Desktop.instance;
158 StructureSelectionManager ssm = StructureSelectionManager
159 .getStructureSelectionManager(Desktop.instance);
162 AlignFrame af1 = new FileLoader()
163 .LoadFileWaitTillLoaded(">Seq1\nRSVQ\n", DataSourceType.PASTE);
164 AlignFrame af2 = new FileLoader()
165 .LoadFileWaitTillLoaded(">Seq2\nDGEL\n", DataSourceType.PASTE);
166 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
167 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
168 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
169 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
170 // need to be distinct
171 AlignedCodonFrame acf1 = new AlignedCodonFrame();
173 new MapList(new int[]
174 { 1, 4 }, new int[] { 1, 12 }, 1, 3));
175 AlignedCodonFrame acf2 = new AlignedCodonFrame();
177 new MapList(new int[]
178 { 1, 4 }, new int[] { 1, 12 }, 1, 3));
179 AlignedCodonFrame acf3 = new AlignedCodonFrame();
180 acf3.addMap(cs2, cs2,
181 new MapList(new int[]
182 { 1, 12 }, new int[] { 1, 12 }, 1, 1));
184 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
186 af1.getViewport().getAlignment().setCodonFrames(mappings1);
188 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
191 af2.getViewport().getAlignment().setCodonFrames(mappings2);
194 * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
197 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
198 assertEquals(0, ssmMappings.size());
199 ssm.registerMapping(acf1);
200 assertEquals(1, ssmMappings.size());
201 ssm.registerMapping(acf2);
202 assertEquals(2, ssmMappings.size());
203 ssm.registerMapping(acf3);
204 assertEquals(3, ssmMappings.size());
207 * Closing AlignFrame2 should remove its mappings from
208 * StructureSelectionManager, since AlignFrame1 has no reference to them
210 af2.closeMenuItem_actionPerformed(true);
211 assertEquals(1, ssmMappings.size());
212 assertTrue(ssmMappings.contains(acf1));
216 * Test that a mapping is not deregistered if another alignment holds a
219 @Test(groups = { "Functional" })
220 public void testDeregisterMapping_withReference()
222 Desktop d = Desktop.instance;
224 StructureSelectionManager ssm = StructureSelectionManager
225 .getStructureSelectionManager(Desktop.instance);
228 AlignFrame af1 = new FileLoader()
229 .LoadFileWaitTillLoaded(">Seq1\nRSVQ\n", DataSourceType.PASTE);
230 AlignFrame af2 = new FileLoader()
231 .LoadFileWaitTillLoaded(">Seq2\nDGEL\n", DataSourceType.PASTE);
232 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
233 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
234 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
235 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
236 // need to be distinct
237 AlignedCodonFrame acf1 = new AlignedCodonFrame();
239 new MapList(new int[]
240 { 1, 4 }, new int[] { 1, 12 }, 1, 3));
241 AlignedCodonFrame acf2 = new AlignedCodonFrame();
243 new MapList(new int[]
244 { 1, 4 }, new int[] { 1, 12 }, 1, 3));
245 AlignedCodonFrame acf3 = new AlignedCodonFrame();
246 acf3.addMap(cs2, cs2,
247 new MapList(new int[]
248 { 1, 12 }, new int[] { 1, 12 }, 1, 1));
250 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
253 af1.getViewport().getAlignment().setCodonFrames(mappings1);
255 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
258 af2.getViewport().getAlignment().setCodonFrames(mappings2);
261 * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
264 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
265 assertEquals(0, ssmMappings.size());
266 ssm.registerMapping(acf1);
267 assertEquals(1, ssmMappings.size());
268 ssm.registerMapping(acf2);
269 assertEquals(2, ssmMappings.size());
270 ssm.registerMapping(acf3);
271 assertEquals(3, ssmMappings.size());
274 * Closing AlignFrame2 should remove mapping acf3 from
275 * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
278 af2.closeMenuItem_actionPerformed(true);
279 assertEquals(2, ssmMappings.size());
280 assertTrue(ssmMappings.contains(acf1));
281 assertTrue(ssmMappings.contains(acf2));
282 assertFalse(ssmMappings.contains(acf3));
286 * Test for JAL-1306 - conservation thread should run even when only Quality
287 * (and not Conservation) is enabled in Preferences
289 @Test(groups = { "Functional" }, timeOut = 2000)
290 public void testUpdateConservation_qualityOnly()
292 Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS",
293 Boolean.TRUE.toString());
294 Cache.applicationProperties.setProperty("SHOW_QUALITY",
295 Boolean.TRUE.toString());
296 Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
297 Boolean.FALSE.toString());
298 Cache.applicationProperties.setProperty("SHOW_OCCUPANCY",
299 Boolean.FALSE.toString());
300 Cache.applicationProperties.setProperty("SHOW_IDENTITY",
301 Boolean.FALSE.toString());
302 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
303 "examples/uniref50.fa", DataSourceType.FILE);
306 * wait for Conservation thread to complete
308 AlignViewport viewport = af.getViewport();
309 waitForCalculations(viewport);
310 AlignmentAnnotation[] anns = viewport.getAlignment()
311 .getAlignmentAnnotation();
312 assertNotNull("No annotations found", anns);
313 assertEquals("More than one annotation found", 1, anns.length);
314 assertTrue("Annotation is not Quality",
315 anns[0].description.startsWith("Alignment Quality"));
316 Annotation[] annotations = anns[0].annotations;
317 assertNotNull("Quality annotations are null", annotations);
318 assertNotNull("Quality in column 1 is null", annotations[0]);
319 assertTrue("No quality value in column 1", annotations[0].value > 10f);
323 * Wait for consensus etc calculation threads to complete
327 protected void waitForCalculations(AlignViewport viewport)
336 } catch (InterruptedException e)
339 } while (viewport.getCalcManager().isWorking());
343 @Test(groups = { "Functional" })
344 public void testSetGlobalColourScheme()
347 * test for JAL-2283: don't inadvertently turn on colour by conservation
349 Cache.applicationProperties.setProperty("DEFAULT_COLOUR_PROT", "None");
350 Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
351 Boolean.TRUE.toString());
352 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
353 "examples/uniref50.fa", DataSourceType.FILE);
354 ColourSchemeI cs = new PIDColourScheme();
355 AlignViewport viewport = af.getViewport();
356 viewport.setGlobalColourScheme(cs);
357 assertFalse(viewport.getResidueShading().conservationApplied());
360 * JAL-3201 groups have their own ColourSchemeI instances
362 AlignmentI aln = viewport.getAlignment();
363 SequenceGroup sg1 = new SequenceGroup();
364 sg1.addSequence(aln.getSequenceAt(0), false);
365 sg1.addSequence(aln.getSequenceAt(2), false);
366 SequenceGroup sg2 = new SequenceGroup();
367 sg2.addSequence(aln.getSequenceAt(1), false);
368 sg2.addSequence(aln.getSequenceAt(3), false);
371 viewport.setColourAppliesToAllGroups(true);
372 viewport.setGlobalColourScheme(new ClustalxColourScheme());
373 ColourSchemeI cs0 = viewport.getGlobalColourScheme();
374 ColourSchemeI cs1 = sg1.getColourScheme();
375 ColourSchemeI cs2 = sg2.getColourScheme();
376 assertTrue(cs0 instanceof ClustalxColourScheme);
377 assertTrue(cs1 instanceof ClustalxColourScheme);
378 assertTrue(cs2 instanceof ClustalxColourScheme);
379 assertNotSame(cs0, cs1);
380 assertNotSame(cs0, cs2);
381 assertNotSame(cs1, cs2);
384 @Test(groups = { "Functional" })
385 public void testSetGetHasSearchResults()
387 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
388 "examples/uniref50.fa", DataSourceType.FILE);
389 SearchResultsI sr = new SearchResults();
390 SequenceI s1 = af.getViewport().getAlignment().getSequenceAt(0);
392 // create arbitrary range on first sequence
393 sr.addResult(s1, s1.getStart() + 10, s1.getStart() + 15);
396 af.getViewport().setSearchResults(sr);
398 assertTrue(af.getViewport().hasSearchResults());
400 assertEquals(sr, af.getViewport().getSearchResults());
402 // set(null) results in has -> false
404 af.getViewport().setSearchResults(null);
405 assertFalse(af.getViewport().hasSearchResults());
409 * Verify that setting the selection group has the side-effect of setting the
410 * context on the group, unless it already has one, but does not change
411 * whether the group is defined or not.
413 @Test(groups = { "Functional" })
414 public void testSetSelectionGroup()
416 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
417 "examples/uniref50.fa", DataSourceType.FILE);
418 AlignViewport av = af.getViewport();
419 SequenceGroup sg1 = new SequenceGroup();
420 SequenceGroup sg2 = new SequenceGroup();
421 SequenceGroup sg3 = new SequenceGroup();
423 av.setSelectionGroup(sg1);
424 assertSame(sg1.getContext(), av.getAlignment()); // context set
425 assertFalse(sg1.isDefined()); // group not defined
427 sg2.setContext(sg1, false);
428 av.setSelectionGroup(sg2);
429 assertFalse(sg2.isDefined()); // unchanged
430 assertSame(sg2.getContext(), sg1); // unchanged
432 // create a defined group
433 sg3.setContext(av.getAlignment(), true);
434 av.setSelectionGroup(sg3);
435 assertTrue(sg3.isDefined()); // unchanged
439 * Verify that setting/clearing SHOW_OCCUPANCY preference adds or omits
440 * occupancy row from viewport
442 @Test(groups = { "Functional" })
443 public void testShowOrDontShowOccupancy()
446 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY",
447 Boolean.FALSE.toString());
448 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
449 "examples/uniref50.fa", DataSourceType.FILE);
450 AlignViewport av = af.getViewport();
451 Assert.assertNull(av.getAlignmentGapAnnotation(),
452 "Preference did not disable occupancy row.");
454 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
459 Assert.assertEquals(c, 0, "Expected zero occupancy rows.");
462 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY",
463 Boolean.TRUE.toString());
464 af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
465 DataSourceType.FILE);
466 av = af.getViewport();
467 Assert.assertNotNull(av.getAlignmentGapAnnotation(),
468 "Preference did not enable occupancy row.");
470 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
471 null, av.getAlignmentGapAnnotation().label))
476 Assert.assertEquals(c, 1, "Expected to find one occupancy row.");
479 @Test(groups = { "Functional" })
480 public void testGetConsensusSeq()
487 * consensus expected to be A-C
489 String fasta = ">s1\nA-C\n>s2\nA-C\n>s3\nA-D\n>s4\n--D\n";
490 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(fasta,
491 DataSourceType.PASTE);
492 AlignViewport testme = af.getViewport();
493 waitForCalculations(testme);
494 SequenceI cons = testme.getConsensusSeq();
495 assertEquals("A-C", cons.getSequenceAsString());
498 @Test(groups = { "Functional" })
499 public void testHideRevealSequences()
501 ViewportRanges ranges = testee.getRanges();
502 assertEquals(3, al.getHeight());
503 assertEquals(0, ranges.getStartSeq());
504 assertEquals(2, ranges.getEndSeq());
507 * hide first sequence
509 testee.hideSequence(new SequenceI[] { al.getSequenceAt(0) });
510 assertEquals(2, al.getHeight());
511 assertEquals(0, ranges.getStartSeq());
512 assertEquals(1, ranges.getEndSeq());
515 * reveal hidden sequences above the first
517 testee.showSequence(0);
518 assertEquals(3, al.getHeight());
519 assertEquals(0, ranges.getStartSeq());
520 assertEquals(2, ranges.getEndSeq());
523 * hide first and third sequences
527 { al.getSequenceAt(0), al.getSequenceAt(2) });
528 assertEquals(1, al.getHeight());
529 assertEquals(0, ranges.getStartSeq());
530 assertEquals(0, ranges.getEndSeq());
533 * reveal all hidden sequences
535 testee.showAllHiddenSeqs();
536 assertEquals(3, al.getHeight());
537 assertEquals(0, ranges.getStartSeq());
538 assertEquals(2, ranges.getEndSeq());