Merge branch 'develop' into features/filetypeEnum
[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.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.api.AlignViewportI;
30 import jalview.api.AlignmentViewPanel;
31 import jalview.api.ViewStyleI;
32 import jalview.bin.Cache;
33 import jalview.bin.Jalview;
34 import jalview.datamodel.AlignmentAnnotation;
35 import jalview.datamodel.AlignmentI;
36 import jalview.datamodel.HiddenSequences;
37 import jalview.datamodel.PDBEntry;
38 import jalview.datamodel.SequenceCollectionI;
39 import jalview.datamodel.SequenceGroup;
40 import jalview.datamodel.SequenceI;
41 import jalview.gui.AlignFrame;
42 import jalview.gui.AlignmentPanel;
43 import jalview.gui.Desktop;
44 import jalview.gui.Jalview2XML;
45 import jalview.schemes.AnnotationColourGradient;
46 import jalview.schemes.ColourSchemeI;
47 import jalview.structure.StructureImportSettings;
48 import jalview.viewmodel.AlignmentViewport;
49
50 import java.io.File;
51 import java.util.ArrayList;
52 import java.util.Date;
53 import java.util.HashMap;
54 import java.util.List;
55 import java.util.Map;
56
57 import org.testng.Assert;
58 import org.testng.AssertJUnit;
59 import org.testng.annotations.AfterClass;
60 import org.testng.annotations.BeforeClass;
61 import org.testng.annotations.Test;
62
63 @Test(singleThreaded = true)
64 public class Jalview2xmlTests
65 {
66
67   /**
68    * @throws java.lang.Exception
69    */
70   @BeforeClass(alwaysRun = true)
71   public static void setUpBeforeClass() throws Exception
72   {
73     /*
74      * use read-only test properties file
75      */
76     Cache.loadProperties("test/jalview/io/testProps.jvprops");
77
78     /*
79      * set news feed last read to a future time to ensure no
80      * 'unread' news item is displayed
81      */
82     Date oneHourFromNow = new Date(System.currentTimeMillis() + 3600 * 1000);
83     Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow);
84
85     Jalview.main(new String[] {});
86   }
87
88   /**
89    * @throws java.lang.Exception
90    */
91   @AfterClass(alwaysRun = true)
92   public static void tearDownAfterClass() throws Exception
93   {
94     Desktop.instance.closeAll_actionPerformed(null);
95   }
96
97   int countDsAnn(jalview.viewmodel.AlignmentViewport avp)
98   {
99     int numdsann = 0;
100     for (SequenceI sq : avp.getAlignment().getDataset().getSequences())
101     {
102       if (sq.getAnnotation() != null)
103       {
104         for (AlignmentAnnotation dssa : sq.getAnnotation())
105         {
106           if (dssa.isValidStruc())
107           {
108             numdsann++;
109           }
110         }
111       }
112     }
113     return numdsann;
114   }
115
116   @Test(groups = { "Functional" })
117   public void testRNAStructureRecovery() throws Exception
118   {
119     String inFile = "examples/RF00031_folded.stk";
120     String tfile = File.createTempFile("JalviewTest", ".jvp")
121             .getAbsolutePath();
122     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
123             inFile, DataSourceType.FILE);
124     assertTrue("Didn't read input file " + inFile, af != null);
125     int olddsann = countDsAnn(af.getViewport());
126     assertTrue("Didn't find any dataset annotations", olddsann > 0);
127     af.rnahelicesColour_actionPerformed(null);
128     assertTrue(
129             "Couldn't apply RNA helices colourscheme",
130             af.getViewport().getGlobalColourScheme() instanceof jalview.schemes.RNAHelicesColour);
131     assertTrue("Failed to store as a project.",
132             af.saveAlignment(tfile, FileFormat.Jalview));
133     af.closeMenuItem_actionPerformed(true);
134     af = null;
135     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
136             DataSourceType.FILE);
137     assertTrue("Failed to import new project", af != null);
138     int newdsann = countDsAnn(af.getViewport());
139     assertTrue(
140             "Differing numbers of dataset sequence annotation\nOriginally "
141                     + olddsann + " and now " + newdsann,
142             olddsann == newdsann);
143     System.out
144             .println("Read in same number of annotations as originally present ("
145                     + olddsann + ")");
146     assertTrue(
147             "RNA helices colourscheme was not applied on import.",
148             af.getViewport().getGlobalColourScheme() instanceof jalview.schemes.RNAHelicesColour);
149   }
150
151   @Test(groups = { "Functional" })
152   public void testTCoffeeScores() throws Exception
153   {
154     String inFile = "examples/uniref50.fa", inAnnot = "examples/uniref50.score_ascii";
155     String tfile = File.createTempFile("JalviewTest", ".jvp")
156             .getAbsolutePath();
157     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
158             inFile, DataSourceType.FILE);
159     assertTrue("Didn't read input file " + inFile, af != null);
160     af.loadJalviewDataFile(inAnnot, DataSourceType.FILE, null, null);
161     assertTrue(
162             "Didn't set T-coffee colourscheme",
163             af.getViewport().getGlobalColourScheme().getClass()
164                     .equals(jalview.schemes.TCoffeeColourScheme.class));
165     assertTrue(
166             "Recognise T-Coffee score from string",
167             jalview.schemes.ColourSchemeProperty.getColour(af.getViewport()
168                     .getAlignment(),
169                     jalview.schemes.ColourSchemeProperty.getColourName(af
170                             .getViewport().getGlobalColourScheme())) != null);
171
172     assertTrue("Failed to store as a project.",
173             af.saveAlignment(tfile, FileFormat.Jalview));
174     af.closeMenuItem_actionPerformed(true);
175     af = null;
176     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
177             DataSourceType.FILE);
178     assertTrue("Failed to import new project", af != null);
179     assertTrue(
180             "Didn't set T-coffee colourscheme for imported project.",
181             af.getViewport().getGlobalColourScheme().getClass()
182                     .equals(jalview.schemes.TCoffeeColourScheme.class));
183     System.out
184             .println("T-Coffee score shading successfully recovered from project.");
185   }
186
187   @Test(groups = { "Functional" })
188   public void testColourByAnnotScores() throws Exception
189   {
190     String inFile = "examples/uniref50.fa", inAnnot = "examples/testdata/uniref50_iupred.jva";
191     String tfile = File.createTempFile("JalviewTest", ".jvp")
192             .getAbsolutePath();
193     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
194             inFile, DataSourceType.FILE);
195     assertTrue("Didn't read input file " + inFile, af != null);
196     af.loadJalviewDataFile(inAnnot, DataSourceType.FILE, null, null);
197     AlignmentAnnotation[] aa = af.getViewport().getAlignment()
198             .getSequenceAt(0).getAnnotation("IUPredWS (Short)");
199     assertTrue(
200             "Didn't find any IUPred annotation to use to shade alignment.",
201             aa != null && aa.length > 0);
202     AnnotationColourGradient cs = new jalview.schemes.AnnotationColourGradient(
203             aa[0], null, AnnotationColourGradient.ABOVE_THRESHOLD);
204     AnnotationColourGradient gcs = new jalview.schemes.AnnotationColourGradient(
205             aa[0], null, AnnotationColourGradient.BELOW_THRESHOLD);
206     cs.setSeqAssociated(true);
207     gcs.setSeqAssociated(true);
208     af.changeColour(cs);
209     SequenceGroup sg = new SequenceGroup();
210     sg.setStartRes(57);
211     sg.setEndRes(92);
212     sg.cs = gcs;
213     af.getViewport().getAlignment().addGroup(sg);
214     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(1), false);
215     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(2), true);
216     af.alignPanel.alignmentChanged();
217     assertTrue("Failed to store as a project.",
218             af.saveAlignment(tfile, FileFormat.Jalview));
219     af.closeMenuItem_actionPerformed(true);
220     af = null;
221     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
222             DataSourceType.FILE);
223     assertTrue("Failed to import new project", af != null);
224
225     // check for group and alignment colourschemes
226
227     ColourSchemeI _rcs = af.getViewport().getGlobalColourScheme();
228     ColourSchemeI _rgcs = af.getViewport().getAlignment().getGroups()
229             .get(0).cs;
230     assertTrue("Didn't recover global colourscheme", _rcs != null);
231     assertTrue("Didn't recover annotation colour global scheme",
232             _rcs instanceof AnnotationColourGradient);
233     AnnotationColourGradient __rcs = (AnnotationColourGradient) _rcs;
234     assertTrue("Annotation colourscheme wasn't sequence associated",
235             __rcs.isSeqAssociated());
236
237     boolean diffseqcols = false, diffgseqcols = false;
238     SequenceI[] sqs = af.getViewport().getAlignment().getSequencesArray();
239     for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
240             && (!diffseqcols || !diffgseqcols); p++)
241     {
242       if (_rcs.findColour(sqs[0].getCharAt(p), p, sqs[0]) != _rcs
243               .findColour(sqs[5].getCharAt(p), p, sqs[5]))
244       {
245         diffseqcols = true;
246       }
247     }
248     assertTrue("Got Different sequence colours", diffseqcols);
249     System.out
250             .println("Per sequence colourscheme (Background) successfully applied and recovered.");
251
252     assertTrue("Didn't recover group colourscheme", _rgcs != null);
253     assertTrue("Didn't recover annotation colour group colourscheme",
254             _rgcs instanceof AnnotationColourGradient);
255     __rcs = (AnnotationColourGradient) _rgcs;
256     assertTrue("Group Annotation colourscheme wasn't sequence associated",
257             __rcs.isSeqAssociated());
258
259     for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
260             && (!diffseqcols || !diffgseqcols); p++)
261     {
262       if (_rgcs.findColour(sqs[1].getCharAt(p), p, sqs[1]) != _rgcs
263               .findColour(sqs[2].getCharAt(p), p, sqs[2]))
264       {
265         diffgseqcols = true;
266       }
267     }
268     assertTrue("Got Different group sequence colours", diffgseqcols);
269     System.out
270             .println("Per sequence (Group) colourscheme successfully applied and recovered.");
271   }
272
273   @Test(groups = { "Functional" })
274   public void gatherViewsHere() throws Exception
275   {
276     int origCount = Desktop.getAlignFrames() == null ? 0 : Desktop
277             .getAlignFrames().length;
278     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
279             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
280     assertTrue("Didn't read in the example file correctly.", af != null);
281     assertTrue("Didn't gather the views in the example file.",
282             Desktop.getAlignFrames().length == 1 + origCount);
283
284   }
285
286   @Test(groups = { "Functional" })
287   public void viewRefPdbAnnotation() throws Exception
288   {
289     // TODO: Make this pass without setting StructureParser.JALVIEW_PARSER
290     // StructureImportSettings
291     // .setDefaultPDBFileParser(StructureParser.JALVIEW_PARSER);
292     StructureImportSettings.setProcessSecondaryStructure(true);
293     StructureImportSettings.setVisibleChainAnnotation(true);
294     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
295             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
296     assertTrue("Didn't read in the example file correctly.", af != null);
297     AlignmentViewPanel sps = null;
298     for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
299     {
300       if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
301       {
302         sps = ap;
303         break;
304       }
305     }
306     assertTrue("Couldn't find the structure view", sps != null);
307     SequenceI sq = sps.getAlignment().findName("1A70|");
308     AlignmentAnnotation refan = null;
309     for (AlignmentAnnotation ra : sps.getAlignment()
310             .getAlignmentAnnotation())
311     {
312       if (ra.graph != 0)
313       {
314         refan = ra;
315         break;
316       }
317     }
318     assertTrue("Annotation secondary structure not found.", refan != null);
319     assertTrue("Couldn't find 1a70 null chain", sq != null);
320     // compare the manually added temperature factor annotation
321     // to the track automatically transferred from the pdb structure on load
322     for (AlignmentAnnotation ala : sq.getDatasetSequence().getAnnotation())
323     {
324       AlignmentAnnotation alaa;
325       sq.addAlignmentAnnotation(alaa = new AlignmentAnnotation(ala));
326       alaa.adjustForAlignment();
327       if (ala.graph == refan.graph)
328       {
329         for (int p = 0; p < ala.annotations.length; p++)
330         {
331           sq.findPosition(p);
332           try
333           {
334             assertTrue(
335                     "Mismatch at alignment position " + p,
336                     (alaa.annotations[p] == null && refan.annotations[p] == null)
337                             || alaa.annotations[p].value == refan.annotations[p].value);
338           } catch (NullPointerException q)
339           {
340             Assert.fail("Mismatch of alignment annotations at position "
341                     + p + " Ref seq ann: " + refan.annotations[p]
342                     + " alignment " + alaa.annotations[p]);
343           }
344         }
345       }
346     }
347
348   }
349
350   @Test(groups = { "Functional" })
351   public void testCopyViewSettings() throws Exception
352   {
353     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
354             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
355     assertTrue("Didn't read in the example file correctly.", af != null);
356     AlignmentViewPanel sps = null, groups = null;
357     for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
358     {
359       if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
360       {
361         sps = ap;
362       }
363       if (ap.getViewName().contains("MAFFT"))
364       {
365         groups = ap;
366       }
367     }
368     assertTrue("Couldn't find the structure view", sps != null);
369     assertTrue("Couldn't find the MAFFT view", groups != null);
370
371     ViewStyleI structureStyle = sps.getAlignViewport().getViewStyle();
372     ViewStyleI groupStyle = groups.getAlignViewport().getViewStyle();
373     AssertJUnit.assertFalse(structureStyle.sameStyle(groupStyle));
374
375     groups.getAlignViewport().setViewStyle(structureStyle);
376     AssertJUnit.assertFalse(groupStyle.sameStyle(groups.getAlignViewport()
377             .getViewStyle()));
378     Assert.assertTrue(structureStyle.sameStyle(groups.getAlignViewport()
379             .getViewStyle()));
380
381   }
382
383   /**
384    * test store and recovery of expanded views
385    * 
386    * @throws Exception
387    */
388   @Test(groups = { "Functional" }, enabled = true)
389   public void testStoreAndRecoverExpandedviews() throws Exception
390   {
391     Desktop.instance.closeAll_actionPerformed(null);
392
393     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
394             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
395     assertTrue("Didn't read in the example file correctly.", af != null);
396     Assert.assertEquals(Desktop.getAlignFrames().length, 1);
397     String afid = af.getViewport().getSequenceSetId();
398
399     // check FileLoader returned a reference to the one alignFrame that is
400     // actually on the Desktop
401     assertTrue(
402             "Jalview2XML.loadAlignFrame() didn't return correct AlignFrame reference for multiple view window",
403             af == Desktop.getAlignFrameFor(af.getViewport()));
404
405     Desktop.explodeViews(af);
406
407     int oldviews = Desktop.getAlignFrames().length;
408     Assert.assertEquals(Desktop.getAlignFrames().length,
409             Desktop.getAlignmentPanels(afid).length);
410     File tfile = File.createTempFile("testStoreAndRecoverExpanded", ".jvp");
411     try
412     {
413       new Jalview2XML(false).saveState(tfile);
414     } catch (Error e)
415     {
416       Assert.fail("Didn't save the expanded view state", e);
417     } catch (Exception e)
418     {
419       Assert.fail("Didn't save the expanded view state", e);
420     }
421     Desktop.instance.closeAll_actionPerformed(null);
422     if (Desktop.getAlignFrames() != null)
423     {
424       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
425     }
426     af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
427             tfile.getAbsolutePath(), DataSourceType.FILE);
428     Assert.assertNotNull(af);
429     Assert.assertEquals(
430             Desktop.getAlignFrames().length,
431             Desktop.getAlignmentPanels(af.getViewport().getSequenceSetId()).length);
432     Assert.assertEquals(
433             oldviews,
434             Desktop.getAlignmentPanels(af.getViewport().getSequenceSetId()).length);
435   }
436
437   /**
438    * Test save and reload of a project with a different representative sequence
439    * in each view.
440    * 
441    * @throws Exception
442    */
443   @Test(groups = { "Functional" })
444   public void testStoreAndRecoverReferenceSeqSettings() throws Exception
445   {
446     Desktop.instance.closeAll_actionPerformed(null);
447     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
448             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
449     assertTrue("Didn't read in the example file correctly.", af != null);
450     String afid = af.getViewport().getSequenceSetId();
451
452     // remember reference sequence for each panel
453     Map<String, SequenceI> refseqs = new HashMap<String, SequenceI>();
454
455     /*
456      * mark sequence 2, 3, 4.. in panels 1, 2, 3...
457      * as reference sequence for itself and the preceding sequence
458      */
459     int n = 1;
460     for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
461     {
462       AlignViewportI av = ap.getAlignViewport();
463       AlignmentI alignment = ap.getAlignment();
464       int repIndex = n % alignment.getHeight();
465       SequenceI rep = alignment.getSequenceAt(repIndex);
466       refseqs.put(ap.getViewName(), rep);
467
468       // code from mark/unmark sequence as reference in jalview.gui.PopupMenu
469       // todo refactor this to an alignment view controller
470       av.setDisplayReferenceSeq(true);
471       av.setColourByReferenceSeq(true);
472       av.getAlignment().setSeqrep(rep);
473
474       n++;
475     }
476     File tfile = File.createTempFile("testStoreAndRecoverReferenceSeq",
477             ".jvp");
478     try
479     {
480       new Jalview2XML(false).saveState(tfile);
481     } catch (Throwable e)
482     {
483       Assert.fail("Didn't save the expanded view state", e);
484     }
485     Desktop.instance.closeAll_actionPerformed(null);
486     if (Desktop.getAlignFrames() != null)
487     {
488       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
489     }
490
491     af = new FileLoader().LoadFileWaitTillLoaded(
492             tfile.getAbsolutePath(), DataSourceType.FILE);
493     afid = af.getViewport().getSequenceSetId();
494
495     for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
496     {
497       // check representative
498       AlignmentI alignment = ap.getAlignment();
499       SequenceI rep = alignment.getSeqrep();
500       Assert.assertNotNull(rep,
501               "Couldn't restore sequence representative from project");
502       // can't use a strong equals here, because by definition, the sequence IDs
503       // will be different.
504       // could set vamsas session save/restore flag to preserve IDs across
505       // load/saves.
506       Assert.assertEquals(refseqs.get(ap.getViewName()).toString(),
507               rep.toString(),
508               "Representative wasn't the same when recovered.");
509       Assert.assertTrue(ap.getAlignViewport().isDisplayReferenceSeq(),
510               "Display reference sequence view setting not set.");
511       Assert.assertTrue(ap.getAlignViewport().isColourByReferenceSeq(),
512               "Colour By Reference Seq view setting not set.");
513     }
514   }
515
516   @Test(groups = { "Functional" })
517   public void testIsVersionStringLaterThan()
518   {
519     /*
520      * No version / development / test / autobuild is leniently assumed to be
521      * compatible
522      */
523     assertTrue(Jalview2XML.isVersionStringLaterThan(null, null));
524     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", null));
525     assertTrue(Jalview2XML.isVersionStringLaterThan(null, "2.8.3"));
526     assertTrue(Jalview2XML.isVersionStringLaterThan(null,
527             "Development Build"));
528     assertTrue(Jalview2XML.isVersionStringLaterThan(null,
529             "DEVELOPMENT BUILD"));
530     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3",
531             "Development Build"));
532     assertTrue(Jalview2XML.isVersionStringLaterThan(null, "Test"));
533     assertTrue(Jalview2XML.isVersionStringLaterThan(null, "TEST"));
534     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "Test"));
535     assertTrue(Jalview2XML
536             .isVersionStringLaterThan(null, "Automated Build"));
537     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3",
538             "Automated Build"));
539     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3",
540             "AUTOMATED BUILD"));
541
542     /*
543      * same version returns true i.e. compatible
544      */
545     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8", "2.8"));
546     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.3"));
547     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3b1", "2.8.3b1"));
548     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3B1", "2.8.3b1"));
549     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3b1", "2.8.3B1"));
550
551     /*
552      * later version returns true
553      */
554     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.4"));
555     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.9"));
556     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.9.2"));
557     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8", "2.8.3"));
558     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.3b1"));
559
560     /*
561      * earlier version returns false
562      */
563     assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8"));
564     assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.4", "2.8.3"));
565     assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.3b1", "2.8.3"));
566     assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.2b1"));
567     assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.0b2", "2.8.0b1"));
568   }
569
570   /**
571    * Test save and reload of a project with a different sequence group (and
572    * representative sequence) in each view.
573    * 
574    * @throws Exception
575    */
576   @Test(groups = { "Functional" })
577   public void testStoreAndRecoverGroupRepSeqs() throws Exception
578   {
579     Desktop.instance.closeAll_actionPerformed(null);
580     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
581             "examples/uniref50.fa", DataSourceType.FILE);
582     assertTrue("Didn't read in the example file correctly.", af != null);
583     String afid = af.getViewport().getSequenceSetId();
584     // make a second view of the alignment
585     af.newView_actionPerformed(null);
586   
587     /*
588      * remember representative and hidden sequences marked 
589      * on each panel
590      */
591     Map<String, SequenceI> repSeqs = new HashMap<String, SequenceI>();
592     Map<String, List<String>> hiddenSeqNames = new HashMap<String, List<String>>();
593   
594     /*
595      * mark sequence 2, 3, 4.. in panels 1, 2, 3...
596      * as reference sequence for itself and the preceding sequence
597      */
598     int n = 1;
599     for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
600     {
601       AlignViewportI av = ap.getAlignViewport();
602       AlignmentI alignment = ap.getAlignment();
603       int repIndex = n % alignment.getHeight();
604       // ensure at least one preceding sequence i.e. index >= 1
605       repIndex = Math.max(repIndex, 1);
606       SequenceI repSeq = alignment.getSequenceAt(repIndex);
607       repSeqs.put(ap.getViewName(), repSeq);
608       List<String> hiddenNames = new ArrayList<String>();
609       hiddenSeqNames.put(ap.getViewName(), hiddenNames);
610   
611       /*
612        * have rep sequence represent itself and the one before it
613        * this hides the group (except for the rep seq)
614        */
615       SequenceGroup sg = new SequenceGroup();
616       sg.addSequence(repSeq, false);
617       SequenceI precedingSeq = alignment.getSequenceAt(repIndex - 1);
618       sg.addSequence(precedingSeq, false);
619       sg.setSeqrep(repSeq);
620       assertTrue(sg.getSequences().contains(repSeq));
621       assertTrue(sg.getSequences().contains(precedingSeq));
622       av.setSelectionGroup(sg);
623       assertSame(repSeq, sg.getSeqrep());
624
625       /*
626        * represent group with sequence adds to a map of hidden rep sequences
627        * (it does not create a group on the alignment) 
628        */
629       ((AlignmentViewport) av).hideSequences(repSeq, true);
630       assertSame(repSeq, sg.getSeqrep());
631       assertTrue(sg.getSequences().contains(repSeq));
632       assertTrue(sg.getSequences().contains(precedingSeq));
633       assertTrue("alignment has groups", alignment.getGroups().isEmpty());
634       Map<SequenceI, SequenceCollectionI> hiddenRepSeqsMap = av.getHiddenRepSequences();
635       assertNotNull(hiddenRepSeqsMap);
636       assertEquals(1, hiddenRepSeqsMap.size());
637       assertSame(sg, hiddenRepSeqsMap.get(repSeq));
638       assertTrue(alignment.getHiddenSequences().isHidden(precedingSeq));
639       assertFalse(alignment.getHiddenSequences().isHidden(repSeq));
640       hiddenNames.add(precedingSeq.getName());
641
642       n++;
643     }
644     File tfile = File
645             .createTempFile("testStoreAndRecoverGroupReps",
646             ".jvp");
647     try
648     {
649       new Jalview2XML(false).saveState(tfile);
650     } catch (Throwable e)
651     {
652       Assert.fail("Didn't save the expanded view state", e);
653     }
654     Desktop.instance.closeAll_actionPerformed(null);
655     if (Desktop.getAlignFrames() != null)
656     {
657       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
658     }
659   
660     af = new FileLoader().LoadFileWaitTillLoaded(
661             tfile.getAbsolutePath(), DataSourceType.FILE);
662     afid = af.getViewport().getSequenceSetId();
663   
664     for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
665     {
666       String viewName = ap.getViewName();
667       AlignViewportI av = ap.getAlignViewport();
668       AlignmentI alignment = ap.getAlignment();
669       List<SequenceGroup> groups = alignment.getGroups();
670       assertNotNull(groups);
671       assertTrue("Alignment has groups", groups.isEmpty());
672       Map<SequenceI, SequenceCollectionI> hiddenRepSeqsMap = av
673               .getHiddenRepSequences();
674       assertNotNull("No hidden represented sequences", hiddenRepSeqsMap);
675       assertEquals(1, hiddenRepSeqsMap.size());
676       assertEquals(repSeqs.get(viewName).getDisplayId(true),
677               hiddenRepSeqsMap.keySet().iterator().next()
678                       .getDisplayId(true));
679
680       /*
681        * verify hidden sequences in restored panel
682        */
683       List<String> hidden = hiddenSeqNames.get(ap.getViewName());
684       HiddenSequences hs = alignment.getHiddenSequences();
685       assertEquals(
686               "wrong number of restored hidden sequences in "
687                       + ap.getViewName(),
688               hidden.size(), hs.getSize());
689     }
690   }
691
692   /**
693    * Test save and reload of PDBEntry in Jalview project
694    * 
695    * @throws Exception
696    */
697   @Test(groups = { "Functional" })
698   public void testStoreAndRecoverPDBEntry() throws Exception
699   {
700     Desktop.instance.closeAll_actionPerformed(null);
701     String exampleFile = "examples/3W5V.pdb";
702     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(exampleFile,
703             DataSourceType.FILE);
704     assertTrue("Didn't read in the example file correctly.", af != null);
705     String afid = af.getViewport().getSequenceSetId();
706
707     AlignmentPanel[] alignPanels = Desktop.getAlignmentPanels(afid);
708     System.out.println();
709     AlignmentViewPanel ap = alignPanels[0];
710     String tfileBase = new File(".").getAbsolutePath().replace(".", "");
711     String testFile = tfileBase + exampleFile;
712     AlignmentI alignment = ap.getAlignment();
713     System.out.println("blah");
714     SequenceI[] seqs = alignment.getSequencesArray();
715     Assert.assertNotNull(seqs[0]);
716     Assert.assertNotNull(seqs[1]);
717     Assert.assertNotNull(seqs[2]);
718     Assert.assertNotNull(seqs[3]);
719     Assert.assertNotNull(seqs[0].getDatasetSequence());
720     Assert.assertNotNull(seqs[1].getDatasetSequence());
721     Assert.assertNotNull(seqs[2].getDatasetSequence());
722     Assert.assertNotNull(seqs[3].getDatasetSequence());
723     PDBEntry[] pdbEntries = new PDBEntry[4];
724     pdbEntries[0] = new PDBEntry("3W5V", "A", null, testFile);
725     pdbEntries[1] = new PDBEntry("3W5V", "B", null, testFile);
726     pdbEntries[2] = new PDBEntry("3W5V", "C", null, testFile);
727     pdbEntries[3] = new PDBEntry("3W5V", "D", null, testFile);
728     Assert.assertTrue(seqs[0].getDatasetSequence().getAllPDBEntries()
729             .get(0).equals(pdbEntries[0]));
730     Assert.assertTrue(seqs[1].getDatasetSequence().getAllPDBEntries()
731             .get(0).equals(pdbEntries[1]));
732     Assert.assertTrue(seqs[2].getDatasetSequence().getAllPDBEntries()
733             .get(0).equals(pdbEntries[2]));
734     Assert.assertTrue(seqs[3].getDatasetSequence().getAllPDBEntries()
735             .get(0).equals(pdbEntries[3]));
736
737     File tfile = File.createTempFile("testStoreAndRecoverPDBEntry", ".jvp");
738     try
739     {
740       new Jalview2XML(false).saveState(tfile);
741     } catch (Throwable e)
742     {
743       Assert.fail("Didn't save the state", e);
744     }
745     Desktop.instance.closeAll_actionPerformed(null);
746     if (Desktop.getAlignFrames() != null)
747     {
748       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
749     }
750
751     AlignFrame restoredFrame = new FileLoader().LoadFileWaitTillLoaded(
752             tfile.getAbsolutePath(), DataSourceType.FILE);
753     String rfid = restoredFrame.getViewport().getSequenceSetId();
754     AlignmentPanel[] rAlignPanels = Desktop.getAlignmentPanels(rfid);
755     AlignmentViewPanel rap = rAlignPanels[0];
756     AlignmentI rAlignment = rap.getAlignment();
757     System.out.println("blah");
758     SequenceI[] rseqs = rAlignment.getSequencesArray();
759     Assert.assertNotNull(rseqs[0]);
760     Assert.assertNotNull(rseqs[1]);
761     Assert.assertNotNull(rseqs[2]);
762     Assert.assertNotNull(rseqs[3]);
763     Assert.assertNotNull(rseqs[0].getDatasetSequence());
764     Assert.assertNotNull(rseqs[1].getDatasetSequence());
765     Assert.assertNotNull(rseqs[2].getDatasetSequence());
766     Assert.assertNotNull(rseqs[3].getDatasetSequence());
767
768     // The Asserts below are expected to fail until the PDB chainCode is
769     // recoverable from a Jalview projects
770     Assert.assertTrue(rseqs[0].getDatasetSequence().getAllPDBEntries()
771             .get(0).equals(pdbEntries[0]));
772     Assert.assertTrue(rseqs[1].getDatasetSequence().getAllPDBEntries()
773             .get(0).equals(pdbEntries[1]));
774     Assert.assertTrue(rseqs[2].getDatasetSequence().getAllPDBEntries()
775             .get(0).equals(pdbEntries[2]));
776     Assert.assertTrue(rseqs[3].getDatasetSequence().getAllPDBEntries()
777             .get(0).equals(pdbEntries[3]));
778   }
779 }