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.Instance;
32 import jalview.bin.Jalview;
33 import jalview.datamodel.AlignedCodonFrame;
34 import jalview.datamodel.Alignment;
35 import jalview.datamodel.AlignmentAnnotation;
36 import jalview.datamodel.AlignmentI;
37 import jalview.datamodel.Annotation;
38 import jalview.datamodel.SearchResults;
39 import jalview.datamodel.SearchResultsI;
40 import jalview.datamodel.Sequence;
41 import jalview.datamodel.SequenceGroup;
42 import jalview.datamodel.SequenceI;
43 import jalview.io.DataSourceType;
44 import jalview.io.FileLoader;
45 import jalview.schemes.ClustalxColourScheme;
46 import jalview.schemes.ColourSchemeI;
47 import jalview.schemes.PIDColourScheme;
48 import jalview.structure.StructureSelectionManager;
49 import jalview.util.MapList;
50 import jalview.viewmodel.ViewportRanges;
52 import java.util.ArrayList;
53 import java.util.List;
55 import org.testng.Assert;
56 import org.testng.annotations.BeforeClass;
57 import org.testng.annotations.BeforeMethod;
58 import org.testng.annotations.Test;
60 public class AlignViewportTest
63 @BeforeClass(alwaysRun = true)
64 public void setUpJvOptionPane()
66 JvOptionPane.setInteractiveMode(false);
67 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
74 @BeforeClass(alwaysRun = true)
75 public static void setUpBeforeClass() throws Exception
77 Jalview.main(new String[] { "-nonews", "-props",
78 "test/jalview/testProps.jvprops" });
81 * remove any sequence mappings left lying around by other tests
83 StructureSelectionManager ssm = StructureSelectionManager
84 .getStructureSelectionManager(Instance.getDesktop());
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().LoadFileWaitTillLoaded(
111 ">Seq1\nCAGT\n", DataSourceType.PASTE);
113 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
114 AlignedCodonFrame acf1 = new AlignedCodonFrame();
115 acf1.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 1, 4 },
117 AlignedCodonFrame acf2 = new AlignedCodonFrame();
118 acf2.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 4, 1 },
121 List<AlignedCodonFrame> mappings = new ArrayList<>();
124 af1.getViewport().getAlignment().setCodonFrames(mappings);
125 af1.newView_actionPerformed(null);
128 * Verify that creating the alignment for the new View has registered the
131 StructureSelectionManager ssm = StructureSelectionManager
132 .getStructureSelectionManager(Instance.getDesktop());
133 List<AlignedCodonFrame> sequenceMappings = ssm.getSequenceMappings();
134 assertEquals(2, sequenceMappings.size());
135 assertTrue(sequenceMappings.contains(acf1));
136 assertTrue(sequenceMappings.contains(acf2));
139 * Close the second view. Verify that mappings are not removed as the first
140 * view still holds a reference to them.
142 af1.closeMenuItem_actionPerformed(false);
143 assertEquals(2, sequenceMappings.size());
144 assertTrue(sequenceMappings.contains(acf1));
145 assertTrue(sequenceMappings.contains(acf2));
149 * Test that a mapping is deregistered if no alignment holds a reference to it
151 @Test(groups = { "Functional" })
152 public void testDeregisterMapping_withNoReference()
154 Desktop d = Instance.getDesktop();
156 StructureSelectionManager ssm = StructureSelectionManager
157 .getStructureSelectionManager(Instance.getDesktop());
160 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
161 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
162 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
163 ">Seq2\nDGEL\n", DataSourceType.PASTE);
164 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
165 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
166 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
167 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
168 // need to be distinct
169 AlignedCodonFrame acf1 = new AlignedCodonFrame();
170 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
171 new int[] { 1, 12 }, 1, 3));
172 AlignedCodonFrame acf2 = new AlignedCodonFrame();
173 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
174 new int[] { 1, 12 }, 1, 3));
175 AlignedCodonFrame acf3 = new AlignedCodonFrame();
176 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
179 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
181 af1.getViewport().getAlignment().setCodonFrames(mappings1);
183 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
186 af2.getViewport().getAlignment().setCodonFrames(mappings2);
189 * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
192 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
193 assertEquals(0, ssmMappings.size());
194 ssm.registerMapping(acf1);
195 assertEquals(1, ssmMappings.size());
196 ssm.registerMapping(acf2);
197 assertEquals(2, ssmMappings.size());
198 ssm.registerMapping(acf3);
199 assertEquals(3, ssmMappings.size());
202 * Closing AlignFrame2 should remove its mappings from
203 * StructureSelectionManager, since AlignFrame1 has no reference to them
205 af2.closeMenuItem_actionPerformed(true);
206 assertEquals(1, ssmMappings.size());
207 assertTrue(ssmMappings.contains(acf1));
211 * Test that a mapping is not deregistered if another alignment holds a
214 @Test(groups = { "Functional" })
215 public void testDeregisterMapping_withReference()
217 Desktop d = Instance.getDesktop();
219 StructureSelectionManager ssm = StructureSelectionManager
220 .getStructureSelectionManager(Instance.getDesktop());
223 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
224 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
225 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
226 ">Seq2\nDGEL\n", DataSourceType.PASTE);
227 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
228 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
229 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
230 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
231 // need to be distinct
232 AlignedCodonFrame acf1 = new AlignedCodonFrame();
233 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
234 new int[] { 1, 12 }, 1, 3));
235 AlignedCodonFrame acf2 = new AlignedCodonFrame();
236 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
237 new int[] { 1, 12 }, 1, 3));
238 AlignedCodonFrame acf3 = new AlignedCodonFrame();
239 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
242 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
245 af1.getViewport().getAlignment().setCodonFrames(mappings1);
247 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
250 af2.getViewport().getAlignment().setCodonFrames(mappings2);
253 * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
256 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
257 assertEquals(0, ssmMappings.size());
258 ssm.registerMapping(acf1);
259 assertEquals(1, ssmMappings.size());
260 ssm.registerMapping(acf2);
261 assertEquals(2, ssmMappings.size());
262 ssm.registerMapping(acf3);
263 assertEquals(3, ssmMappings.size());
266 * Closing AlignFrame2 should remove mapping acf3 from
267 * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
270 af2.closeMenuItem_actionPerformed(true);
271 assertEquals(2, ssmMappings.size());
272 assertTrue(ssmMappings.contains(acf1));
273 assertTrue(ssmMappings.contains(acf2));
274 assertFalse(ssmMappings.contains(acf3));
278 * Test for JAL-1306 - conservation thread should run even when only Quality
279 * (and not Conservation) is enabled in Preferences
281 @Test(groups = { "Functional" }, timeOut=2000)
282 public void testUpdateConservation_qualityOnly()
284 Cache.setPropertyNoSave("SHOW_ANNOTATIONS",
285 Boolean.TRUE.toString());
286 Cache.setPropertyNoSave("SHOW_QUALITY",
287 Boolean.TRUE.toString());
288 Cache.setPropertyNoSave("SHOW_CONSERVATION",
289 Boolean.FALSE.toString());
290 Cache.setPropertyNoSave("SHOW_OCCUPANCY",
291 Boolean.FALSE.toString());
292 Cache.setPropertyNoSave("SHOW_IDENTITY",
293 Boolean.FALSE.toString());
294 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
295 "examples/uniref50.fa", DataSourceType.FILE);
298 * wait for Conservation thread to complete
300 AlignViewport viewport = af.getViewport();
303 while (viewport.getAlignmentConservationAnnotation() != null)
308 } catch (InterruptedException e)
313 AlignmentAnnotation[] anns = viewport.getAlignment()
314 .getAlignmentAnnotation();
315 assertNotNull("No annotations found", anns);
316 assertEquals("More than one annotation found", 1, anns.length);
317 assertTrue("Annotation is not Quality",
318 anns[0].description.startsWith("Alignment Quality"));
319 Annotation[] annotations = anns[0].annotations;
320 assertNotNull("Quality annotations are null", annotations);
321 assertNotNull("Quality in column 1 is null", annotations[0]);
322 assertTrue("No quality value in column 1", annotations[0].value > 10f);
325 @Test(groups = { "Functional" })
326 public void testSetGlobalColourScheme()
329 * test for JAL-2283: don't inadvertently turn on colour by conservation
331 Cache.setPropertyNoSave("DEFAULT_COLOUR_PROT", "None");
332 Cache.setPropertyNoSave("SHOW_CONSERVATION",
333 Boolean.TRUE.toString());
334 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
335 "examples/uniref50.fa", DataSourceType.FILE);
336 ColourSchemeI cs = new PIDColourScheme();
337 AlignViewport viewport = af.getViewport();
338 viewport.setGlobalColourScheme(cs);
339 assertFalse(viewport.getResidueShading()
340 .conservationApplied());
343 * JAL-3201 groups have their own ColourSchemeI instances
345 AlignmentI aln = viewport.getAlignment();
346 SequenceGroup sg1 = new SequenceGroup();
347 sg1.addSequence(aln.getSequenceAt(0), false);
348 sg1.addSequence(aln.getSequenceAt(2), false);
349 SequenceGroup sg2 = new SequenceGroup();
350 sg2.addSequence(aln.getSequenceAt(1), false);
351 sg2.addSequence(aln.getSequenceAt(3), false);
354 viewport.setColourAppliesToAllGroups(true);
355 viewport.setGlobalColourScheme(new ClustalxColourScheme());
356 ColourSchemeI cs0 = viewport.getGlobalColourScheme();
357 ColourSchemeI cs1 = sg1.getColourScheme();
358 ColourSchemeI cs2 = sg2.getColourScheme();
359 assertTrue(cs0 instanceof ClustalxColourScheme);
360 assertTrue(cs1 instanceof ClustalxColourScheme);
361 assertTrue(cs2 instanceof ClustalxColourScheme);
362 assertNotSame(cs0, cs1);
363 assertNotSame(cs0, cs2);
364 assertNotSame(cs1, cs2);
367 @Test(groups = { "Functional" })
368 public void testSetGetHasSearchResults()
370 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
371 "examples/uniref50.fa", DataSourceType.FILE);
372 SearchResultsI sr = new SearchResults();
373 SequenceI s1 = af.getViewport().getAlignment().getSequenceAt(0);
375 // create arbitrary range on first sequence
376 sr.addResult(s1, s1.getStart() + 10, s1.getStart() + 15);
379 af.getViewport().setSearchResults(sr);
381 assertTrue(af.getViewport().hasSearchResults());
383 assertEquals(sr, af.getViewport().getSearchResults());
385 // set(null) results in has -> false
387 af.getViewport().setSearchResults(null);
388 assertFalse(af.getViewport().hasSearchResults());
392 * Verify that setting the selection group has the side-effect of setting the
393 * context on the group, unless it already has one, but does not change
394 * whether the group is defined or not.
396 @Test(groups = { "Functional" })
397 public void testSetSelectionGroup()
399 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
400 "examples/uniref50.fa", DataSourceType.FILE);
401 AlignViewport av = af.getViewport();
402 SequenceGroup sg1 = new SequenceGroup();
403 SequenceGroup sg2 = new SequenceGroup();
404 SequenceGroup sg3 = new SequenceGroup();
406 av.setSelectionGroup(sg1);
407 assertSame(sg1.getContext(), av.getAlignment()); // context set
408 assertFalse(sg1.isDefined()); // group not defined
410 sg2.setContext(sg1, false);
411 av.setSelectionGroup(sg2);
412 assertFalse(sg2.isDefined()); // unchanged
413 assertSame(sg2.getContext(), sg1); // unchanged
415 // create a defined group
416 sg3.setContext(av.getAlignment(), true);
417 av.setSelectionGroup(sg3);
418 assertTrue(sg3.isDefined()); // unchanged
421 * Verify that setting/clearing SHOW_OCCUPANCY preference adds or omits occupancy row from viewport
423 @Test(groups = { "Functional" })
424 public void testShowOrDontShowOccupancy()
427 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.FALSE.toString());
428 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
429 "examples/uniref50.fa", DataSourceType.FILE);
430 AlignViewport av = af.getViewport();
431 Assert.assertNull(av.getAlignmentGapAnnotation(), "Preference did not disable occupancy row.");
433 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
438 Assert.assertEquals(c, 0, "Expected zero occupancy rows.");
441 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.TRUE.toString());
442 af = new FileLoader().LoadFileWaitTillLoaded(
443 "examples/uniref50.fa", DataSourceType.FILE);
444 av = af.getViewport();
445 Assert.assertNotNull(av.getAlignmentGapAnnotation(), "Preference did not enable occupancy row.");
447 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
448 null, av.getAlignmentGapAnnotation().label))
453 Assert.assertEquals(c, 1, "Expected to find one occupancy row.");
456 @Test(groups = { "Functional" })
457 public void testGetConsensusSeq()
464 * consensus expected to be A-C
466 String fasta = ">s1\nA-C\n>s2\nA-C\n>s3\nA-D\n>s4\n--D\n";
467 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(fasta,
468 DataSourceType.PASTE);
469 AlignViewport testme = af.getViewport();
470 SequenceI cons = testme.getConsensusSeq();
471 assertEquals("A-C", cons.getSequenceAsString());
474 @Test(groups = { "Functional" })
475 public void testHideRevealSequences()
477 ViewportRanges ranges = testee.getRanges();
478 assertEquals(3, al.getHeight());
479 assertEquals(0, ranges.getStartSeq());
480 assertEquals(2, ranges.getEndSeq());
483 * hide first sequence
485 testee.hideSequence(new SequenceI[] { al.getSequenceAt(0) });
486 assertEquals(2, al.getHeight());
487 assertEquals(0, ranges.getStartSeq());
488 assertEquals(1, ranges.getEndSeq());
491 * reveal hidden sequences above the first
493 testee.showSequence(0);
494 assertEquals(3, al.getHeight());
495 assertEquals(0, ranges.getStartSeq());
496 assertEquals(2, ranges.getEndSeq());
499 * hide first and third sequences
501 testee.hideSequence(new SequenceI[] { al.getSequenceAt(0),
502 al.getSequenceAt(2) });
503 assertEquals(1, al.getHeight());
504 assertEquals(0, ranges.getStartSeq());
505 assertEquals(0, ranges.getEndSeq());
508 * reveal all hidden sequences
510 testee.showAllHiddenSeqs();
511 assertEquals(3, al.getHeight());
512 assertEquals(0, ranges.getStartSeq());
513 assertEquals(2, ranges.getEndSeq());