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