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 @BeforeMethod(alwaysRun = true)
83 SequenceI seq1 = new Sequence("Seq1", "ABC");
84 SequenceI seq2 = new Sequence("Seq2", "ABC");
85 SequenceI seq3 = new Sequence("Seq3", "ABC");
86 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3 };
87 al = new Alignment(seqs);
89 testee = new AlignViewport(al);
92 @Test(groups = { "Functional" })
93 public void testCollateForPdb()
95 // JBP: What behaviour is this supposed to test ?
97 * Set up sequence pdb ids
99 PDBEntry pdb1 = new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb");
100 PDBEntry pdb2 = new PDBEntry("2ABC", "C", Type.PDB, "2ABC.pdb");
101 PDBEntry pdb3 = new PDBEntry("3ABC", "D", Type.PDB, "3ABC.pdb");
104 * seq1 and seq3 refer to 1abcB, seq2 to 2abcC, none to 3abcD
106 al.getSequenceAt(0).getDatasetSequence()
107 .addPDBId(new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
108 al.getSequenceAt(2).getDatasetSequence()
109 .addPDBId(new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
110 al.getSequenceAt(1).getDatasetSequence()
111 .addPDBId(new PDBEntry("2ABC", "C", Type.PDB, "2ABC.pdb"));
113 * Add a second chain PDB xref to Seq2 - should not result in a duplicate in
116 al.getSequenceAt(1).getDatasetSequence()
117 .addPDBId(new PDBEntry("2ABC", "D", Type.PDB, "2ABC.pdb"));
119 * Seq3 refers to 3abc - this does not match 3ABC (as the code stands)
121 al.getSequenceAt(2).getDatasetSequence()
122 .addPDBId(new PDBEntry("3abc", "D", Type.PDB, "3ABC.pdb"));
125 * run method under test
127 SequenceI[][] seqs = testee.collateForPDB(new PDBEntry[] { pdb1, pdb2,
130 // seq1 and seq3 refer to PDBEntry[0]
131 assertEquals(2, seqs[0].length);
132 assertSame(al.getSequenceAt(0), seqs[0][0]);
133 assertSame(al.getSequenceAt(2), seqs[0][1]);
135 // seq2 refers to PDBEntry[1]
136 assertEquals(1, seqs[1].length);
137 assertSame(al.getSequenceAt(1), seqs[1][0]);
139 // no sequence refers to PDBEntry[2]
140 assertEquals(0, seqs[2].length);
144 * Test that a mapping is not deregistered when a second view is closed but
145 * the first still holds a reference to the mapping
147 @Test(groups = { "Functional" })
148 public void testDeregisterMapping_onCloseView()
151 * alignment with reference to mappings
153 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
154 ">Seq1\nCAGT\n", DataSourceType.PASTE);
156 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
157 AlignedCodonFrame acf1 = new AlignedCodonFrame();
158 acf1.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 1, 4 },
160 AlignedCodonFrame acf2 = new AlignedCodonFrame();
161 acf2.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 4, 1 },
164 List<AlignedCodonFrame> mappings = new ArrayList<>();
167 af1.getViewport().getAlignment().setCodonFrames(mappings);
168 af1.newView_actionPerformed(null);
171 * Verify that creating the alignment for the new View has registered the
174 StructureSelectionManager ssm = StructureSelectionManager
175 .getStructureSelectionManager(Desktop.instance);
176 assertEquals(2, ssm.getSequenceMappings().size());
177 assertTrue(ssm.getSequenceMappings().contains(acf1));
178 assertTrue(ssm.getSequenceMappings().contains(acf2));
181 * Close the second view. Verify that mappings are not removed as the first
182 * view still holds a reference to them.
184 af1.closeMenuItem_actionPerformed(false);
185 assertEquals(2, ssm.getSequenceMappings().size());
186 assertTrue(ssm.getSequenceMappings().contains(acf1));
187 assertTrue(ssm.getSequenceMappings().contains(acf2));
191 * Test that a mapping is deregistered if no alignment holds a reference to it
193 @Test(groups = { "Functional" })
194 public void testDeregisterMapping_withNoReference()
196 Desktop d = Desktop.instance;
198 StructureSelectionManager ssm = StructureSelectionManager
199 .getStructureSelectionManager(Desktop.instance);
202 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
203 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
204 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
205 ">Seq2\nDGEL\n", DataSourceType.PASTE);
206 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
207 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
208 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
209 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
210 // need to be distinct
211 AlignedCodonFrame acf1 = new AlignedCodonFrame();
212 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
213 new int[] { 1, 12 }, 1, 3));
214 AlignedCodonFrame acf2 = new AlignedCodonFrame();
215 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
216 new int[] { 1, 12 }, 1, 3));
217 AlignedCodonFrame acf3 = new AlignedCodonFrame();
218 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
221 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
223 af1.getViewport().getAlignment().setCodonFrames(mappings1);
225 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
228 af2.getViewport().getAlignment().setCodonFrames(mappings2);
231 * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
234 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
235 assertEquals(0, ssmMappings.size());
236 ssm.registerMapping(acf1);
237 assertEquals(1, ssmMappings.size());
238 ssm.registerMapping(acf2);
239 assertEquals(2, ssmMappings.size());
240 ssm.registerMapping(acf3);
241 assertEquals(3, ssmMappings.size());
244 * Closing AlignFrame2 should remove its mappings from
245 * StructureSelectionManager, since AlignFrame1 has no reference to them
247 af2.closeMenuItem_actionPerformed(true);
248 assertEquals(1, ssmMappings.size());
249 assertTrue(ssmMappings.contains(acf1));
253 * Test that a mapping is not deregistered if another alignment holds a
256 @Test(groups = { "Functional" })
257 public void testDeregisterMapping_withReference()
259 Desktop d = Desktop.instance;
261 StructureSelectionManager ssm = StructureSelectionManager
262 .getStructureSelectionManager(Desktop.instance);
265 AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
266 ">Seq1\nRSVQ\n", DataSourceType.PASTE);
267 AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
268 ">Seq2\nDGEL\n", DataSourceType.PASTE);
269 SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
270 SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
271 SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
272 SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
273 // need to be distinct
274 AlignedCodonFrame acf1 = new AlignedCodonFrame();
275 acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
276 new int[] { 1, 12 }, 1, 3));
277 AlignedCodonFrame acf2 = new AlignedCodonFrame();
278 acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
279 new int[] { 1, 12 }, 1, 3));
280 AlignedCodonFrame acf3 = new AlignedCodonFrame();
281 acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
284 List<AlignedCodonFrame> mappings1 = new ArrayList<>();
287 af1.getViewport().getAlignment().setCodonFrames(mappings1);
289 List<AlignedCodonFrame> mappings2 = new ArrayList<>();
292 af2.getViewport().getAlignment().setCodonFrames(mappings2);
295 * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
298 List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
299 assertEquals(0, ssmMappings.size());
300 ssm.registerMapping(acf1);
301 assertEquals(1, ssmMappings.size());
302 ssm.registerMapping(acf2);
303 assertEquals(2, ssmMappings.size());
304 ssm.registerMapping(acf3);
305 assertEquals(3, ssmMappings.size());
308 * Closing AlignFrame2 should remove mapping acf3 from
309 * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
312 af2.closeMenuItem_actionPerformed(true);
313 assertEquals(2, ssmMappings.size());
314 assertTrue(ssmMappings.contains(acf1));
315 assertTrue(ssmMappings.contains(acf2));
316 assertFalse(ssmMappings.contains(acf3));
320 * Test for JAL-1306 - conservation thread should run even when only Quality
321 * (and not Conservation) is enabled in Preferences
323 @Test(groups = { "Functional" })
324 public void testUpdateConservation_qualityOnly()
326 Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS",
327 Boolean.TRUE.toString());
328 Cache.applicationProperties.setProperty("SHOW_QUALITY",
329 Boolean.TRUE.toString());
330 Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
331 Boolean.FALSE.toString());
332 Cache.applicationProperties.setProperty("SHOW_OCCUPANCY",
333 Boolean.FALSE.toString());
334 Cache.applicationProperties.setProperty("SHOW_IDENTITY",
335 Boolean.FALSE.toString());
336 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
337 "examples/uniref50.fa", DataSourceType.FILE);
338 AlignmentAnnotation[] anns = af.viewport.getAlignment()
339 .getAlignmentAnnotation();
340 assertNotNull("No annotations found", anns);
341 assertEquals("More than one annotation found", 1, anns.length);
342 assertTrue("Annotation is not Quality",
343 anns[0].description.startsWith("Alignment Quality"));
344 Annotation[] annotations = anns[0].annotations;
345 assertNotNull("Quality annotations are null", annotations);
346 assertNotNull("Quality in column 1 is null", annotations[0]);
347 assertTrue("No quality value in column 1", annotations[0].value > 10f);
350 @Test(groups = { "Functional" })
351 public void testSetGlobalColourScheme()
354 * test for JAL-2283: don't inadvertently turn on colour by conservation
356 Cache.applicationProperties.setProperty("DEFAULT_COLOUR_PROT", "None");
357 Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
358 Boolean.TRUE.toString());
359 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
360 "examples/uniref50.fa", DataSourceType.FILE);
361 ColourSchemeI cs = new PIDColourScheme();
362 af.getViewport().setGlobalColourScheme(cs);
363 assertFalse(af.getViewport().getResidueShading()
364 .conservationApplied());
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());