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