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