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.getInstance().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.getInstance()
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.getInstance()
153 .getStructureSelectionManager();
156 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
157 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
158 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
159 ">Seq2\nDGEL\n", DataSourceType.PASTE);
160 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
161 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
162 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
163 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
164 // need to be distinct
165 AlignedCodonFrame acf1 = new AlignedCodonFrame();
166 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
167 new int[] { 1, 12 }, 1, 3));
168 AlignedCodonFrame acf2 = new AlignedCodonFrame();
169 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
170 new int[] { 1, 12 }, 1, 3));
171 AlignedCodonFrame acf3 = new AlignedCodonFrame();
172 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
175 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
177 af1.getViewport().getAlignment().setCodonFrames(mappings1);
179 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
182 af2.getViewport().getAlignment().setCodonFrames(mappings2);
185 * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
188 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
189 assertEquals(0, ssmMappings.size());
190 ssm.registerMapping(acf1);
191 assertEquals(1, ssmMappings.size());
192 ssm.registerMapping(acf2);
193 assertEquals(2, ssmMappings.size());
194 ssm.registerMapping(acf3);
195 assertEquals(3, ssmMappings.size());
198 * Closing AlignFrame2 should remove its mappings from
199 * StructureSelectionManager, since AlignFrame1 has no reference to them
201 af2.closeMenuItem_actionPerformed(true);
202 assertEquals(1, ssmMappings.size());
203 assertTrue(ssmMappings.contains(acf1));
207 * Test that a mapping is not deregistered if another alignment holds a
210 @Test(groups = { "Functional" })
211 public void testDeregisterMapping_withReference()
213 Desktop d = Desktop.getInstance();
215 StructureSelectionManager ssm = Desktop.getInstance()
216 .getStructureSelectionManager();
219 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
220 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
221 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
222 ">Seq2\nDGEL\n", DataSourceType.PASTE);
223 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
224 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
225 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
226 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
227 // need to be distinct
228 AlignedCodonFrame acf1 = new AlignedCodonFrame();
229 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
230 new int[] { 1, 12 }, 1, 3));
231 AlignedCodonFrame acf2 = new AlignedCodonFrame();
232 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
233 new int[] { 1, 12 }, 1, 3));
234 AlignedCodonFrame acf3 = new AlignedCodonFrame();
235 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
238 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
241 af1.getViewport().getAlignment().setCodonFrames(mappings1);
243 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
246 af2.getViewport().getAlignment().setCodonFrames(mappings2);
249 * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
252 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
253 assertEquals(0, ssmMappings.size());
254 ssm.registerMapping(acf1);
255 assertEquals(1, ssmMappings.size());
256 ssm.registerMapping(acf2);
257 assertEquals(2, ssmMappings.size());
258 ssm.registerMapping(acf3);
259 assertEquals(3, ssmMappings.size());
262 * Closing AlignFrame2 should remove mapping acf3 from
263 * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
266 af2.closeMenuItem_actionPerformed(true);
267 assertEquals(2, ssmMappings.size());
268 assertTrue(ssmMappings.contains(acf1));
269 assertTrue(ssmMappings.contains(acf2));
270 assertFalse(ssmMappings.contains(acf3));
274 * Test for JAL-1306 - conservation thread should run even when only Quality
275 * (and not Conservation) is enabled in Preferences
277 @Test(groups = { "Functional" }, timeOut=2000)
278 public void testUpdateConservation_qualityOnly()
280 Cache.setPropertyNoSave("SHOW_ANNOTATIONS",
281 Boolean.TRUE.toString());
282 Cache.setPropertyNoSave("SHOW_QUALITY",
283 Boolean.TRUE.toString());
284 Cache.setPropertyNoSave("SHOW_CONSERVATION",
285 Boolean.FALSE.toString());
286 Cache.setPropertyNoSave("SHOW_OCCUPANCY",
287 Boolean.FALSE.toString());
288 Cache.setPropertyNoSave("SHOW_IDENTITY",
289 Boolean.FALSE.toString());
290 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
291 "examples/uniref50.fa", DataSourceType.FILE);
294 * wait for Conservation thread to complete
296 AlignViewport viewport = af.getViewport();
299 while (viewport.getAlignmentConservationAnnotation() != null)
304 } catch (InterruptedException e)
309 AlignmentAnnotation[] anns = viewport.getAlignment()
310 .getAlignmentAnnotation();
311 assertNotNull("No annotations found", anns);
312 assertEquals("More than one annotation found", 1, anns.length);
313 assertTrue("Annotation is not Quality",
314 anns[0].description.startsWith("Alignment Quality"));
315 Annotation[] annotations = anns[0].annotations;
316 assertNotNull("Quality annotations are null", annotations);
317 assertNotNull("Quality in column 1 is null", annotations[0]);
318 assertTrue("No quality value in column 1", annotations[0].value > 10f);
321 @Test(groups = { "Functional" })
322 public void testSetGlobalColourScheme()
325 * test for JAL-2283: don't inadvertently turn on colour by conservation
327 Cache.setPropertyNoSave("DEFAULT_COLOUR_PROT", "None");
328 Cache.setPropertyNoSave("SHOW_CONSERVATION",
329 Boolean.TRUE.toString());
330 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
331 "examples/uniref50.fa", DataSourceType.FILE);
332 ColourSchemeI cs = new PIDColourScheme();
333 AlignViewport viewport = af.getViewport();
334 viewport.setGlobalColourScheme(cs);
335 assertFalse(viewport.getResidueShading()
336 .conservationApplied());
339 * JAL-3201 groups have their own ColourSchemeI instances
341 AlignmentI aln = viewport.getAlignment();
342 SequenceGroup sg1 = new SequenceGroup();
343 sg1.addSequence(aln.getSequenceAt(0), false);
344 sg1.addSequence(aln.getSequenceAt(2), false);
345 SequenceGroup sg2 = new SequenceGroup();
346 sg2.addSequence(aln.getSequenceAt(1), false);
347 sg2.addSequence(aln.getSequenceAt(3), false);
350 viewport.setColourAppliesToAllGroups(true);
351 viewport.setGlobalColourScheme(new ClustalxColourScheme());
352 ColourSchemeI cs0 = viewport.getGlobalColourScheme();
353 ColourSchemeI cs1 = sg1.getColourScheme();
354 ColourSchemeI cs2 = sg2.getColourScheme();
355 assertTrue(cs0 instanceof ClustalxColourScheme);
356 assertTrue(cs1 instanceof ClustalxColourScheme);
357 assertTrue(cs2 instanceof ClustalxColourScheme);
358 assertNotSame(cs0, cs1);
359 assertNotSame(cs0, cs2);
360 assertNotSame(cs1, cs2);
363 @Test(groups = { "Functional" })
364 public void testSetGetHasSearchResults()
366 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
367 "examples/uniref50.fa", DataSourceType.FILE);
368 SearchResultsI sr = new SearchResults();
369 SequenceI s1 = af.getViewport().getAlignment().getSequenceAt(0);
371 // create arbitrary range on first sequence
372 sr.addResult(s1, s1.getStart() + 10, s1.getStart() + 15);
375 af.getViewport().setSearchResults(sr);
377 assertTrue(af.getViewport().hasSearchResults());
379 assertEquals(sr, af.getViewport().getSearchResults());
381 // set(null) results in has -> false
383 af.getViewport().setSearchResults(null);
384 assertFalse(af.getViewport().hasSearchResults());
388 * Verify that setting the selection group has the side-effect of setting the
389 * context on the group, unless it already has one, but does not change
390 * whether the group is defined or not.
392 @Test(groups = { "Functional" })
393 public void testSetSelectionGroup()
395 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
396 "examples/uniref50.fa", DataSourceType.FILE);
397 AlignViewport av = af.getViewport();
398 SequenceGroup sg1 = new SequenceGroup();
399 SequenceGroup sg2 = new SequenceGroup();
400 SequenceGroup sg3 = new SequenceGroup();
402 av.setSelectionGroup(sg1);
403 assertSame(sg1.getContext(), av.getAlignment()); // context set
404 assertFalse(sg1.isDefined()); // group not defined
406 sg2.setContext(sg1, false);
407 av.setSelectionGroup(sg2);
408 assertFalse(sg2.isDefined()); // unchanged
409 assertSame(sg2.getContext(), sg1); // unchanged
411 // create a defined group
412 sg3.setContext(av.getAlignment(), true);
413 av.setSelectionGroup(sg3);
414 assertTrue(sg3.isDefined()); // unchanged
417 * Verify that setting/clearing SHOW_OCCUPANCY preference adds or omits occupancy row from viewport
419 @Test(groups = { "Functional" })
420 public void testShowOrDontShowOccupancy()
423 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.FALSE.toString());
424 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
425 "examples/uniref50.fa", DataSourceType.FILE);
426 AlignViewport av = af.getViewport();
427 Assert.assertNull(av.getAlignmentGapAnnotation(), "Preference did not disable occupancy row.");
429 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
434 Assert.assertEquals(c, 0, "Expected zero occupancy rows.");
437 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.TRUE.toString());
438 af = new FileLoader().LoadFileWaitTillLoaded(
439 "examples/uniref50.fa", DataSourceType.FILE);
440 av = af.getViewport();
441 Assert.assertNotNull(av.getAlignmentGapAnnotation(), "Preference did not enable occupancy row.");
443 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
444 null, av.getAlignmentGapAnnotation().label))
448 Assert.assertEquals(c, 1, "Expected to find one occupancy row.");
451 @Test(groups = { "Functional" })
452 public void testGetConsensusSeq()
459 * consensus expected to be A-C
461 String fasta = ">s1\nA-C\n>s2\nA-C\n>s3\nA-D\n>s4\n--D\n";
462 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(fasta,
463 DataSourceType.PASTE);
464 AlignViewport testme = af.getViewport();
465 SequenceI cons = testme.getConsensusSeq();
466 assertEquals("A-C", cons.getSequenceAsString());
469 @Test(groups = { "Functional" })
470 public void testHideRevealSequences()
472 ViewportRanges ranges = testee.getRanges();
473 assertEquals(3, al.getHeight());
474 assertEquals(0, ranges.getStartSeq());
475 assertEquals(2, ranges.getEndSeq());
478 * hide first sequence
480 testee.hideSequence(new SequenceI[] { al.getSequenceAt(0) });
481 assertEquals(2, al.getHeight());
482 assertEquals(0, ranges.getStartSeq());
483 assertEquals(1, ranges.getEndSeq());
486 * reveal hidden sequences above the first
488 testee.showSequence(0);
489 assertEquals(3, al.getHeight());
490 assertEquals(0, ranges.getStartSeq());
491 assertEquals(2, ranges.getEndSeq());
494 * hide first and third sequences
496 testee.hideSequence(new SequenceI[] { al.getSequenceAt(0),
497 al.getSequenceAt(2) });
498 assertEquals(1, al.getHeight());
499 assertEquals(0, ranges.getStartSeq());
500 assertEquals(0, ranges.getEndSeq());
503 * reveal all hidden sequences
505 testee.showAllHiddenSeqs();
506 assertEquals(3, al.getHeight());
507 assertEquals(0, ranges.getStartSeq());
508 assertEquals(2, ranges.getEndSeq());