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