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