Merge branch 'features/JAL-1767pcaInProject' into bug/JAL-3171_maintain_datasets_acro...
[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.Assert.assertEquals;
24 import static org.testng.Assert.assertFalse;
25 import static org.testng.Assert.assertNotNull;
26 import static org.testng.Assert.assertNull;
27 import static org.testng.Assert.assertSame;
28 import static org.testng.Assert.assertTrue;
29 import static org.testng.Assert.fail;
30
31 import jalview.analysis.PCA;
32 import jalview.api.AlignViewportI;
33 import jalview.api.AlignmentViewPanel;
34 import jalview.api.FeatureColourI;
35 import jalview.api.ViewStyleI;
36 import jalview.bin.Cache;
37 import jalview.datamodel.AlignmentAnnotation;
38 import jalview.datamodel.AlignmentI;
39 import jalview.datamodel.HiddenSequences;
40 import jalview.datamodel.PDBEntry;
41 import jalview.datamodel.PDBEntry.Type;
42 import jalview.datamodel.Point;
43 import jalview.datamodel.SequenceCollectionI;
44 import jalview.datamodel.SequenceFeature;
45 import jalview.datamodel.SequenceGroup;
46 import jalview.datamodel.SequenceI;
47 import jalview.datamodel.SequencePoint;
48 import jalview.datamodel.features.FeatureMatcher;
49 import jalview.datamodel.features.FeatureMatcherSet;
50 import jalview.datamodel.features.FeatureMatcherSetI;
51 import jalview.gui.AlignFrame;
52 import jalview.gui.AlignViewport;
53 import jalview.gui.AlignmentPanel;
54 import jalview.gui.CalculationChooser;
55 import jalview.gui.Desktop;
56 import jalview.gui.FeatureRenderer;
57 import jalview.gui.Jalview2XML;
58 import jalview.gui.JvOptionPane;
59 import jalview.gui.PCAPanel;
60 import jalview.gui.PopupMenu;
61 import jalview.gui.RotatableCanvas;
62 import jalview.gui.SliderPanel;
63 import jalview.math.MatrixTest;
64 import jalview.renderer.ResidueShaderI;
65 import jalview.schemes.AnnotationColourGradient;
66 import jalview.schemes.BuriedColourScheme;
67 import jalview.schemes.ColourSchemeI;
68 import jalview.schemes.ColourSchemeProperty;
69 import jalview.schemes.FeatureColour;
70 import jalview.schemes.JalviewColourScheme;
71 import jalview.schemes.RNAHelicesColour;
72 import jalview.schemes.StrandColourScheme;
73 import jalview.schemes.TCoffeeColourScheme;
74 import jalview.structure.StructureImportSettings;
75 import jalview.util.matcher.Condition;
76 import jalview.viewmodel.AlignmentViewport;
77 import jalview.viewmodel.PCAModel;
78
79 import java.awt.Color;
80 import java.io.File;
81 import java.io.IOException;
82 import java.util.ArrayList;
83 import java.util.HashMap;
84 import java.util.List;
85 import java.util.Map;
86
87 import javax.swing.JInternalFrame;
88 import javax.swing.JRadioButton;
89
90 import org.testng.Assert;
91 import org.testng.AssertJUnit;
92 import org.testng.annotations.BeforeClass;
93 import org.testng.annotations.BeforeMethod;
94 import org.testng.annotations.Test;
95
96 import junit.extensions.PA;
97
98 @Test(singleThreaded = true)
99 public class Jalview2xmlTests extends Jalview2xmlBase
100 {
101   @BeforeClass(alwaysRun = true)
102   public void setUp()
103   {
104     Cache.loadProperties("test/jalview/io/testProps.jvprops");
105   }
106
107   @BeforeMethod(alwaysRun = true)
108   public void setUpMethod()
109   {
110     Desktop.instance.closeAll_actionPerformed(null);
111   }
112
113   @Override
114   @BeforeClass(alwaysRun = true)
115   public void setUpJvOptionPane()
116   {
117     JvOptionPane.setInteractiveMode(false);
118     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
119   }
120
121   @Test(groups = { "Functional" })
122   public void testRNAStructureRecovery() throws Exception
123   {
124     String inFile = "examples/RF00031_folded.stk";
125     String tfile = File.createTempFile("JalviewTest", ".jvp")
126             .getAbsolutePath();
127     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
128             DataSourceType.FILE);
129     assertNotNull(af, "Didn't read input file " + inFile);
130     int olddsann = countDsAnn(af.getViewport());
131     assertTrue(olddsann > 0, "Didn't find any dataset annotations");
132     af.changeColour_actionPerformed(JalviewColourScheme.RNAHelices
133             .toString());
134     assertTrue(
135             af.getViewport().getGlobalColourScheme() instanceof RNAHelicesColour,
136             "Couldn't apply RNA helices colourscheme");
137     assertTrue(af.saveAlignment(tfile, FileFormat.Jalview),
138             "Failed to store as a project.");
139     af.closeMenuItem_actionPerformed(true);
140     af = null;
141     af = new FileLoader()
142             .LoadFileWaitTillLoaded(tfile, DataSourceType.FILE);
143     assertNotNull(af, "Failed to import new project");
144     int newdsann = countDsAnn(af.getViewport());
145     assertEquals(olddsann, newdsann,
146             "Differing numbers of dataset sequence annotation\nOriginally "
147                     + olddsann + " and now " + newdsann);
148     System.out
149             .println("Read in same number of annotations as originally present ("
150                     + olddsann + ")");
151     assertTrue(
152
153     af.getViewport().getGlobalColourScheme() instanceof RNAHelicesColour,
154             "RNA helices colourscheme was not applied on import.");
155   }
156
157   @Test(groups = { "Functional" })
158   public void testTCoffeeScores() throws Exception
159   {
160     String inFile = "examples/uniref50.fa", inAnnot = "examples/uniref50.score_ascii";
161     String tfile = File.createTempFile("JalviewTest", ".jvp")
162             .getAbsolutePath();
163     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
164             DataSourceType.FILE);
165     assertNotNull(af, "Didn't read input file " + inFile);
166     af.loadJalviewDataFile(inAnnot, DataSourceType.FILE, null, null);
167     assertSame(af.getViewport().getGlobalColourScheme().getClass(),
168             TCoffeeColourScheme.class, "Didn't set T-coffee colourscheme");
169     assertNotNull(ColourSchemeProperty.getColourScheme(af.getViewport()
170             .getAlignment(), af.getViewport().getGlobalColourScheme()
171             .getSchemeName()), "Recognise T-Coffee score from string");
172
173     assertTrue(af.saveAlignment(tfile, FileFormat.Jalview),
174             "Failed to store as a project.");
175     af.closeMenuItem_actionPerformed(true);
176     af = null;
177     af = new FileLoader()
178             .LoadFileWaitTillLoaded(tfile, DataSourceType.FILE);
179     assertNotNull(af, "Failed to import new project");
180     assertSame(af.getViewport().getGlobalColourScheme().getClass(),
181             TCoffeeColourScheme.class,
182             "Didn't set T-coffee colourscheme for imported project.");
183     System.out
184             .println("T-Coffee score shading successfully recovered from project.");
185   }
186
187   @Test(groups = { "Functional" })
188   public void testColourByAnnotScores() throws Exception
189   {
190     String inFile = "examples/uniref50.fa", inAnnot = "examples/testdata/uniref50_iupred.jva";
191     String tfile = File.createTempFile("JalviewTest", ".jvp")
192             .getAbsolutePath();
193     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
194             DataSourceType.FILE);
195     assertNotNull(af, "Didn't read input file " + inFile);
196     af.loadJalviewDataFile(inAnnot, DataSourceType.FILE, null, null);
197     AlignmentAnnotation[] aa = af.getViewport().getAlignment()
198             .getSequenceAt(0).getAnnotation("IUPredWS (Short)");
199     assertTrue(
200
201     aa != null && aa.length > 0,
202             "Didn't find any IUPred annotation to use to shade alignment.");
203     AnnotationColourGradient cs = new AnnotationColourGradient(aa[0], null,
204             AnnotationColourGradient.ABOVE_THRESHOLD);
205     AnnotationColourGradient gcs = new AnnotationColourGradient(aa[0],
206             null, AnnotationColourGradient.BELOW_THRESHOLD);
207     cs.setSeqAssociated(true);
208     gcs.setSeqAssociated(true);
209     af.changeColour(cs);
210     SequenceGroup sg = new SequenceGroup();
211     sg.setStartRes(57);
212     sg.setEndRes(92);
213     sg.cs.setColourScheme(gcs);
214     af.getViewport().getAlignment().addGroup(sg);
215     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(1), false);
216     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(2), true);
217     af.alignPanel.alignmentChanged();
218     assertTrue(af.saveAlignment(tfile, FileFormat.Jalview),
219             "Failed to store as a project.");
220     af.closeMenuItem_actionPerformed(true);
221     af = null;
222     af = new FileLoader()
223             .LoadFileWaitTillLoaded(tfile, DataSourceType.FILE);
224     assertNotNull(af, "Failed to import new project");
225
226     // check for group and alignment colourschemes
227
228     ColourSchemeI _rcs = af.getViewport().getGlobalColourScheme();
229     ColourSchemeI _rgcs = af.getViewport().getAlignment().getGroups()
230             .get(0).getColourScheme();
231     assertNotNull(_rcs, "Didn't recover global colourscheme");
232     assertTrue(_rcs instanceof AnnotationColourGradient,
233             "Didn't recover annotation colour global scheme");
234     AnnotationColourGradient __rcs = (AnnotationColourGradient) _rcs;
235     assertTrue(__rcs.isSeqAssociated(),
236             "Annotation colourscheme wasn't sequence associated");
237
238     boolean diffseqcols = false, diffgseqcols = false;
239     SequenceI[] sqs = af.getViewport().getAlignment().getSequencesArray();
240     for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
241             && (!diffseqcols || !diffgseqcols); p++)
242     {
243       if (_rcs.findColour(sqs[0].getCharAt(p), p, sqs[0], null, 0f) != _rcs
244               .findColour(sqs[5].getCharAt(p), p, sqs[5], null, 0f))
245       {
246         diffseqcols = true;
247       }
248     }
249     assertTrue(diffseqcols, "Got Different sequence colours");
250     System.out
251             .println("Per sequence colourscheme (Background) successfully applied and recovered.");
252
253     assertNotNull(_rgcs, "Didn't recover group colourscheme");
254     assertTrue(_rgcs instanceof AnnotationColourGradient,
255             "Didn't recover annotation colour group colourscheme");
256     __rcs = (AnnotationColourGradient) _rgcs;
257     assertTrue(__rcs.isSeqAssociated(),
258             "Group Annotation colourscheme wasn't sequence associated");
259
260     for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
261             && (!diffseqcols || !diffgseqcols); p++)
262     {
263       if (_rgcs.findColour(sqs[1].getCharAt(p), p, sqs[1], null, 0f) != _rgcs
264               .findColour(sqs[2].getCharAt(p), p, sqs[2], null, 0f))
265       {
266         diffgseqcols = true;
267       }
268     }
269     assertTrue(diffgseqcols, "Got Different group sequence colours");
270     System.out
271             .println("Per sequence (Group) colourscheme successfully applied and recovered.");
272   }
273
274   @Test(groups = { "Functional" })
275   public void gatherViewsHere() throws Exception
276   {
277     int origCount = Desktop.getAlignFrames() == null ? 0 : Desktop
278             .getAlignFrames().length;
279     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
280             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
281     assertNotNull(af, "Didn't read in the example file correctly.");
282     assertTrue(Desktop.getAlignFrames().length == 1 + origCount,
283             "Didn't gather the views in the example file.");
284
285   }
286
287   /**
288    * Test for JAL-2223 - multiple mappings in View Mapping report
289    * 
290    * @throws Exception
291    */
292   @Test(groups = { "Functional" })
293   public void noDuplicatePdbMappingsMade() throws Exception
294   {
295     StructureImportSettings.setProcessSecondaryStructure(true);
296     StructureImportSettings.setVisibleChainAnnotation(true);
297     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
298             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
299     assertNotNull(af, "Didn't read in the example file correctly.");
300
301     // locate Jmol viewer
302     // count number of PDB mappings the structure selection manager holds -
303     String pdbFile = af.getCurrentView().getStructureSelectionManager()
304             .findFileForPDBId("1A70");
305     assertEquals(
306             af.getCurrentView().getStructureSelectionManager()
307                     .getMapping(pdbFile).length,
308             2, "Expected only two mappings for 1A70");
309
310   }
311
312   @Test(groups = { "Functional" })
313   public void viewRefPdbAnnotation() throws Exception
314   {
315     StructureImportSettings.setProcessSecondaryStructure(true);
316     StructureImportSettings.setVisibleChainAnnotation(true);
317     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
318             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
319     assertNotNull(af, "Didn't read in the example file correctly.");
320     AlignmentViewPanel sps = null;
321     for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
322     {
323       if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
324       {
325         sps = ap;
326         break;
327       }
328     }
329     assertNotNull(sps, "Couldn't find the structure view");
330     AlignmentAnnotation refan = null;
331     for (AlignmentAnnotation ra : sps.getAlignment()
332             .getAlignmentAnnotation())
333     {
334       if (ra.graph != 0)
335       {
336         refan = ra;
337         break;
338       }
339     }
340     assertNotNull(refan, "Annotation secondary structure not found.");
341     SequenceI sq = sps.getAlignment().findName("1A70|");
342     assertNotNull(sq, "Couldn't find 1a70 null chain");
343     // compare the manually added temperature factor annotation
344     // to the track automatically transferred from the pdb structure on load
345     assertNotNull(sq.getDatasetSequence().getAnnotation(),
346             "1a70 has no annotation");
347     for (AlignmentAnnotation ala : sq.getDatasetSequence().getAnnotation())
348     {
349       AlignmentAnnotation alaa;
350       sq.addAlignmentAnnotation(alaa = new AlignmentAnnotation(ala));
351       alaa.adjustForAlignment();
352       if (ala.graph == refan.graph)
353       {
354         for (int p = 0; p < ala.annotations.length; p++)
355         {
356           sq.findPosition(p);
357           try
358           {
359             assertTrue(
360                     (alaa.annotations[p] == null && refan.annotations[p] == null)
361                             || alaa.annotations[p].value == refan.annotations[p].value,
362                     "Mismatch at alignment position " + p);
363           } catch (NullPointerException q)
364           {
365             Assert.fail("Mismatch of alignment annotations at position "
366                     + p + " Ref seq ann: " + refan.annotations[p]
367                     + " alignment " + alaa.annotations[p]);
368           }
369         }
370       }
371     }
372
373   }
374
375   @Test(groups = { "Functional" })
376   public void testCopyViewSettings() throws Exception
377   {
378     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
379             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
380     assertNotNull(af, "Didn't read in the example file correctly.");
381     AlignmentViewPanel sps = null, groups = null;
382     for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
383     {
384       if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
385       {
386         sps = ap;
387       }
388       if (ap.getViewName().contains("MAFFT"))
389       {
390         groups = ap;
391       }
392     }
393     assertNotNull(sps, "Couldn't find the structure view");
394     assertNotNull(groups, "Couldn't find the MAFFT view");
395
396     ViewStyleI structureStyle = sps.getAlignViewport().getViewStyle();
397     ViewStyleI groupStyle = groups.getAlignViewport().getViewStyle();
398     AssertJUnit.assertFalse(structureStyle.sameStyle(groupStyle));
399
400     groups.getAlignViewport().setViewStyle(structureStyle);
401     AssertJUnit.assertFalse(groupStyle.sameStyle(groups.getAlignViewport()
402             .getViewStyle()));
403     Assert.assertTrue(structureStyle.sameStyle(groups.getAlignViewport()
404             .getViewStyle()));
405
406   }
407
408   /**
409    * test store and recovery of expanded views
410    * 
411    * @throws Exception
412    */
413   @Test(groups = { "Functional" }, enabled = true)
414   public void testStoreAndRecoverExpandedviews() throws Exception
415   {
416     Desktop.instance.closeAll_actionPerformed(null);
417
418     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
419             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
420     Assert.assertEquals(Desktop.getAlignFrames().length, 1);
421     String afid = af.getViewport().getSequenceSetId();
422
423     // check FileLoader returned a reference to the one alignFrame that is
424     // actually on the Desktop
425     assertSame(
426             af,
427             Desktop.getAlignFrameFor(af.getViewport()),
428             "Jalview2XML.loadAlignFrame() didn't return correct AlignFrame reference for multiple view window");
429
430     Desktop.explodeViews(af);
431
432     int oldviews = Desktop.getAlignFrames().length;
433     Assert.assertEquals(Desktop.getAlignFrames().length,
434             Desktop.getAlignmentPanels(afid).length);
435     File tfile = File.createTempFile("testStoreAndRecoverExpanded", ".jvp");
436     try
437     {
438       new Jalview2XML(false).saveState(tfile);
439     } catch (Error e)
440     {
441       Assert.fail("Didn't save the expanded view state", e);
442     } catch (Exception e)
443     {
444       Assert.fail("Didn't save the expanded view state", e);
445     }
446     Desktop.instance.closeAll_actionPerformed(null);
447     if (Desktop.getAlignFrames() != null)
448     {
449       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
450     }
451     af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
452             DataSourceType.FILE);
453     Assert.assertNotNull(af);
454     Assert.assertEquals(
455             Desktop.getAlignFrames().length,
456             Desktop.getAlignmentPanels(af.getViewport().getSequenceSetId()).length);
457     Assert.assertEquals(
458             Desktop.getAlignmentPanels(af.getViewport().getSequenceSetId()).length,
459             oldviews);
460   }
461
462   /**
463    * Test save and reload of a project with a different representative sequence
464    * in each view.
465    * 
466    * @throws Exception
467    */
468   @Test(groups = { "Functional" })
469   public void testStoreAndRecoverReferenceSeqSettings() throws Exception
470   {
471     Desktop.instance.closeAll_actionPerformed(null);
472     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
473             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
474     assertNotNull(af, "Didn't read in the example file correctly.");
475     String afid = af.getViewport().getSequenceSetId();
476
477     // remember reference sequence for each panel
478     Map<String, SequenceI> refseqs = new HashMap<>();
479
480     /*
481      * mark sequence 2, 3, 4.. in panels 1, 2, 3...
482      * as reference sequence for itself and the preceding sequence
483      */
484     int n = 1;
485     for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
486     {
487       AlignViewportI av = ap.getAlignViewport();
488       AlignmentI alignment = ap.getAlignment();
489       int repIndex = n % alignment.getHeight();
490       SequenceI rep = alignment.getSequenceAt(repIndex);
491       refseqs.put(ap.getViewName(), rep);
492
493       // code from mark/unmark sequence as reference in jalview.gui.PopupMenu
494       // todo refactor this to an alignment view controller
495       av.setDisplayReferenceSeq(true);
496       av.setColourByReferenceSeq(true);
497       av.getAlignment().setSeqrep(rep);
498
499       n++;
500     }
501     File tfile = File.createTempFile("testStoreAndRecoverReferenceSeq",
502             ".jvp");
503     try
504     {
505       new Jalview2XML(false).saveState(tfile);
506     } catch (Throwable e)
507     {
508       Assert.fail("Didn't save the expanded view state", e);
509     }
510     Desktop.instance.closeAll_actionPerformed(null);
511     if (Desktop.getAlignFrames() != null)
512     {
513       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
514     }
515
516     af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
517             DataSourceType.FILE);
518     afid = af.getViewport().getSequenceSetId();
519
520     for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
521     {
522       // check representative
523       AlignmentI alignment = ap.getAlignment();
524       SequenceI rep = alignment.getSeqrep();
525       Assert.assertNotNull(rep,
526               "Couldn't restore sequence representative from project");
527       // can't use a strong equals here, because by definition, the sequence IDs
528       // will be different.
529       // could set vamsas session save/restore flag to preserve IDs across
530       // load/saves.
531       Assert.assertEquals(refseqs.get(ap.getViewName()).toString(),
532               rep.toString(),
533               "Representative wasn't the same when recovered.");
534       Assert.assertTrue(ap.getAlignViewport().isDisplayReferenceSeq(),
535               "Display reference sequence view setting not set.");
536       Assert.assertTrue(ap.getAlignViewport().isColourByReferenceSeq(),
537               "Colour By Reference Seq view setting not set.");
538     }
539   }
540
541   @Test(groups = { "Functional" })
542   public void testIsVersionStringLaterThan()
543   {
544     /*
545      * No version / development / test / autobuild is leniently assumed to be
546      * compatible
547      */
548     assertTrue(Jalview2XML.isVersionStringLaterThan(null, null));
549     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", null));
550     assertTrue(Jalview2XML.isVersionStringLaterThan(null, "2.8.3"));
551     assertTrue(Jalview2XML.isVersionStringLaterThan(null,
552             "Development Build"));
553     assertTrue(Jalview2XML.isVersionStringLaterThan(null,
554             "DEVELOPMENT BUILD"));
555     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3",
556             "Development Build"));
557     assertTrue(Jalview2XML.isVersionStringLaterThan(null, "Test"));
558     assertTrue(Jalview2XML.isVersionStringLaterThan(null, "TEST"));
559     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "Test"));
560     assertTrue(Jalview2XML
561             .isVersionStringLaterThan(null, "Automated Build"));
562     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3",
563             "Automated Build"));
564     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3",
565             "AUTOMATED BUILD"));
566
567     /*
568      * same version returns true i.e. compatible
569      */
570     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8", "2.8"));
571     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.3"));
572     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3b1", "2.8.3b1"));
573     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3B1", "2.8.3b1"));
574     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3b1", "2.8.3B1"));
575
576     /*
577      * later version returns true
578      */
579     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.4"));
580     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.9"));
581     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.9.2"));
582     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8", "2.8.3"));
583     assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.3b1"));
584
585     /*
586      * earlier version returns false
587      */
588     assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8"));
589     assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.4", "2.8.3"));
590     assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.3b1", "2.8.3"));
591     assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.2b1"));
592     assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.0b2", "2.8.0b1"));
593   }
594
595   /**
596    * Test save and reload of a project with a different sequence group (and
597    * representative sequence) in each view.
598    * 
599    * @throws Exception
600    */
601   @Test(groups = { "Functional" })
602   public void testStoreAndRecoverGroupRepSeqs() throws Exception
603   {
604     Desktop.instance.closeAll_actionPerformed(null);
605     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
606             "examples/uniref50.fa", DataSourceType.FILE);
607     assertNotNull(af, "Didn't read in the example file correctly.");
608     String afid = af.getViewport().getSequenceSetId();
609     // make a second view of the alignment
610     af.newView_actionPerformed(null);
611
612     /*
613      * remember representative and hidden sequences marked 
614      * on each panel
615      */
616     Map<String, SequenceI> repSeqs = new HashMap<>();
617     Map<String, List<String>> hiddenSeqNames = new HashMap<>();
618
619     /*
620      * mark sequence 2, 3, 4.. in panels 1, 2, 3...
621      * as reference sequence for itself and the preceding sequence
622      */
623     int n = 1;
624     for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
625     {
626       AlignViewportI av = ap.getAlignViewport();
627       AlignmentI alignment = ap.getAlignment();
628       int repIndex = n % alignment.getHeight();
629       // ensure at least one preceding sequence i.e. index >= 1
630       repIndex = Math.max(repIndex, 1);
631       SequenceI repSeq = alignment.getSequenceAt(repIndex);
632       repSeqs.put(ap.getViewName(), repSeq);
633       List<String> hiddenNames = new ArrayList<>();
634       hiddenSeqNames.put(ap.getViewName(), hiddenNames);
635
636       /*
637        * have rep sequence represent itself and the one before it
638        * this hides the group (except for the rep seq)
639        */
640       SequenceGroup sg = new SequenceGroup();
641       sg.addSequence(repSeq, false);
642       SequenceI precedingSeq = alignment.getSequenceAt(repIndex - 1);
643       sg.addSequence(precedingSeq, false);
644       sg.setSeqrep(repSeq);
645       assertTrue(sg.getSequences().contains(repSeq));
646       assertTrue(sg.getSequences().contains(precedingSeq));
647       av.setSelectionGroup(sg);
648       assertSame(repSeq, sg.getSeqrep());
649
650       /*
651        * represent group with sequence adds to a map of hidden rep sequences
652        * (it does not create a group on the alignment) 
653        */
654       ((AlignmentViewport) av).hideSequences(repSeq, true);
655       assertSame(repSeq, sg.getSeqrep());
656       assertTrue(sg.getSequences().contains(repSeq));
657       assertTrue(sg.getSequences().contains(precedingSeq));
658       assertTrue(alignment.getGroups().isEmpty(), "alignment has groups");
659       Map<SequenceI, SequenceCollectionI> hiddenRepSeqsMap = av
660               .getHiddenRepSequences();
661       assertNotNull(hiddenRepSeqsMap);
662       assertEquals(1, hiddenRepSeqsMap.size());
663       assertSame(sg, hiddenRepSeqsMap.get(repSeq));
664       assertTrue(alignment.getHiddenSequences().isHidden(precedingSeq));
665       assertFalse(alignment.getHiddenSequences().isHidden(repSeq));
666       hiddenNames.add(precedingSeq.getName());
667
668       n++;
669     }
670     File tfile = File
671             .createTempFile("testStoreAndRecoverGroupReps", ".jvp");
672     try
673     {
674       new Jalview2XML(false).saveState(tfile);
675     } catch (Throwable e)
676     {
677       Assert.fail("Didn't save the expanded view state", e);
678     }
679     Desktop.instance.closeAll_actionPerformed(null);
680     if (Desktop.getAlignFrames() != null)
681     {
682       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
683     }
684
685     af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
686             DataSourceType.FILE);
687     afid = af.getViewport().getSequenceSetId();
688
689     for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
690     {
691       String viewName = ap.getViewName();
692       AlignViewportI av = ap.getAlignViewport();
693       AlignmentI alignment = ap.getAlignment();
694       List<SequenceGroup> groups = alignment.getGroups();
695       assertNotNull(groups);
696       assertTrue(groups.isEmpty(), "Alignment has groups");
697       Map<SequenceI, SequenceCollectionI> hiddenRepSeqsMap = av
698               .getHiddenRepSequences();
699       assertNotNull(hiddenRepSeqsMap, "No hidden represented sequences");
700       assertEquals(1, hiddenRepSeqsMap.size());
701       assertEquals(repSeqs.get(viewName).getDisplayId(true),
702               hiddenRepSeqsMap.keySet().iterator().next()
703                       .getDisplayId(true));
704
705       /*
706        * verify hidden sequences in restored panel
707        */
708       List<String> hidden = hiddenSeqNames.get(ap.getViewName());
709       HiddenSequences hs = alignment.getHiddenSequences();
710       assertEquals(
711               hidden.size(),
712               hs.getSize(),
713               "wrong number of restored hidden sequences in "
714                       + ap.getViewName());
715     }
716   }
717
718   /**
719    * Test save and reload of PDBEntry in Jalview project
720    * 
721    * @throws Exception
722    */
723   @Test(groups = { "Functional" })
724   public void testStoreAndRecoverPDBEntry() throws Exception
725   {
726     Desktop.instance.closeAll_actionPerformed(null);
727     String exampleFile = "examples/3W5V.pdb";
728     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(exampleFile,
729             DataSourceType.FILE);
730     assertNotNull(af, "Didn't read in the example file correctly.");
731     String afid = af.getViewport().getSequenceSetId();
732
733     AlignmentPanel[] alignPanels = Desktop.getAlignmentPanels(afid);
734     System.out.println();
735     AlignmentViewPanel ap = alignPanels[0];
736     String tfileBase = new File(".").getAbsolutePath().replace(".", "");
737     String testFile = tfileBase + exampleFile;
738     AlignmentI alignment = ap.getAlignment();
739     System.out.println("blah");
740     SequenceI[] seqs = alignment.getSequencesArray();
741     Assert.assertNotNull(seqs[0]);
742     Assert.assertNotNull(seqs[1]);
743     Assert.assertNotNull(seqs[2]);
744     Assert.assertNotNull(seqs[3]);
745     Assert.assertNotNull(seqs[0].getDatasetSequence());
746     Assert.assertNotNull(seqs[1].getDatasetSequence());
747     Assert.assertNotNull(seqs[2].getDatasetSequence());
748     Assert.assertNotNull(seqs[3].getDatasetSequence());
749     PDBEntry[] pdbEntries = new PDBEntry[4];
750     pdbEntries[0] = new PDBEntry("3W5V", "A", Type.PDB, testFile);
751     pdbEntries[1] = new PDBEntry("3W5V", "B", Type.PDB, testFile);
752     pdbEntries[2] = new PDBEntry("3W5V", "C", Type.PDB, testFile);
753     pdbEntries[3] = new PDBEntry("3W5V", "D", Type.PDB, testFile);
754     Assert.assertEquals(seqs[0].getDatasetSequence().getAllPDBEntries()
755             .get(0), pdbEntries[0]);
756     Assert.assertEquals(seqs[1].getDatasetSequence().getAllPDBEntries()
757             .get(0), pdbEntries[1]);
758     Assert.assertEquals(seqs[2].getDatasetSequence().getAllPDBEntries()
759             .get(0), pdbEntries[2]);
760     Assert.assertEquals(seqs[3].getDatasetSequence().getAllPDBEntries()
761             .get(0), pdbEntries[3]);
762
763     File tfile = File.createTempFile("testStoreAndRecoverPDBEntry", ".jvp");
764     try
765     {
766       new Jalview2XML(false).saveState(tfile);
767     } catch (Throwable e)
768     {
769       Assert.fail("Didn't save the state", e);
770     }
771     Desktop.instance.closeAll_actionPerformed(null);
772     if (Desktop.getAlignFrames() != null)
773     {
774       Assert.assertEquals(Desktop.getAlignFrames().length, 0);
775     }
776
777     AlignFrame restoredFrame = new FileLoader().LoadFileWaitTillLoaded(
778             tfile.getAbsolutePath(), DataSourceType.FILE);
779     String rfid = restoredFrame.getViewport().getSequenceSetId();
780     AlignmentPanel[] rAlignPanels = Desktop.getAlignmentPanels(rfid);
781     AlignmentViewPanel rap = rAlignPanels[0];
782     AlignmentI rAlignment = rap.getAlignment();
783     System.out.println("blah");
784     SequenceI[] rseqs = rAlignment.getSequencesArray();
785     Assert.assertNotNull(rseqs[0]);
786     Assert.assertNotNull(rseqs[1]);
787     Assert.assertNotNull(rseqs[2]);
788     Assert.assertNotNull(rseqs[3]);
789     Assert.assertNotNull(rseqs[0].getDatasetSequence());
790     Assert.assertNotNull(rseqs[1].getDatasetSequence());
791     Assert.assertNotNull(rseqs[2].getDatasetSequence());
792     Assert.assertNotNull(rseqs[3].getDatasetSequence());
793
794     // The Asserts below are expected to fail until the PDB chainCode is
795     // recoverable from a Jalview projects
796     for (int chain = 0; chain < 4; chain++)
797     {
798       PDBEntry recov = rseqs[chain].getDatasetSequence().getAllPDBEntries()
799               .get(0);
800       PDBEntry expected = pdbEntries[chain];
801       Assert.assertEquals(recov.getId(), expected.getId(),
802               "Mismatch PDB ID");
803       Assert.assertEquals(recov.getChainCode(), expected.getChainCode(),
804               "Mismatch PDB ID");
805       Assert.assertEquals(recov.getType(), expected.getType(),
806               "Mismatch PDBEntry 'Type'");
807       Assert.assertNotNull(recov.getFile(),
808               "Recovered PDBEntry should have a non-null file entry");
809     }
810   }
811
812   /**
813    * Configure an alignment and a sub-group each with distinct colour schemes,
814    * Conservation and PID thresholds, and confirm these are restored from the
815    * saved project.
816    * 
817    * @throws IOException
818    */
819   @Test(groups = { "Functional" })
820   public void testStoreAndRecoverColourThresholds() throws IOException
821   {
822     Desktop.instance.closeAll_actionPerformed(null);
823     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
824             "examples/uniref50.fa", DataSourceType.FILE);
825
826     AlignViewport av = af.getViewport();
827     AlignmentI al = av.getAlignment();
828
829     /*
830      * Colour alignment by Buried Index, Above 10% PID, By Conservation 20%
831      */
832     af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
833     assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
834     af.abovePIDThreshold_actionPerformed(true);
835     SliderPanel sp = SliderPanel.getSliderPanel();
836     assertFalse(sp.isForConservation());
837     sp.valueChanged(10);
838     af.conservationMenuItem_actionPerformed(true);
839     sp = SliderPanel.getSliderPanel();
840     assertTrue(sp.isForConservation());
841     sp.valueChanged(20);
842     ResidueShaderI rs = av.getResidueShading();
843     assertEquals(rs.getThreshold(), 10);
844     assertTrue(rs.conservationApplied());
845     assertEquals(rs.getConservationInc(), 20);
846
847     /*
848      * create a group with Strand colouring, 30% Conservation
849      * and 40% PID threshold
850      */
851     SequenceGroup sg = new SequenceGroup();
852     sg.addSequence(al.getSequenceAt(0), false);
853     sg.setStartRes(15);
854     sg.setEndRes(25);
855     av.setSelectionGroup(sg);
856     PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
857     popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
858             .toString());
859     assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
860     assertEquals(al.getGroups().size(), 1);
861     assertSame(al.getGroups().get(0), sg);
862     popupMenu.conservationMenuItem_actionPerformed(true);
863     sp = SliderPanel.getSliderPanel();
864     assertTrue(sp.isForConservation());
865     sp.valueChanged(30);
866     popupMenu.abovePIDColour_actionPerformed(true);
867     sp = SliderPanel.getSliderPanel();
868     assertFalse(sp.isForConservation());
869     sp.valueChanged(40);
870     assertTrue(sg.getGroupColourScheme().conservationApplied());
871     assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
872     assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
873
874     /*
875      * save project, close windows, reload project, verify
876      */
877     File tfile = File.createTempFile("testStoreAndRecoverColourThresholds",
878             ".jvp");
879     tfile.deleteOnExit();
880     new Jalview2XML(false).saveState(tfile);
881     Desktop.instance.closeAll_actionPerformed(null);
882     af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
883             DataSourceType.FILE);
884     Assert.assertNotNull(af, "Failed to reload project");
885
886     /*
887      * verify alignment (background) colouring
888      */
889     rs = af.getViewport().getResidueShading();
890     assertTrue(rs.getColourScheme() instanceof BuriedColourScheme);
891     assertEquals(rs.getThreshold(), 10);
892     assertTrue(rs.conservationApplied());
893     assertEquals(rs.getConservationInc(), 20);
894
895     /*
896      * verify group colouring
897      */
898     assertEquals(1, af.getViewport().getAlignment().getGroups().size(), 1);
899     rs = af.getViewport().getAlignment().getGroups().get(0)
900             .getGroupColourScheme();
901     assertTrue(rs.getColourScheme() instanceof StrandColourScheme);
902     assertEquals(rs.getThreshold(), 40);
903     assertTrue(rs.conservationApplied());
904     assertEquals(rs.getConservationInc(), 30);
905   }
906
907   /**
908    * Test save and reload of feature colour schemes and filter settings
909    * 
910    * @throws IOException
911    */
912   @Test(groups = { "Functional" })
913   public void testSaveLoadFeatureColoursAndFilters() throws IOException
914   {
915     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
916             ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
917     SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
918
919     /*
920      * add some features to the sequence
921      */
922     int score = 1;
923     addFeatures(seq1, "type1", score++);
924     addFeatures(seq1, "type2", score++);
925     addFeatures(seq1, "type3", score++);
926     addFeatures(seq1, "type4", score++);
927     addFeatures(seq1, "type5", score++);
928
929     /*
930      * set colour schemes for features
931      */
932     FeatureRenderer fr = af.getFeatureRenderer();
933     fr.findAllFeatures(true);
934
935     // type1: red
936     fr.setColour("type1", new FeatureColour(Color.red));
937
938     // type2: by label
939     FeatureColourI byLabel = new FeatureColour();
940     byLabel.setColourByLabel(true);
941     fr.setColour("type2", byLabel);
942
943     // type3: by score above threshold
944     FeatureColourI byScore = new FeatureColour(Color.BLACK, Color.BLUE, 1,
945             10);
946     byScore.setAboveThreshold(true);
947     byScore.setThreshold(2f);
948     fr.setColour("type3", byScore);
949
950     // type4: by attribute AF
951     FeatureColourI byAF = new FeatureColour();
952     byAF.setColourByLabel(true);
953     byAF.setAttributeName("AF");
954     fr.setColour("type4", byAF);
955
956     // type5: by attribute CSQ:PolyPhen below threshold
957     FeatureColourI byPolyPhen = new FeatureColour(Color.BLACK, Color.BLUE,
958             1, 10);
959     byPolyPhen.setBelowThreshold(true);
960     byPolyPhen.setThreshold(3f);
961     byPolyPhen.setAttributeName("CSQ", "PolyPhen");
962     fr.setColour("type5", byPolyPhen);
963
964     /*
965      * set filters for feature types
966      */
967
968     // filter type1 features by (label contains "x")
969     FeatureMatcherSetI filterByX = new FeatureMatcherSet();
970     filterByX.and(FeatureMatcher.byLabel(Condition.Contains, "x"));
971     fr.setFeatureFilter("type1", filterByX);
972
973     // filter type2 features by (score <= 2.4 and score > 1.1)
974     FeatureMatcherSetI filterByScore = new FeatureMatcherSet();
975     filterByScore.and(FeatureMatcher.byScore(Condition.LE, "2.4"));
976     filterByScore.and(FeatureMatcher.byScore(Condition.GT, "1.1"));
977     fr.setFeatureFilter("type2", filterByScore);
978
979     // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
980     FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
981     filterByXY
982             .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
983     filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
984             "PolyPhen"));
985     fr.setFeatureFilter("type3", filterByXY);
986
987     /*
988      * save as Jalview project
989      */
990     File tfile = File.createTempFile("JalviewTest", ".jvp");
991     tfile.deleteOnExit();
992     String filePath = tfile.getAbsolutePath();
993     assertTrue(af.saveAlignment(filePath, FileFormat.Jalview),
994             "Failed to store as a project.");
995
996     /*
997      * close current alignment and load the saved project
998      */
999     af.closeMenuItem_actionPerformed(true);
1000     af = null;
1001     af = new FileLoader()
1002             .LoadFileWaitTillLoaded(filePath, DataSourceType.FILE);
1003     assertNotNull(af, "Failed to import new project");
1004
1005     /*
1006      * verify restored feature colour schemes and filters
1007      */
1008     fr = af.getFeatureRenderer();
1009     FeatureColourI fc = fr.getFeatureStyle("type1");
1010     assertTrue(fc.isSimpleColour());
1011     assertEquals(fc.getColour(), Color.red);
1012     fc = fr.getFeatureStyle("type2");
1013     assertTrue(fc.isColourByLabel());
1014     fc = fr.getFeatureStyle("type3");
1015     assertTrue(fc.isGraduatedColour());
1016     assertNull(fc.getAttributeName());
1017     assertTrue(fc.isAboveThreshold());
1018     assertEquals(fc.getThreshold(), 2f);
1019     fc = fr.getFeatureStyle("type4");
1020     assertTrue(fc.isColourByLabel());
1021     assertTrue(fc.isColourByAttribute());
1022     assertEquals(fc.getAttributeName(), new String[] { "AF" });
1023     fc = fr.getFeatureStyle("type5");
1024     assertTrue(fc.isGraduatedColour());
1025     assertTrue(fc.isColourByAttribute());
1026     assertEquals(fc.getAttributeName(), new String[] { "CSQ", "PolyPhen" });
1027     assertTrue(fc.isBelowThreshold());
1028     assertEquals(fc.getThreshold(), 3f);
1029
1030     assertEquals(fr.getFeatureFilter("type1").toStableString(),
1031             "Label Contains x");
1032     assertEquals(fr.getFeatureFilter("type2").toStableString(),
1033             "(Score LE 2.4) AND (Score GT 1.1)");
1034     assertEquals(fr.getFeatureFilter("type3").toStableString(),
1035             "(AF Contains X) OR (CSQ:PolyPhen NE 0.0)");
1036   }
1037
1038   private void addFeature(SequenceI seq, String featureType, int score)
1039   {
1040     SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
1041             score, "grp");
1042     sf.setValue("AF", score);
1043     sf.setValue("CSQ", new HashMap<String, String>()
1044     {
1045       {
1046         put("PolyPhen", Integer.toString(score));
1047       }
1048     });
1049     seq.addSequenceFeature(sf);
1050   }
1051
1052   /**
1053    * Adds two features of the given type to the given sequence, also setting the
1054    * score as the value of attribute "AF" and sub-attribute "CSQ:PolyPhen"
1055    * 
1056    * @param seq
1057    * @param featureType
1058    * @param score
1059    */
1060   private void addFeatures(SequenceI seq, String featureType, int score)
1061   {
1062     addFeature(seq, featureType, score++);
1063     addFeature(seq, featureType, score);
1064   }
1065
1066   /**
1067    * Test save and reload of a PCA viewer
1068    * 
1069    * @throws IOException
1070    */
1071   @Test(groups = { "Functional" })
1072   public void testSaveLoadPCA() throws IOException
1073   {
1074     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
1075             "examples/uniref50.fa", DataSourceType.FILE);
1076     assertEquals(af.getViewport().getAlignment().getHeight(), 15);
1077
1078     /*
1079      * calculate and open PCA (calculates in a separate thread)
1080      */
1081     CalculationChooser chooser = new CalculationChooser(af);
1082     ((JRadioButton) PA.getValue(chooser, "pca")).setSelected(true);
1083     PA.invokeMethod(chooser, "calculate_actionPerformed()");
1084     PCAPanel pcaPanel = (PCAPanel) PA.getValue(chooser, "pcaPanel");
1085     assertNotNull(pcaPanel);
1086     waitFor(50); // let it get started!!
1087     while (pcaPanel.isWorking())
1088     {
1089       waitFor(50);
1090     }
1091     PA.invokeMethod(chooser, "close_actionPerformed()");
1092
1093     /*
1094      * rotate, zoom in, change background colour
1095      */
1096     RotatableCanvas rc = (RotatableCanvas) PA.getValue(pcaPanel, "rc");
1097     PA.setValue(rc, "bgColour", Color.PINK);
1098     rc.zoom(1.9f);
1099     rc.rotate(20, 40f);
1100
1101     /*
1102      * save as Jalview project
1103      */
1104     File tfile = File.createTempFile("JalviewTest", ".jvp");
1105     tfile.deleteOnExit();
1106     String filePath = tfile.getAbsolutePath();
1107     assertTrue(af.saveAlignment(filePath, FileFormat.Jalview),
1108             "Failed to store as a project.");
1109   
1110     /*
1111      * load the saved project and locate the restored PCA panel
1112      */
1113     new FileLoader().LoadFileWaitTillLoaded(filePath, DataSourceType.FILE);
1114     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1115     PCAPanel pcaPanel2 = null;
1116     for (JInternalFrame frame : frames)
1117     {
1118       if (frame instanceof PCAPanel && frame != pcaPanel)
1119       {
1120         pcaPanel2 = (PCAPanel) frame;
1121       }
1122     }
1123     assertNotNull(pcaPanel2);
1124
1125     /*
1126      * compare restored and original PCA
1127      */
1128     PCAModel pcaModel = (PCAModel) PA.getValue(pcaPanel, "pcaModel");
1129     PCAModel pcaModel2 = (PCAModel) PA.getValue(pcaPanel2, "pcaModel");
1130     RotatableCanvas rc2 = (RotatableCanvas) PA.getValue(pcaPanel2, "rc");
1131     assertNotNull(pcaModel);
1132     assertNotNull(pcaModel2);
1133     assertNotNull(rc2);
1134     assertEquals(rc2.getBackgroundColour(), Color.PINK);
1135     assertEquals(PA.getValue(rc2, "scaleFactor"), 1.9f);
1136
1137     // original has input data
1138     assertNotNull(pcaModel.getInputData());
1139     // restored has no input data (JAL-2647 to do)
1140     assertNull(pcaModel2.getInputData());
1141
1142     // verify sequence points are at the same positions
1143     List<SequencePoint> seqPts = pcaModel.getSequencePoints();
1144     List<SequencePoint> seqPts2 = pcaModel2.getSequencePoints();
1145     assertEquals(seqPts.size(), seqPts2.size());
1146     for (int i = 0; i < seqPts.size(); i++)
1147     {
1148       SequencePoint sp = seqPts.get(i);
1149       SequencePoint sp2 = seqPts2.get(i);
1150       assertEquals(sp.getSequence().getName(), sp2.getSequence().getName());
1151       assertEquals(sp.coord, sp2.coord);
1152     }
1153
1154     // verify axis end points are at the same positions
1155     Point[] axes = (Point[]) PA.getValue(rc, "axisEndPoints");
1156     Point[] axes2 = (Point[]) PA.getValue(rc2, "axisEndPoints");
1157     assertEquals(axes.length, 3);
1158     assertEquals(axes2.length, 3);
1159     for (int i = 0; i < 3; i++)
1160     {
1161       assertEquals(axes[i], axes2[i]);
1162     }
1163
1164     // compare PCA data
1165     PCA pca = (PCA) PA.getValue(pcaModel, "pca");
1166     PCA pca2 = (PCA) PA.getValue(pcaModel2, "pca");
1167     assertNotNull(pca);
1168     assertNotNull(pca2);
1169     // same (BLOSUM62) score model (a singleton object)
1170     assertSame(PA.getValue(pca, "scoreModel"),
1171             PA.getValue(pca2, "scoreModel"));
1172     assertEquals(PA.getValue(pca, "similarityParams"),
1173             PA.getValue(pca2, "similarityParams"));
1174     MatrixTest.assertMatricesMatch(pca.getPairwiseScores(),
1175             pca2.getPairwiseScores());
1176     MatrixTest.assertMatricesMatch(pca.getTridiagonal(),
1177             pca2.getTridiagonal());
1178     MatrixTest.assertMatricesMatch(pca.getEigenmatrix(),
1179             pca2.getEigenmatrix());
1180   }
1181
1182   protected void waitFor(long t)
1183   {
1184     synchronized (this)
1185     {
1186       try
1187       {
1188         wait(t);
1189       } catch (InterruptedException e)
1190       {
1191         fail(e.getMessage());
1192       }
1193     }
1194   }
1195 }