JAL-3253-applet feature parameter processing fixed
[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.assertNotSame;
27 import static org.testng.AssertJUnit.assertSame;
28 import static org.testng.AssertJUnit.assertTrue;
29
30 import jalview.bin.Cache;
31 import jalview.bin.Jalview;
32 import jalview.datamodel.AlignedCodonFrame;
33 import jalview.datamodel.Alignment;
34 import jalview.datamodel.AlignmentAnnotation;
35 import jalview.datamodel.AlignmentI;
36 import jalview.datamodel.Annotation;
37 import jalview.datamodel.SearchResults;
38 import jalview.datamodel.SearchResultsI;
39 import jalview.datamodel.Sequence;
40 import jalview.datamodel.SequenceGroup;
41 import jalview.datamodel.SequenceI;
42 import jalview.io.DataSourceType;
43 import jalview.io.FileLoader;
44 import jalview.schemes.ClustalxColourScheme;
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     Desktop.getInstance().getStructureSelectionManager().resetAll();
83   }
84
85   @BeforeMethod(alwaysRun = true)
86   public void setUp()
87   {
88     SequenceI seq1 = new Sequence("Seq1", "ABC");
89     SequenceI seq2 = new Sequence("Seq2", "ABC");
90     SequenceI seq3 = new Sequence("Seq3", "ABC");
91     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3 };
92     al = new Alignment(seqs);
93     al.setDataset(null);
94     testee = new AlignViewport(al);
95   }
96
97   /**
98    * Test that a mapping is not deregistered when a second view is closed but
99    * the first still holds a reference to the mapping
100    */
101   @Test(groups = { "Functional" })
102   public void testDeregisterMapping_onCloseView()
103   {
104     /*
105      * alignment with reference to mappings
106      */
107     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
108             ">Seq1\nCAGT\n", DataSourceType.PASTE);
109
110     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
111     AlignedCodonFrame acf1 = new AlignedCodonFrame();
112     acf1.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 1, 4 },
113             1, 1));
114     AlignedCodonFrame acf2 = new AlignedCodonFrame();
115     acf2.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 4, 1 },
116             1, 1));
117
118     List<AlignedCodonFrame> mappings = new ArrayList<>();
119     mappings.add(acf1);
120     mappings.add(acf2);
121     af1.getViewport().getAlignment().setCodonFrames(mappings);
122     af1.newView_actionPerformed(null);
123
124     /*
125      * Verify that creating the alignment for the new View has registered the
126      * mappings
127      */
128     List<AlignedCodonFrame> sequenceMappings = Desktop.getInstance()
129             .getStructureSelectionManager().getSequenceMappings();
130     assertEquals(2, sequenceMappings.size());
131     assertTrue(sequenceMappings.contains(acf1));
132     assertTrue(sequenceMappings.contains(acf2));
133
134     /*
135      * Close the second view. Verify that mappings are not removed as the first
136      * view still holds a reference to them.
137      */
138     af1.closeMenuItem_actionPerformed(false);
139     assertEquals(2, sequenceMappings.size());
140     assertTrue(sequenceMappings.contains(acf1));
141     assertTrue(sequenceMappings.contains(acf2));
142   }
143
144   /**
145    * Test that a mapping is deregistered if no alignment holds a reference to it
146    */
147   @Test(groups = { "Functional" })
148   public void testDeregisterMapping_withNoReference()
149   {
150     Desktop d = Desktop.getInstance();
151     assertNotNull(d);
152     StructureSelectionManager ssm = Desktop.getInstance()
153             .getStructureSelectionManager();
154     ssm.resetAll();
155
156     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
157             ">Seq1\nRSVQ\n", DataSourceType.PASTE);
158     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
159             ">Seq2\nDGEL\n", DataSourceType.PASTE);
160     SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
161     SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
162     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
163     SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
164     // need to be distinct
165     AlignedCodonFrame acf1 = new AlignedCodonFrame();
166     acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
167             new int[] { 1, 12 }, 1, 3));
168     AlignedCodonFrame acf2 = new AlignedCodonFrame();
169     acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
170             new int[] { 1, 12 }, 1, 3));
171     AlignedCodonFrame acf3 = new AlignedCodonFrame();
172     acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
173         12 }, 1, 1));
174
175     List<AlignedCodonFrame> mappings1 = new ArrayList<>();
176     mappings1.add(acf1);
177     af1.getViewport().getAlignment().setCodonFrames(mappings1);
178
179     List<AlignedCodonFrame> mappings2 = new ArrayList<>();
180     mappings2.add(acf2);
181     mappings2.add(acf3);
182     af2.getViewport().getAlignment().setCodonFrames(mappings2);
183
184     /*
185      * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
186      */
187
188     List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
189     assertEquals(0, ssmMappings.size());
190     ssm.registerMapping(acf1);
191     assertEquals(1, ssmMappings.size());
192     ssm.registerMapping(acf2);
193     assertEquals(2, ssmMappings.size());
194     ssm.registerMapping(acf3);
195     assertEquals(3, ssmMappings.size());
196
197     /*
198      * Closing AlignFrame2 should remove its mappings from
199      * StructureSelectionManager, since AlignFrame1 has no reference to them
200      */
201     af2.closeMenuItem_actionPerformed(true);
202     assertEquals(1, ssmMappings.size());
203     assertTrue(ssmMappings.contains(acf1));
204   }
205
206   /**
207    * Test that a mapping is not deregistered if another alignment holds a
208    * reference to it
209    */
210   @Test(groups = { "Functional" })
211   public void testDeregisterMapping_withReference()
212   {
213     Desktop d = Desktop.getInstance();
214     assertNotNull(d);
215     StructureSelectionManager ssm = Desktop.getInstance()
216             .getStructureSelectionManager();
217     ssm.resetAll();
218
219     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
220             ">Seq1\nRSVQ\n", DataSourceType.PASTE);
221     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
222             ">Seq2\nDGEL\n", DataSourceType.PASTE);
223     SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
224     SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
225     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
226     SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
227     // need to be distinct
228     AlignedCodonFrame acf1 = new AlignedCodonFrame();
229     acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
230             new int[] { 1, 12 }, 1, 3));
231     AlignedCodonFrame acf2 = new AlignedCodonFrame();
232     acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
233             new int[] { 1, 12 }, 1, 3));
234     AlignedCodonFrame acf3 = new AlignedCodonFrame();
235     acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
236         12 }, 1, 1));
237
238     List<AlignedCodonFrame> mappings1 = new ArrayList<>();
239     mappings1.add(acf1);
240     mappings1.add(acf2);
241     af1.getViewport().getAlignment().setCodonFrames(mappings1);
242
243     List<AlignedCodonFrame> mappings2 = new ArrayList<>();
244     mappings2.add(acf2);
245     mappings2.add(acf3);
246     af2.getViewport().getAlignment().setCodonFrames(mappings2);
247
248     /*
249      * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
250      */
251
252     List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
253     assertEquals(0, ssmMappings.size());
254     ssm.registerMapping(acf1);
255     assertEquals(1, ssmMappings.size());
256     ssm.registerMapping(acf2);
257     assertEquals(2, ssmMappings.size());
258     ssm.registerMapping(acf3);
259     assertEquals(3, ssmMappings.size());
260
261     /*
262      * Closing AlignFrame2 should remove mapping acf3 from
263      * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
264      * reference to it
265      */
266     af2.closeMenuItem_actionPerformed(true);
267     assertEquals(2, ssmMappings.size());
268     assertTrue(ssmMappings.contains(acf1));
269     assertTrue(ssmMappings.contains(acf2));
270     assertFalse(ssmMappings.contains(acf3));
271   }
272
273   /**
274    * Test for JAL-1306 - conservation thread should run even when only Quality
275    * (and not Conservation) is enabled in Preferences
276    */
277   @Test(groups = { "Functional" }, timeOut=2000)
278   public void testUpdateConservation_qualityOnly()
279   {
280     Cache.setPropertyNoSave("SHOW_ANNOTATIONS",
281             Boolean.TRUE.toString());
282     Cache.setPropertyNoSave("SHOW_QUALITY",
283             Boolean.TRUE.toString());
284     Cache.setPropertyNoSave("SHOW_CONSERVATION",
285             Boolean.FALSE.toString());
286     Cache.setPropertyNoSave("SHOW_OCCUPANCY",
287             Boolean.FALSE.toString());
288     Cache.setPropertyNoSave("SHOW_IDENTITY",
289             Boolean.FALSE.toString());
290     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
291             "examples/uniref50.fa", DataSourceType.FILE);
292
293     /*
294      * wait for Conservation thread to complete
295      */
296     AlignViewport viewport = af.getViewport();
297     synchronized (this)
298     {
299       while (viewport.getAlignmentConservationAnnotation() != null)
300       {
301         try
302         {
303           wait(50);
304         } catch (InterruptedException e)
305         {
306         }
307       }
308     }
309     AlignmentAnnotation[] anns = viewport.getAlignment()
310             .getAlignmentAnnotation();
311     assertNotNull("No annotations found", anns);
312     assertEquals("More than one annotation found", 1, anns.length);
313     assertTrue("Annotation is not Quality",
314             anns[0].description.startsWith("Alignment Quality"));
315     Annotation[] annotations = anns[0].annotations;
316     assertNotNull("Quality annotations are null", annotations);
317     assertNotNull("Quality in column 1 is null", annotations[0]);
318     assertTrue("No quality value in column 1", annotations[0].value > 10f);
319   }
320
321   @Test(groups = { "Functional" })
322   public void testSetGlobalColourScheme()
323   {
324     /*
325      * test for JAL-2283: don't inadvertently turn on colour by conservation
326      */
327     Cache.setPropertyNoSave("DEFAULT_COLOUR_PROT", "None");
328     Cache.setPropertyNoSave("SHOW_CONSERVATION",
329             Boolean.TRUE.toString());
330     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
331             "examples/uniref50.fa", DataSourceType.FILE);
332     ColourSchemeI cs = new PIDColourScheme();
333     AlignViewport viewport = af.getViewport();
334     viewport.setGlobalColourScheme(cs);
335     assertFalse(viewport.getResidueShading()
336             .conservationApplied());
337
338     /*
339      * JAL-3201 groups have their own ColourSchemeI instances
340      */
341     AlignmentI aln = viewport.getAlignment();
342     SequenceGroup sg1 = new SequenceGroup();
343     sg1.addSequence(aln.getSequenceAt(0), false);
344     sg1.addSequence(aln.getSequenceAt(2), false);
345     SequenceGroup sg2 = new SequenceGroup();
346     sg2.addSequence(aln.getSequenceAt(1), false);
347     sg2.addSequence(aln.getSequenceAt(3), false);
348     aln.addGroup(sg1);
349     aln.addGroup(sg2);
350     viewport.setColourAppliesToAllGroups(true);
351     viewport.setGlobalColourScheme(new ClustalxColourScheme());
352     ColourSchemeI cs0 = viewport.getGlobalColourScheme();
353     ColourSchemeI cs1 = sg1.getColourScheme();
354     ColourSchemeI cs2 = sg2.getColourScheme();
355     assertTrue(cs0 instanceof ClustalxColourScheme);
356     assertTrue(cs1 instanceof ClustalxColourScheme);
357     assertTrue(cs2 instanceof ClustalxColourScheme);
358     assertNotSame(cs0, cs1);
359     assertNotSame(cs0, cs2);
360     assertNotSame(cs1, cs2);
361   }
362
363   @Test(groups = { "Functional" })
364   public void testSetGetHasSearchResults()
365   {
366     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
367             "examples/uniref50.fa", DataSourceType.FILE);
368     SearchResultsI sr = new SearchResults();
369     SequenceI s1 = af.getViewport().getAlignment().getSequenceAt(0);
370
371     // create arbitrary range on first sequence
372     sr.addResult(s1, s1.getStart() + 10, s1.getStart() + 15);
373
374     // test set
375     af.getViewport().setSearchResults(sr);
376     // has -> true
377     assertTrue(af.getViewport().hasSearchResults());
378     // get == original
379     assertEquals(sr, af.getViewport().getSearchResults());
380
381     // set(null) results in has -> false
382
383     af.getViewport().setSearchResults(null);
384     assertFalse(af.getViewport().hasSearchResults());
385   }
386
387   /**
388    * Verify that setting the selection group has the side-effect of setting the
389    * context on the group, unless it already has one, but does not change
390    * whether the group is defined or not.
391    */
392   @Test(groups = { "Functional" })
393   public void testSetSelectionGroup()
394   {
395     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
396             "examples/uniref50.fa", DataSourceType.FILE);
397     AlignViewport av = af.getViewport();
398     SequenceGroup sg1 = new SequenceGroup();
399     SequenceGroup sg2 = new SequenceGroup();
400     SequenceGroup sg3 = new SequenceGroup();
401
402     av.setSelectionGroup(sg1);
403     assertSame(sg1.getContext(), av.getAlignment()); // context set
404     assertFalse(sg1.isDefined()); // group not defined
405
406     sg2.setContext(sg1, false);
407     av.setSelectionGroup(sg2);
408     assertFalse(sg2.isDefined()); // unchanged
409     assertSame(sg2.getContext(), sg1); // unchanged
410
411     // create a defined group
412     sg3.setContext(av.getAlignment(), true);
413     av.setSelectionGroup(sg3);
414     assertTrue(sg3.isDefined()); // unchanged
415   }
416   /**
417    * Verify that setting/clearing SHOW_OCCUPANCY preference adds or omits occupancy row from viewport
418    */
419   @Test(groups = { "Functional" })
420   public void testShowOrDontShowOccupancy()
421   {
422     // disable occupancy
423     jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.FALSE.toString());
424     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
425             "examples/uniref50.fa", DataSourceType.FILE);
426     AlignViewport av = af.getViewport();
427     Assert.assertNull(av.getAlignmentGapAnnotation(), "Preference did not disable occupancy row.");
428     int c = 0;
429     for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
430             null, "Occupancy"))
431     {
432       c++;
433     }
434     Assert.assertEquals(c, 0, "Expected zero occupancy rows.");
435     
436     // enable occupancy
437     jalview.bin.Cache.setProperty("SHOW_OCCUPANCY", Boolean.TRUE.toString());
438     af = new FileLoader().LoadFileWaitTillLoaded(
439             "examples/uniref50.fa", DataSourceType.FILE);
440     av = af.getViewport();
441     Assert.assertNotNull(av.getAlignmentGapAnnotation(), "Preference did not enable occupancy row.");
442     c = 0;
443     for (AlignmentAnnotation aa : av.getAlignment().findAnnotations(null,
444             null, av.getAlignmentGapAnnotation().label))
445     {
446       c++;
447     }
448     Assert.assertEquals(c, 1, "Expected to find one occupancy row.");
449   }
450
451   @Test(groups = { "Functional" })
452   public void testGetConsensusSeq()
453   {
454     /*
455      * A-C
456      * A-C
457      * A-D
458      * --D
459      * consensus expected to be A-C
460      */
461     String fasta = ">s1\nA-C\n>s2\nA-C\n>s3\nA-D\n>s4\n--D\n";
462     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(fasta,
463             DataSourceType.PASTE);
464     AlignViewport testme = af.getViewport();
465     SequenceI cons = testme.getConsensusSeq();
466     assertEquals("A-C", cons.getSequenceAsString());
467   }
468
469   @Test(groups = { "Functional" })
470   public void testHideRevealSequences()
471   {
472     ViewportRanges ranges = testee.getRanges();
473     assertEquals(3, al.getHeight());
474     assertEquals(0, ranges.getStartSeq());
475     assertEquals(2, ranges.getEndSeq());
476
477     /*
478      * hide first sequence
479      */
480     testee.hideSequence(new SequenceI[] { al.getSequenceAt(0) });
481     assertEquals(2, al.getHeight());
482     assertEquals(0, ranges.getStartSeq());
483     assertEquals(1, ranges.getEndSeq());
484
485     /*
486      * reveal hidden sequences above the first
487      */
488     testee.showSequence(0);
489     assertEquals(3, al.getHeight());
490     assertEquals(0, ranges.getStartSeq());
491     assertEquals(2, ranges.getEndSeq());
492
493     /*
494      * hide first and third sequences
495      */
496     testee.hideSequence(new SequenceI[] { al.getSequenceAt(0),
497         al.getSequenceAt(2) });
498     assertEquals(1, al.getHeight());
499     assertEquals(0, ranges.getStartSeq());
500     assertEquals(0, ranges.getEndSeq());
501
502     /*
503      * reveal all hidden sequences
504      */
505     testee.showAllHiddenSeqs();
506     assertEquals(3, al.getHeight());
507     assertEquals(0, ranges.getStartSeq());
508     assertEquals(2, ranges.getEndSeq());
509   }
510 }