Clover icon

Coverage Report

  1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.project

File Jalview2xmlTests.java

 

Code metrics

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

Classes

Class Line # Actions
Jalview2xmlTests 111 824 87
0.966558896.7%
 

Contributing tests

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