Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 17:01:39 GMT
  2. Package jalview.project

File Jalview2xmlTests.java

 

Code metrics

72
824
31
1
1,833
1,372
87
0.11
26.58
31
2.81

Classes

Class Line # Actions
Jalview2xmlTests 111 824 87
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.project;
22   
23    import static org.testng.Assert.assertEquals;
24    import static org.testng.Assert.assertFalse;
25    import static org.testng.Assert.assertNotNull;
26    import static org.testng.Assert.assertNotSame;
27    import static org.testng.Assert.assertNull;
28    import static org.testng.Assert.assertSame;
29    import static org.testng.Assert.assertTrue;
30   
31    import java.awt.Color;
32    import java.awt.Rectangle;
33    import java.io.File;
34    import java.io.IOException;
35    import java.util.ArrayList;
36    import java.util.BitSet;
37    import java.util.HashMap;
38    import java.util.List;
39    import java.util.Locale;
40    import java.util.Map;
41   
42    import javax.swing.JInternalFrame;
43   
44    import org.testng.Assert;
45    import org.testng.AssertJUnit;
46    import org.testng.annotations.BeforeClass;
47    import org.testng.annotations.Test;
48   
49    import jalview.analysis.AlignmentUtils;
50    import jalview.analysis.scoremodels.SimilarityParams;
51    import jalview.api.AlignViewportI;
52    import jalview.api.AlignmentViewPanel;
53    import jalview.api.FeatureColourI;
54    import jalview.api.ViewStyleI;
55    import jalview.bin.Cache;
56    import jalview.datamodel.AlignmentAnnotation;
57    import jalview.datamodel.AlignmentI;
58    import jalview.datamodel.Annotation;
59    import jalview.datamodel.ContactListI;
60    import jalview.datamodel.ContactMatrix;
61    import jalview.datamodel.ContactMatrixI;
62    import jalview.datamodel.DBRefEntry;
63    import jalview.datamodel.GeneLocus;
64    import jalview.datamodel.GroupSet;
65    import jalview.datamodel.HiddenSequences;
66    import jalview.datamodel.Mapping;
67    import jalview.datamodel.PDBEntry;
68    import jalview.datamodel.PDBEntry.Type;
69    import jalview.datamodel.Sequence;
70    import jalview.datamodel.Sequence.DBModList;
71    import jalview.datamodel.SequenceCollectionI;
72    import jalview.datamodel.SequenceFeature;
73    import jalview.datamodel.SequenceGroup;
74    import jalview.datamodel.SequenceI;
75    import jalview.datamodel.features.FeatureMatcher;
76    import jalview.datamodel.features.FeatureMatcherSet;
77    import jalview.datamodel.features.FeatureMatcherSetI;
78    import jalview.gui.AlignFrame;
79    import jalview.gui.AlignViewport;
80    import jalview.gui.AlignmentPanel;
81    import jalview.gui.Desktop;
82    import jalview.gui.JvOptionPane;
83    import jalview.gui.OverviewPanel;
84    import jalview.gui.PCAPanel;
85    import jalview.gui.PopupMenu;
86    import jalview.gui.Preferences;
87    import jalview.gui.SliderPanel;
88    import jalview.io.DataSourceType;
89    import jalview.io.FileFormat;
90    import jalview.io.FileLoader;
91    import jalview.io.Jalview2xmlBase;
92    import jalview.renderer.ResidueShaderI;
93    import jalview.schemes.AnnotationColourGradient;
94    import jalview.schemes.BuriedColourScheme;
95    import jalview.schemes.ColourSchemeI;
96    import jalview.schemes.ColourSchemeProperty;
97    import jalview.schemes.FeatureColour;
98    import jalview.schemes.JalviewColourScheme;
99    import jalview.schemes.RNAHelicesColour;
100    import jalview.schemes.StrandColourScheme;
101    import jalview.schemes.TCoffeeColourScheme;
102    import jalview.structure.StructureImportSettings;
103    import jalview.util.MapList;
104    import jalview.util.matcher.Condition;
105    import jalview.viewmodel.AlignmentViewport;
106    import jalview.viewmodel.seqfeatures.FeatureRendererModel;
107    import jalview.ws.datamodel.MappableContactMatrixI;
108    import jalview.ws.datamodel.alphafold.PAEContactMatrix;
109   
110    @Test(singleThreaded = true)
 
111    public class Jalview2xmlTests extends Jalview2xmlBase
112    {
113   
 
114  0 toggle @Override
115    @BeforeClass(alwaysRun = true)
116    public void setUpJvOptionPane()
117    {
118  0 if (Desktop.instance != null)
119  0 Desktop.instance.closeAll_actionPerformed(null);
120  0 JvOptionPane.setInteractiveMode(false);
121  0 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
122    }
123   
 
124  0 toggle @Test(groups = { "Functional" })
125    public void testRNAStructureRecovery() throws Exception
126    {
127  0 String inFile = "examples/RF00031_folded.stk";
128  0 String tfile = File.createTempFile("JalviewTest", ".jvp")
129    .getAbsolutePath();
130  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
131    DataSourceType.FILE);
132  0 assertNotNull(af, "Didn't read input file " + inFile);
133  0 int olddsann = countDsAnn(af.getViewport());
134  0 assertTrue(olddsann > 0, "Didn't find any dataset annotations");
135  0 af.changeColour_actionPerformed(
136    JalviewColourScheme.RNAHelices.toString());
137  0 assertTrue(
138    af.getViewport()
139    .getGlobalColourScheme() instanceof RNAHelicesColour,
140    "Couldn't apply RNA helices colourscheme");
141  0 af.saveAlignment(tfile, FileFormat.Jalview);
142  0 assertTrue(af.isSaveAlignmentSuccessful(),
143    "Failed to store as a project.");
144  0 af.closeMenuItem_actionPerformed(true);
145  0 af = null;
146  0 af = new FileLoader().LoadFileWaitTillLoaded(tfile,
147    DataSourceType.FILE);
148  0 assertNotNull(af, "Failed to import new project");
149  0 int newdsann = countDsAnn(af.getViewport());
150  0 assertEquals(olddsann, newdsann,
151    "Differing numbers of dataset sequence annotation\nOriginally "
152    + olddsann + " and now " + newdsann);
153  0 System.out.println(
154    "Read in same number of annotations as originally present ("
155    + olddsann + ")");
156  0 assertTrue(
157   
158    af.getViewport()
159    .getGlobalColourScheme() instanceof RNAHelicesColour,
160    "RNA helices colourscheme was not applied on import.");
161    }
162   
 
163  0 toggle @Test(groups = { "Functional" })
164    public void testTCoffeeScores() throws Exception
165    {
166  0 String inFile = "examples/uniref50.fa",
167    inAnnot = "examples/uniref50.score_ascii";
168  0 String tfile = File.createTempFile("JalviewTest", ".jvp")
169    .getAbsolutePath();
170  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
171    DataSourceType.FILE);
172  0 assertNotNull(af, "Didn't read input file " + inFile);
173  0 af.loadJalviewDataFile(inAnnot, DataSourceType.FILE, null, null);
174  0 AlignViewport viewport = af.getViewport();
175  0 assertSame(viewport.getGlobalColourScheme().getClass(),
176    TCoffeeColourScheme.class, "Didn't set T-coffee colourscheme");
177  0 assertNotNull(
178    ColourSchemeProperty.getColourScheme(viewport,
179    viewport.getAlignment(),
180    viewport.getGlobalColourScheme().getSchemeName()),
181    "Recognise T-Coffee score from string");
182   
183  0 af.saveAlignment(tfile, FileFormat.Jalview);
184  0 assertTrue(af.isSaveAlignmentSuccessful(),
185    "Failed to store as a project.");
186  0 af.closeMenuItem_actionPerformed(true);
187  0 af = null;
188  0 af = new FileLoader().LoadFileWaitTillLoaded(tfile,
189    DataSourceType.FILE);
190  0 assertNotNull(af, "Failed to import new project");
191  0 assertSame(af.getViewport().getGlobalColourScheme().getClass(),
192    TCoffeeColourScheme.class,
193    "Didn't set T-coffee colourscheme for imported project.");
194  0 System.out.println(
195    "T-Coffee score shading successfully recovered from project.");
196    }
197   
 
