JAL-2738 copy to spikes/mungo
[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      * remove any sequence mappings left lying around by other tests
81      */
82     StructureSelectionManager ssm = StructureSelectionManager
83             .getStructureSelectionManager(Desktop.instance);
84     ssm.resetAll();
85   }
86
87   @BeforeMethod(alwaysRun = true)
88   public void setUp()
89   {
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);
95     al.setDataset(null);
96     testee = new AlignViewport(al);
97   }
98
99   @Test(groups = { "Functional" })
100   public void testCollateForPdb()
101   {
102     // JBP: What behaviour is this supposed to test ?
103     /*
104      * Set up sequence pdb ids
105      */
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");
109
110     /*
111      * seq1 and seq3 refer to 1abcB, seq2 to 2abcC, none to 3abcD
112      */
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"));
119     /*
120      * Add a second chain PDB xref to Seq2 - should not result in a duplicate in
121      * the results
122      */
123     al.getSequenceAt(1).getDatasetSequence()
124             .addPDBId(new PDBEntry("2ABC", "D", Type.PDB, "2ABC.pdb"));
125     /*
126      * Seq3 refers to 3abc - this does not match 3ABC (as the code stands)
127      */
128     al.getSequenceAt(2).getDatasetSequence()
129             .addPDBId(new PDBEntry("3abc", "D", Type.PDB, "3ABC.pdb"));
130
131     /*
132      * run method under test
133      */
134     SequenceI[][] seqs = testee.collateForPDB(new PDBEntry[] { pdb1, pdb2,
135         pdb3 });
136
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]);
141
142     // seq2 refers to PDBEntry[1]
143     assertEquals(1, seqs[1].length);
144     assertSame(al.getSequenceAt(1), seqs[1][0]);
145
146     // no sequence refers to PDBEntry[2]
147     assertEquals(0, seqs[2].length);
148   }
149
150   /**
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
153    */
154   @Test(groups = { "Functional" })
155   public void testDeregisterMapping_onCloseView()
156   {
157     /*
158      * alignment with reference to mappings
159      */
160     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
161             ">Seq1\nCAGT\n", DataSourceType.PASTE);
162
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 },
166             1, 1));
167     AlignedCodonFrame acf2 = new AlignedCodonFrame();
168     acf2.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 4, 1 },
169             1, 1));
170
171     List<AlignedCodonFrame> mappings = new ArrayList<>();
172     mappings.add(acf1);
173     mappings.add(acf2);
174     af1.getViewport().getAlignment().setCodonFrames(mappings);
175     af1.newView_actionPerformed(null);
176
177     /*
178      * Verify that creating the alignment for the new View has registered the
179      * mappings
180      */
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));
187
188     /*
189      * Close the second view. Verify that mappings are not removed as the first
190      * view still holds a reference to them.
191      */
192     af1.closeMenuItem_actionPerformed(false);
193     assertEquals(2, sequenceMappings.size());
194     assertTrue(sequenceMappings.contains(acf1));
195     assertTrue(sequenceMappings.contains(acf2));
196   }
197
198   /**
199    * Test that a mapping is deregistered if no alignment holds a reference to it
200    */
201   @Test(groups = { "Functional" })
202   public void testDeregisterMapping_withNoReference()
203   {
204     Desktop d = Desktop.instance;
205     assertNotNull(d);
206     StructureSelectionManager ssm = StructureSelectionManager
207             .getStructureSelectionManager(Desktop.instance);
208     ssm.resetAll();
209
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,
227         12 }, 1, 1));
228
229     List<AlignedCodonFrame> mappings1 = new ArrayList<>();
230     mappings1.add(acf1);
231     af1.getViewport().getAlignment().setCodonFrames(mappings1);
232
233     List<AlignedCodonFrame> mappings2 = new ArrayList<>();
234     mappings2.add(acf2);
235     mappings2.add(acf3);
236     af2.getViewport().getAlignment().setCodonFrames(mappings2);
237
238     /*
239      * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
240      */
241
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());
250
251     /*
252      * Closing AlignFrame2 should remove its mappings from
253      * StructureSelectionManager, since AlignFrame1 has no reference to them
254      */
255     af2.closeMenuItem_actionPerformed(true);
256     assertEquals(1, ssmMappings.size());
257     assertTrue(ssmMappings.contains(acf1));
258   }
259
260   /**
261    * Test that a mapping is not deregistered if another alignment holds a
262    * reference to it
263    */
264   @Test(groups = { "Functional" })
265   public void testDeregisterMapping_withReference()
266   {
267     Desktop d = Desktop.instance;
268     assertNotNull(d);
269     StructureSelectionManager ssm = StructureSelectionManager
270             .getStructureSelectionManager(Desktop.instance);
271     ssm.resetAll();
272
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,
290         12 }, 1, 1));
291
292     List<AlignedCodonFrame> mappings1 = new ArrayList<>();
293     mappings1.add(acf1);
294     mappings1.add(acf2);
295     af1.getViewport().getAlignment().setCodonFrames(mappings1);
296
297     List<AlignedCodonFrame> mappings2 = new ArrayList<>();
298     mappings2.add(acf2);
299     mappings2.add(acf3);
300     af2.getViewport().getAlignment().setCodonFrames(mappings2);
301
302     /*
303      * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
304      */
305
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());
314
315     /*
316      * Closing AlignFrame2 should remove mapping acf3 from
317      * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
318      * reference to it
319      */
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));
325   }
326
327   /**
328    * Test for JAL-1306 - conservation thread should run even when only Quality
329    * (and not Conservation) is enabled in Preferences
330    */
331   @Test(groups = { "Functional" })
332   public void testUpdateConservation_qualityOnly()
333   {
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);
356   }
357
358   @Test(groups = { "Functional" })
359   public void testSetGlobalColourScheme()
360   {
361     /*
362      * test for JAL-2283: don't inadvertently turn on colour by conservation
363      */
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());
373   }
374
375   @Test(groups = { "Functional" })
376   public void testSetGetHasSearchResults()
377   {
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);
382
383     // create arbitrary range on first sequence
384     sr.addResult(s1, s1.getStart() + 10, s1.getStart() + 15);
385
386     // test set
387     af.getViewport().setSearchResults(sr);
388     // has -> true
389     assertTrue(af.getViewport().hasSearchResults());
390     // get == original
391     assertEquals(sr, af.getViewport().getSearchResults());
392
393     // set(null) results in has -> false
394
395     af.getViewport().setSearchResults(null);
396     assertFalse(af.getViewport().hasSearchResults());
397   }
398
399   /**
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.
403    */
404   @Test(groups = { "Functional" })
405   public void testSetSelectionGroup()
406   {
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();
413
414     av.setSelectionGroup(sg1);
415     assertSame(sg1.getContext(), av.getAlignment()); // context set
416     assertFalse(sg1.isDefined()); // group not defined
417
418     sg2.setContext(sg1, false);
419     av.setSelectionGroup(sg2);
420     assertFalse(sg2.isDefined()); // unchanged
421     assertSame(sg2.getContext(), sg1); // unchanged
422
423     // create a defined group
424     sg3.setContext(av.getAlignment(), true);
425     av.setSelectionGroup(sg3);
426     assertTrue(sg3.isDefined()); // unchanged
427   }
428   /**
429    * Verify that setting/clearing SHOW_OCCUPANCY preference adds or omits occupancy row from viewport
430    */
431   @Test(groups = { "Functional" })
432   public void testShowOrDontShowOccupancy()
433   {
434     // disable occupancy
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.");
440     int c = 0;
441     for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
442             null, "Occupancy"))
443     {
444       c++;
445     }
446     Assert.assertEquals(c, 0, "Expected zero occupancy rows.");
447     
448     // enable occupancy
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.");
454     c = 0;
455     for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
456             null, av.getAlignmentGapAnnotation().label))
457     {
458       c++;
459     }
460     ;
461     Assert.assertEquals(c, 1, "Expected to find one occupancy row.");
462   }
463
464   @Test(groups = { "Functional" })
465   public void testGetConsensusSeq()
466   {
467     /*
468      * A-C
469      * A-C
470      * A-D
471      * --D
472      * consensus expected to be A-C
473      */
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());
480   }
481
482   @Test(groups = { "Functional" })
483   public void testHideRevealSequences()
484   {
485     ViewportRanges ranges = testee.getRanges();
486     assertEquals(3, al.getHeight());
487     assertEquals(0, ranges.getStartSeq());
488     assertEquals(2, ranges.getEndSeq());
489
490     /*
491      * hide first sequence
492      */
493     testee.hideSequence(new SequenceI[] { al.getSequenceAt(0) });
494     assertEquals(2, al.getHeight());
495     assertEquals(0, ranges.getStartSeq());
496     assertEquals(1, ranges.getEndSeq());
497
498     /*
499      * reveal hidden sequences above the first
500      */
501     testee.showSequence(0);
502     assertEquals(3, al.getHeight());
503     assertEquals(0, ranges.getStartSeq());
504     assertEquals(2, ranges.getEndSeq());
505
506     /*
507      * hide first and third sequences
508      */
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());
514
515     /*
516      * reveal all hidden sequences
517      */
518     testee.showAllHiddenSeqs();
519     assertEquals(3, al.getHeight());
520     assertEquals(0, ranges.getStartSeq());
521     assertEquals(2, ranges.getEndSeq());
522   }
523 }