Merge branch 'develop' into trialMerge
[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.SequenceI;
42 import jalview.io.DataSourceType;
43 import jalview.io.FileLoader;
44 import jalview.schemes.ColourSchemeI;
45 import jalview.schemes.PIDColourScheme;
46 import jalview.structure.StructureSelectionManager;
47 import jalview.util.MapList;
48
49 import java.util.ArrayList;
50 import java.util.List;
51
52 import org.testng.annotations.BeforeClass;
53 import org.testng.annotations.BeforeMethod;
54 import org.testng.annotations.Test;
55
56 public class AlignViewportTest
57 {
58
59   AlignmentI al;
60
61   AlignViewport testee;
62
63   @BeforeClass(alwaysRun = true)
64   public static void setUpBeforeClass() throws Exception
65   {
66     Jalview.main(new String[] { "-props", "test/jalview/testProps.jvprops" });
67   }
68
69   @BeforeMethod(alwaysRun = true)
70   public void setUp()
71   {
72     SequenceI seq1 = new Sequence("Seq1", "ABC");
73     SequenceI seq2 = new Sequence("Seq2", "ABC");
74     SequenceI seq3 = new Sequence("Seq3", "ABC");
75     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3 };
76     al = new Alignment(seqs);
77     al.setDataset(null);
78     testee = new AlignViewport(al);
79   }
80
81   @Test(groups = { "Functional" })
82   public void testCollateForPdb()
83   {
84     // JBP: What behaviour is this supposed to test ?
85     /*
86      * Set up sequence pdb ids
87      */
88     PDBEntry pdb1 = new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb");
89     PDBEntry pdb2 = new PDBEntry("2ABC", "C", Type.PDB, "2ABC.pdb");
90     PDBEntry pdb3 = new PDBEntry("3ABC", "D", Type.PDB, "3ABC.pdb");
91
92     /*
93      * seq1 and seq3 refer to 1abcB, seq2 to 2abcC, none to 3abcD
94      */
95     al.getSequenceAt(0).getDatasetSequence()
96             .addPDBId(new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
97     al.getSequenceAt(2).getDatasetSequence()
98             .addPDBId(new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
99     al.getSequenceAt(1).getDatasetSequence()
100             .addPDBId(new PDBEntry("2ABC", "C", Type.PDB, "2ABC.pdb"));
101     /*
102      * Add a second chain PDB xref to Seq2 - should not result in a duplicate in
103      * the results
104      */
105     al.getSequenceAt(1).getDatasetSequence()
106             .addPDBId(new PDBEntry("2ABC", "D", Type.PDB, "2ABC.pdb"));
107     /*
108      * Seq3 refers to 3abc - this does not match 3ABC (as the code stands)
109      */
110     al.getSequenceAt(2).getDatasetSequence()
111             .addPDBId(new PDBEntry("3abc", "D", Type.PDB, "3ABC.pdb"));
112
113     /*
114      * run method under test
115      */
116     SequenceI[][] seqs = testee.collateForPDB(new PDBEntry[] { pdb1, pdb2,
117         pdb3 });
118
119     // seq1 and seq3 refer to PDBEntry[0]
120     assertEquals(2, seqs[0].length);
121     assertSame(al.getSequenceAt(0), seqs[0][0]);
122     assertSame(al.getSequenceAt(2), seqs[0][1]);
123
124     // seq2 refers to PDBEntry[1]
125     assertEquals(1, seqs[1].length);
126     assertSame(al.getSequenceAt(1), seqs[1][0]);
127
128     // no sequence refers to PDBEntry[2]
129     assertEquals(0, seqs[2].length);
130   }
131
132   /**
133    * Test that a mapping is not deregistered when a second view is closed but
134    * the first still holds a reference to the mapping
135    */
136   @Test(groups = { "Functional" })
137   public void testDeregisterMapping_onCloseView()
138   {
139     /*
140      * alignment with reference to mappings
141      */
142     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
143             ">Seq1\nCAGT\n", DataSourceType.PASTE);
144
145     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
146     AlignedCodonFrame acf1 = new AlignedCodonFrame();
147     acf1.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 1, 4 },
148             1, 1));
149     AlignedCodonFrame acf2 = new AlignedCodonFrame();
150     acf2.addMap(s1, s1, new MapList(new int[] { 1, 4 }, new int[] { 4, 1 },
151             1, 1));
152
153     List<AlignedCodonFrame> mappings = new ArrayList<AlignedCodonFrame>();
154     mappings.add(acf1);
155     mappings.add(acf2);
156     af1.getViewport().getAlignment().setCodonFrames(mappings);
157     af1.newView_actionPerformed(null);
158
159     /*
160      * Verify that creating the alignment for the new View has registered the
161      * mappings
162      */
163     StructureSelectionManager ssm = StructureSelectionManager
164             .getStructureSelectionManager(Desktop.instance);
165     assertEquals(2, ssm.getSequenceMappings().size());
166     assertTrue(ssm.getSequenceMappings().contains(acf1));
167     assertTrue(ssm.getSequenceMappings().contains(acf2));
168
169     /*
170      * Close the second view. Verify that mappings are not removed as the first
171      * view still holds a reference to them.
172      */
173     af1.closeMenuItem_actionPerformed(false);
174     assertEquals(2, ssm.getSequenceMappings().size());
175     assertTrue(ssm.getSequenceMappings().contains(acf1));
176     assertTrue(ssm.getSequenceMappings().contains(acf2));
177   }
178
179   /**
180    * Test that a mapping is deregistered if no alignment holds a reference to it
181    */
182   @Test(groups = { "Functional" })
183   public void testDeregisterMapping_withNoReference()
184   {
185     Desktop d = Desktop.instance;
186     assertNotNull(d);
187     StructureSelectionManager ssm = StructureSelectionManager
188             .getStructureSelectionManager(Desktop.instance);
189     ssm.resetAll();
190
191     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
192             ">Seq1\nRSVQ\n", DataSourceType.PASTE);
193     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
194             ">Seq2\nDGEL\n", DataSourceType.PASTE);
195     SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
196     SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
197     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
198     SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
199     // need to be distinct
200     AlignedCodonFrame acf1 = new AlignedCodonFrame();
201     acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
202             new int[] { 1, 12 }, 1, 3));
203     AlignedCodonFrame acf2 = new AlignedCodonFrame();
204     acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
205             new int[] { 1, 12 }, 1, 3));
206     AlignedCodonFrame acf3 = new AlignedCodonFrame();
207     acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
208         12 }, 1, 1));
209
210     List<AlignedCodonFrame> mappings1 = new ArrayList<AlignedCodonFrame>();
211     mappings1.add(acf1);
212     af1.getViewport().getAlignment().setCodonFrames(mappings1);
213
214     List<AlignedCodonFrame> mappings2 = new ArrayList<AlignedCodonFrame>();
215     mappings2.add(acf2);
216     mappings2.add(acf3);
217     af2.getViewport().getAlignment().setCodonFrames(mappings2);
218
219     /*
220      * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
221      */
222
223     List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
224     assertEquals(0, ssmMappings.size());
225     ssm.registerMapping(acf1);
226     assertEquals(1, ssmMappings.size());
227     ssm.registerMapping(acf2);
228     assertEquals(2, ssmMappings.size());
229     ssm.registerMapping(acf3);
230     assertEquals(3, ssmMappings.size());
231
232     /*
233      * Closing AlignFrame2 should remove its mappings from
234      * StructureSelectionManager, since AlignFrame1 has no reference to them
235      */
236     af2.closeMenuItem_actionPerformed(true);
237     assertEquals(1, ssmMappings.size());
238     assertTrue(ssmMappings.contains(acf1));
239   }
240
241   /**
242    * Test that a mapping is not deregistered if another alignment holds a
243    * reference to it
244    */
245   @Test(groups = { "Functional" })
246   public void testDeregisterMapping_withReference()
247   {
248     Desktop d = Desktop.instance;
249     assertNotNull(d);
250     StructureSelectionManager ssm = StructureSelectionManager
251             .getStructureSelectionManager(Desktop.instance);
252     ssm.resetAll();
253
254     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
255             ">Seq1\nRSVQ\n", DataSourceType.PASTE);
256     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
257             ">Seq2\nDGEL\n", DataSourceType.PASTE);
258     SequenceI cs1 = new Sequence("cseq1", "CCCGGGTTTAAA");
259     SequenceI cs2 = new Sequence("cseq2", "CTTGAGTCTAGA");
260     SequenceI s1 = af1.getViewport().getAlignment().getSequenceAt(0);
261     SequenceI s2 = af2.getViewport().getAlignment().getSequenceAt(0);
262     // need to be distinct
263     AlignedCodonFrame acf1 = new AlignedCodonFrame();
264     acf1.addMap(cs1, s1, new MapList(new int[] { 1, 4 },
265             new int[] { 1, 12 }, 1, 3));
266     AlignedCodonFrame acf2 = new AlignedCodonFrame();
267     acf2.addMap(cs2, s2, new MapList(new int[] { 1, 4 },
268             new int[] { 1, 12 }, 1, 3));
269     AlignedCodonFrame acf3 = new AlignedCodonFrame();
270     acf3.addMap(cs2, cs2, new MapList(new int[] { 1, 12 }, new int[] { 1,
271         12 }, 1, 1));
272
273     List<AlignedCodonFrame> mappings1 = new ArrayList<AlignedCodonFrame>();
274     mappings1.add(acf1);
275     mappings1.add(acf2);
276     af1.getViewport().getAlignment().setCodonFrames(mappings1);
277
278     List<AlignedCodonFrame> mappings2 = new ArrayList<AlignedCodonFrame>();
279     mappings2.add(acf2);
280     mappings2.add(acf3);
281     af2.getViewport().getAlignment().setCodonFrames(mappings2);
282
283     /*
284      * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
285      */
286
287     List<AlignedCodonFrame> ssmMappings = ssm.getSequenceMappings();
288     assertEquals(0, ssmMappings.size());
289     ssm.registerMapping(acf1);
290     assertEquals(1, ssmMappings.size());
291     ssm.registerMapping(acf2);
292     assertEquals(2, ssmMappings.size());
293     ssm.registerMapping(acf3);
294     assertEquals(3, ssmMappings.size());
295
296     /*
297      * Closing AlignFrame2 should remove mapping acf3 from
298      * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
299      * reference to it
300      */
301     af2.closeMenuItem_actionPerformed(true);
302     assertEquals(2, ssmMappings.size());
303     assertTrue(ssmMappings.contains(acf1));
304     assertTrue(ssmMappings.contains(acf2));
305     assertFalse(ssmMappings.contains(acf3));
306   }
307
308   /**
309    * Test for JAL-1306 - conservation thread should run even when only Quality
310    * (and not Conservation) is enabled in Preferences
311    */
312   @Test(groups = { "Functional" })
313   public void testUpdateConservation_qualityOnly()
314   {
315     Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS",
316             Boolean.TRUE.toString());
317     Cache.applicationProperties.setProperty("SHOW_QUALITY",
318             Boolean.TRUE.toString());
319     Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
320             Boolean.FALSE.toString());
321     Cache.applicationProperties.setProperty("SHOW_IDENTITY",
322             Boolean.FALSE.toString());
323     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
324             "examples/uniref50.fa", DataSourceType.FILE);
325     AlignmentAnnotation[] anns = af.viewport.getAlignment()
326             .getAlignmentAnnotation();
327     assertNotNull("No annotations found", anns);
328     assertEquals("More than one annotation found", 1, anns.length);
329     assertTrue("Annotation is not Quality",
330             anns[0].description.startsWith("Alignment Quality"));
331     Annotation[] annotations = anns[0].annotations;
332     assertNotNull("Quality annotations are null", annotations);
333     assertNotNull("Quality in column 1 is null", annotations[0]);
334     assertTrue("No quality value in column 1", annotations[0].value > 10f);
335   }
336
337   @Test(groups = { "Functional" })
338   public void testSetGlobalColourScheme()
339   {
340     /*
341      * test for JAL-2283 don't inadvertently turn on colour by conservation
342      */
343     Cache.applicationProperties.setProperty("DEFAULT_COLOUR_PROT", "NONE");
344     Cache.applicationProperties.setProperty("SHOW_CONSERVATION",
345             Boolean.TRUE.toString());
346     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
347             "examples/uniref50.fa", DataSourceType.FILE);
348     ColourSchemeI cs = new PIDColourScheme();
349     af.getViewport().setGlobalColourScheme(cs);
350     assertFalse(cs.conservationApplied());
351   }
352
353   @Test(groups = { "Functional" })
354   public void testSetGetHasSearchResults()
355   {
356     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
357             "examples/uniref50.fa", DataSourceType.FILE);
358     SearchResultsI sr = new SearchResults();
359     SequenceI s1 = af.getViewport().getAlignment().getSequenceAt(0);
360
361     // create arbitrary range on first sequence
362     sr.addResult(s1, s1.getStart() + 10, s1.getStart() + 15);
363
364     // test set
365     af.getViewport().setSearchResults(sr);
366     // has -> true
367     assertTrue(af.getViewport().hasSearchResults());
368     // get == original
369     assertEquals(sr, af.getViewport().getSearchResults());
370
371     // set(null) results in has -> false
372
373     af.getViewport().setSearchResults(null);
374     assertFalse(af.getViewport().hasSearchResults());
375   }
376 }