JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / test / jalview / io / Jalview2xmlTests.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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.io;
22
23 import static org.testng.AssertJUnit.assertTrue;
24
25 import jalview.api.AlignmentViewPanel;
26 import jalview.api.ViewStyleI;
27 import jalview.bin.Cache;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
31 import jalview.gui.AlignFrame;
32 import jalview.gui.Desktop;
33 import jalview.gui.Jalview2XML;
34 import jalview.schemes.AnnotationColourGradient;
35 import jalview.schemes.ColourSchemeI;
36
37 import java.io.File;
38
39 import org.testng.Assert;
40 import org.testng.AssertJUnit;
41 import org.testng.annotations.AfterClass;
42 import org.testng.annotations.BeforeClass;
43 import org.testng.annotations.Test;
44
45 public class Jalview2xmlTests
46 {
47
48   /**
49    * @throws java.lang.Exception
50    */
51   @BeforeClass(alwaysRun = true)
52   public static void setUpBeforeClass() throws Exception
53   {
54     jalview.bin.Jalview.main(new String[] { "-props",
55         "test/jalview/io/testProps.jvprops" });
56   }
57
58   /**
59    * @throws java.lang.Exception
60    */
61   @AfterClass
62   public static void tearDownAfterClass() throws Exception
63   {
64     jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
65
66   }
67
68   public int countDsAnn(jalview.viewmodel.AlignmentViewport avp)
69   {
70     int numdsann = 0;
71     for (SequenceI sq : avp.getAlignment().getDataset().getSequences())
72     {
73       if (sq.getAnnotation() != null)
74       {
75         for (AlignmentAnnotation dssa : sq.getAnnotation())
76         {
77           if (dssa.isValidStruc())
78           {
79             numdsann++;
80           }
81         }
82       }
83     }
84     return numdsann;
85   }
86
87   @Test(groups = { "Functional" })
88   public void testRNAStructureRecovery() throws Exception
89   {
90     String inFile = "examples/RF00031_folded.stk";
91     String tfile = File.createTempFile("JalviewTest", ".jvp")
92             .getAbsolutePath();
93     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
94             inFile, FormatAdapter.FILE);
95     assertTrue("Didn't read input file " + inFile, af != null);
96     int olddsann = countDsAnn(af.getViewport());
97     assertTrue("Didn't find any dataset annotations", olddsann > 0);
98     af.rnahelicesColour_actionPerformed(null);
99     assertTrue(
100             "Couldn't apply RNA helices colourscheme",
101             af.getViewport().getGlobalColourScheme() instanceof jalview.schemes.RNAHelicesColour);
102     assertTrue("Failed to store as a project.",
103             af.saveAlignment(tfile, "Jalview"));
104     af.closeMenuItem_actionPerformed(true);
105     af = null;
106     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
107             FormatAdapter.FILE);
108     assertTrue("Failed to import new project", af != null);
109     int newdsann = countDsAnn(af.getViewport());
110     assertTrue(
111             "Differing numbers of dataset sequence annotation\nOriginally "
112                     + olddsann + " and now " + newdsann,
113             olddsann == newdsann);
114     System.out
115             .println("Read in same number of annotations as originally present ("
116                     + olddsann + ")");
117     assertTrue(
118             "RNA helices colourscheme was not applied on import.",
119             af.getViewport().getGlobalColourScheme() instanceof jalview.schemes.RNAHelicesColour);
120   }
121
122   @Test(groups = { "Functional" })
123   public void testTCoffeeScores() throws Exception
124   {
125     String inFile = "examples/uniref50.fa", inAnnot = "examples/uniref50.score_ascii";
126     String tfile = File.createTempFile("JalviewTest", ".jvp")
127             .getAbsolutePath();
128     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
129             inFile, FormatAdapter.FILE);
130     assertTrue("Didn't read input file " + inFile, af != null);
131     af.loadJalviewDataFile(inAnnot, FormatAdapter.FILE, null, null);
132     assertTrue(
133             "Didn't set T-coffee colourscheme",
134             af.getViewport().getGlobalColourScheme().getClass()
135                     .equals(jalview.schemes.TCoffeeColourScheme.class));
136     assertTrue(
137             "Recognise T-Coffee score from string",
138             jalview.schemes.ColourSchemeProperty.getColour(af.getViewport()
139                     .getAlignment(),
140                     jalview.schemes.ColourSchemeProperty.getColourName(af
141                             .getViewport().getGlobalColourScheme())) != null);
142
143     assertTrue("Failed to store as a project.",
144             af.saveAlignment(tfile, "Jalview"));
145     af.closeMenuItem_actionPerformed(true);
146     af = null;
147     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
148             FormatAdapter.FILE);
149     assertTrue("Failed to import new project", af != null);
150     assertTrue(
151             "Didn't set T-coffee colourscheme for imported project.",
152             af.getViewport().getGlobalColourScheme().getClass()
153                     .equals(jalview.schemes.TCoffeeColourScheme.class));
154     System.out
155             .println("T-Coffee score shading successfully recovered from project.");
156   }
157
158   @Test(groups = { "Functional" })
159   public void testColourByAnnotScores() throws Exception
160   {
161     String inFile = "examples/uniref50.fa", inAnnot = "examples/testdata/uniref50_iupred.jva";
162     String tfile = File.createTempFile("JalviewTest", ".jvp")
163             .getAbsolutePath();
164     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
165             inFile, FormatAdapter.FILE);
166     assertTrue("Didn't read input file " + inFile, af != null);
167     af.loadJalviewDataFile(inAnnot, FormatAdapter.FILE, null, null);
168     AlignmentAnnotation[] aa = af.getViewport().getAlignment()
169             .getSequenceAt(0).getAnnotation("IUPredWS (Short)");
170     assertTrue(
171             "Didn't find any IUPred annotation to use to shade alignment.",
172             aa != null && aa.length > 0);
173     AnnotationColourGradient cs = new jalview.schemes.AnnotationColourGradient(
174             aa[0], null, AnnotationColourGradient.ABOVE_THRESHOLD);
175     AnnotationColourGradient gcs = new jalview.schemes.AnnotationColourGradient(
176             aa[0], null, AnnotationColourGradient.BELOW_THRESHOLD);
177     cs.setSeqAssociated(true);
178     gcs.setSeqAssociated(true);
179     af.changeColour(cs);
180     SequenceGroup sg = new SequenceGroup();
181     sg.setStartRes(57);
182     sg.setEndRes(92);
183     sg.cs = gcs;
184     af.getViewport().getAlignment().addGroup(sg);
185     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(1), false);
186     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(2), true);
187     af.alignPanel.alignmentChanged();
188     assertTrue("Failed to store as a project.",
189             af.saveAlignment(tfile, "Jalview"));
190     af.closeMenuItem_actionPerformed(true);
191     af = null;
192     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
193             FormatAdapter.FILE);
194     assertTrue("Failed to import new project", af != null);
195
196     // check for group and alignment colourschemes
197
198     ColourSchemeI _rcs = af.getViewport().getGlobalColourScheme();
199     ColourSchemeI _rgcs = af.getViewport().getAlignment().getGroups()
200             .get(0).cs;
201     assertTrue("Didn't recover global colourscheme", _rcs != null);
202     assertTrue("Didn't recover annotation colour global scheme",
203             _rcs instanceof AnnotationColourGradient);
204     AnnotationColourGradient __rcs = (AnnotationColourGradient) _rcs;
205     assertTrue("Annotation colourscheme wasn't sequence associated",
206             __rcs.isSeqAssociated());
207
208     boolean diffseqcols = false, diffgseqcols = false;
209     SequenceI[] sqs = af.getViewport().getAlignment().getSequencesArray();
210     for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
211             && (!diffseqcols || !diffgseqcols); p++)
212     {
213       if (_rcs.findColour(sqs[0].getCharAt(p), p, sqs[0]) != _rcs
214               .findColour(sqs[5].getCharAt(p), p, sqs[5]))
215       {
216         diffseqcols = true;
217       }
218     }
219     assertTrue("Got Different sequence colours", diffseqcols);
220     System.out
221             .println("Per sequence colourscheme (Background) successfully applied and recovered.");
222
223     assertTrue("Didn't recover group colourscheme", _rgcs != null);
224     assertTrue("Didn't recover annotation colour group colourscheme",
225             _rgcs instanceof AnnotationColourGradient);
226     __rcs = (AnnotationColourGradient) _rgcs;
227     assertTrue("Group Annotation colourscheme wasn't sequence associated",
228             __rcs.isSeqAssociated());
229
230     for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
231             && (!diffseqcols || !diffgseqcols); p++)
232     {
233       if (_rgcs.findColour(sqs[1].getCharAt(p), p, sqs[1]) != _rgcs
234               .findColour(sqs[2].getCharAt(p), p, sqs[2]))
235       {
236         diffgseqcols = true;
237       }
238     }
239     assertTrue("Got Different group sequence colours", diffgseqcols);
240     System.out
241             .println("Per sequence (Group) colourscheme successfully applied and recovered.");
242   }
243
244   @Test(groups = { "Functional" })
245   public void gatherViewsHere() throws Exception
246   {
247     int origCount = Desktop.getAlignFrames() == null ? 0 : Desktop
248             .getAlignFrames().length;
249     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
250             "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
251     assertTrue("Didn't read in the example file correctly.", af != null);
252     assertTrue("Didn't gather the views in the example file.",
253             Desktop.getAlignFrames().length == 1 + origCount);
254
255   }
256
257   @Test(groups = { "Functional" })
258   public void viewRefPdbAnnotation() throws Exception
259   {
260     Cache.applicationProperties.setProperty("STRUCT_FROM_PDB",
261             Boolean.TRUE.toString());
262     Cache.applicationProperties.setProperty("ADD_SS_ANN",
263             Boolean.TRUE.toString());
264     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
265             "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
266     assertTrue("Didn't read in the example file correctly.", af != null);
267     AlignmentViewPanel sps = null;
268     for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
269     {
270       if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
271       {
272         sps = ap;
273         break;
274       }
275     }
276     assertTrue("Couldn't find the structure view", sps != null);
277     SequenceI sq = sps.getAlignment().findName("1A70|");
278     AlignmentAnnotation refan = null;
279     for (AlignmentAnnotation ra : sps.getAlignment()
280             .getAlignmentAnnotation())
281     {
282       if (ra.graph != 0)
283       {
284         refan = ra;
285         break;
286       }
287     }
288     assertTrue("Annotation secondary structure not found.", refan != null);
289     assertTrue("Couldn't find 1a70 null chain", sq != null);
290     // compare the manually added temperature factor annotation
291     // to the track automatically transferred from the pdb structure on load
292     for (AlignmentAnnotation ala : sq.getDatasetSequence().getAnnotation())
293     {
294       AlignmentAnnotation alaa;
295       sq.addAlignmentAnnotation(alaa = new AlignmentAnnotation(ala));
296       alaa.adjustForAlignment();
297       if (ala.graph == refan.graph)
298       {
299         for (int p = 0; p < ala.annotations.length; p++)
300         {
301           sq.findPosition(p);
302           try
303           {
304             assertTrue(
305                     "Mismatch at alignment position " + p,
306                     (alaa.annotations[p] == null && refan.annotations[p] == null)
307                             || alaa.annotations[p].value == refan.annotations[p].value);
308           } catch (NullPointerException q)
309           {
310             Assert.fail("Mismatch of alignment annotations at position "
311                     + p + " Ref seq ann: " + refan.annotations[p]
312                     + " alignment " + alaa.annotations[p]);
313           }
314         }
315       }
316     }
317
318   }
319
320   @Test(groups = { "Functional" })
321   public void testCopyViewSettings() throws Exception
322   {
323     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
324             "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
325     assertTrue("Didn't read in the example file correctly.", af != null);
326     AlignmentViewPanel sps = null, groups = null;
327     for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
328     {
329       if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
330       {
331         sps = ap;
332       }
333       if (ap.getViewName().contains("MAFFT"))
334       {
335         groups = ap;
336       }
337     }
338     assertTrue("Couldn't find the structure view", sps != null);
339     assertTrue("Couldn't find the MAFFT view", groups != null);
340
341     ViewStyleI structureStyle = sps.getAlignViewport().getViewStyle();
342     ViewStyleI groupStyle = groups.getAlignViewport().getViewStyle();
343     AssertJUnit.assertFalse(structureStyle.sameStyle(groupStyle));
344
345     groups.getAlignViewport().setViewStyle(structureStyle);
346     AssertJUnit.assertFalse(groupStyle.sameStyle(groups.getAlignViewport()
347             .getViewStyle()));
348     Assert.assertTrue(structureStyle.sameStyle(groups.getAlignViewport()
349             .getViewStyle()));
350
351   }
352
353   /**
354    * test store and recovery of expanded views - currently this is disabled
355    * since the Desktop.explodeViews method doesn't seem to result in the views
356    * being expanded to distinct align frames when executed programmatically.
357    * 
358    * @throws Exception
359    */
360   @Test(groups = { "Functional" }, enabled = false)
361   public void testStoreAndRecoverExpandedviews() throws Exception
362   {
363     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
364             "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
365     assertTrue("Didn't read in the example file correctly.", af != null);
366     String afid = af.getViewport().getSequenceSetId();
367     {
368       final AlignFrame xaf = af;
369       af = null;
370       new Thread(new Runnable()
371       {
372         @Override
373         public void run()
374         {
375           Desktop.instance.explodeViews(xaf);
376         }
377       }).start();
378       Thread.sleep(1000);
379     }
380     // int times = 0;
381     // while (++times < 5 && Desktop.getAlignFrames().length < )
382     // {
383     // Thread.sleep(300);
384     // }
385     int oldviews = Desktop.getAlignFrames().length;
386     Assert.assertEquals(Desktop.getAlignFrames().length,
387             Desktop.getAlignmentPanels(afid).length);
388     File tfile = File.createTempFile("testStoreAndRecoverExpanded", ".jvp");
389     try
390     {
391       new Jalview2XML(false).saveState(tfile);
392     } catch (Error e)
393     {
394       Assert.fail("Didn't save the expanded view state", e);
395     } catch (Exception e)
396     {
397       Assert.fail("Didn't save the expanded view state", e);
398     }
399     Desktop.instance.closeAll_actionPerformed(null);
400     if (Desktop.getAlignFrames() != null)
401     {
402       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
403     }
404     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
405             tfile.getAbsolutePath(), FormatAdapter.FILE);
406     Assert.assertNotNull(af);
407     Assert.assertEquals(
408             Desktop.getAlignFrames().length,
409             Desktop.getAlignmentPanels(af.getViewport().getSequenceSetId()).length);
410     Assert.assertEquals(
411             oldviews,
412             Desktop.getAlignmentPanels(af.getViewport().getSequenceSetId()).length);
413   }
414
415 }