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