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