JAL-3748 store an AlignmentView for the complement within an viewport’s AlignView...
[jalview.git] / test / jalview / gui / SplitFrameTest.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.junit.Assert.assertNotEquals;
24 import static org.testng.Assert.assertEquals;
25 import static org.testng.Assert.assertFalse;
26 import static org.testng.Assert.assertNotNull;
27 import static org.testng.Assert.assertNotSame;
28 import static org.testng.Assert.assertSame;
29 import static org.testng.Assert.assertTrue;
30
31 import jalview.api.FeatureColourI;
32 import jalview.bin.Cache;
33 import jalview.bin.Jalview;
34 import jalview.datamodel.Alignment;
35 import jalview.datamodel.AlignmentI;
36 import jalview.datamodel.AlignmentView;
37 import jalview.datamodel.HiddenColumns;
38 import jalview.datamodel.Sequence;
39 import jalview.datamodel.SequenceFeature;
40 import jalview.datamodel.SequenceGroup;
41 import jalview.datamodel.SequenceI;
42 import jalview.io.DataSourceType;
43 import jalview.io.FileLoader;
44 import jalview.project.Jalview2xmlTests;
45 import jalview.renderer.ResidueShaderI;
46 import jalview.schemes.BuriedColourScheme;
47 import jalview.schemes.FeatureColour;
48 import jalview.schemes.HelixColourScheme;
49 import jalview.schemes.JalviewColourScheme;
50 import jalview.schemes.StrandColourScheme;
51 import jalview.schemes.TurnColourScheme;
52 import jalview.util.MessageManager;
53
54 import java.awt.Color;
55 import java.util.Arrays;
56 import java.util.Iterator;
57
58 import org.testng.annotations.AfterMethod;
59 import org.testng.annotations.BeforeClass;
60 import org.testng.annotations.BeforeMethod;
61 import org.testng.annotations.Test;
62
63 public class SplitFrameTest
64 {
65   AlignFrame dnaAf, proteinAf;
66
67   SplitFrame testSplitFrame;
68
69   @BeforeClass(alwaysRun = true)
70   public static void setUpBeforeClass() throws Exception
71   {
72     setUpJvOptionPane();
73     /*
74      * use read-only test properties file
75      */
76     Cache.loadProperties("test/jalview/io/testProps.jvprops");
77     Jalview.main(new String[] { "-nonews" });
78   }
79
80   @AfterMethod(alwaysRun = true)
81   public void tearDown()
82   {
83     Desktop.instance.closeAll_actionPerformed(null);
84   }
85
86   /**
87    * configure (read-only) properties for test to ensure Consensus is computed
88    * for colour Above PID testing
89    */
90   @BeforeMethod(alwaysRun = true)
91   public void setUp()
92   {
93     Cache.loadProperties("test/jalview/io/testProps.jvprops");
94     Cache.applicationProperties.setProperty("SHOW_IDENTITY",
95             Boolean.TRUE.toString());
96     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
97             "examples/testdata/MN908947.jvp", DataSourceType.FILE);
98
99     /*
100      * wait for Consensus thread to complete
101      */
102     synchronized (this)
103     {
104       while (af.getViewport().getConsensusSeq() == null)
105       {
106         try
107         {
108           wait(50);
109         } catch (InterruptedException e)
110         {
111         }
112       }
113     }
114     testSplitFrame = (SplitFrame) af.getSplitViewContainer();
115     proteinAf=af.getViewport().getAlignment().isNucleotide() ? testSplitFrame.getComplementAlignFrame(af) : af;
116     dnaAf=testSplitFrame.getComplementAlignFrame(proteinAf);
117   }
118
119   public static void setUpJvOptionPane()
120   {
121     JvOptionPane.setInteractiveMode(false);
122     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
123   }
124
125   @Test(groups= {"Functional"})
126   public void testAlignAsSplitFrame()
127   {
128
129     /*
130      * If alignment was requested from one half of a SplitFrame, show in a
131      * SplitFrame with the other pane similarly aligned.
132      */
133     AlignFrame requestedBy = proteinAf;
134     AlignmentI wholeProteinAl = proteinAf.getViewport().getAlignment();
135     SequenceI[] sel = wholeProteinAl.getSequencesArray();
136     // Select 3 sequences, from columns 3-7 inclusive
137     SequenceGroup selRegion = new SequenceGroup(
138             Arrays.asList(sel[0], sel[1], sel[3]));
139     selRegion.setStartRes(3);
140     selRegion.setEndRes(7);
141     proteinAf.getViewport().setSelectionGroup(selRegion);
142     proteinAf.getViewport().sendSelection();
143     assertNotNull(dnaAf.getViewport().getSelectionGroup());
144     AlignmentView inputView = proteinAf.gatherSequencesForAlignment();
145     assertEquals(inputView.getSequences().length, 3);
146     assertEquals(inputView.getWidth(), 5);
147     assertNotNull(inputView.getComplementView());
148
149     Object alAndHidden[] = inputView.getAlignmentAndHiddenColumns(
150             proteinAf.getViewport().getGapCharacter());
151     AlignmentI result = new Alignment((SequenceI[]) alAndHidden[0]);
152     result.setHiddenColumns((HiddenColumns) alAndHidden[1]);
153     // check we are referring to the expected alignment
154     assertEquals(
155             requestedBy.getSplitViewContainer().getComplement(requestedBy),
156             dnaAf.getCurrentView().getAlignment());
157     // and that datasets are consistent (if not, there's a problem elsewhere in
158     // splitframe construction
159     AlignmentI complementDs = requestedBy.getSplitViewContainer()
160             .getComplement(requestedBy).getDataset();
161     assertTrue(dnaAf.getViewport().getAlignment().getDataset() == dnaAf
162             .getViewport().getAlignment().getDataset());
163     assertTrue(complementDs == proteinAf.getViewport().getAlignment()
164             .getDataset());
165
166     char gc = requestedBy.getSplitViewContainer().getComplement(requestedBy)
167             .getGapCharacter();
168
169     AlignmentI complement = inputView.getComplementView()
170             .getVisibleAlignment(gc);
171     String complementTitle = requestedBy.getSplitViewContainer()
172             .getComplementTitle(requestedBy);
173     // becomes null if the alignment window was closed before the alignment
174     // job finished.
175     AlignmentI copyComplement = new Alignment(complement);
176     // todo should this be done by copy constructor?
177     copyComplement.setGapCharacter(complement.getGapCharacter());
178     // share the same dataset (and the mappings it holds)
179     copyComplement.setDataset(complementDs);
180     copyComplement.alignAs(result);
181     // check shape is as expected
182     assertEquals(copyComplement.getWidth(), result.getWidth() * 3);
183     assertEquals(copyComplement.getHeight(), result.getHeight());
184   }
185 }