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 jalview.api.AlignViewportI;
31 import java.util.ArrayList;
32 import java.util.List;
34 import org.testng.Assert;
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.BeforeMethod;
37 import org.testng.annotations.Test;
39 import jalview.bin.Cache;
40 import jalview.bin.Jalview;
41 import jalview.datamodel.AlignedCodonFrame;
42 import jalview.datamodel.Alignment;
43 import jalview.datamodel.AlignmentAnnotation;
44 import jalview.datamodel.AlignmentI;
45 import jalview.datamodel.Annotation;
46 import jalview.datamodel.SearchResults;
47 import jalview.datamodel.SearchResultsI;
48 import jalview.datamodel.Sequence;
49 import jalview.datamodel.SequenceGroup;
50 import jalview.datamodel.SequenceI;
51 import jalview.io.DataSourceType;
52 import jalview.io.FileLoader;
53 import jalview.schemes.ClustalxColourScheme;
54 import jalview.schemes.ColourSchemeI;
55 import jalview.schemes.PIDColourScheme;
56 import jalview.structure.StructureSelectionManager;
57 import jalview.util.MapList;
58 import jalview.viewmodel.AlignmentViewport;
59 import jalview.viewmodel.ViewportRanges;
60 import jalview.workers.AlignCalcManager;
62 public class AlignViewportTest
65 @BeforeClass(alwaysRun = true)
66 public void setUpJvOptionPane()
68 JvOptionPane.setInteractiveMode(false);
69 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
74 AlignmentViewport testee;
76 @BeforeClass(alwaysRun = true)
77 public static void setUpBeforeClass() throws Exception
79 Jalview.main(new String[] {
82 "test/jalview/testProps.jvprops" });
85 * remove any sequence mappings left lying around by other tests
87 StructureSelectionManager ssm = StructureSelectionManager
88 .getStructureSelectionManager(Desktop.getInstance());
92 @BeforeMethod(alwaysRun = true)
95 SequenceI seq1 = new Sequence("Seq1", "ABC");
96 SequenceI seq2 = new Sequence("Seq2", "ABC");
97 SequenceI seq3 = new Sequence("Seq3", "ABC");
98 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3 };
99 al = new Alignment(seqs);
101 testee = new AlignViewport(al);
105 * Test that a mapping is not deregistered when a second view is closed but
106 * the first still holds a reference to the mapping
108 @Test(groups = { "Functional" })
109 public void testDeregisterMapping_onCloseView()
112 * alignment with reference to mappings
114 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
115 ">Seq1\nCAGT\n", DataSourceType.PASTE);
117 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
118 AlignedCodonFrame acf1 = new AlignedCodonFrame();
119 acf1.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 1, 4 },
121 AlignedCodonFrame acf2 = new AlignedCodonFrame();
122 acf2.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 4, 1 },
125 List<AlignedCodonFrame> mappings = new ArrayList<>();
128 af1.getViewport().getAlignment().setCodonFrames(mappings);
129 af1.newView_actionPerformed(null);
132 * Verify that creating the alignment for the new View has registered the
135 StructureSelectionManager ssm = StructureSelectionManager
136 .getStructureSelectionManager(Desktop.getInstance());
137 List<AlignedCodonFrame> sequenceMappings = ssm.getSequenceMappings();
138 assertEquals(2, sequenceMappings.size());
139 assertTrue(sequenceMappings.contains(acf1));
140 assertTrue(sequenceMappings.contains(acf2));
143 * Close the second view. Verify that mappings are not removed as the first
144 * view still holds a reference to them.
146 af1.closeMenuItem_actionPerformed(false);
147 assertEquals(2, sequenceMappings.size());
148 assertTrue(sequenceMappings.contains(acf1));
149 assertTrue(sequenceMappings.contains(acf2));
153 * Test that a mapping is deregistered if no alignment holds a reference to it
155 @Test(groups = { "Functional" })
156 public void testDeregisterMapping_withNoReference()
158 Desktop d = Desktop.getInstance();
160 StructureSelectionManager ssm = StructureSelectionManager
161 .getStructureSelectionManager(Desktop.getInstance());
164 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
165 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
166 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
167 ">Seq2\nDGEL\n", DataSourceType.PASTE);
168 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
169 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
170 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
171 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
172 // need to be distinct
173 AlignedCodonFrame acf1 = new AlignedCodonFrame();
174 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
175 new int[] { 1, 12 }, 1, 3));
176 AlignedCodonFrame acf2 = new AlignedCodonFrame();
177 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
178 new int[] { 1, 12 }, 1, 3));
179 AlignedCodonFrame acf3 = new AlignedCodonFrame();
180 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
183 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
185 af1.getViewport().getAlignment().setCodonFrames(mappings1);
187 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
190 af2.getViewport().getAlignment().setCodonFrames(mappings2);
193 * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
196 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
197 assertEquals(0, ssmMappings.size());
198 ssm.registerMapping(acf1);
199 assertEquals(1, ssmMappings.size());
200 ssm.registerMapping(acf2);
201 assertEquals(2, ssmMappings.size());
202 ssm.registerMapping(acf3);
203 assertEquals(3, ssmMappings.size());
206 * Closing AlignFrame2 should remove its mappings from
207 * StructureSelectionManager, since AlignFrame1 has no reference to them
209 af2.closeMenuItem_actionPerformed(true);
210 assertEquals(1, ssmMappings.size());
211 assertTrue(ssmMappings.contains(acf1));
215 * Test that a mapping is not deregistered if another alignment holds a
218 @Test(groups = { "Functional" })
219 public void testDeregisterMapping_withReference()
221 Desktop d = Desktop.getInstance();
223 StructureSelectionManager ssm = StructureSelectionManager
224 .getStructureSelectionManager(Desktop.getInstance());
227 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
228 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
229 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
230 ">Seq2\nDGEL\n", DataSourceType.PASTE);
231 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
232 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
233 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
234 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
235 // need to be distinct
236 AlignedCodonFrame acf1 = new AlignedCodonFrame();
237 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
238 new int[] { 1, 12 }, 1, 3));
239 AlignedCodonFrame acf2 = new AlignedCodonFrame();
240 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
241 new int[] { 1, 12 }, 1, 3));
242 AlignedCodonFrame acf3 = new AlignedCodonFrame();
243 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
246 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
249 af1.getViewport().getAlignment().setCodonFrames(mappings1);
251 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
254 af2.getViewport().getAlignment().setCodonFrames(mappings2);
257 * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
260 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
261 assertEquals(0, ssmMappings.size());
262 ssm.registerMapping(acf1);
263 assertEquals(1, ssmMappings.size());
264 ssm.registerMapping(acf2);
265 assertEquals(2, ssmMappings.size());
266 ssm.registerMapping(acf3);
267 assertEquals(3, ssmMappings.size());
270 * Closing AlignFrame2 should remove mapping acf3 from
271 * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
274 af2.closeMenuItem_actionPerformed(true);
275 assertEquals(2, ssmMappings.size());
276 assertTrue(ssmMappings.contains(acf1));
277 assertTrue(ssmMappings.contains(acf2));
278 assertFalse(ssmMappings.contains(acf3));
282 * Test for JAL-1306 - conservation thread should run even when only Quality
283 * (and not Conservation) is enabled in Preferences
285 @Test(groups = { "Functional" }, timeOut=2000)
286 public void testUpdateConservation_qualityOnly()
288 Cache.setPropertyNoSave("SHOW_ANNOTATIONS",
289 Boolean.TRUE.toString());
290 Cache.setPropertyNoSave("SHOW_QUALITY",
291 Boolean.TRUE.toString());
292 Cache.setPropertyNoSave("SHOW_CONSERVATION",
293 Boolean.FALSE.toString());
294 Cache.setPropertyNoSave("SHOW_OCCUPANCY",
295 Boolean.FALSE.toString());
296 Cache.setPropertyNoSave("SHOW_IDENTITY",
297 Boolean.FALSE.toString());
298 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
299 "examples/uniref50.fa", DataSourceType.FILE);
302 * wait for Conservation thread to complete
304 AlignViewport viewport = af.getViewport();
305 waitForCalculations(viewport);
306 AlignmentAnnotation[] anns = viewport.getAlignment()
307 .getAlignmentAnnotation();
308 assertNotNull("No annotations found", anns);
309 assertEquals("More than one annotation found", 1, anns.length);
310 assertTrue("Annotation is not Quality",
311 anns[0].description.startsWith("Alignment Quality"));
312 Annotation[] annotations = anns[0].annotations;
313 assertNotNull("Quality annotations are null", annotations);
314 assertNotNull("Quality in column 1 is null", annotations[0]);
315 assertTrue("No quality value in column 1", annotations[0].value > 10f);
319 * Wait for consensus etc calculation threads to complete
323 protected void waitForCalculations(AlignViewport viewport)
327 System.out.print("waiting...");
329 while (--n >= 0 || viewport.getCalcManager().isWorking())
333 System.out.print(((AlignCalcManager) viewport.getCalcManager()).getQueueLength());
335 } catch (InterruptedException e)
339 System.out.println("...done");
343 @Test(groups = { "Functional" })
344 public void testSetGlobalColourScheme()
347 * test for JAL-2283: don't inadvertently turn on colour by conservation
349 Cache.setPropertyNoSave("DEFAULT_COLOUR_PROT", "None");
350 Cache.setPropertyNoSave("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()
358 .conservationApplied());
361 * JAL-3201 groups have their own ColourSchemeI instances
363 AlignmentI aln = viewport.getAlignment();
364 SequenceGroup sg1 = new SequenceGroup();
365 sg1.addSequence(aln.getSequenceAt(0), false);
366 sg1.addSequence(aln.getSequenceAt(2), false);
367 SequenceGroup sg2 = new SequenceGroup();
368 sg2.addSequence(aln.getSequenceAt(1), false);
369 sg2.addSequence(aln.getSequenceAt(3), false);
372 viewport.setColourAppliesToAllGroups(true);
373 viewport.setGlobalColourScheme(new ClustalxColourScheme());
374 ColourSchemeI cs0 = viewport.getGlobalColourScheme();
375 ColourSchemeI cs1 = sg1.getColourScheme();
376 ColourSchemeI cs2 = sg2.getColourScheme();
377 assertTrue(cs0 instanceof ClustalxColourScheme);
378 assertTrue(cs1 instanceof ClustalxColourScheme);
379 assertTrue(cs2 instanceof ClustalxColourScheme);
380 assertNotSame(cs0, cs1);
381 assertNotSame(cs0, cs2);
382 assertNotSame(cs1, cs2);
385 @Test(groups = { "Functional" })
386 public void testSetGetHasSearchResults()
388 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
389 "examples/uniref50.fa", DataSourceType.FILE);
390 SearchResultsI sr = new SearchResults();
391 SequenceI s1 = af.getViewport().getAlignment().getSequenceAt(0);
393 // create arbitrary range on first sequence
394 sr.addResult(s1, s1.getStart() + 10, s1.getStart() + 15);
397 af.getViewport().setSearchResults(sr);
399 assertTrue(af.getViewport().hasSearchResults());
401 assertEquals(sr, af.getViewport().getSearchResults());
403 // set(null) results in has -> false
405 af.getViewport().setSearchResults(null);
406 assertFalse(af.getViewport().hasSearchResults());
410 * Verify that setting the selection group has the side-effect of setting the
411 * context on the group, unless it already has one, but does not change
412 * whether the group is defined or not.
414 @Test(groups = { "Functional" })
415 public void testSetSelectionGroup()
417 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
418 "examples/uniref50.fa", DataSourceType.FILE);
419 AlignViewportI av = af.getViewport();
420 SequenceGroup sg1 = new SequenceGroup();
421 SequenceGroup sg2 = new SequenceGroup();
422 SequenceGroup sg3 = new SequenceGroup();
424 av.setSelectionGroup(sg1);
425 assertSame(sg1.getContext(), av.getAlignment()); // context set
426 assertFalse(sg1.isDefined()); // group not defined
428 sg2.setContext(sg1, false);
429 av.setSelectionGroup(sg2);
430 assertFalse(sg2.isDefined()); // unchanged
431 assertSame(sg2.getContext(), sg1); // unchanged
433 // create a defined group
434 sg3.setContext(av.getAlignment(), true);
435 av.setSelectionGroup(sg3);
436 assertTrue(sg3.isDefined()); // unchanged
439 * Verify that setting/clearing SHOW_OCCUPANCY preference adds or omits occupancy row from viewport
441 @Test(groups = { "Functional" })
442 public void testShowOrDontShowOccupancy()
445 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.FALSE.toString());
446 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
447 "examples/uniref50.fa", DataSourceType.FILE);
448 AlignViewportI av = af.getViewport();
449 Assert.assertNull(av.getAlignmentGapAnnotation(),
450 "Preference did not disable occupancy row.");
452 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
457 Assert.assertEquals(c, 0, "Expected zero occupancy rows.");
460 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.TRUE.toString());
461 af = new FileLoader().LoadFileWaitTillLoaded(
462 "examples/uniref50.fa", DataSourceType.FILE);
463 av = af.getViewport();
464 Assert.assertNotNull(av.getAlignmentGapAnnotation(),
465 "Preference did not enable occupancy row.");
467 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
468 null, av.getAlignmentGapAnnotation().label))
473 Assert.assertEquals(c, 1, "Expected to find one occupancy row.");
476 @Test(groups = { "Functional" })
477 public void testGetConsensusSeq()
484 * consensus expected to be A-C
486 String fasta = ">s1\nA-C\n>s2\nA-C\n>s3\nA-D\n>s4\n--D\n";
487 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(fasta,
488 DataSourceType.PASTE);
489 AlignViewport testme = af.getViewport();
490 waitForCalculations(testme);
491 SequenceI cons = testme.getConsensusSeq();
492 String s = cons.getSequenceAsString();
493 System.out.println("s is " + s);
495 assertEquals("A-C", s);
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
525 testee.hideSequence(new SequenceI[] { al.getSequenceAt(0),
526 al.getSequenceAt(2) });
527 assertEquals(1, al.getHeight());
528 assertEquals(0, ranges.getStartSeq());
529 assertEquals(0, ranges.getEndSeq());
532 * reveal all hidden sequences
534 testee.showAllHiddenSeqs();
535 assertEquals(3, al.getHeight());
536 assertEquals(0, ranges.getStartSeq());
537 assertEquals(2, ranges.getEndSeq());