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