JAL-147 unit test for hide/show sequences update ViewportRanges
[jalview.git] / test / jalview / gui / AlignViewportTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
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;
28
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;
50
51 import java.util.ArrayList;
52 import java.util.List;
53
54 import org.testng.Assert;
55 import org.testng.annotations.BeforeClass;
56 import org.testng.annotations.BeforeMethod;
57 import org.testng.annotations.Test;
58
59 public class AlignViewportTest
60 {
61
62   @BeforeClass(alwaysRun = true)
63   public void setUpJvOptionPane()
64   {
65     JvOptionPane.setInteractiveMode(false);
66     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
67   }
68
69   AlignmentI al;
70
71   AlignViewport testee;
72
73   @BeforeClass(alwaysRun = true)
74   public static void setUpBeforeClass() throws Exception
75   {
76     Jalview.main(new String[] { "-nonews", "-props",
77         "test/jalview/testProps.jvprops" });
78   }
79
80   @BeforeMethod(alwaysRun = true)
81   public void setUp()
82   {
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);
88     al.setDataset(null);
89     testee = new AlignViewport(al);
90   }
91
92   @Test(groups = { "Functional" })
93   public void testCollateForPdb()
94   {
95     // JBP: What behaviour is this supposed to test ?
96     /*
97      * Set up sequence pdb ids
98      */
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");
102
103     /*
104      * seq1 and seq3 refer to 1abcB, seq2 to 2abcC, none to 3abcD
105      */
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"));
112     /*
113      * Add a second chain PDB xref to Seq2 - should not result in a duplicate in
114      * the results
115      */
116     al.getSequenceAt(1).getDatasetSequence()
117             .addPDBId(new PDBEntry("2ABC", "D", Type.PDB, "2ABC.pdb"));
118     /*
119      * Seq3 refers to 3abc - this does not match 3ABC (as the code stands)
120      */
121     al.getSequenceAt(2).getDatasetSequence()
122             .addPDBId(new PDBEntry("3abc", "D", Type.PDB, "3ABC.pdb"));
123
124     /*
125      * run method under test
126      */
127     SequenceI[][] seqs = testee.collateForPDB(new PDBEntry[] { pdb1, pdb2,
128         pdb3 });
129
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]);
134
135     // seq2 refers to PDBEntry[1]
136     assertEquals(1, seqs[1].length);
137     assertSame(al.getSequenceAt(1), seqs[1][0]);
138
139     // no sequence refers to PDBEntry[2]
140     assertEquals(0, seqs[2].length);
141   }
142
143   /**
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
146    */
147   @Test(groups = { "Functional" })
148   public void testDeregisterMapping_onCloseView()
149   {
150     /*
151      * alignment with reference to mappings
152      */
153     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
154             ">Seq1\nCAGT\n", DataSourceType.PASTE);
155
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 },
159             1, 1));
160     AlignedCodonFrame acf2 = new AlignedCodonFrame();
161     acf2.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 4, 1 },
162             1, 1));
163
164     List<AlignedCodonFrame> mappings = new ArrayList<>();
165     mappings.add(acf1);
166     mappings.add(acf2);
167     af1.getViewport().getAlignment().setCodonFrames(mappings);
168     af1.newView_actionPerformed(null);
169
170     /*
171      * Verify that creating the alignment for the new View has registered the
172      * mappings
173      */
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));
179
180     /*
181      * Close the second view. Verify that mappings are not removed as the first
182      * view still holds a reference to them.
183      */
184     af1.closeMenuItem_actionPerformed(false);
185     assertEquals(2, ssm.getSequenceMappings().size());
186     assertTrue(ssm.getSequenceMappings().contains(acf1));
187     assertTrue(ssm.getSequenceMappings().contains(acf2));
188   }
189
190   /**
191    * Test that a mapping is deregistered if no alignment holds a reference to it
192    */
193   @Test(groups = { "Functional" })
194   public void testDeregisterMapping_withNoReference()
195   {
196     Desktop d = Desktop.instance;
197     assertNotNull(d);
198     StructureSelectionManager ssm = StructureSelectionManager
199             .getStructureSelectionManager(Desktop.instance);
200     ssm.resetAll();
201
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,
219         12 }, 1, 1));
220
221     List<AlignedCodonFrame> mappings1 = new ArrayList<>();
222     mappings1.add(acf1);
223     af1.getViewport().getAlignment().setCodonFrames(mappings1);
224
225     List<AlignedCodonFrame> mappings2 = new ArrayList<>();
226     mappings2.add(acf2);
227     mappings2.add(acf3);
228     af2.getViewport().getAlignment().setCodonFrames(mappings2);
229
230     /*
231      * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
232      */
233
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());
242
243     /*
244      * Closing AlignFrame2 should remove its mappings from
245      * StructureSelectionManager, since AlignFrame1 has no reference to them
246      */
247     af2.closeMenuItem_actionPerformed(true);
248     assertEquals(1, ssmMappings.size());
249     assertTrue(ssmMappings.contains(acf1));
250   }
251
252   /**
253    * Test that a mapping is not deregistered if another alignment holds a
254    * reference to it
255    */
256   @Test(groups = { "Functional" })
257   public void testDeregisterMapping_withReference()
258   {
259     Desktop d = Desktop.instance;
260     assertNotNull(d);
261     StructureSelectionManager ssm = StructureSelectionManager
262             .getStructureSelectionManager(Desktop.instance);
263     ssm.resetAll();
264
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,
282         12 }, 1, 1));
283
284     List<AlignedCodonFrame> mappings1 = new ArrayList<>();
285     mappings1.add(acf1);
286     mappings1.add(acf2);
287     af1.getViewport().getAlignment().setCodonFrames(mappings1);
288
289     List<AlignedCodonFrame> mappings2 = new ArrayList<>();
290     mappings2.add(acf2);
291     mappings2.add(acf3);
292     af2.getViewport().getAlignment().setCodonFrames(mappings2);
293
294     /*
295      * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
296      */
297
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());
306
307     /*
308      * Closing AlignFrame2 should remove mapping acf3 from
309      * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
310      * reference to it
311      */
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));
317   }
318
319   /**
320    * Test for JAL-1306 - conservation thread should run even when only Quality
321    * (and not Conservation) is enabled in Preferences
322    */
323   @Test(groups = { "Functional" })
324   public void testUpdateConservation_qualityOnly()
325   {
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);
348   }
349
350   @Test(groups = { "Functional" })
351   public void testSetGlobalColourScheme()
352   {
353     /*
354      * test for JAL-2283: don't inadvertently turn on colour by conservation
355      */
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());
365   }
366
367   @Test(groups = { "Functional" })
368   public void testSetGetHasSearchResults()
369   {
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);
374
375     // create arbitrary range on first sequence
376     sr.addResult(s1, s1.getStart() + 10, s1.getStart() + 15);
377
378     // test set
379     af.getViewport().setSearchResults(sr);
380     // has -> true
381     assertTrue(af.getViewport().hasSearchResults());
382     // get == original
383     assertEquals(sr, af.getViewport().getSearchResults());
384
385     // set(null) results in has -> false
386
387     af.getViewport().setSearchResults(null);
388     assertFalse(af.getViewport().hasSearchResults());
389   }
390
391   /**
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.
395    */
396   @Test(groups = { "Functional" })
397   public void testSetSelectionGroup()
398   {
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();
405
406     av.setSelectionGroup(sg1);
407     assertSame(sg1.getContext(), av.getAlignment()); // context set
408     assertFalse(sg1.isDefined()); // group not defined
409
410     sg2.setContext(sg1, false);
411     av.setSelectionGroup(sg2);
412     assertFalse(sg2.isDefined()); // unchanged
413     assertSame(sg2.getContext(), sg1); // unchanged
414
415     // create a defined group
416     sg3.setContext(av.getAlignment(), true);
417     av.setSelectionGroup(sg3);
418     assertTrue(sg3.isDefined()); // unchanged
419   }
420   /**
421    * Verify that setting/clearing SHOW_OCCUPANCY preference adds or omits occupancy row from viewport
422    */
423   @Test(groups = { "Functional" })
424   public void testShowOrDontShowOccupancy()
425   {
426     // disable occupancy
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.");
432     int c = 0;
433     for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
434             null, "Occupancy"))
435     {
436       c++;
437     }
438     Assert.assertEquals(c, 0, "Expected zero occupancy rows.");
439     
440     // enable occupancy
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.");
446     c = 0;
447     for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
448             null, av.getAlignmentGapAnnotation().label))
449     {
450       c++;
451     }
452     ;
453     Assert.assertEquals(c, 1, "Expected to find one occupancy row.");
454   }
455
456   @Test(groups = { "Functional" })
457   public void testGetConsensusSeq()
458   {
459     /*
460      * A-C
461      * A-C
462      * A-D
463      * --D
464      * consensus expected to be A-C
465      */
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());
472   }
473
474   @Test(groups = { "Functional" })
475   public void testHideRevealSequences()
476   {
477     ViewportRanges ranges = testee.getRanges();
478     assertEquals(3, al.getHeight());
479     assertEquals(0, ranges.getStartSeq());
480     assertEquals(2, ranges.getEndSeq());
481
482     /*
483      * hide first sequence
484      */
485     testee.hideSequence(new SequenceI[] { al.getSequenceAt(0) });
486     assertEquals(2, al.getHeight());
487     assertEquals(0, ranges.getStartSeq());
488     assertEquals(1, ranges.getEndSeq());
489
490     /*
491      * reveal hidden sequences above the first
492      */
493     testee.showSequence(0);
494     assertEquals(3, al.getHeight());
495     assertEquals(0, ranges.getStartSeq());
496     assertEquals(2, ranges.getEndSeq());
497
498     /*
499      * hide first and third sequences
500      */
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());
506
507     /*
508      * reveal all hidden sequences
509      */
510     testee.showAllHiddenSeqs();
511     assertEquals(3, al.getHeight());
512     assertEquals(0, ranges.getStartSeq());
513     assertEquals(2, ranges.getEndSeq());
514   }
515 }