198  0 toggle @Test(groups = { "Functional" })
199    public void testColourByAnnotScores() throws Exception
200    {
201  0 String inFile = "examples/uniref50.fa",
202    inAnnot = "examples/testdata/uniref50_iupred.jva";
203  0 String tfile = File.createTempFile("JalviewTest", ".jvp")
204    .getAbsolutePath();
205  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
206    DataSourceType.FILE);
207  0 assertNotNull(af, "Didn't read input file " + inFile);
208  0 af.loadJalviewDataFile(inAnnot, DataSourceType.FILE, null, null);
209  0 AlignmentAnnotation[] aa = af.getViewport().getAlignment()
210    .getSequenceAt(0).getAnnotation("IUPredWS (Short)");
211  0 assertTrue(
212   
213    aa != null && aa.length > 0,
214    "Didn't find any IUPred annotation to use to shade alignment.");
215  0 AnnotationColourGradient cs = new AnnotationColourGradient(aa[0], null,
216    AnnotationColourGradient.ABOVE_THRESHOLD);
217  0 AnnotationColourGradient gcs = new AnnotationColourGradient(aa[0], null,
218    AnnotationColourGradient.BELOW_THRESHOLD);
219  0 cs.setSeqAssociated(true);
220  0 gcs.setSeqAssociated(true);
221  0 af.changeColour(cs);
222  0 SequenceGroup sg = new SequenceGroup();
223  0 sg.setStartRes(57);
224  0 sg.setEndRes(92);
225  0 sg.cs.setColourScheme(gcs);
226  0 af.getViewport().getAlignment().addGroup(sg);
227  0 sg.addSequence(af.getViewport().getAlignment().getSequenceAt(1), false);
228  0 sg.addSequence(af.getViewport().getAlignment().getSequenceAt(2), true);
229  0 af.alignPanel.alignmentChanged();
230  0 af.saveAlignment(tfile, FileFormat.Jalview);
231  0 assertTrue(af.isSaveAlignmentSuccessful(),
232    "Failed to store as a project.");
233  0 af.closeMenuItem_actionPerformed(true);
234  0 af = null;
235  0 af = new FileLoader().LoadFileWaitTillLoaded(tfile,
236    DataSourceType.FILE);
237  0 assertNotNull(af, "Failed to import new project");
238   
239    // check for group and alignment colourschemes
240   
241  0 ColourSchemeI _rcs = af.getViewport().getGlobalColourScheme();
242  0 ColourSchemeI _rgcs = af.getViewport().getAlignment().getGroups().get(0)
243    .getColourScheme();
244  0 assertNotNull(_rcs, "Didn't recover global colourscheme");
245  0 assertTrue(_rcs instanceof AnnotationColourGradient,
246    "Didn't recover annotation colour global scheme");
247  0 AnnotationColourGradient __rcs = (AnnotationColourGradient) _rcs;
248  0 assertTrue(__rcs.isSeqAssociated(),
249    "Annotation colourscheme wasn't sequence associated");
250   
251  0 boolean diffseqcols = false, diffgseqcols = false;
252  0 SequenceI[] sqs = af.getViewport().getAlignment().getSequencesArray();
253  0 for (int p = 0, pSize = af.getViewport().getAlignment()
254  0 .getWidth(); p < pSize && (!diffseqcols || !diffgseqcols); p++)
255    {
256  0 if (_rcs.findColour(sqs[0].getCharAt(p), p, sqs[0], null, 0f) != _rcs
257    .findColour(sqs[5].getCharAt(p), p, sqs[5], null, 0f))
258    {
259  0 diffseqcols = true;
260    }
261    }
262  0 assertTrue(diffseqcols, "Got Different sequence colours");
263  0 System.out.println(
264    "Per sequence colourscheme (Background) successfully applied and recovered.");
265   
266  0 assertNotNull(_rgcs, "Didn't recover group colourscheme");
267  0 assertTrue(_rgcs instanceof AnnotationColourGradient,
268    "Didn't recover annotation colour group colourscheme");
269  0 __rcs = (AnnotationColourGradient) _rgcs;
270  0 assertTrue(__rcs.isSeqAssociated(),
271    "Group Annotation colourscheme wasn't sequence associated");
272   
273  0 for (int p = 0, pSize = af.getViewport().getAlignment()
274  0 .getWidth(); p < pSize && (!diffseqcols || !diffgseqcols); p++)
275    {
276  0 if (_rgcs.findColour(sqs[1].getCharAt(p), p, sqs[1], null,
277    0f) != _rgcs.findColour(sqs[2].getCharAt(p), p, sqs[2], null,
278    0f))
279    {
280  0 diffgseqcols = true;
281    }
282    }
283  0 assertTrue(diffgseqcols, "Got Different group sequence colours");
284  0 System.out.println(
285    "Per sequence (Group) colourscheme successfully applied and recovered.");
286    }
287   
 
288  0 toggle @Test(groups = { "Functional" })
289    public void gatherViewsHere() throws Exception
290    {
291  0 int origCount = Desktop.getDesktopAlignFrames() == null ? 0
292    : Desktop.getDesktopAlignFrames().length;
293  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
294    "examples/exampleFile_2_7.jar", DataSourceType.FILE);
295  0 assertNotNull(af, "Didn't read in the example file correctly.");
296  0 assertTrue(Desktop.getDesktopAlignFrames().length == 1 + origCount,
297    "Didn't gather the views in the example file.");
298   
299    }
300   
301    /**
302    * Test for JAL-2223 - multiple mappings in View Mapping report
303    *
304    * @throws Exception
305    */
 
306  0 toggle @Test(groups = { "Functional" })
307    public void noDuplicatePdbMappingsMade() throws Exception
308    {
309  0 StructureImportSettings.setProcessSecondaryStructure(true);
310  0 StructureImportSettings.setVisibleChainAnnotation(true);
311  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
312    "examples/exampleFile_2_7.jar", DataSourceType.FILE);
313  0 assertNotNull(af, "Didn't read in the example file correctly.");
314   
315    // locate Jmol viewer
316    // count number of PDB mappings the structure selection manager holds -
317  0 String pdbFile = af.getCurrentView().getStructureSelectionManager()
318    .findFileForPDBId("1A70");
319  0 assertEquals(
320    af.getCurrentView().getStructureSelectionManager()
321    .getMapping(pdbFile).length,
322    2, "Expected only two mappings for 1A70");
323   
324    }
325   
 
326  0 toggle @Test(groups = { "Functional" })
327    public void viewRefPdbAnnotation() throws Exception
328    {
329  0 StructureImportSettings.setProcessSecondaryStructure(true);
330  0 StructureImportSettings.setVisibleChainAnnotation(true);
331  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
332    "examples/exampleFile_2_7.jar", DataSourceType.FILE);
333  0 assertNotNull(af, "Didn't read in the example file correctly.");
334  0 AlignmentViewPanel sps = null;
335  0 for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
336    {
337  0 if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
338    {
339  0 sps = ap;
340  0 break;
341    }
342    }
343  0 assertNotNull(sps, "Couldn't find the structure view");
344  0 AlignmentAnnotation refan = null;
345  0 for (AlignmentAnnotation ra : sps.getAlignment()
346    .getAlignmentAnnotation())
347    {
348  0 if (ra.graph != 0)
349    {
350  0 refan = ra;
351  0 break;
352    }
353    }
354  0 assertNotNull(refan, "Annotation secondary structure not found.");
355  0 SequenceI sq = sps.getAlignment().findName("1A70|");
356  0 assertNotNull(sq, "Couldn't find 1a70 null chain");
357    // compare the manually added temperature factor annotation
358    // to the track automatically transferred from the pdb structure on load
359  0 assertNotNull(sq.getDatasetSequence().getAnnotation(),
360    "1a70 has no annotation");
361  0 for (AlignmentAnnotation ala : sq.getDatasetSequence().getAnnotation())
362    {
363  0 AlignmentAnnotation alaa;
364  0 sq.addAlignmentAnnotation(alaa = new AlignmentAnnotation(ala));
365  0 alaa.adjustForAlignment();
366  0 if (ala.graph == refan.graph)
367    {
368  0 for (int p = 0; p < ala.annotations.length; p++)
369    {
370  0 sq.findPosition(p);
371  0 try
372    {
373  0 assertTrue((alaa.annotations[p] == null
374    && refan.annotations[p] == null)
375    || alaa.annotations[p].value == refan.annotations[p].value,
376    "Mismatch at alignment position " + p);
377    } catch (NullPointerException q)
378    {
379  0 Assert.fail("Mismatch of alignment annotations at position " + p
380    + " Ref seq ann: " + refan.annotations[p]
381    + " alignment " + alaa.annotations[p]);
382    }
383    }
384    }
385    }
386   
387    }
388   
 
389  0 toggle @Test(groups = { "Functional" })
390    public void testCopyViewSettings() throws Exception
391    {
392  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
393    "examples/exampleFile_2_7.jar", DataSourceType.FILE);
394  0 assertNotNull(af, "Didn't read in the example file correctly.");
395  0 AlignmentViewPanel sps = null, groups = null;
396  0 for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
397    {
398  0 if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
399    {
400  0 sps = ap;
401    }
402  0 if (ap.getViewName().contains("MAFFT"))
403    {
404  0 groups = ap;
405    }
406    }
407  0 assertNotNull(sps, "Couldn't find the structure view");
408  0 assertNotNull(groups, "Couldn't find the MAFFT view");
409   
410  0 ViewStyleI structureStyle = sps.getAlignViewport().getViewStyle();
411  0 ViewStyleI groupStyle = groups.getAlignViewport().getViewStyle();
412  0 AssertJUnit.assertFalse(structureStyle.sameStyle(groupStyle));
413   
414  0 groups.getAlignViewport().setViewStyle(structureStyle);
415  0 AssertJUnit.assertFalse(
416    groupStyle.sameStyle(groups.getAlignViewport().getViewStyle()));
417  0 Assert.assertTrue(structureStyle
418    .sameStyle(groups.getAlignViewport().getViewStyle()));
419   
420    }
421   
422    /**
423    * test store and recovery of expanded views
424    *
425    * @throws Exception
426    */
 
427  0 toggle @Test(groups = { "Functional" }, enabled = true)
428    public void testStoreAndRecoverExpandedviews() throws Exception
429    {
430  0 Desktop.instance.closeAll_actionPerformed(null);
431   
432  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
433    "examples/exampleFile_2_7.jar", DataSourceType.FILE);
434  0 Assert.assertEquals(Desktop.getDesktopAlignFrames().length, 1);
435  0 String afid = af.getViewport().getSequenceSetId();
436   
437    // check FileLoader returned a reference to the one alignFrame that is
438    // actually on the Desktop
439  0 assertSame(af, Desktop.getAlignFrameFor(af.getViewport()),
440    "Jalview2XML.loadAlignFrame() didn't return correct AlignFrame reference for multiple view window");
441   
442  0 Desktop.explodeViews(af);
443   
444  0 int oldviews = Desktop.getDesktopAlignFrames().length;
445  0 Assert.assertEquals(Desktop.getDesktopAlignFrames().length,
446    Desktop.getAlignmentPanels(afid).length);
447  0 File tfile = File.createTempFile("testStoreAndRecoverExpanded", ".jvp");
448  0 try
449    {
450  0 new Jalview2XML(false).saveState(tfile);
451    } catch (Error e)
452    {
453  0 Assert.fail("Didn't save the expanded view state", e);
454    } catch (Exception e)
455    {
456  0 Assert.fail("Didn't save the expanded view state", e);
457    }
458  0 Desktop.instance.closeAll_actionPerformed(null);
459  0 if (Desktop.getDesktopAlignFrames() != null)
460    {
461  0 Assert.assertEquals(Desktop.getDesktopAlignFrames().length, 0);
462    }
463  0 af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
464    DataSourceType.FILE);
465  0 Assert.assertNotNull(af);
466  0 Assert.assertEquals(Desktop.getDesktopAlignFrames().length,
467    Desktop.getAlignmentPanels(
468    af.getViewport().getSequenceSetId()).length);
469  0 Assert.assertEquals(Desktop
470    .getAlignmentPanels(af.getViewport().getSequenceSetId()).length,
471    oldviews);
472    }
473   
474    /**
475    * Test save and reload of a project with a different representative sequence
476    * in each view.
477    *
478    * @throws Exception
479    */
 
