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