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