480  0 toggle @Test(groups = { "Functional" })
481    public void testStoreAndRecoverReferenceSeqSettings() throws Exception
482    {
483  0 Desktop.instance.closeAll_actionPerformed(null);
484  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
485    "examples/exampleFile_2_7.jar", DataSourceType.FILE);
486  0 assertNotNull(af, "Didn't read in the example file correctly.");
487  0 String afid = af.getViewport().getSequenceSetId();
488   
489    // remember reference sequence for each panel
490  0 Map<String, SequenceI> refseqs = new HashMap<>();
491   
492    /*
493    * mark sequence 2, 3, 4.. in panels 1, 2, 3...
494    * as reference sequence for itself and the preceding sequence
495    */
496  0 int n = 1;
497  0 for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
498    {
499  0 AlignViewportI av = ap.getAlignViewport();
500  0 AlignmentI alignment = ap.getAlignment();
501  0 int repIndex = n % alignment.getHeight();
502  0 SequenceI rep = alignment.getSequenceAt(repIndex);
503  0 refseqs.put(ap.getViewName(), rep);
504   
505    // code from mark/unmark sequence as reference in jalview.gui.PopupMenu
506    // todo refactor this to an alignment view controller
507  0 av.setDisplayReferenceSeq(true);
508  0 av.setColourByReferenceSeq(true);
509  0 av.getAlignment().setSeqrep(rep);
510   
511  0 n++;
512    }
513  0 File tfile = File.createTempFile("testStoreAndRecoverReferenceSeq",
514    ".jvp");
515  0 try
516    {
517  0 new Jalview2XML(false).saveState(tfile);
518    } catch (Throwable e)
519    {
520  0 Assert.fail("Didn't save the expanded view state", e);
521    }
522  0 Desktop.instance.closeAll_actionPerformed(null);
523  0 if (Desktop.getDesktopAlignFrames() != null)
524    {
525  0 Assert.assertEquals(Desktop.getDesktopAlignFrames().length, 0);
526    }
527   
528  0 af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
529    DataSourceType.FILE);
530  0 afid = af.getViewport().getSequenceSetId();
531   
532  0 for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
533    {
534    // check representative
535  0 AlignmentI alignment = ap.getAlignment();
536  0 SequenceI rep = alignment.getSeqrep();
537  0 Assert.assertNotNull(rep,
538    "Couldn't restore sequence representative from project");
539    // can't use a strong equals here, because by definition, the sequence IDs
540    // will be different.
541    // could set vamsas session save/restore flag to preserve IDs across
542    // load/saves.
543  0 Assert.assertEquals(refseqs.get(ap.getViewName()).toString(),
544    rep.toString(),
545    "Representative wasn't the same when recovered.");
546  0 Assert.assertTrue(ap.getAlignViewport().isDisplayReferenceSeq(),
547    "Display reference sequence view setting not set.");
548  0 Assert.assertTrue(ap.getAlignViewport().isColourByReferenceSeq(),
549    "Colour By Reference Seq view setting not set.");
550    }
551    }
552   
 
553  0 toggle @Test(groups = { "Functional" })
554    public void testIsVersionStringLaterThan()
555    {
556    /*
557    * No version / development / test / autobuild is leniently assumed to be
558    * compatible
559    */
560  0 assertTrue(Jalview2XML.isVersionStringLaterThan(null, null));
561  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", null));
562  0 assertTrue(Jalview2XML.isVersionStringLaterThan(null, "2.8.3"));
563  0 assertTrue(Jalview2XML.isVersionStringLaterThan(null,
564    "Development Build"));
565  0 assertTrue(Jalview2XML.isVersionStringLaterThan(null,
566    "DEVELOPMENT BUILD"));
567  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3",
568    "Development Build"));
569  0 assertTrue(Jalview2XML.isVersionStringLaterThan(null, "Test"));
570  0 assertTrue(Jalview2XML.isVersionStringLaterThan(null, "TEST"));
571  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "Test"));
572  0 assertTrue(
573    Jalview2XML.isVersionStringLaterThan(null, "Automated Build"));
574  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3",
575    "Automated Build"));
576  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3",
577    "AUTOMATED BUILD"));
578   
579    /*
580    * same version returns true i.e. compatible
581    */
582  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8", "2.8"));
583  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.3"));
584  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3b1", "2.8.3b1"));
585  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3B1", "2.8.3b1"));
586  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3b1", "2.8.3B1"));
587   
588    /*
589    * later version returns true
590    */
591  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.4"));
592  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.9"));
593  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.9.2"));
594  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8", "2.8.3"));
595  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.3b1"));
596   
597    /*
598    * earlier version returns false
599    */
600  0 assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8"));
601  0 assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.4", "2.8.3"));
602  0 assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.3b1", "2.8.3"));
603  0 assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.2b1"));
604  0 assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.0b2", "2.8.0b1"));
605    /*
606    * test for patch release versions
607    */
608  0 assertFalse(Jalview2XML.isVersionStringLaterThan("2.11.3.0", "2.11.2"));
609  0 assertTrue(Jalview2XML.isVersionStringLaterThan("2.11.3.0", "2.11.4"));
610  0 assertFalse(
611    Jalview2XML.isVersionStringLaterThan("2.12.2.0b1", "2.12.2.0"));
612  0 assertFalse(
613    Jalview2XML.isVersionStringLaterThan("2.12.2.3", "2.12.2.2"));
614   
615    }
616   
617    /**
618    * Test save and reload of a project with a different sequence group (and
619    * representative sequence) in each view.
620    *
621    * @throws Exception
622    */
 
623  0 toggle @Test(groups = { "Functional" })
624    public void testStoreAndRecoverGroupRepSeqs() throws Exception
625    {
626  0 Desktop.instance.closeAll_actionPerformed(null);
627  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
628    "examples/uniref50.fa", DataSourceType.FILE);
629  0 assertNotNull(af, "Didn't read in the example file correctly.");
630  0 String afid = af.getViewport().getSequenceSetId();
631    // make a second view of the alignment
632  0 af.newView_actionPerformed(null);
633   
634    /*
635    * remember representative and hidden sequences marked
636    * on each panel
637    */
638  0 Map<String, SequenceI> repSeqs = new HashMap<>();
639  0 Map<String, List<String>> hiddenSeqNames = new HashMap<>();
640   
641    /*
642    * mark sequence 2, 3, 4.. in panels 1, 2, 3...
643    * as reference sequence for itself and the preceding sequence
644    */
645  0 int n = 1;
646  0 for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
647    {
648  0 AlignViewportI av = ap.getAlignViewport();
649  0 AlignmentI alignment = ap.getAlignment();
650  0 int repIndex = n % alignment.getHeight();
651    // ensure at least one preceding sequence i.e. index >= 1
652  0 repIndex = Math.max(repIndex, 1);
653  0 SequenceI repSeq = alignment.getSequenceAt(repIndex);
654  0 repSeqs.put(ap.getViewName(), repSeq);
655  0 List<String> hiddenNames = new ArrayList<>();
656  0 hiddenSeqNames.put(ap.getViewName(), hiddenNames);
657   
658    /*
659    * have rep sequence represent itself and the one before it
660    * this hides the group (except for the rep seq)
661    */
662  0 SequenceGroup sg = new SequenceGroup();
663  0 sg.addSequence(repSeq, false);
664  0 SequenceI precedingSeq = alignment.getSequenceAt(repIndex - 1);
665  0 sg.addSequence(precedingSeq, false);
666  0 sg.setSeqrep(repSeq);
667  0 assertTrue(sg.getSequences().contains(repSeq));
668  0 assertTrue(sg.getSequences().contains(precedingSeq));
669  0 av.setSelectionGroup(sg);
670  0 assertSame(repSeq, sg.getSeqrep());
671   
672    /*
673    * represent group with sequence adds to a map of hidden rep sequences
674    * (it does not create a group on the alignment)
675    */
676  0 ((AlignmentViewport) av).hideSequences(repSeq, true);
677  0 assertSame(repSeq, sg.getSeqrep());
678  0 assertTrue(sg.getSequences().contains(repSeq));
679  0 assertTrue(sg.getSequences().contains(precedingSeq));
680  0 assertTrue(alignment.getGroups().isEmpty(), "alignment has groups");
681  0 Map<SequenceI, SequenceCollectionI> hiddenRepSeqsMap = av
682    .getHiddenRepSequences();
683  0 assertNotNull(hiddenRepSeqsMap);
684  0 assertEquals(1, hiddenRepSeqsMap.size());
685  0 assertSame(sg, hiddenRepSeqsMap.get(repSeq));
686  0 assertTrue(alignment.getHiddenSequences().isHidden(precedingSeq));
687  0 assertFalse(alignment.getHiddenSequences().isHidden(repSeq));
688  0 hiddenNames.add(precedingSeq.getName());
689   
690  0 n++;
691    }
692  0 File tfile = File.createTempFile("testStoreAndRecoverGroupReps",
693    ".jvp");
694  0 try
695    {
696  0 new Jalview2XML(false).saveState(tfile);
697    } catch (Throwable e)
698    {
699  0 Assert.fail("Didn't save the expanded view state", e);
700    }
701  0 Desktop.instance.closeAll_actionPerformed(null);
702  0 if (Desktop.getDesktopAlignFrames() != null)
703    {
704  0 Assert.assertEquals(Desktop.getDesktopAlignFrames().length, 0);
705    }
706   
707  0 af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
708    DataSourceType.FILE);
709  0 afid = af.getViewport().getSequenceSetId();
710   
711  0 for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
712    {
713  0 String viewName = ap.getViewName();
714  0 AlignViewportI av = ap.getAlignViewport();
715  0 AlignmentI alignment = ap.getAlignment();
716  0 List<SequenceGroup> groups = alignment.getGroups();
717  0 assertNotNull(groups);
718  0 assertTrue(groups.isEmpty(), "Alignment has groups");
719  0 Map<SequenceI, SequenceCollectionI> hiddenRepSeqsMap = av
720    .getHiddenRepSequences();
721  0 assertNotNull(hiddenRepSeqsMap, "No hidden represented sequences");
722  0 assertEquals(1, hiddenRepSeqsMap.size());
723  0 assertEquals(repSeqs.get(viewName).getDisplayId(true),
724    hiddenRepSeqsMap.keySet().iterator().next()
725    .getDisplayId(true));
726   
727    /*
728    * verify hidden sequences in restored panel
729    */
730  0 List<String> hidden = hiddenSeqNames.get(ap.getViewName());
731  0 HiddenSequences hs = alignment.getHiddenSequences();
732  0 assertEquals(hidden.size(), hs.getSize(),
733    "wrong number of restored hidden sequences in "
734    + ap.getViewName());
735    }
736    }
737   
738    /**
739    * Test save and reload of PDBEntry in Jalview project
740    *
741    * @throws Exception
742    */
 
