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);
99 @Test(groups = { "Functional" })
100 public void testCollateForPdb()
102 // JBP: What behaviour is this supposed to test ?
104 * Set up sequence pdb ids
106 PDBEntry pdb1 = new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb");
107 PDBEntry pdb2 = new PDBEntry("2ABC", "C", Type.PDB, "2ABC.pdb");
108 PDBEntry pdb3 = new PDBEntry("3ABC", "D", Type.PDB, "3ABC.pdb");
111 * seq1 and seq3 refer to 1abcB, seq2 to 2abcC, none to 3abcD
113 al.getSequenceAt(0).getDatasetSequence()
114 .addPDBId(new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
115 al.getSequenceAt(2).getDatasetSequence()
116 .addPDBId(new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
117 al.getSequenceAt(1).getDatasetSequence()
118 .addPDBId(new PDBEntry("2ABC", "C", Type.PDB, "2ABC.pdb"));
120 * Add a second chain PDB xref to Seq2 - should not result in a duplicate in
123 al.getSequenceAt(1).getDatasetSequence()
124 .addPDBId(new PDBEntry("2ABC", "D", Type.PDB, "2ABC.pdb"));
126 * Seq3 refers to 3abc - this does not match 3ABC (as the code stands)
128 al.getSequenceAt(2).getDatasetSequence()
129 .addPDBId(new PDBEntry("3abc", "D", Type.PDB, "3ABC.pdb"));
132 * run method under test
134 SequenceI[][] seqs = testee.collateForPDB(new PDBEntry[] { pdb1, pdb2,
137 // seq1 and seq3 refer to PDBEntry[0]
138 assertEquals(2, seqs[0].length);
139 assertSame(al.getSequenceAt(0), seqs[0][0]);
140 assertSame(al.getSequenceAt(2), seqs[0][1]);
142 // seq2 refers to PDBEntry[1]
143 assertEquals(1, seqs[1].length);
144 assertSame(al.getSequenceAt(1), seqs[1][0]);
146 // no sequence refers to PDBEntry[2]
147 assertEquals(0, seqs[2].length);
151 * Test that a mapping is not deregistered when a second view is closed but
152 * the first still holds a reference to the mapping
154 @Test(groups = { "Functional" })
155 public void testDeregisterMapping_onCloseView()
158 * alignment with reference to mappings
160 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
161 ">Seq1\nCAGT\n", DataSourceType.PASTE);
163 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
164 AlignedCodonFrame acf1 = new AlignedCodonFrame();
165 acf1.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 1, 4 },
167 AlignedCodonFrame acf2 = new AlignedCodonFrame();
168 acf2.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 4, 1 },
171 List<AlignedCodonFrame> mappings = new ArrayList<>();
174 af1.getViewport().getAlignment().setCodonFrames(mappings);
175 af1.newView_actionPerformed(null);
178 * Verify that creating the alignment for the new View has registered the
181 StructureSelectionManager ssm = StructureSelectionManager
182 .getStructureSelectionManager(Desktop.instance);
183 List<AlignedCodonFrame> sequenceMappings = ssm.getSequenceMappings();
184 assertEquals(2, sequenceMappings.size());
185 assertTrue(sequenceMappings.contains(acf1));
186 assertTrue(sequenceMappings.contains(acf2));
189 * Close the second view. Verify that mappings are not removed as the first
190 * view still holds a reference to them.
192 af1.closeMenuItem_actionPerformed(false);
193 assertEquals(2, sequenceMappings.size());
194 assertTrue(sequenceMappings.contains(acf1));
195 assertTrue(sequenceMappings.contains(acf2));
199 * Test that a mapping is deregistered if no alignment holds a reference to it
201 @Test(groups = { "Functional" })
202 public void testDeregisterMapping_withNoReference()
204 Desktop d = Desktop.instance;
206 StructureSelectionManager ssm = StructureSelectionManager
207 .getStructureSelectionManager(Desktop.instance);
210 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
211 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
212 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
213 ">Seq2\nDGEL\n", DataSourceType.PASTE);
214 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
215 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
216 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
217 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
218 // need to be distinct
219 AlignedCodonFrame acf1 = new AlignedCodonFrame();
220 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
221 new int[] { 1, 12 }, 1, 3));
222 AlignedCodonFrame acf2 = new AlignedCodonFrame();
223 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
224 new int[] { 1, 12 }, 1, 3));
225 AlignedCodonFrame acf3 = new AlignedCodonFrame();
226 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
229 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
231 af1.getViewport().getAlignment().setCodonFrames(mappings1);
233 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
236 af2.getViewport().getAlignment().setCodonFrames(mappings2);
239 * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
242 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
243 assertEquals(0, ssmMappings.size());
244 ssm.registerMapping(acf1);
245 assertEquals(1, ssmMappings.size());
246 ssm.registerMapping(acf2);
247 assertEquals(2, ssmMappings.size());
248 ssm.registerMapping(acf3);
249 assertEquals(3, ssmMappings.size());
252 * Closing AlignFrame2 should remove its mappings from
253 * StructureSelectionManager, since AlignFrame1 has no reference to them
255 af2.closeMenuItem_actionPerformed(true);
256 assertEquals(1, ssmMappings.size());
257 assertTrue(ssmMappings.contains(acf1));
261 * Test that a mapping is not deregistered if another alignment holds a
264 @Test(groups = { "Functional" })
265 public void testDeregisterMapping_withReference()
267 Desktop d = Desktop.instance;
269 StructureSelectionManager ssm = StructureSelectionManager
270 .getStructureSelectionManager(Desktop.instance);
273 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
274 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
275 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
276 ">Seq2\nDGEL\n", DataSourceType.PASTE);
277 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
278 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
279 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
280 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
281 // need to be distinct
282 AlignedCodonFrame acf1 = new AlignedCodonFrame();
283 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
284 new int[] { 1, 12 }, 1, 3));
285 AlignedCodonFrame acf2 = new AlignedCodonFrame();
286 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
287 new int[] { 1, 12 }, 1, 3));
288 AlignedCodonFrame acf3 = new AlignedCodonFrame();
289 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
292 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
295 af1.getViewport().getAlignment().setCodonFrames(mappings1);
297 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
300 af2.getViewport().getAlignment().setCodonFrames(mappings2);
303 * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
306 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
307 assertEquals(0, ssmMappings.size());
308 ssm.registerMapping(acf1);
309 assertEquals(1, ssmMappings.size());
310 ssm.registerMapping(acf2);
311 assertEquals(2, ssmMappings.size());
312 ssm.registerMapping(acf3);
313 assertEquals(3, ssmMappings.size());
316 * Closing AlignFrame2 should remove mapping acf3 from
317 * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
320 af2.closeMenuItem_actionPerformed(true);
321 assertEquals(2, ssmMappings.size());
322 assertTrue(ssmMappings.contains(acf1));
323 assertTrue(ssmMappings.contains(acf2));
324 assertFalse(ssmMappings.contains(acf3));
328 * Test for JAL-1306 - conservation thread should run even when only Quality
329 * (and not Conservation) is enabled in Preferences
331 @Test(groups = { "Functional" })
332 public void testUpdateConservation_qualityOnly()
334 Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS",
335 Boolean.TRUE.toString());
336 Cache.applicationProperties.setProperty("SHOW_QUALITY",
337 Boolean.TRUE.toString());
338 Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
339 Boolean.FALSE.toString());
340 Cache.applicationProperties.setProperty("SHOW_OCCUPANCY",
341 Boolean.FALSE.toString());
342 Cache.applicationProperties.setProperty("SHOW_IDENTITY",
343 Boolean.FALSE.toString());
344 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
345 "examples/uniref50.fa", DataSourceType.FILE);
346 AlignmentAnnotation[] anns = af.viewport.getAlignment()
347 .getAlignmentAnnotation();
348 assertNotNull("No annotations found", anns);
349 assertEquals("More than one annotation found", 1, anns.length);
350 assertTrue("Annotation is not Quality",
351 anns[0].description.startsWith("Alignment Quality"));
352 Annotation[] annotations = anns[0].annotations;
353 assertNotNull("Quality annotations are null", annotations);
354 assertNotNull("Quality in column 1 is null", annotations[0]);
355 assertTrue("No quality value in column 1", annotations[0].value > 10f);
358 @Test(groups = { "Functional" })
359 public void testSetGlobalColourScheme()
362 * test for JAL-2283: don't inadvertently turn on colour by conservation
364 Cache.applicationProperties.setProperty("DEFAULT_COLOUR_PROT", "None");
365 Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
366 Boolean.TRUE.toString());
367 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
368 "examples/uniref50.fa", DataSourceType.FILE);
369 ColourSchemeI cs = new PIDColourScheme();
370 af.getViewport().setGlobalColourScheme(cs);
371 assertFalse(af.getViewport().getResidueShading()
372 .conservationApplied());
375 @Test(groups = { "Functional" })
376 public void testSetGetHasSearchResults()
378 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
379 "examples/uniref50.fa", DataSourceType.FILE);
380 SearchResultsI sr = new SearchResults();
381 SequenceI s1 = af.getViewport().getAlignment().getSequenceAt(0);
383 // create arbitrary range on first sequence
384 sr.addResult(s1, s1.getStart() + 10, s1.getStart() + 15);
387 af.getViewport().setSearchResults(sr);
389 assertTrue(af.getViewport().hasSearchResults());
391 assertEquals(sr, af.getViewport().getSearchResults());
393 // set(null) results in has -> false
395 af.getViewport().setSearchResults(null);
396 assertFalse(af.getViewport().hasSearchResults());
400 * Verify that setting the selection group has the side-effect of setting the
401 * context on the group, unless it already has one, but does not change
402 * whether the group is defined or not.
404 @Test(groups = { "Functional" })
405 public void testSetSelectionGroup()
407 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
408 "examples/uniref50.fa", DataSourceType.FILE);
409 AlignViewport av = af.getViewport();
410 SequenceGroup sg1 = new SequenceGroup();
411 SequenceGroup sg2 = new SequenceGroup();
412 SequenceGroup sg3 = new SequenceGroup();
414 av.setSelectionGroup(sg1);
415 assertSame(sg1.getContext(), av.getAlignment()); // context set
416 assertFalse(sg1.isDefined()); // group not defined
418 sg2.setContext(sg1, false);
419 av.setSelectionGroup(sg2);
420 assertFalse(sg2.isDefined()); // unchanged
421 assertSame(sg2.getContext(), sg1); // unchanged
423 // create a defined group
424 sg3.setContext(av.getAlignment(), true);
425 av.setSelectionGroup(sg3);
426 assertTrue(sg3.isDefined()); // unchanged
429 * Verify that setting/clearing SHOW_OCCUPANCY preference adds or omits occupancy row from viewport
431 @Test(groups = { "Functional" })
432 public void testShowOrDontShowOccupancy()
435 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.FALSE.toString());
436 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
437 "examples/uniref50.fa", DataSourceType.FILE);
438 AlignViewport av = af.getViewport();
439 Assert.assertNull(av.getAlignmentGapAnnotation(), "Preference did not disable occupancy row.");
441 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
446 Assert.assertEquals(c, 0, "Expected zero occupancy rows.");
449 jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.TRUE.toString());
450 af = new FileLoader().LoadFileWaitTillLoaded(
451 "examples/uniref50.fa", DataSourceType.FILE);
452 av = af.getViewport();
453 Assert.assertNotNull(av.getAlignmentGapAnnotation(), "Preference did not enable occupancy row.");
455 for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
456 null, av.getAlignmentGapAnnotation().label))
461 Assert.assertEquals(c, 1, "Expected to find one occupancy row.");
464 @Test(groups = { "Functional" })
465 public void testGetConsensusSeq()
472 * consensus expected to be A-C
474 String fasta = ">s1\nA-C\n>s2\nA-C\n>s3\nA-D\n>s4\n--D\n";
475 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(fasta,
476 DataSourceType.PASTE);
477 AlignViewport testme = af.getViewport();
478 SequenceI cons = testme.getConsensusSeq();
479 assertEquals("A-C", cons.getSequenceAsString());
482 @Test(groups = { "Functional" })
483 public void testHideRevealSequences()
485 ViewportRanges ranges = testee.getRanges();
486 assertEquals(3, al.getHeight());
487 assertEquals(0, ranges.getStartSeq());
488 assertEquals(2, ranges.getEndSeq());
491 * hide first sequence
493 testee.hideSequence(new SequenceI[] { al.getSequenceAt(0) });
494 assertEquals(2, al.getHeight());
495 assertEquals(0, ranges.getStartSeq());
496 assertEquals(1, ranges.getEndSeq());
499 * reveal hidden sequences above the first
501 testee.showSequence(0);
502 assertEquals(3, al.getHeight());
503 assertEquals(0, ranges.getStartSeq());
504 assertEquals(2, ranges.getEndSeq());
507 * hide first and third sequences
509 testee.hideSequence(new SequenceI[] { al.getSequenceAt(0),
510 al.getSequenceAt(2) });
511 assertEquals(1, al.getHeight());
512 assertEquals(0, ranges.getStartSeq());
513 assertEquals(0, ranges.getEndSeq());
516 * reveal all hidden sequences
518 testee.showAllHiddenSeqs();
519 assertEquals(3, al.getHeight());
520 assertEquals(0, ranges.getStartSeq());
521 assertEquals(2, ranges.getEndSeq());