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