743  0 toggle @Test(groups = { "Functional" })
744    public void testStoreAndRecoverPDBEntry() throws Exception
745    {
746  0 Desktop.instance.closeAll_actionPerformed(null);
747  0 String exampleFile = "examples/3W5V.pdb";
748  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(exampleFile,
749    DataSourceType.FILE);
750  0 assertNotNull(af, "Didn't read in the example file correctly.");
751  0 String afid = af.getViewport().getSequenceSetId();
752   
753  0 AlignmentPanel[] alignPanels = Desktop.getAlignmentPanels(afid);
754  0 System.out.println();
755  0 AlignmentViewPanel ap = alignPanels[0];
756  0 String tfileBase = new File(".").getAbsolutePath().replace(".", "");
757  0 String testFile = tfileBase + exampleFile;
758  0 AlignmentI alignment = ap.getAlignment();
759  0 System.out.println("blah");
760  0 SequenceI[] seqs = alignment.getSequencesArray();
761  0 Assert.assertNotNull(seqs[0]);
762  0 Assert.assertNotNull(seqs[1]);
763  0 Assert.assertNotNull(seqs[2]);
764  0 Assert.assertNotNull(seqs[3]);
765  0 Assert.assertNotNull(seqs[0].getDatasetSequence());
766  0 Assert.assertNotNull(seqs[1].getDatasetSequence());
767  0 Assert.assertNotNull(seqs[2].getDatasetSequence());
768  0 Assert.assertNotNull(seqs[3].getDatasetSequence());
769  0 PDBEntry[] pdbEntries = new PDBEntry[4];
770  0 pdbEntries[0] = new PDBEntry("3W5V", "A", Type.PDB, testFile);
771  0 pdbEntries[1] = new PDBEntry("3W5V", "B", Type.PDB, testFile);
772  0 pdbEntries[2] = new PDBEntry("3W5V", "C", Type.PDB, testFile);
773  0 pdbEntries[3] = new PDBEntry("3W5V", "D", Type.PDB, testFile);
774  0 Assert.assertEquals(
775    seqs[0].getDatasetSequence().getAllPDBEntries().get(0),
776    pdbEntries[0]);
777  0 Assert.assertEquals(
778    seqs[1].getDatasetSequence().getAllPDBEntries().get(0),
779    pdbEntries[1]);
780  0 Assert.assertEquals(
781    seqs[2].getDatasetSequence().getAllPDBEntries().get(0),
782    pdbEntries[2]);
783  0 Assert.assertEquals(
784    seqs[3].getDatasetSequence().getAllPDBEntries().get(0),
785    pdbEntries[3]);
786   
787  0 File tfile = File.createTempFile("testStoreAndRecoverPDBEntry", ".jvp");
788  0 try
789    {
790  0 new Jalview2XML(false).saveState(tfile);
791    } catch (Throwable e)
792    {
793  0 Assert.fail("Didn't save the state", e);
794    }
795  0 Desktop.instance.closeAll_actionPerformed(null);
796  0 if (Desktop.getDesktopAlignFrames() != null)
797    {
798  0 Assert.assertEquals(Desktop.getDesktopAlignFrames().length, 0);
799    }
800   
801  0 AlignFrame restoredFrame = new FileLoader().LoadFileWaitTillLoaded(
802    tfile.getAbsolutePath(), DataSourceType.FILE);
803  0 String rfid = restoredFrame.getViewport().getSequenceSetId();
804  0 AlignmentPanel[] rAlignPanels = Desktop.getAlignmentPanels(rfid);
805  0 AlignmentViewPanel rap = rAlignPanels[0];
806  0 AlignmentI rAlignment = rap.getAlignment();
807  0 System.out.println("blah");
808  0 SequenceI[] rseqs = rAlignment.getSequencesArray();
809  0 Assert.assertNotNull(rseqs[0]);
810  0 Assert.assertNotNull(rseqs[1]);
811  0 Assert.assertNotNull(rseqs[2]);
812  0 Assert.assertNotNull(rseqs[3]);
813  0 Assert.assertNotNull(rseqs[0].getDatasetSequence());
814  0 Assert.assertNotNull(rseqs[1].getDatasetSequence());
815  0 Assert.assertNotNull(rseqs[2].getDatasetSequence());
816  0 Assert.assertNotNull(rseqs[3].getDatasetSequence());
817   
818    // The Asserts below are expected to fail until the PDB chainCode is
819    // recoverable from a Jalview projects
820  0 for (int chain = 0; chain < 4; chain++)
821    {
822  0 PDBEntry recov = rseqs[chain].getDatasetSequence().getAllPDBEntries()
823    .get(0);
824  0 PDBEntry expected = pdbEntries[chain];
825  0 Assert.assertEquals(recov.getId(), expected.getId(),
826    "Mismatch PDB ID");
827  0 Assert.assertEquals(recov.getChainCode(), expected.getChainCode(),
828    "Mismatch PDB ID");
829  0 Assert.assertEquals(recov.getType(), expected.getType(),
830    "Mismatch PDBEntry 'Type'");
831  0 Assert.assertNotNull(recov.getFile(),
832    "Recovered PDBEntry should have a non-null file entry");
833  0 Assert.assertEquals(
834    recov.getFile().toLowerCase(Locale.ENGLISH)
835    .lastIndexOf("pdb"),
836    recov.getFile().length() - 3,
837    "Recovered PDBEntry file should have PDB suffix");
838    }
839    }
840   
841    /**
842    * Configure an alignment and a sub-group each with distinct colour schemes,
843    * Conservation and PID thresholds, and confirm these are restored from the
844    * saved project.
845    *
846    * @throws IOException
847    */
 
848  0 toggle @Test(groups = { "Functional" })
849    public void testStoreAndRecoverAnnotationRowElementColours()
850    throws IOException
851    {
852  0 Desktop.instance.closeAll_actionPerformed(null);
853  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded("SEQ\tMNQ",
854    DataSourceType.PASTE);
855   
856  0 AlignViewport av = af.getViewport();
857  0 AlignmentI al = av.getAlignment();
858  0 SequenceI fsq;
859  0 fsq = al.getSequenceAt(0);
860  0 Annotation annots[] = new Annotation[fsq.getLength()];
861  0 AlignmentAnnotation ala = new AlignmentAnnotation("Colour", "Annots",
862    annots);
863  0 annots[0] = new Annotation(1.0f);
864  0 annots[1] = new Annotation(2.0f);
865  0 annots[2] = new Annotation(3.0f);
866  0 annots[0].colour = Color.RED;
867  0 annots[1].colour = Color.GREEN;
868  0 annots[2].colour = Color.BLUE;
869  0 ala.validateRangeAndDisplay();
870  0 al.getSequenceAt(0).addAlignmentAnnotation(ala);
871  0 al.addAnnotation(ala);
872    /*
873    * and colour by annotation
874    */
875  0 AnnotationColourGradient acg = new AnnotationColourGradient(ala,
876    af.alignPanel.av.getGlobalColourScheme(), 0);
877  0 acg.setSeqAssociated(true);
878  0 acg.setPredefinedColours(true);
879  0 af.changeColour(acg);
880  0 Color seqcol[] = new Color[3];
881  0 for (int iStart = fsq.findIndex(fsq.getStart()), i = 0; i < 3; i++)
882    {
883  0 seqcol[i] = af.alignPanel.getSeqPanel().seqCanvas
884    .getSequenceRenderer()
885    .getResidueColour(fsq, iStart + i, null);
886    }
887    /*
888    * save project, close windows, reload project, verify
889    */
890  0 File tfile = File.createTempFile(
891    "testStoreAndRecoverAnnotRowElemColors", ".jvp");
892  0 tfile.deleteOnExit();
893  0 new Jalview2XML(false).saveState(tfile);
894    // Desktop.instance.closeAll_actionPerformed(null);
895  0 af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
896    DataSourceType.FILE);
897  0 Assert.assertNotNull(af, "Failed to reload project");
898    /*
899    * verify alignment annotation has colors
900    */
901  0 av = af.getViewport();
902   
903  0 ColourSchemeI loadedCscheme = av.getGlobalColourScheme();
904  0 Assert.assertTrue(loadedCscheme instanceof AnnotationColourGradient,
905    "Didn't apply Annotation colour gradient");
906  0 acg = (AnnotationColourGradient) loadedCscheme;
907  0 assertTrue(acg.isSeqAssociated());
908  0 assertTrue(acg.isPredefinedColours());
909   
910  0 al = av.getAlignment();
911  0 fsq = al.getSequenceAt(0);
912  0 ala = fsq.getAnnotation()[0];
913  0 Assert.assertNotNull(ala, "No annotation row recovered");
914  0 Assert.assertNotNull(ala.annotations);
915  0 for (int iStart = al.getSequenceAt(0)
916  0 .findIndex(al.getSequenceAt(0).getStart()), i = 0; i < 3; i++)
917    {
918  0 Assert.assertTrue(ala.annotations[i].colour != null);
919  0 Assert.assertTrue(ala.annotations[i].colour.equals(annots[i].colour));
920  0 Color newseqcol = af.alignPanel.getSeqPanel().seqCanvas
921    .getSequenceRenderer()
922    .getResidueColour(fsq, iStart + i, null);
923  0 Assert.assertTrue(seqcol[i].equals(newseqcol),
924    "Sequence shading is different");
925   
926    }
927   
928    }
929   
930    /**
931    * Configure an alignment and a sub-group each with distinct colour schemes,
932    * Conservation and PID thresholds, and confirm these are restored from the
933    * saved project.
934    *
935    * @throws IOException
936    */
 
