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