Clover icon

Coverage Report

  1. Project Clover database Wed Mar 4 2026 12:01:44 GMT
  2. Package jalview.project

File Jalview2xmlTests.java

 

Code metrics

94
1,003
39
1
2,291
1,707
114
0.11
25.72
39
2.92

Classes

Class Line # Actions
Jalview2xmlTests 127 1,003 114
0.9286971792.9%
 

Contributing tests

This file is covered by 29 tests. .

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