937  0 toggle @Test(groups = { "Functional" })
938    public void testStoreAndRecoverColourThresholds() throws IOException
939    {
940  0 Desktop.instance.closeAll_actionPerformed(null);
941  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
942    "examples/uniref50.fa", DataSourceType.FILE);
943   
944  0 AlignViewport av = af.getViewport();
945  0 AlignmentI al = av.getAlignment();
946   
947    /*
948    * Colour alignment by Buried Index, Above 10% PID, By Conservation 20%
949    */
950  0 av.setColourAppliesToAllGroups(false);
951  0 af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
952  0 assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
953  0 af.abovePIDThreshold_actionPerformed(true);
954  0 SliderPanel sp = SliderPanel.getSliderPanel();
955  0 assertFalse(sp.isForConservation());
956  0 sp.valueChanged(10);
957  0 af.conservationMenuItem_actionPerformed(true);
958  0 sp = SliderPanel.getSliderPanel();
959  0 assertTrue(sp.isForConservation());
960  0 sp.valueChanged(20);
961  0 ResidueShaderI rs = av.getResidueShading();
962  0 assertEquals(rs.getThreshold(), 10);
963  0 assertTrue(rs.conservationApplied());
964  0 assertEquals(rs.getConservationInc(), 20);
965   
966    /*
967    * create a group with Strand colouring, 30% Conservation
968    * and 40% PID threshold
969    * (notice menu action applies to selection group even if mouse click
970    * is at a sequence not in the group)
971    */
972  0 SequenceGroup sg = new SequenceGroup();
973  0 sg.addSequence(al.getSequenceAt(0), false);
974  0 sg.setStartRes(15);
975  0 sg.setEndRes(25);
976  0 av.setSelectionGroup(sg);
977  0 PopupMenu popupMenu = new PopupMenu(af.alignPanel, al.getSequenceAt(2),
978    null);
979  0 popupMenu.changeColour_actionPerformed(
980    JalviewColourScheme.Strand.toString());
981  0 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
982  0 assertEquals(al.getGroups().size(), 1);
983  0 assertSame(al.getGroups().get(0), sg);
984  0 popupMenu.conservationMenuItem_actionPerformed(true);
985  0 sp = SliderPanel.getSliderPanel();
986  0 assertTrue(sp.isForConservation());
987  0 sp.valueChanged(30);
988  0 popupMenu.abovePIDColour_actionPerformed(true);
989  0 sp = SliderPanel.getSliderPanel();
990  0 assertFalse(sp.isForConservation());
991  0 sp.valueChanged(40);
992  0 assertTrue(sg.getGroupColourScheme().conservationApplied());
993  0 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
994  0 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
995   
996    /*
997    * save project, close windows, reload project, verify
998    */
999  0 File tfile = File.createTempFile("testStoreAndRecoverColourThresholds",
1000    ".jvp");
1001  0 tfile.deleteOnExit();
1002  0 new Jalview2XML(false).saveState(tfile);
1003  0 Desktop.instance.closeAll_actionPerformed(null);
1004  0 af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
1005    DataSourceType.FILE);
1006  0 Assert.assertNotNull(af, "Failed to reload project");
1007   
1008    /*
1009    * verify alignment (background) colouring
1010    */
1011  0 rs = af.getViewport().getResidueShading();
1012  0 assertTrue(rs.getColourScheme() instanceof BuriedColourScheme);
1013  0 assertEquals(rs.getThreshold(), 10);
1014  0 assertTrue(rs.conservationApplied());
1015  0 assertEquals(rs.getConservationInc(), 20);
1016   
1017    /*
1018    * verify group colouring
1019    */
1020  0 assertEquals(1, af.getViewport().getAlignment().getGroups().size(), 1);
1021  0 rs = af.getViewport().getAlignment().getGroups().get(0)
1022    .getGroupColourScheme();
1023  0 assertTrue(rs.getColourScheme() instanceof StrandColourScheme);
1024  0 assertEquals(rs.getThreshold(), 40);
1025  0 assertTrue(rs.conservationApplied());
1026  0 assertEquals(rs.getConservationInc(), 30);
1027    }
1028   
1029    /**
1030    * Test save and reload of feature colour schemes and filter settings
1031    *
1032    * @throws IOException
1033    */
 
1034  0 toggle @Test(groups = { "Functional" })
1035    public void testSaveLoadFeatureColoursAndFilters() throws IOException
1036    {
1037  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
1038    ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
1039  0 SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
1040   
1041    /*
1042    * add some features to the sequence
1043    */
1044  0 int score = 1;
1045  0 addFeatures(seq1, "type1", score++);
1046  0 addFeatures(seq1, "type2", score++);
1047  0 addFeatures(seq1, "type3", score++);
1048  0 addFeatures(seq1, "type4", score++);
1049  0 addFeatures(seq1, "type5", score++);
1050   
1051    /*
1052    * set colour schemes for features
1053    */
1054  0 FeatureRendererModel fr = af.getFeatureRenderer();
1055  0 fr.findAllFeatures(true);
1056   
1057    // type1: red
1058  0 fr.setColour("type1", new FeatureColour(Color.red));
1059   
1060    // type2: by label
1061  0 FeatureColourI byLabel = new FeatureColour();
1062  0 byLabel.setColourByLabel(true);
1063  0 fr.setColour("type2", byLabel);
1064   
1065    // type3: by score above threshold
1066  0 FeatureColourI byScore = new FeatureColour(null, Color.BLACK,
1067    Color.BLUE, null, 1, 10);
1068  0 byScore.setAboveThreshold(true);
1069  0 byScore.setThreshold(2f);
1070  0 fr.setColour("type3", byScore);
1071   
1072    // type4: by attribute AF
1073  0 FeatureColourI byAF = new FeatureColour();
1074  0 byAF.setColourByLabel(true);
1075  0 byAF.setAttributeName("AF");
1076  0 fr.setColour("type4", byAF);
1077   
1078    // type5: by attribute CSQ:PolyPhen below threshold
1079  0 FeatureColourI byPolyPhen = new FeatureColour(null, Color.BLACK,
1080    Color.BLUE, null, 1, 10);
1081  0 byPolyPhen.setBelowThreshold(true);
1082  0 byPolyPhen.setThreshold(3f);
1083  0 byPolyPhen.setAttributeName("CSQ", "PolyPhen");
1084  0 fr.setColour("type5", byPolyPhen);
1085   
1086    /*
1087    * set filters for feature types
1088    */
1089   
1090    // filter type1 features by (label contains "x")
1091  0 FeatureMatcherSetI filterByX = new FeatureMatcherSet();
1092  0 filterByX.and(FeatureMatcher.byLabel(Condition.Contains, "x"));
1093  0 fr.setFeatureFilter("type1", filterByX);
1094   
1095    // filter type2 features by (score <= 2.4 and score > 1.1)
1096  0 FeatureMatcherSetI filterByScore = new FeatureMatcherSet();
1097  0 filterByScore.and(FeatureMatcher.byScore(Condition.LE, "2.4"));
1098  0 filterByScore.and(FeatureMatcher.byScore(Condition.GT, "1.1"));
1099  0 fr.setFeatureFilter("type2", filterByScore);
1100   
1101    // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
1102  0 FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
1103  0 filterByXY
1104    .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
1105  0 filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
1106    "PolyPhen"));
1107  0 fr.setFeatureFilter("type3", filterByXY);
1108   
1109    /*
1110    * save as Jalview project
1111    */
1112  0 File tfile = File.createTempFile("JalviewTest", ".jvp");
1113  0 tfile.deleteOnExit();
1114  0 String filePath = tfile.getAbsolutePath();
1115  0 af.saveAlignment(filePath, FileFormat.Jalview);
1116  0 assertTrue(af.isSaveAlignmentSuccessful(),
1117    "Failed to store as a project.");
1118   
1119    /*
1120    * close current alignment and load the saved project
1121    */
1122  0 af.closeMenuItem_actionPerformed(true);
1123  0 af = null;
1124  0 af = new FileLoader().LoadFileWaitTillLoaded(filePath,
1125    DataSourceType.FILE);
1126  0 assertNotNull(af, "Failed to import new project");
1127   
1128    /*
1129    * verify restored feature colour schemes and filters
1130    */
1131  0 fr = af.getFeatureRenderer();
1132  0 FeatureColourI fc = fr.getFeatureStyle("type1");
1133  0 assertTrue(fc.isSimpleColour());
1134  0 assertEquals(fc.getColour(), Color.red);
1135  0 fc = fr.getFeatureStyle("type2");
1136  0 assertTrue(fc.isColourByLabel());
1137  0 fc = fr.getFeatureStyle("type3");
1138  0 assertTrue(fc.isGraduatedColour());
1139  0 assertNull(fc.getAttributeName());
1140  0 assertTrue(fc.isAboveThreshold());
1141  0 assertEquals(fc.getThreshold(), 2f);
1142  0 fc = fr.getFeatureStyle("type4");
1143  0 assertTrue(fc.isColourByLabel());
1144  0 assertTrue(fc.isColourByAttribute());
1145  0 assertEquals(fc.getAttributeName(), new String[] { "AF" });
1146  0 fc = fr.getFeatureStyle("type5");
1147  0 assertTrue(fc.isGraduatedColour());
1148  0 assertTrue(fc.isColourByAttribute());
1149  0 assertEquals(fc.getAttributeName(), new String[] { "CSQ", "PolyPhen" });
1150  0 assertTrue(fc.isBelowThreshold());
1151  0 assertEquals(fc.getThreshold(), 3f);
1152   
1153  0 assertEquals(fr.getFeatureFilter("type1").toStableString(),
1154    "Label Contains x");
1155  0 assertEquals(fr.getFeatureFilter("type2").toStableString(),
1156    "(Score LE 2.4) AND (Score GT 1.1)");
1157  0 assertEquals(fr.getFeatureFilter("type3").toStableString(),
1158    "(AF Contains X) OR (CSQ:PolyPhen NE 0)");
1159    }
1160   
 
1161  0 toggle private void addFeature(SequenceI seq, String featureType, int score)
1162    {
1163  0 SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
1164    score, "grp");
1165  0 sf.setValue("AF", score);
1166  0 sf.setValue("CSQ", new HashMap<String, String>()
1167    {
 
1168  0 toggle {
1169  0 put("PolyPhen", Integer.toString(score));
1170    }
1171    });
1172  0 seq.addSequenceFeature(sf);
1173    }
1174   
1175    /**
1176    * Adds two features of the given type to the given sequence, also setting the
1177    * score as the value of attribute "AF" and sub-attribute "CSQ:PolyPhen"
1178    *
1179    * @param seq
1180    * @param featureType
1181    * @param score
1182    */
 
1183  0 toggle private void addFeatures(SequenceI seq, String featureType, int score)
1184    {
1185  0 addFeature(seq, featureType, score++);
1186  0 addFeature(seq, featureType, score);
1187    }
1188   
1189    /**
1190    * pre 2.11 - jalview 2.10 erroneously created new dataset entries for each
1191    * view (JAL-3171) this test ensures we can import and merge those views
1192    */
 
