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