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.assertSame;
27 import static org.testng.AssertJUnit.assertTrue;
29 import jalview.bin.Cache;
30 import jalview.bin.Jalview;
31 import jalview.datamodel.AlignedCodonFrame;
32 import jalview.datamodel.Alignment;
33 import jalview.datamodel.AlignmentAnnotation;
34 import jalview.datamodel.AlignmentI;
35 import jalview.datamodel.Annotation;
36 import jalview.datamodel.PDBEntry;
37 import jalview.datamodel.PDBEntry.Type;
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.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 StructureSelectionManager ssm = StructureSelectionManager
83 .getStructureSelectionManager(Desktop.instance);
87 @BeforeMethod(alwaysRun = true)
90 SequenceI seq1 = new Sequence("Seq1", "ABC");
91 SequenceI seq2 = new Sequence("Seq2", "ABC");
92 SequenceI seq3 = new Sequence("Seq3", "ABC");
93 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3 };
94 al = new Alignment(seqs);
96 testee = new AlignViewport(al);
100 * Test that a mapping is not deregistered when a second view is closed but
101 * the first still holds a reference to the mapping
103 @Test(groups = { "Functional" })
104 public void testDeregisterMapping_onCloseView()
107 * alignment with reference to mappings
109 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
110 ">Seq1\nCAGT\n", DataSourceType.PASTE);
112 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
113 AlignedCodonFrame acf1 = new AlignedCodonFrame();
114 acf1.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 1, 4 },
116 AlignedCodonFrame acf2 = new AlignedCodonFrame();
117 acf2.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 4, 1 },
120 List<AlignedCodonFrame> mappings = new ArrayList<>();
123 af1.getViewport().getAlignment().setCodonFrames(mappings);
124 af1.newView_actionPerformed(null);
127 * Verify that creating the alignment for the new View has registered the
130 StructureSelectionManager ssm = StructureSelectionManager
131 .getStructureSelectionManager(Desktop.instance);
132 List<AlignedCodonFrame> sequenceMappings = ssm.getSequenceMappings();
133 assertEquals(2, sequenceMappings.size());
134 assertTrue(sequenceMappings.contains(acf1));
135 assertTrue(sequenceMappings.contains(acf2));
138 * Close the second view. Verify that mappings are not removed as the first
139 * view still holds a reference to them.
141 af1.closeMenuItem_actionPerformed(false);
142 assertEquals(2, sequenceMappings.size());
143 assertTrue(sequenceMappings.contains(acf1));
144 assertTrue(sequenceMappings.contains(acf2));
148 * Test that a mapping is deregistered if no alignment holds a reference to it
150 @Test(groups = { "Functional" })
151 public void testDeregisterMapping_withNoReference()
153 Desktop d = Desktop.instance;
155 StructureSelectionManager ssm = StructureSelectionManager
156 .getStructureSelectionManager(Desktop.instance);
159 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
160 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
161 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
162 ">Seq2\nDGEL\n", DataSourceType.PASTE);
163 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
164 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
165 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
166 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
167 // need to be distinct
168 AlignedCodonFrame acf1 = new AlignedCodonFrame();
169 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
170 new int[] { 1, 12 }, 1, 3));
171 AlignedCodonFrame acf2 = new AlignedCodonFrame();
172 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
173 new int[] { 1, 12 }, 1, 3));
174 AlignedCodonFrame acf3 = new AlignedCodonFrame();
175 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
178 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
180 af1.getViewport().getAlignment().setCodonFrames(mappings1);
182 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
185 af2.getViewport().getAlignment().setCodonFrames(mappings2);
188 * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
191 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
192 assertEquals(0, ssmMappings.size());
193 ssm.registerMapping(acf1);
194 assertEquals(1, ssmMappings.size());
195 ssm.registerMapping(acf2);
196 assertEquals(2, ssmMappings.size());
197 ssm.registerMapping(acf3);
198 assertEquals(3, ssmMappings.size());
201 * Closing AlignFrame2 should remove its mappings from
202 * StructureSelectionManager, since AlignFrame1 has no reference to them
204 af2.closeMenuItem_actionPerformed(true);
205 assertEquals(1, ssmMappings.size());
206 assertTrue(ssmMappings.contains(acf1));
210 * Test that a mapping is not deregistered if another alignment holds a
213 @Test(groups = { "Functional" })
214 public void testDeregisterMapping_withReference()
216 Desktop d = Desktop.instance;
218 StructureSelectionManager ssm = StructureSelectionManager
219 .getStructureSelectionManager(Desktop.instance);
222 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
223 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
224 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
225 ">Seq2\nDGEL\n", DataSourceType.PASTE);
226 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
227 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
228 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
229 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
230 // need to be distinct
231 AlignedCodonFrame acf1 = new AlignedCodonFrame();
232 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
233 new int[] { 1, 12 }, 1, 3));
234 AlignedCodonFrame acf2 = new AlignedCodonFrame();
235 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
236 new int[] { 1, 12 }, 1, 3));
237 AlignedCodonFrame acf3 = new AlignedCodonFrame();
238 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
241 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
244 af1.getViewport().getAlignment().setCodonFrames(mappings1);
246 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
249 af2.getViewport().getAlignment().setCodonFrames(mappings2);
252 * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
255 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
256 assertEquals(0, ssmMappings.size());
257 ssm.registerMapping(acf1);
258 assertEquals(1, ssmMappings.size());
259 ssm.registerMapping(acf2);
260 assertEquals(2, ssmMappings.size());
261 ssm.registerMapping(acf3);
262 assertEquals(3, ssmMappings.size());
265 * Closing AlignFrame2 should remove mapping acf3 from
266 * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
269 af2.closeMenuItem_actionPerformed(true);
270 assertEquals(2, ssmMappings.size());
271 assertTrue(ssmMappings.contains(acf1));
272 assertTrue(ssmMappings.contains(acf2));
273 assertFalse(ssmMappings.contains(acf3));
277 * Test for JAL-1306 - conservation thread should run even when only Quality
278 * (and not Conservation) is enabled in Preferences
280 @Test(groups = { "Functional" }, timeOut=2000)
281 public void testUpdateConservation_qualityOnly()
283 Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS",
284 Boolean.TRUE.toString());
285 Cache.applicationProperties.setProperty("SHOW_QUALITY",
286 Boolean.TRUE.toString());
287 Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
288 Boolean.FALSE.toString());
289 Cache.applicationProperties.setProperty("SHOW_OCCUPANCY",
290 Boolean.FALSE.toString());
291 Cache.applicationProperties.setProperty("SHOW_IDENTITY",
292 Boolean.FALSE.toString());
293 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
294 "examples/uniref50.fa", DataSourceType.FILE);
297 * wait for Conservation thread to complete
299 AlignViewport viewport = af.getViewport();
302 while (viewport.getAlignmentConservationAnnotation() != null)
307 } catch (InterruptedException e)
312 AlignmentAnnotation[] anns = viewport.getAlignment()
313 .getAlignmentAnnotation();
314 assertNotNull("No annotations found", anns);
315 assertEquals("More than one annotation found", 1, anns.length);
316 assertTrue("Annotation is not Quality",
317 anns[0].description.startsWith("Alignment Quality"));
318 Annotation[] annotations = anns[0].annotations;
319 assertNotNull("Quality annotations are null", annotations);
320 assertNotNull("Quality in column 1 is null", annotations[0]);
321 assertTrue("No quality value in column 1", annotations[0].value > 10f);
324 @Test(groups = { "Functional" })
325 public void testSetGlobalColourScheme()
328 * test for JAL-2283: don't inadvertently turn on colour by conservation
330 Cache.applicationProperties.setProperty("DEFAULT_COLOUR_PROT", "None");
331 Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
332 Boolean.TRUE.toString());
333 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
334 "examples/uniref50.fa", DataSourceType.FILE);
335 ColourSchemeI cs = new PIDColourScheme();
336 af.getViewport().setGlobalColourScheme(cs);
337 assertFalse(af.getViewport().getResidueShading()
338 .conservationApplied());
341 @Test(groups = { "Functional" })
342 public void testSetGetHasSearchResults()
344 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
345 "examples/uniref50.fa", DataSourceType.FILE);
346 SearchResultsI sr = new SearchResults();
347 SequenceI s1 = af.getViewport().getAlignment().getSequenceAt(0);
349 // create arbitrary range on first sequence
350 sr.addResult(s1, s1.getStart() + 10, s1.getStart() + 15);
353 af.getViewport().setSearchResults(sr);
355 assertTrue(af.getViewport().hasSearchResults());
357 assertEquals(sr, af.getViewport().getSearchResults());
359 // set(null) results in has -> false
361 af.getViewport().setSearchResults(null);
362 assertFalse(af.getViewport().hasSearchResults());
366 * Verify that setting the selection group has the side-effect of setting the
367 * context on the group, unless it already has one, but does not change
368 * whether the group is defined or not.
370 @Test(groups = { "Functional" })
371 public void testSetSelectionGroup()
373 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
374 "examples/uniref50.fa", DataSourceType.FILE);
375 AlignViewport av = af.getViewport();
376 SequenceGroup sg1 = new SequenceGroup();
377 SequenceGroup sg2 = new SequenceGroup();
378 SequenceGroup sg3 = new SequenceGroup();
380 av.setSelectionGroup(sg1);
381 assertSame(sg1.getContext(), av.getAlignment()); // context set
382 assertFalse(sg1.isDefined()); // group not defined
384 sg2.setContext(sg1, false);
385 av.setSelectionGroup(sg2);
386 assertFalse(sg2.isDefined()); // unchanged
387 assertSame(sg2.getContext(), sg1); // unchanged
389 // create a defined group
390 sg3.setContext(av.getAlignment(), true);
391 av.setSelectionGroup(sg3);
392 assertTrue(sg3.isDefined()); // unchanged
395 * Verify that setting/clearing SHOW_OCCUPANCY preference adds or omits occupancy row from viewport
397 @Test(groups = { "Functional" })
398 public void testShowOrDontShowOccupancy()
401 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.FALSE.toString());
402 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
403 "examples/uniref50.fa", DataSourceType.FILE);
404 AlignViewport av = af.getViewport();
405 Assert.assertNull(av.getAlignmentGapAnnotation(), "Preference did not disable occupancy row.");
407 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
412 Assert.assertEquals(c, 0, "Expected zero occupancy rows.");
415 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.TRUE.toString());
416 af = new FileLoader().LoadFileWaitTillLoaded(
417 "examples/uniref50.fa", DataSourceType.FILE);
418 av = af.getViewport();
419 Assert.assertNotNull(av.getAlignmentGapAnnotation(), "Preference did not enable occupancy row.");
421 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
422 null, av.getAlignmentGapAnnotation().label))
427 Assert.assertEquals(c, 1, "Expected to find one occupancy row.");
430 @Test(groups = { "Functional" })
431 public void testGetConsensusSeq()
438 * consensus expected to be A-C
440 String fasta = ">s1\nA-C\n>s2\nA-C\n>s3\nA-D\n>s4\n--D\n";
441 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(fasta,
442 DataSourceType.PASTE);
443 AlignViewport testme = af.getViewport();
444 SequenceI cons = testme.getConsensusSeq();
445 assertEquals("A-C", cons.getSequenceAsString());
448 @Test(groups = { "Functional" })
449 public void testHideRevealSequences()
451 ViewportRanges ranges = testee.getRanges();
452 assertEquals(3, al.getHeight());
453 assertEquals(0, ranges.getStartSeq());
454 assertEquals(2, ranges.getEndSeq());
457 * hide first sequence
459 testee.hideSequence(new SequenceI[] { al.getSequenceAt(0) });
460 assertEquals(2, al.getHeight());
461 assertEquals(0, ranges.getStartSeq());
462 assertEquals(1, ranges.getEndSeq());
465 * reveal hidden sequences above the first
467 testee.showSequence(0);
468 assertEquals(3, al.getHeight());
469 assertEquals(0, ranges.getStartSeq());
470 assertEquals(2, ranges.getEndSeq());
473 * hide first and third sequences
475 testee.hideSequence(new SequenceI[] { al.getSequenceAt(0),
476 al.getSequenceAt(2) });
477 assertEquals(1, al.getHeight());
478 assertEquals(0, ranges.getStartSeq());
479 assertEquals(0, ranges.getEndSeq());
482 * reveal all hidden sequences
484 testee.showAllHiddenSeqs();
485 assertEquals(3, al.getHeight());
486 assertEquals(0, ranges.getStartSeq());
487 assertEquals(2, ranges.getEndSeq());