1193  0 toggle @Test(groups = { "Functional" })
1194    public void testMergeDatasetsforViews() throws IOException
1195    {
1196    // simple project - two views on one alignment
1197  0 AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
1198    "examples/testdata/projects/twoViews.jvp", DataSourceType.FILE);
1199  0 assertNotNull(af);
1200  0 assertTrue(af.getAlignPanels().size() > 1);
1201  0 verifyDs(af);
1202    }
1203   
1204    /**
1205    * pre 2.11 - jalview 2.10 erroneously created new dataset entries for each
1206    * view (JAL-3171) this test ensures we can import and merge those views This
1207    * is a more complex project
1208    */
 
1209  0 toggle @Test(groups = { "Functional" })
1210    public void testMergeDatasetsforManyViews() throws IOException
1211    {
1212  0 Desktop.instance.closeAll_actionPerformed(null);
1213   
1214    // complex project - one dataset, several views on several alignments
1215  0 AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
1216    "examples/testdata/projects/manyViews.jvp",
1217    DataSourceType.FILE);
1218  0 assertNotNull(af);
1219   
1220  0 AlignmentI ds = null;
1221  0 for (AlignFrame alignFrame : Desktop.getDesktopAlignFrames())
1222    {
1223  0 if (ds == null)
1224    {
1225  0 ds = verifyDs(alignFrame);
1226    }
1227    else
1228    {
1229    // check that this frame's dataset matches the last
1230  0 assertTrue(ds == verifyDs(alignFrame));
1231    }
1232    }
1233    }
1234   
 
1235  0 toggle private AlignmentI verifyDs(AlignFrame af)
1236    {
1237  0 AlignmentI ds = null;
1238  0 for (AlignmentViewPanel ap : af.getAlignPanels())
1239    {
1240  0 if (ds == null)
1241    {
1242  0 ds = ap.getAlignment().getDataset();
1243    }
1244    else
1245    {
1246  0 assertTrue(ap.getAlignment().getDataset() == ds,
1247    "Dataset was not the same for imported 2.10.5 project with several alignment views");
1248    }
1249    }
1250  0 return ds;
1251    }
1252   
 
1253  0 toggle @Test(groups = "Functional")
1254    public void testPcaViewAssociation() throws IOException
1255    {
1256  0 Desktop.instance.closeAll_actionPerformed(null);
1257  0 final String PCAVIEWNAME = "With PCA";
1258    // create a new tempfile
1259  0 File tempfile = File.createTempFile("jvPCAviewAssoc", "jvp");
1260   
1261    {
1262  0 String exampleFile = "examples/uniref50.fa";
1263  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(exampleFile,
1264    DataSourceType.FILE);
1265  0 assertNotNull(af, "Didn't read in the example file correctly.");
1266  0 AlignmentPanel origView = (AlignmentPanel) af.getAlignPanels().get(0);
1267  0 AlignmentPanel newview = af.newView(PCAVIEWNAME, true);
1268    // create another for good measure
1269  0 af.newView("Not the PCA View", true);
1270  0 PCAPanel pcaPanel = new PCAPanel(origView, "BLOSUM62",
1271    new SimilarityParams(true, true, true, false));
1272    // we're in the test exec thread, so we can just run synchronously here
1273  0 pcaPanel.run();
1274   
1275    // now switch the linked view
1276  0 pcaPanel.selectAssociatedView(newview);
1277   
1278  0 assertTrue(pcaPanel.getAlignViewport() == newview.getAlignViewport(),
1279    "PCA should be associated with 'With PCA' view: test is broken");
1280   
1281    // now save and reload project
1282  0 Jalview2XML jv2xml = new jalview.project.Jalview2XML(false);
1283  0 tempfile.delete();
1284  0 jv2xml.saveState(tempfile);
1285  0 assertTrue(jv2xml.errorMessage == null,
1286    "Failed to save dummy project with PCA: test broken");
1287    }
1288   
1289    // load again.
1290  0 Desktop.instance.closeAll_actionPerformed(null);
1291  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
1292    tempfile.getCanonicalPath(), DataSourceType.FILE);
1293  0 JInternalFrame[] frames = Desktop.instance.getAllFrames();
1294    // PCA and the tabbed alignment view should be the only two windows on the
1295    // desktop
1296  0 assertEquals(frames.length, 2,
1297    "PCA and the tabbed alignment view should be the only two windows on the desktop");
1298  0 PCAPanel pcaPanel = (PCAPanel) frames[frames[0] == af ? 1 : 0];
1299   
1300  0 AlignmentViewPanel restoredNewView = null;
1301  0 for (AlignmentViewPanel alignpanel : Desktop.getAlignmentPanels(null))
1302    {
1303  0 if (alignpanel.getAlignViewport() == pcaPanel.getAlignViewport())
1304    {
1305  0 restoredNewView = alignpanel;
1306    }
1307    }
1308  0 assertEquals(restoredNewView.getViewName(), PCAVIEWNAME);
1309  0 assertTrue(
1310    restoredNewView.getAlignViewport() == pcaPanel
1311    .getAlignViewport(),
1312    "Didn't restore correct view association for the PCA view");
1313    }
1314   
1315    /**
1316    * Test save and reload of DBRefEntry including GeneLocus in project
1317    *
1318    * @throws Exception
1319    */
 
1320  0 toggle @Test(groups = { "Functional" })
1321    public void testStoreAndRecoverGeneLocus() throws Exception
1322    {
1323  0 Desktop.instance.closeAll_actionPerformed(null);
1324  0 String seqData = ">P30419\nACDE\n>X1235\nGCCTGTGACGAA";
1325  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(seqData,
1326    DataSourceType.PASTE);
1327  0 assertNotNull(af, "Didn't read in the example file correctly.");
1328   
1329  0 AlignmentViewPanel ap = Desktop.getAlignmentPanels(null)[0];
1330  0 SequenceI pep = ap.getAlignment().getSequenceAt(0);
1331  0 SequenceI cds = ap.getAlignment().getSequenceAt(1);
1332   
1333    /*
1334    * give 'protein' a dbref to self, a dbref with map to CDS,
1335    * and a dbref with map to gene 'locus'
1336    */
1337  0 DBRefEntry dbref1 = new DBRefEntry("Uniprot", "1", "P30419", null);
1338  0 pep.addDBRef(dbref1);
1339  0 Mapping cdsmap = new Mapping(cds,
1340    new MapList(new int[]
1341    { 1, 4 }, new int[] { 1, 12 }, 1, 3));
1342  0 DBRefEntry dbref2 = new DBRefEntry("EMBLCDS", "2", "X1235", cdsmap);
1343  0 pep.addDBRef(dbref2);
1344  0 Mapping locusmap = new Mapping(null,
1345    new MapList(new int[]
1346    { 1, 4 }, new int[] { 2674123, 2674135 }, 1, 3));
1347  0 DBRefEntry dbref3 = new GeneLocus("human", "GRCh38", "5", locusmap);
1348  0 pep.addDBRef(dbref3);
1349   
1350  0 File tfile = File.createTempFile("testStoreAndRecoverGeneLocus",
1351    ".jvp");
1352  0 try
1353    {
1354  0 new Jalview2XML(false).saveState(tfile);
1355    } catch (Throwable e)
1356    {
1357  0 Assert.fail("Didn't save the state", e);
1358    }
1359  0 Desktop.instance.closeAll_actionPerformed(null);
1360   
1361  0 new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
1362    DataSourceType.FILE);
1363  0 AlignmentViewPanel rap = Desktop.getAlignmentPanels(null)[0];
1364  0 SequenceI rpep = rap.getAlignment().getSequenceAt(0);
1365  0 DBModList<DBRefEntry> dbrefs = rpep.getDBRefs();
1366  0 assertEquals(rpep.getName(), "P30419");
1367  0 assertEquals(dbrefs.size(), 3);
1368  0 DBRefEntry dbRef = dbrefs.get(0);
1369  0 assertFalse(dbRef instanceof GeneLocus);
1370  0 assertNull(dbRef.getMap());
1371  0 assertEquals(dbRef, dbref1);
1372   
1373    /*
1374    * restored dbrefs with mapping have a different 'map to'
1375    * sequence but otherwise match the original dbrefs
1376    */
1377  0 dbRef = dbrefs.get(1);
1378  0 assertFalse(dbRef instanceof GeneLocus);
1379  0 assertTrue(dbRef.equalRef(dbref2));
1380  0 assertNotNull(dbRef.getMap());
1381  0 SequenceI rcds = rap.getAlignment().getSequenceAt(1);
1382  0 assertSame(dbRef.getMap().getTo(), rcds);
1383    // compare MapList but not map.to
1384  0 assertEquals(dbRef.getMap().getMap(), dbref2.getMap().getMap());
1385   
1386    /*
1387    * GeneLocus map.to is null so can compare Mapping objects
1388    */
1389  0 dbRef = dbrefs.get(2);
1390  0 assertTrue(dbRef instanceof GeneLocus);
1391  0 assertEquals(dbRef, dbref3);
1392    }
1393   
1394    /**
1395    * test store and recovery of Overview windows
1396    *
1397    * @throws Exception
1398    */
 
