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