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