1399  0 toggle @Test(groups = { "Functional" }, enabled = true)
1400    public void testStoreAndRecoverOverview() throws Exception
1401    {
1402  0 Desktop.instance.closeAll_actionPerformed(null);
1403   
1404  0 Cache.setProperty("SHOW_OVERVIEW", "false");
1405  0 Cache.setProperty(Preferences.USE_LEGACY_GAP, "false");
1406  0 Cache.setColourProperty(Preferences.GAP_COLOUR, Color.green);
1407  0 Cache.setColourProperty(Preferences.HIDDEN_COLOUR, Color.yellow);
1408  0 Cache.setProperty(Preferences.SHOW_OV_HIDDEN_AT_START, "true");
1409   
1410  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
1411    "examples/uniref50.fa", DataSourceType.FILE);
1412   
1413    /*
1414    * open and resize / reposition overview
1415    */
1416  0 af.overviewMenuItem_actionPerformed(null);
1417  0 OverviewPanel ov1 = af.alignPanel.getOverviewPanel();
1418  0 assertNotNull(ov1);
1419  0 ov1.setFrameBounds(20, 30, 200, 400);
1420  0 assertEquals(ov1.getTitle(), "Overview examples/uniref50.fa");
1421  0 assertTrue(ov1.isShowHiddenRegions());
1422   
1423    /*
1424    * open a New View and its Overview and reposition it
1425    */
1426  0 af.newView_actionPerformed(null);
1427  0 af.overviewMenuItem_actionPerformed(null);
1428  0 OverviewPanel ov2 = af.alignPanel.getOverviewPanel();
1429  0 assertNotNull(ov2);
1430  0 assertNotSame(ov1, ov2);
1431  0 ov2.setFrameBounds(25, 35, 205, 405);
1432  0 assertEquals(ov1.getTitle(), "Overview examples/uniref50.fa Original");
1433  0 assertEquals(ov2.getTitle(), "Overview examples/uniref50.fa View 1");
1434   
1435  0 File tfile = File.createTempFile("testStoreAndRecoverOverview", ".jvp");
1436  0 new Jalview2XML(false).saveState(tfile);
1437  0 Desktop.instance.closeAll_actionPerformed(null);
1438   
1439    /*
1440    * change preferences (should _not_ affect reloaded Overviews)
1441    */
1442  0 Cache.setProperty("SHOW_OVERVIEW", "true");
1443  0 Cache.setProperty(Preferences.USE_LEGACY_GAP, "true");
1444  0 Cache.setColourProperty(Preferences.GAP_COLOUR, Color.blue);
1445  0 Cache.setColourProperty(Preferences.HIDDEN_COLOUR, Color.orange);
1446  0 Cache.setProperty(Preferences.SHOW_OV_HIDDEN_AT_START, "false");
1447   
1448  0 af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
1449    DataSourceType.FILE);
1450   
1451    /*
1452    * workaround: explicitly select View 1 (not in focus after restore)
1453    */
1454  0 af.tabSelectionChanged(1);
1455   
1456    /*
1457    * verify restored overview for View 1
1458    */
1459  0 ov2 = af.alignPanel.getOverviewPanel();
1460  0 assertEquals(ov2.getCanvas().getGapColour(), Color.green);
1461    // 'non-legacy' colouring uses white for non-gapped residues
1462  0 assertEquals(ov2.getCanvas().getResidueColour(), Color.white);
1463  0 assertEquals(ov2.getCanvas().getHiddenColour(), Color.yellow);
1464  0 assertEquals(ov2.getTitle(), "Overview examples/uniref50.fa View 1");
1465  0 assertEquals(ov2.getFrameBounds(), new Rectangle(25, 35, 205, 405));
1466  0 assertTrue(ov2.isShowHiddenRegions());
1467   
1468    /*
1469    * verify restored overview for Original view
1470    */
1471  0 af.tabSelectionChanged(0);
1472  0 ov1 = af.alignPanel.getOverviewPanel();
1473  0 assertEquals(ov1.getCanvas().getGapColour(), Color.green);
1474    // 'non-legacy' colouring uses white for non-gapped residues
1475  0 assertEquals(ov1.getCanvas().getResidueColour(), Color.white);
1476  0 assertEquals(ov1.getCanvas().getHiddenColour(), Color.yellow);
1477  0 assertEquals(ov1.getTitle(), "Overview examples/uniref50.fa Original");
1478   
1479  0 double scaling = jalview.gui.JvSwingUtilsTest.getScaling(ov1);
1480    // int width = scaling == 1.0 ? 225 : 200;
1481    // int width = scaling == 1.0 ? 225 : 200;
1482  0 Rectangle ov1Rectangle = ov1.getFrameBounds();
1483  0 assertEquals(ov1Rectangle,
1484    new Rectangle(20, 30, ov1Rectangle.width, 400));
1485  0 int width = ov1Rectangle.width;
1486  0 assertTrue(width >= 200 && width <= 225,
1487    "Rectangle width was not in the range expected (200<=width<=225; width="
1488    + width + ")");
1489  0 assertTrue(ov1.isShowHiddenRegions());
1490    }
1491   
1492    /**
1493    * Test that a view with no Overview is restored with no Overview, even if
1494    * 'Open Overview' is selected in Preferences
1495    *
1496    * @throws Exception
1497    */
 
1498  0 toggle @Test(groups = { "Functional" }, enabled = true)
1499    public void testStoreAndRecoverNoOverview() throws Exception
1500    {
1501  0 Cache.setProperty("SHOW_OVERVIEW", "false");
1502  0 Desktop.instance.closeAll_actionPerformed(null);
1503  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
1504    ">seq1\nMATRSQFLVNF\n", DataSourceType.PASTE);
1505   
1506  0 File tfile = File.createTempFile("testStoreAndRecoverOverview", ".jvp");
1507  0 new Jalview2XML(false).saveState(tfile);
1508  0 Desktop.instance.closeAll_actionPerformed(null);
1509   
1510  0 Cache.setProperty("SHOW_OVERVIEW", "true");
1511  0 af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
1512    DataSourceType.FILE);
1513   
1514  0 assertNull(af.alignPanel.getOverviewPanel());
1515    }
1516   
1517    /**
1518    * Test that a view from an older version of Jalview is restored with Overview
1519    * automatically shown when the preference is set
1520    *
1521    * @throws Exception
1522    */
 
1523  0 toggle @Test(groups = { "Functional" }, enabled = true)
1524    public void testAutoShowOverviewForLegacyProjects() throws Exception
1525    {
1526  0 Desktop.instance.closeAll_actionPerformed(null);
1527  0 Cache.setProperty("SHOW_OVERVIEW", "true");
1528  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
1529    "examples/exampleFile.jvp", DataSourceType.FILE);
1530   
1531  0 Cache.setProperty("SHOW_OVERVIEW", "false");
1532  0 assertNotNull(af.alignPanel.getOverviewPanel());
1533    }
1534   
1535    /**
1536    * Test that loading example.jvp, doing some stuff, then hitting reload
1537    * doesn't leave the modified window still open
1538    *
1539    * See JAL-4127 - interactively performing the same actions and reloading
1540    * works fine, but programmatically they do not
1541    *
1542    * @throws Exception
1543    */
 
1544  0 toggle @Test(groups = { "Functional" }, enabled = false)
1545    public void testReloadActuallyReloads() throws Exception
1546    {
1547  0 Desktop.instance.closeAll_actionPerformed(null);
1548  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
1549    "examples/exampleFile.jvp", DataSourceType.FILE);
1550  0 af.getViewport().getColumnSelection().addElement(3);
1551  0 af.hideSelColumns_actionPerformed(null);
1552  0 af.newView("new", true);
1553  0 af.reload_actionPerformed(null);
1554  0 Thread.sleep(30);
1555    // af exists still but isn't shown
1556  0 assertTrue(af.isClosed());
1557    }
1558   
 
1559  0 toggle @Test(groups = { "Functional" })
1560    public void testMatrixToFloatsAndBack()
1561    {
1562  0 int imax = 2000;
1563  0 int i = imax;
1564  0 SequenceI sq = new Sequence("dummy", "SEQ");
1565  0 while (sq.getLength() < i)
1566    {
1567  0 sq.setSequence(sq.getSequenceAsString() + 'Q');
1568    }
1569  0 float[][] paevals = new float[i][i];
1570  0 for (i = imax - 1; i >= 0; i--)
1571    {
1572  0 for (int j = 0; j <= i; j++)
1573    {
1574  0 paevals[i][j] = ((i - j < 2)
1575    || ((i > 1 && i < 5) && (j > 1 && i < 5))) ? 1 : 0f;
1576  0 paevals[j][i] = -paevals[i][j];
1577    }
1578    }
1579  0 PAEContactMatrix dummyMat = new PAEContactMatrix(sq, paevals);
1580  0 String content = ContactMatrix.contactToFloatString(dummyMat);
1581  0 Assert.assertTrue(content.contains("\t1.")); // at least one element must be
1582    // 1
1583  0 float[][] vals = ContactMatrix.fromFloatStringToContacts(content,
1584    sq.getLength(), sq.getLength());
1585  0 assertEquals(vals[3][4], paevals[3][4]);
1586  0 assertEquals(vals[4][3], paevals[4][3]);
1587   
1588    // test recovery
1589  0 for (i = 0; i < imax; i++)
1590    {
1591  0 for (int j = 0; j < imax; j++)
1592    {
1593  0 assertEquals(vals[i][j], paevals[i][j]);
1594    }
1595    }
1596    }
1597   
 
