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