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