1598  0 toggle @Test(groups = { "Functional" })
1599    public void testPAEsaveRestore() throws Exception
1600    {
1601  0 Desktop.instance.closeAll_actionPerformed(null);
1602  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
1603    ">seq1\nMATRSQFLVNF\n", DataSourceType.PASTE);
1604  0 AlignmentI al = af.getViewport().getAlignment();
1605    // PAE matrices are added as reference annotation to the dataset sequence
1606    // at least for now.
1607  0 SequenceI sq = al.getSequenceAt(0).getDatasetSequence();
1608  0 int i = sq.getLength();
1609  0 float[][] paevals = new float[i][i];
1610  0 for (i = i - 1; i >= 0; i--)
1611    {
1612  0 for (int j = 0; j <= i; j++)
1613    {
1614  0 paevals[i][j] = ((i - j < 2)
1615    || ((i > 1 && i < 5) && (j > 1 && i < 5))) ? 1 : 0f;
1616  0 paevals[j][i] = -paevals[i][j];
1617    }
1618    }
1619  0 PAEContactMatrix dummyMat = new PAEContactMatrix(sq, paevals);
1620  0 String content = ContactMatrix.contactToFloatString(dummyMat);
1621  0 Assert.assertTrue(content.contains("\t1.")); // at least one element must be
1622    // 1
1623  0 float[][] vals = ContactMatrix.fromFloatStringToContacts(content,
1624    sq.getLength(), sq.getLength());
1625  0 assertEquals(vals[3][4], paevals[3][4]);
1626  0 assertEquals(vals[4][3], paevals[4][3]);
1627  0 dummyMat.setGroupSet(GroupSet.makeGroups(dummyMat, false, 0.5f, false));
1628  0 Assert.assertNotSame(dummyMat.getNewick(), "");
1629  0 AlignmentAnnotation paeCm = sq.addContactList(dummyMat);
1630  0 al.addAnnotation(paeCm);
1631    // verify store/restore of group bitsets
1632  0 for (BitSet gp : dummyMat.getGroups())
1633    {
1634  0 StringBuilder sb = new StringBuilder();
1635  0 for (long val : gp.toLongArray())
1636    {
1637  0 if (sb.length() > 0)
1638    {
1639  0 sb.append(",");
1640    }
1641  0 sb.append(val);
1642    }
1643  0 String[] longvals = sb.toString().split(",");
1644  0 long[] newlongvals = new long[longvals.length];
1645  0 for (int lv = 0; lv < longvals.length; lv++)
1646    {
1647  0 try
1648    {
1649  0 newlongvals[lv] = Long.valueOf(longvals[lv]);
1650    } catch (Exception x)
1651    {
1652  0 Assert.fail("failed to deserialise bitset element ");
1653    }
1654    }
1655  0 BitSet newGp = BitSet.valueOf(newlongvals);
1656  0 assertTrue(gp.equals(newGp));
1657    }
1658  0 File tfile = File.createTempFile("testStoreAndRecoverPAEmatrix",
1659    ".jvp");
1660  0 new Jalview2XML(false).saveState(tfile);
1661  0 Desktop.instance.closeAll_actionPerformed(null);
1662   
1663  0 af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
1664    DataSourceType.FILE);
1665  0 AlignmentI newAl = af.getViewport().getAlignment();
1666  0 SequenceI newSeq = newAl.getSequenceAt(0).getDatasetSequence();
1667    // check annotation of the expected type exists
1668  0 Assert.assertEquals(newSeq.getAnnotation().length, 1);
1669  0 Assert.assertEquals(newSeq.getAnnotation()[0].graph, paeCm.graph);
1670   
1671    // check a contact matrix was recovered
1672  0 Assert.assertEquals(newSeq.getContactMaps().size(), 1);
1673    // and can be found for the annotation on the sequence
1674  0 ContactMatrixI restoredMat = newSeq
1675    .getContactMatrixFor(newSeq.getAnnotation()[0]);
1676  0 Assert.assertNotNull(restoredMat);
1677  0 MapList oldMap = ((MappableContactMatrixI) dummyMat).getMapFor(sq);
1678  0 MapList newMap = ((MappableContactMatrixI) restoredMat)
1679    .getMapFor(newSeq);
1680  0 Assert.assertEquals(oldMap.getFromRanges(), newMap.getFromRanges());
1681  0 Assert.assertEquals(oldMap.getToRanges(), newMap.getToRanges());
1682  0 Assert.assertEquals(oldMap.getFromRatio(), newMap.getFromRatio());
1683  0 Assert.assertEquals(oldMap.getToRatio(), newMap.getToRatio());
1684  0 for (i = sq.getLength() - 1; i >= 0; i--)
1685    {
1686  0 ContactListI oldCM = dummyMat.getContactList(i),
1687    newCM = restoredMat.getContactList(i);
1688  0 for (int j = oldCM.getContactHeight(); j >= 0; j--)
1689    {
1690  0 double old_j = oldCM.getContactAt(j);
1691  0 double new_j = newCM.getContactAt(j);
1692  0 Assert.assertEquals(old_j, new_j);
1693    }
1694    }
1695  0 Assert.assertEquals(restoredMat.hasGroups(), dummyMat.hasGroups());
1696  0 Assert.assertEquals(restoredMat.getGroups(), dummyMat.getGroups());
1697  0 Assert.assertEquals(restoredMat.hasTree(), dummyMat.hasTree());
1698  0 Assert.assertEquals(restoredMat.getNewick(), dummyMat.getNewick());
1699   
1700    // verify no duplicate PAE matrix data when new view created and saved
1701   
1702    // add reference annotations to view first, then copy
1703  0 AlignmentUtils.addReferenceAnnotationTo(newAl, newAl.getSequenceAt(0),
1704    newSeq.getAnnotation()[0], null);
1705   
1706  0 AlignmentViewPanel newview = af.newView("copy of PAE", true);
1707   
1708    // redundant asserts here check all is good with the new view firest...
1709  0 AlignmentI newviewAl = newview.getAlignment();
1710  0 SequenceI newviewSeq = newviewAl.getSequenceAt(0);
1711    // check annotation of the expected type exists
1712  0 Assert.assertEquals(newviewSeq.getAnnotation().length, 1);
1713  0 Assert.assertEquals(newviewSeq.getAnnotation()[0].graph, paeCm.graph);
1714    // check we have just one contact matrix mapping
1715  0 Assert.assertEquals(newviewSeq.getContactMaps().size(), 1);
1716   
1717    // and can be found for the annotation on the sequence
1718  0 ContactMatrixI newviewMat = newviewSeq
1719    .getContactMatrixFor(newviewSeq.getAnnotation()[0]);
1720  0 Assert.assertNotNull(newviewMat);
1721   
1722  0 Assert.assertTrue(newviewMat == restoredMat);
1723   
1724    // save the two views and restore. Now look at visible annotation to check
1725    // all views have shared refs.
1726   
1727  0 tfile = File.createTempFile("testStoreAndRecoverPAEmatrixTwoViews",
1728    ".jvp");
1729  0 new Jalview2XML(false).saveState(tfile);
1730  0 Desktop.instance.closeAll_actionPerformed(null);
1731   
1732  0 af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
1733    DataSourceType.FILE);
1734  0 newAl = af.getAlignPanels().get(0).getAlignment();
1735  0 AlignmentAnnotation view1aa = newAl.getSequenceAt(0).getAnnotation()[0];
1736   
1737  0 newviewAl = af.getAlignPanels().get(1).getAlignment();
1738  0 AlignmentAnnotation view2aa = newviewAl.getSequenceAt(0)
1739    .getAnnotation()[0];
1740   
1741    // annotations are shared across alignment views - so should still have an
1742    // identical pair of annotations.
1743  0 Assert.assertTrue(view1aa == view2aa);
1744    // identical annotations means identical contact matrix mappings
1745  0 Assert.assertEquals(
1746    newAl.getDataset().getSequenceAt(0).getContactMaps().size(), 1);
1747   
1748    // TODO Verify when distinct mappable PAEs are created, only one PAE dataset
1749    // is actually held.
1750    // Assert.assertTrue(view1aa!=view2aa);
1751    // restoredMat = newAl.getContactMatrixFor(view1aa);
1752    // newviewMat = newviewAl.getContactMatrixFor(view2aa);
1753    // Assert.assertTrue(restoredMat!=newviewMat);
1754   
1755    }
1756   
 
1757  0 toggle @Test(groups = "Functional")
1758    public void testStoreAndRestoreIDwidthAndAnnotationHeight()
1759    throws IOException
1760    {
1761  0 Desktop.instance.closeAll_actionPerformed(null);
1762  0 final String SECONDVIEW = "With Diffferent IDwidth";
1763    // create a new tempfile
1764  0 File tempfile = File.createTempFile("jvIdWidthStoreRestore", "jvp");
1765   
1766  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
1767    "examples/exampleFile.jvp", DataSourceType.FILE);
1768  0 assertNotNull(af, "Didn't read in the example file correctly.");
1769    // FIXME JAL-4281 test made platform dependent to pass, but probably
1770    // shouldn't be platform dependent
1771  0 int idWidth = af.alignPanel.getAlignViewport().getIdWidth();
1772  0 double scaling = jalview.gui.JvSwingUtilsTest.getScaling(af.alignPanel);
1773    // int expectedWidth = Platform.isMac() ? 144 : Platform.isLinux() ? scaling
1774    // == 1.0 ? 131 : 128 : 138;
1775  0 int minExpectedWidth = 128;
1776  0 int maxExpectedWidth = 144;
1777  0 assertTrue(minExpectedWidth <= idWidth && maxExpectedWidth >= idWidth,
1778    "Legacy project import should have fixed ID width. Not within the expected range ("
1779    + minExpectedWidth + "-" + maxExpectedWidth + ")");
1780  0 assertTrue(
1781    af.alignPanel.getIdPanel().getIdCanvas().isManuallyAdjusted());
1782   
1783  0 af.alignPanel.getAlignViewport().setIdWidth(100);
1784  0 af.alignPanel.updateLayout();
1785  0 assertTrue(
1786    af.alignPanel.getIdPanel().getIdCanvas().isManuallyAdjusted());
1787   
1788  0 Jalview2XML jv2xml = new jalview.project.Jalview2XML(false);
1789  0 tempfile.delete();
1790  0 jv2xml.saveState(tempfile);
1791  0 assertTrue(jv2xml.errorMessage == null,
1792    "Failed to save dummy project with PCA: test broken");
1793  0 af = null;
1794    // load again.
1795  0 Desktop.instance.closeAll_actionPerformed(null);
1796  0 af = new FileLoader().LoadFileWaitTillLoaded(
1797    tempfile.getCanonicalPath(), DataSourceType.FILE);
1798  0 assertTrue(
1799    af.alignPanel.getIdPanel().getIdCanvas().isManuallyAdjusted());
1800  0 assertEquals(af.alignPanel.getAlignViewport().getIdWidth(), 100,
1801    "New project exported and import should have adjusted ID width");
1802   
1803  0 af.alignPanel.getAlignViewport().setIdWidth(100);
1804  0 af.alignPanel.updateLayout();
1805  0 assertTrue(
1806    af.alignPanel.getIdPanel().getIdCanvas().isManuallyAdjusted());
1807   
1808    // now make it autoadjusted
1809  0 af.alignPanel.getAlignViewport().setIdWidth(-1);
1810  0 af.alignPanel.getIdPanel().getIdCanvas().setManuallyAdjusted(false);
1811  0 af.alignPanel.updateLayout();
1812  0 assertFalse(
1813    af.alignPanel.getIdPanel().getIdCanvas().isManuallyAdjusted());
1814  0 assertTrue(af.alignPanel.getAlignViewport().getIdWidth() > -1,
1815    "New project exported and import should have adjusted ID width");
1816   
1817  0 jv2xml = new jalview.project.Jalview2XML(false);
1818  0 tempfile.delete();
1819  0 jv2xml.saveState(tempfile);
1820  0 assertTrue(jv2xml.errorMessage == null,
1821    "Failed to save dummy project with PCA: test broken");
1822  0 af = null;
1823    // load again.
1824  0 Desktop.instance.closeAll_actionPerformed(null);
1825  0 af = new FileLoader().LoadFileWaitTillLoaded(
1826    tempfile.getCanonicalPath(), DataSourceType.FILE);
1827  0 assertFalse(
1828    af.alignPanel.getIdPanel().getIdCanvas().isManuallyAdjusted());
1829  0 assertTrue(af.alignPanel.getAlignViewport().getIdWidth() > -1,
1830    "New project exported and import should have adjusted ID width");
1831    }
1832   
1833    }