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.bin.Cache;
31 import jalview.bin.Jalview;
32 import jalview.datamodel.AlignedCodonFrame;
33 import jalview.datamodel.Alignment;
34 import jalview.datamodel.AlignmentAnnotation;
35 import jalview.datamodel.AlignmentI;
36 import jalview.datamodel.Annotation;
37 import jalview.datamodel.SearchResults;
38 import jalview.datamodel.SearchResultsI;
39 import jalview.datamodel.Sequence;
40 import jalview.datamodel.SequenceGroup;
41 import jalview.datamodel.SequenceI;
42 import jalview.io.DataSourceType;
43 import jalview.io.FileLoader;
44 import jalview.schemes.ClustalxColourScheme;
45 import jalview.schemes.ColourSchemeI;
46 import jalview.schemes.PIDColourScheme;
47 import jalview.structure.StructureSelectionManager;
48 import jalview.util.MapList;
49 import jalview.viewmodel.ViewportRanges;
51 import java.util.ArrayList;
52 import java.util.List;
54 import org.testng.Assert;
55 import org.testng.annotations.BeforeClass;
56 import org.testng.annotations.BeforeMethod;
57 import org.testng.annotations.Test;
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
76 Jalview.main(new String[] { "-nonews", "-props",
77 "test/jalview/testProps.jvprops" });
80 * remove any sequence mappings left lying around by other tests
82 Desktop.getStructureSelectionManager().resetAll();
85 @BeforeMethod(alwaysRun = true)
88 SequenceI seq1 = new Sequence("Seq1", "ABC");
89 SequenceI seq2 = new Sequence("Seq2", "ABC");
90 SequenceI seq3 = new Sequence("Seq3", "ABC");
91 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3 };
92 al = new Alignment(seqs);
94 testee = new AlignViewport(al);
98 * Test that a mapping is not deregistered when a second view is closed but
99 * the first still holds a reference to the mapping
101 @Test(groups = { "Functional" })
102 public void testDeregisterMapping_onCloseView()
105 * alignment with reference to mappings
107 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
108 ">Seq1\nCAGT\n", DataSourceType.PASTE);
110 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
111 AlignedCodonFrame acf1 = new AlignedCodonFrame();
112 acf1.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 1, 4 },
114 AlignedCodonFrame acf2 = new AlignedCodonFrame();
115 acf2.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 4, 1 },
118 List<AlignedCodonFrame> mappings = new ArrayList<>();
121 af1.getViewport().getAlignment().setCodonFrames(mappings);
122 af1.newView_actionPerformed(null);
125 * Verify that creating the alignment for the new View has registered the
128 List<AlignedCodonFrame> sequenceMappings = Desktop
129 .getStructureSelectionManager().getSequenceMappings();
130 assertEquals(2, sequenceMappings.size());
131 assertTrue(sequenceMappings.contains(acf1));
132 assertTrue(sequenceMappings.contains(acf2));
135 * Close the second view. Verify that mappings are not removed as the first
136 * view still holds a reference to them.
138 af1.closeMenuItem_actionPerformed(false);
139 assertEquals(2, sequenceMappings.size());
140 assertTrue(sequenceMappings.contains(acf1));
141 assertTrue(sequenceMappings.contains(acf2));
145 * Test that a mapping is deregistered if no alignment holds a reference to it
147 @Test(groups = { "Functional" })
148 public void testDeregisterMapping_withNoReference()
150 Desktop d = Desktop.getInstance();
152 StructureSelectionManager ssm = Desktop.getStructureSelectionManager();
155 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
156 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
157 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
158 ">Seq2\nDGEL\n", DataSourceType.PASTE);
159 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
160 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
161 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
162 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
163 // need to be distinct
164 AlignedCodonFrame acf1 = new AlignedCodonFrame();
165 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
166 new int[] { 1, 12 }, 1, 3));
167 AlignedCodonFrame acf2 = new AlignedCodonFrame();
168 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
169 new int[] { 1, 12 }, 1, 3));
170 AlignedCodonFrame acf3 = new AlignedCodonFrame();
171 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
174 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
176 af1.getViewport().getAlignment().setCodonFrames(mappings1);
178 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
181 af2.getViewport().getAlignment().setCodonFrames(mappings2);
184 * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
187 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
188 assertEquals(0, ssmMappings.size());
189 ssm.registerMapping(acf1);
190 assertEquals(1, ssmMappings.size());
191 ssm.registerMapping(acf2);
192 assertEquals(2, ssmMappings.size());
193 ssm.registerMapping(acf3);
194 assertEquals(3, ssmMappings.size());
197 * Closing AlignFrame2 should remove its mappings from
198 * StructureSelectionManager, since AlignFrame1 has no reference to them
200 af2.closeMenuItem_actionPerformed(true);
201 assertEquals(1, ssmMappings.size());
202 assertTrue(ssmMappings.contains(acf1));
206 * Test that a mapping is not deregistered if another alignment holds a
209 @Test(groups = { "Functional" })
210 public void testDeregisterMapping_withReference()
212 Desktop d = Desktop.getInstance();
214 StructureSelectionManager ssm = Desktop.getStructureSelectionManager();
217 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
218 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
219 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
220 ">Seq2\nDGEL\n", DataSourceType.PASTE);
221 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
222 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
223 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
224 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
225 // need to be distinct
226 AlignedCodonFrame acf1 = new AlignedCodonFrame();
227 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
228 new int[] { 1, 12 }, 1, 3));
229 AlignedCodonFrame acf2 = new AlignedCodonFrame();
230 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
231 new int[] { 1, 12 }, 1, 3));
232 AlignedCodonFrame acf3 = new AlignedCodonFrame();
233 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
236 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
239 af1.getViewport().getAlignment().setCodonFrames(mappings1);
241 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
244 af2.getViewport().getAlignment().setCodonFrames(mappings2);
247 * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
250 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
251 assertEquals(0, ssmMappings.size());
252 ssm.registerMapping(acf1);
253 assertEquals(1, ssmMappings.size());
254 ssm.registerMapping(acf2);
255 assertEquals(2, ssmMappings.size());
256 ssm.registerMapping(acf3);
257 assertEquals(3, ssmMappings.size());
260 * Closing AlignFrame2 should remove mapping acf3 from
261 * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
264 af2.closeMenuItem_actionPerformed(true);
265 assertEquals(2, ssmMappings.size());
266 assertTrue(ssmMappings.contains(acf1));
267 assertTrue(ssmMappings.contains(acf2));
268 assertFalse(ssmMappings.contains(acf3));
272 * Test for JAL-1306 - conservation thread should run even when only Quality
273 * (and not Conservation) is enabled in Preferences
275 @Test(groups = { "Functional" }, timeOut=2000)
276 public void testUpdateConservation_qualityOnly()
278 Cache.setPropertyNoSave("SHOW_ANNOTATIONS",
279 Boolean.TRUE.toString());
280 Cache.setPropertyNoSave("SHOW_QUALITY",
281 Boolean.TRUE.toString());
282 Cache.setPropertyNoSave("SHOW_CONSERVATION",
283 Boolean.FALSE.toString());
284 Cache.setPropertyNoSave("SHOW_OCCUPANCY",
285 Boolean.FALSE.toString());
286 Cache.setPropertyNoSave("SHOW_IDENTITY",
287 Boolean.FALSE.toString());
288 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
289 "examples/uniref50.fa", DataSourceType.FILE);
292 * wait for Conservation thread to complete
294 AlignViewport viewport = af.getViewport();
297 while (viewport.getAlignmentConservationAnnotation() != null)
302 } catch (InterruptedException e)
307 AlignmentAnnotation[] anns = viewport.getAlignment()
308 .getAlignmentAnnotation();
309 assertNotNull("No annotations found", anns);
310 assertEquals("More than one annotation found", 1, anns.length);
311 assertTrue("Annotation is not Quality",
312 anns[0].description.startsWith("Alignment Quality"));
313 Annotation[] annotations = anns[0].annotations;
314 assertNotNull("Quality annotations are null", annotations);
315 assertNotNull("Quality in column 1 is null", annotations[0]);
316 assertTrue("No quality value in column 1", annotations[0].value > 10f);
319 @Test(groups = { "Functional" })
320 public void testSetGlobalColourScheme()
323 * test for JAL-2283: don't inadvertently turn on colour by conservation
325 Cache.setPropertyNoSave("DEFAULT_COLOUR_PROT", "None");
326 Cache.setPropertyNoSave("SHOW_CONSERVATION",
327 Boolean.TRUE.toString());
328 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
329 "examples/uniref50.fa", DataSourceType.FILE);
330 ColourSchemeI cs = new PIDColourScheme();
331 AlignViewport viewport = af.getViewport();
332 viewport.setGlobalColourScheme(cs);
333 assertFalse(viewport.getResidueShading()
334 .conservationApplied());
337 * JAL-3201 groups have their own ColourSchemeI instances
339 AlignmentI aln = viewport.getAlignment();
340 SequenceGroup sg1 = new SequenceGroup();
341 sg1.addSequence(aln.getSequenceAt(0), false);
342 sg1.addSequence(aln.getSequenceAt(2), false);
343 SequenceGroup sg2 = new SequenceGroup();
344 sg2.addSequence(aln.getSequenceAt(1), false);
345 sg2.addSequence(aln.getSequenceAt(3), false);
348 viewport.setColourAppliesToAllGroups(true);
349 viewport.setGlobalColourScheme(new ClustalxColourScheme());
350 ColourSchemeI cs0 = viewport.getGlobalColourScheme();
351 ColourSchemeI cs1 = sg1.getColourScheme();
352 ColourSchemeI cs2 = sg2.getColourScheme();
353 assertTrue(cs0 instanceof ClustalxColourScheme);
354 assertTrue(cs1 instanceof ClustalxColourScheme);
355 assertTrue(cs2 instanceof ClustalxColourScheme);
356 assertNotSame(cs0, cs1);
357 assertNotSame(cs0, cs2);
358 assertNotSame(cs1, cs2);
361 @Test(groups = { "Functional" })
362 public void testSetGetHasSearchResults()
364 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
365 "examples/uniref50.fa", DataSourceType.FILE);
366 SearchResultsI sr = new SearchResults();
367 SequenceI s1 = af.getViewport().getAlignment().getSequenceAt(0);
369 // create arbitrary range on first sequence
370 sr.addResult(s1, s1.getStart() + 10, s1.getStart() + 15);
373 af.getViewport().setSearchResults(sr);
375 assertTrue(af.getViewport().hasSearchResults());
377 assertEquals(sr, af.getViewport().getSearchResults());
379 // set(null) results in has -> false
381 af.getViewport().setSearchResults(null);
382 assertFalse(af.getViewport().hasSearchResults());
386 * Verify that setting the selection group has the side-effect of setting the
387 * context on the group, unless it already has one, but does not change
388 * whether the group is defined or not.
390 @Test(groups = { "Functional" })
391 public void testSetSelectionGroup()
393 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
394 "examples/uniref50.fa", DataSourceType.FILE);
395 AlignViewport av = af.getViewport();
396 SequenceGroup sg1 = new SequenceGroup();
397 SequenceGroup sg2 = new SequenceGroup();
398 SequenceGroup sg3 = new SequenceGroup();
400 av.setSelectionGroup(sg1);
401 assertSame(sg1.getContext(), av.getAlignment()); // context set
402 assertFalse(sg1.isDefined()); // group not defined
404 sg2.setContext(sg1, false);
405 av.setSelectionGroup(sg2);
406 assertFalse(sg2.isDefined()); // unchanged
407 assertSame(sg2.getContext(), sg1); // unchanged
409 // create a defined group
410 sg3.setContext(av.getAlignment(), true);
411 av.setSelectionGroup(sg3);
412 assertTrue(sg3.isDefined()); // unchanged
415 * Verify that setting/clearing SHOW_OCCUPANCY preference adds or omits occupancy row from viewport
417 @Test(groups = { "Functional" })
418 public void testShowOrDontShowOccupancy()
421 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.FALSE.toString());
422 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
423 "examples/uniref50.fa", DataSourceType.FILE);
424 AlignViewport av = af.getViewport();
425 Assert.assertNull(av.getAlignmentGapAnnotation(), "Preference did not disable occupancy row.");
427 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
432 Assert.assertEquals(c, 0, "Expected zero occupancy rows.");
435 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.TRUE.toString());
436 af = new FileLoader().LoadFileWaitTillLoaded(
437 "examples/uniref50.fa", DataSourceType.FILE);
438 av = af.getViewport();
439 Assert.assertNotNull(av.getAlignmentGapAnnotation(), "Preference did not enable occupancy row.");
441 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
442 null, av.getAlignmentGapAnnotation().label))
447 Assert.assertEquals(c, 1, "Expected to find one occupancy row.");
450 @Test(groups = { "Functional" })
451 public void testGetConsensusSeq()
458 * consensus expected to be A-C
460 String fasta = ">s1\nA-C\n>s2\nA-C\n>s3\nA-D\n>s4\n--D\n";
461 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(fasta,
462 DataSourceType.PASTE);
463 AlignViewport testme = af.getViewport();
464 SequenceI cons = testme.getConsensusSeq();
465 assertEquals("A-C", cons.getSequenceAsString());
468 @Test(groups = { "Functional" })
469 public void testHideRevealSequences()
471 ViewportRanges ranges = testee.getRanges();
472 assertEquals(3, al.getHeight());
473 assertEquals(0, ranges.getStartSeq());
474 assertEquals(2, ranges.getEndSeq());
477 * hide first sequence
479 testee.hideSequence(new SequenceI[] { al.getSequenceAt(0) });
480 assertEquals(2, al.getHeight());
481 assertEquals(0, ranges.getStartSeq());
482 assertEquals(1, ranges.getEndSeq());
485 * reveal hidden sequences above the first
487 testee.showSequence(0);
488 assertEquals(3, al.getHeight());
489 assertEquals(0, ranges.getStartSeq());
490 assertEquals(2, ranges.getEndSeq());
493 * hide first and third sequences
495 testee.hideSequence(new SequenceI[] { al.getSequenceAt(0),
496 al.getSequenceAt(2) });
497 assertEquals(1, al.getHeight());
498 assertEquals(0, ranges.getStartSeq());
499 assertEquals(0, ranges.getEndSeq());
502 * reveal all hidden sequences
504 testee.showAllHiddenSeqs();
505 assertEquals(3, al.getHeight());
506 assertEquals(0, ranges.getStartSeq());
507 assertEquals(2, ranges.getEndSeq());