2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertFalse;
25 import static org.testng.Assert.assertNotNull;
26 import static org.testng.Assert.assertNull;
27 import static org.testng.Assert.assertSame;
28 import static org.testng.Assert.assertTrue;
29 import static org.testng.Assert.fail;
31 import jalview.analysis.PCA;
32 import jalview.api.AlignViewportI;
33 import jalview.api.AlignmentViewPanel;
34 import jalview.api.FeatureColourI;
35 import jalview.api.ViewStyleI;
36 import jalview.bin.Cache;
37 import jalview.datamodel.AlignmentAnnotation;
38 import jalview.datamodel.AlignmentI;
39 import jalview.datamodel.HiddenSequences;
40 import jalview.datamodel.PDBEntry;
41 import jalview.datamodel.PDBEntry.Type;
42 import jalview.datamodel.Point;
43 import jalview.datamodel.SequenceCollectionI;
44 import jalview.datamodel.SequenceFeature;
45 import jalview.datamodel.SequenceGroup;
46 import jalview.datamodel.SequenceI;
47 import jalview.datamodel.SequencePoint;
48 import jalview.datamodel.features.FeatureMatcher;
49 import jalview.datamodel.features.FeatureMatcherSet;
50 import jalview.datamodel.features.FeatureMatcherSetI;
51 import jalview.gui.AlignFrame;
52 import jalview.gui.AlignViewport;
53 import jalview.gui.AlignmentPanel;
54 import jalview.gui.CalculationChooser;
55 import jalview.gui.Desktop;
56 import jalview.gui.Jalview2XML;
57 import jalview.gui.JvOptionPane;
58 import jalview.gui.PCAPanel;
59 import jalview.gui.PopupMenu;
60 import jalview.gui.RotatableCanvas;
61 import jalview.gui.SliderPanel;
62 import jalview.math.MatrixTest;
63 import jalview.renderer.ResidueShaderI;
64 import jalview.schemes.AnnotationColourGradient;
65 import jalview.schemes.BuriedColourScheme;
66 import jalview.schemes.ColourSchemeI;
67 import jalview.schemes.ColourSchemeProperty;
68 import jalview.schemes.FeatureColour;
69 import jalview.schemes.JalviewColourScheme;
70 import jalview.schemes.RNAHelicesColour;
71 import jalview.schemes.StrandColourScheme;
72 import jalview.schemes.TCoffeeColourScheme;
73 import jalview.structure.StructureImportSettings;
74 import jalview.util.matcher.Condition;
75 import jalview.viewmodel.AlignmentViewport;
76 import jalview.viewmodel.PCAModel;
77 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
79 import java.awt.Color;
81 import java.io.IOException;
82 import java.util.ArrayList;
83 import java.util.HashMap;
84 import java.util.List;
87 import javax.swing.JInternalFrame;
88 import javax.swing.JRadioButton;
90 import org.testng.Assert;
91 import org.testng.AssertJUnit;
92 import org.testng.annotations.BeforeClass;
93 import org.testng.annotations.BeforeMethod;
94 import org.testng.annotations.Test;
96 import junit.extensions.PA;
98 @Test(singleThreaded = true)
99 public class Jalview2xmlTests extends Jalview2xmlBase
101 @BeforeClass(alwaysRun = true)
104 Cache.loadProperties("test/jalview/io/testProps.jvprops");
107 @BeforeMethod(alwaysRun = true)
108 public void setUpMethod()
110 Desktop.instance.closeAll_actionPerformed(null);
114 @BeforeClass(alwaysRun = true)
115 public void setUpJvOptionPane()
117 JvOptionPane.setInteractiveMode(false);
118 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
121 @Test(groups = { "Functional" })
122 public void testRNAStructureRecovery() throws Exception
124 String inFile = "examples/RF00031_folded.stk";
125 String tfile = File.createTempFile("JalviewTest", ".jvp")
127 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
128 DataSourceType.FILE);
129 assertNotNull(af, "Didn't read input file " + inFile);
130 int olddsann = countDsAnn(af.getViewport());
131 assertTrue(olddsann > 0, "Didn't find any dataset annotations");
132 af.changeColour_actionPerformed(JalviewColourScheme.RNAHelices
135 af.getViewport().getGlobalColourScheme() instanceof RNAHelicesColour,
136 "Couldn't apply RNA helices colourscheme");
137 assertTrue(af.saveAlignment(tfile, FileFormat.Jalview),
138 "Failed to store as a project.");
139 af.closeMenuItem_actionPerformed(true);
141 af = new FileLoader()
142 .LoadFileWaitTillLoaded(tfile, DataSourceType.FILE);
143 assertNotNull(af, "Failed to import new project");
144 int newdsann = countDsAnn(af.getViewport());
145 assertEquals(olddsann, newdsann,
146 "Differing numbers of dataset sequence annotation\nOriginally "
147 + olddsann + " and now " + newdsann);
149 .println("Read in same number of annotations as originally present ("
153 af.getViewport().getGlobalColourScheme() instanceof RNAHelicesColour,
154 "RNA helices colourscheme was not applied on import.");
157 @Test(groups = { "Functional" })
158 public void testTCoffeeScores() throws Exception
160 String inFile = "examples/uniref50.fa", inAnnot = "examples/uniref50.score_ascii";
161 String tfile = File.createTempFile("JalviewTest", ".jvp")
163 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
164 DataSourceType.FILE);
165 assertNotNull(af, "Didn't read input file " + inFile);
166 af.loadJalviewDataFile(inAnnot, DataSourceType.FILE, null, null);
167 assertSame(af.getViewport().getGlobalColourScheme().getClass(),
168 TCoffeeColourScheme.class, "Didn't set T-coffee colourscheme");
169 assertNotNull(ColourSchemeProperty.getColourScheme(af.getViewport()
170 .getAlignment(), af.getViewport().getGlobalColourScheme()
171 .getSchemeName()), "Recognise T-Coffee score from string");
173 assertTrue(af.saveAlignment(tfile, FileFormat.Jalview),
174 "Failed to store as a project.");
175 af.closeMenuItem_actionPerformed(true);
177 af = new FileLoader()
178 .LoadFileWaitTillLoaded(tfile, DataSourceType.FILE);
179 assertNotNull(af, "Failed to import new project");
180 assertSame(af.getViewport().getGlobalColourScheme().getClass(),
181 TCoffeeColourScheme.class,
182 "Didn't set T-coffee colourscheme for imported project.");
184 .println("T-Coffee score shading successfully recovered from project.");
187 @Test(groups = { "Functional" })
188 public void testColourByAnnotScores() throws Exception
190 String inFile = "examples/uniref50.fa", inAnnot = "examples/testdata/uniref50_iupred.jva";
191 String tfile = File.createTempFile("JalviewTest", ".jvp")
193 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
194 DataSourceType.FILE);
195 assertNotNull(af, "Didn't read input file " + inFile);
196 af.loadJalviewDataFile(inAnnot, DataSourceType.FILE, null, null);
197 AlignmentAnnotation[] aa = af.getViewport().getAlignment()
198 .getSequenceAt(0).getAnnotation("IUPredWS (Short)");
201 aa != null && aa.length > 0,
202 "Didn't find any IUPred annotation to use to shade alignment.");
203 AnnotationColourGradient cs = new AnnotationColourGradient(aa[0], null,
204 AnnotationColourGradient.ABOVE_THRESHOLD);
205 AnnotationColourGradient gcs = new AnnotationColourGradient(aa[0],
206 null, AnnotationColourGradient.BELOW_THRESHOLD);
207 cs.setSeqAssociated(true);
208 gcs.setSeqAssociated(true);
210 SequenceGroup sg = new SequenceGroup();
213 sg.cs.setColourScheme(gcs);
214 af.getViewport().getAlignment().addGroup(sg);
215 sg.addSequence(af.getViewport().getAlignment().getSequenceAt(1), false);
216 sg.addSequence(af.getViewport().getAlignment().getSequenceAt(2), true);
217 af.alignPanel.alignmentChanged();
218 assertTrue(af.saveAlignment(tfile, FileFormat.Jalview),
219 "Failed to store as a project.");
220 af.closeMenuItem_actionPerformed(true);
222 af = new FileLoader()
223 .LoadFileWaitTillLoaded(tfile, DataSourceType.FILE);
224 assertNotNull(af, "Failed to import new project");
226 // check for group and alignment colourschemes
228 ColourSchemeI _rcs = af.getViewport().getGlobalColourScheme();
229 ColourSchemeI _rgcs = af.getViewport().getAlignment().getGroups()
230 .get(0).getColourScheme();
231 assertNotNull(_rcs, "Didn't recover global colourscheme");
232 assertTrue(_rcs instanceof AnnotationColourGradient,
233 "Didn't recover annotation colour global scheme");
234 AnnotationColourGradient __rcs = (AnnotationColourGradient) _rcs;
235 assertTrue(__rcs.isSeqAssociated(),
236 "Annotation colourscheme wasn't sequence associated");
238 boolean diffseqcols = false, diffgseqcols = false;
239 SequenceI[] sqs = af.getViewport().getAlignment().getSequencesArray();
240 for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
241 && (!diffseqcols || !diffgseqcols); p++)
243 if (_rcs.findColour(sqs[0].getCharAt(p), p, sqs[0], null, 0f) != _rcs
244 .findColour(sqs[5].getCharAt(p), p, sqs[5], null, 0f))
249 assertTrue(diffseqcols, "Got Different sequence colours");
251 .println("Per sequence colourscheme (Background) successfully applied and recovered.");
253 assertNotNull(_rgcs, "Didn't recover group colourscheme");
254 assertTrue(_rgcs instanceof AnnotationColourGradient,
255 "Didn't recover annotation colour group colourscheme");
256 __rcs = (AnnotationColourGradient) _rgcs;
257 assertTrue(__rcs.isSeqAssociated(),
258 "Group Annotation colourscheme wasn't sequence associated");
260 for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
261 && (!diffseqcols || !diffgseqcols); p++)
263 if (_rgcs.findColour(sqs[1].getCharAt(p), p, sqs[1], null, 0f) != _rgcs
264 .findColour(sqs[2].getCharAt(p), p, sqs[2], null, 0f))
269 assertTrue(diffgseqcols, "Got Different group sequence colours");
271 .println("Per sequence (Group) colourscheme successfully applied and recovered.");
274 @Test(groups = { "Functional" })
275 public void gatherViewsHere() throws Exception
277 int origCount = Desktop.getAlignFrames() == null ? 0 : Desktop
278 .getAlignFrames().length;
279 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
280 "examples/exampleFile_2_7.jar", DataSourceType.FILE);
281 assertNotNull(af, "Didn't read in the example file correctly.");
282 assertTrue(Desktop.getAlignFrames().length == 1 + origCount,
283 "Didn't gather the views in the example file.");
288 * Test for JAL-2223 - multiple mappings in View Mapping report
292 @Test(groups = { "Functional" })
293 public void noDuplicatePdbMappingsMade() throws Exception
295 StructureImportSettings.setProcessSecondaryStructure(true);
296 StructureImportSettings.setVisibleChainAnnotation(true);
297 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
298 "examples/exampleFile_2_7.jar", DataSourceType.FILE);
299 assertNotNull(af, "Didn't read in the example file correctly.");
301 // locate Jmol viewer
302 // count number of PDB mappings the structure selection manager holds -
303 String pdbFile = af.getCurrentView().getStructureSelectionManager()
304 .findFileForPDBId("1A70");
306 af.getCurrentView().getStructureSelectionManager()
307 .getMapping(pdbFile).length,
308 2, "Expected only two mappings for 1A70");
312 @Test(groups = { "Functional" })
313 public void viewRefPdbAnnotation() throws Exception
315 StructureImportSettings.setProcessSecondaryStructure(true);
316 StructureImportSettings.setVisibleChainAnnotation(true);
317 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
318 "examples/exampleFile_2_7.jar", DataSourceType.FILE);
319 assertNotNull(af, "Didn't read in the example file correctly.");
320 AlignmentViewPanel sps = null;
321 for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
323 if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
329 assertNotNull(sps, "Couldn't find the structure view");
330 AlignmentAnnotation refan = null;
331 for (AlignmentAnnotation ra : sps.getAlignment()
332 .getAlignmentAnnotation())
340 assertNotNull(refan, "Annotation secondary structure not found.");
341 SequenceI sq = sps.getAlignment().findName("1A70|");
342 assertNotNull(sq, "Couldn't find 1a70 null chain");
343 // compare the manually added temperature factor annotation
344 // to the track automatically transferred from the pdb structure on load
345 assertNotNull(sq.getDatasetSequence().getAnnotation(),
346 "1a70 has no annotation");
347 for (AlignmentAnnotation ala : sq.getDatasetSequence().getAnnotation())
349 AlignmentAnnotation alaa;
350 sq.addAlignmentAnnotation(alaa = new AlignmentAnnotation(ala));
351 alaa.adjustForAlignment();
352 if (ala.graph == refan.graph)
354 for (int p = 0; p < ala.annotations.length; p++)
360 (alaa.annotations[p] == null && refan.annotations[p] == null)
361 || alaa.annotations[p].value == refan.annotations[p].value,
362 "Mismatch at alignment position " + p);
363 } catch (NullPointerException q)
365 Assert.fail("Mismatch of alignment annotations at position "
366 + p + " Ref seq ann: " + refan.annotations[p]
367 + " alignment " + alaa.annotations[p]);
375 @Test(groups = { "Functional" })
376 public void testCopyViewSettings() throws Exception
378 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
379 "examples/exampleFile_2_7.jar", DataSourceType.FILE);
380 assertNotNull(af, "Didn't read in the example file correctly.");
381 AlignmentViewPanel sps = null, groups = null;
382 for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
384 if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
388 if (ap.getViewName().contains("MAFFT"))
393 assertNotNull(sps, "Couldn't find the structure view");
394 assertNotNull(groups, "Couldn't find the MAFFT view");
396 ViewStyleI structureStyle = sps.getAlignViewport().getViewStyle();
397 ViewStyleI groupStyle = groups.getAlignViewport().getViewStyle();
398 AssertJUnit.assertFalse(structureStyle.sameStyle(groupStyle));
400 groups.getAlignViewport().setViewStyle(structureStyle);
401 AssertJUnit.assertFalse(groupStyle.sameStyle(groups.getAlignViewport()
403 Assert.assertTrue(structureStyle.sameStyle(groups.getAlignViewport()
409 * test store and recovery of expanded views
413 @Test(groups = { "Functional" }, enabled = true)
414 public void testStoreAndRecoverExpandedviews() throws Exception
416 Desktop.instance.closeAll_actionPerformed(null);
418 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
419 "examples/exampleFile_2_7.jar", DataSourceType.FILE);
420 Assert.assertEquals(Desktop.getAlignFrames().length, 1);
421 String afid = af.getViewport().getSequenceSetId();
423 // check FileLoader returned a reference to the one alignFrame that is
424 // actually on the Desktop
427 Desktop.getAlignFrameFor(af.getViewport()),
428 "Jalview2XML.loadAlignFrame() didn't return correct AlignFrame reference for multiple view window");
430 Desktop.explodeViews(af);
432 int oldviews = Desktop.getAlignFrames().length;
433 Assert.assertEquals(Desktop.getAlignFrames().length,
434 Desktop.getAlignmentPanels(afid).length);
435 File tfile = File.createTempFile("testStoreAndRecoverExpanded", ".jvp");
438 new Jalview2XML(false).saveState(tfile);
441 Assert.fail("Didn't save the expanded view state", e);
442 } catch (Exception e)
444 Assert.fail("Didn't save the expanded view state", e);
446 Desktop.instance.closeAll_actionPerformed(null);
447 if (Desktop.getAlignFrames() != null)
449 Assert.assertEquals(Desktop.getAlignFrames().length, 0);
451 af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
452 DataSourceType.FILE);
453 Assert.assertNotNull(af);
455 Desktop.getAlignFrames().length,
456 Desktop.getAlignmentPanels(af.getViewport().getSequenceSetId()).length);
458 Desktop.getAlignmentPanels(af.getViewport().getSequenceSetId()).length,
463 * Test save and reload of a project with a different representative sequence
468 @Test(groups = { "Functional" })
469 public void testStoreAndRecoverReferenceSeqSettings() throws Exception
471 Desktop.instance.closeAll_actionPerformed(null);
472 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
473 "examples/exampleFile_2_7.jar", DataSourceType.FILE);
474 assertNotNull(af, "Didn't read in the example file correctly.");
475 String afid = af.getViewport().getSequenceSetId();
477 // remember reference sequence for each panel
478 Map<String, SequenceI> refseqs = new HashMap<>();
481 * mark sequence 2, 3, 4.. in panels 1, 2, 3...
482 * as reference sequence for itself and the preceding sequence
485 for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
487 AlignViewportI av = ap.getAlignViewport();
488 AlignmentI alignment = ap.getAlignment();
489 int repIndex = n % alignment.getHeight();
490 SequenceI rep = alignment.getSequenceAt(repIndex);
491 refseqs.put(ap.getViewName(), rep);
493 // code from mark/unmark sequence as reference in jalview.gui.PopupMenu
494 // todo refactor this to an alignment view controller
495 av.setDisplayReferenceSeq(true);
496 av.setColourByReferenceSeq(true);
497 av.getAlignment().setSeqrep(rep);
501 File tfile = File.createTempFile("testStoreAndRecoverReferenceSeq",
505 new Jalview2XML(false).saveState(tfile);
506 } catch (Throwable e)
508 Assert.fail("Didn't save the expanded view state", e);
510 Desktop.instance.closeAll_actionPerformed(null);
511 if (Desktop.getAlignFrames() != null)
513 Assert.assertEquals(Desktop.getAlignFrames().length, 0);
516 af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
517 DataSourceType.FILE);
518 afid = af.getViewport().getSequenceSetId();
520 for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
522 // check representative
523 AlignmentI alignment = ap.getAlignment();
524 SequenceI rep = alignment.getSeqrep();
525 Assert.assertNotNull(rep,
526 "Couldn't restore sequence representative from project");
527 // can't use a strong equals here, because by definition, the sequence IDs
528 // will be different.
529 // could set vamsas session save/restore flag to preserve IDs across
531 Assert.assertEquals(refseqs.get(ap.getViewName()).toString(),
533 "Representative wasn't the same when recovered.");
534 Assert.assertTrue(ap.getAlignViewport().isDisplayReferenceSeq(),
535 "Display reference sequence view setting not set.");
536 Assert.assertTrue(ap.getAlignViewport().isColourByReferenceSeq(),
537 "Colour By Reference Seq view setting not set.");
541 @Test(groups = { "Functional" })
542 public void testIsVersionStringLaterThan()
545 * No version / development / test / autobuild is leniently assumed to be
548 assertTrue(Jalview2XML.isVersionStringLaterThan(null, null));
549 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", null));
550 assertTrue(Jalview2XML.isVersionStringLaterThan(null, "2.8.3"));
551 assertTrue(Jalview2XML.isVersionStringLaterThan(null,
552 "Development Build"));
553 assertTrue(Jalview2XML.isVersionStringLaterThan(null,
554 "DEVELOPMENT BUILD"));
555 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3",
556 "Development Build"));
557 assertTrue(Jalview2XML.isVersionStringLaterThan(null, "Test"));
558 assertTrue(Jalview2XML.isVersionStringLaterThan(null, "TEST"));
559 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "Test"));
560 assertTrue(Jalview2XML
561 .isVersionStringLaterThan(null, "Automated Build"));
562 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3",
564 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3",
568 * same version returns true i.e. compatible
570 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8", "2.8"));
571 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.3"));
572 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3b1", "2.8.3b1"));
573 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3B1", "2.8.3b1"));
574 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3b1", "2.8.3B1"));
577 * later version returns true
579 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.4"));
580 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.9"));
581 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.9.2"));
582 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8", "2.8.3"));
583 assertTrue(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.3b1"));
586 * earlier version returns false
588 assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8"));
589 assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.4", "2.8.3"));
590 assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.3b1", "2.8.3"));
591 assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.3", "2.8.2b1"));
592 assertFalse(Jalview2XML.isVersionStringLaterThan("2.8.0b2", "2.8.0b1"));
596 * Test save and reload of a project with a different sequence group (and
597 * representative sequence) in each view.
601 @Test(groups = { "Functional" })
602 public void testStoreAndRecoverGroupRepSeqs() throws Exception
604 Desktop.instance.closeAll_actionPerformed(null);
605 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
606 "examples/uniref50.fa", DataSourceType.FILE);
607 assertNotNull(af, "Didn't read in the example file correctly.");
608 String afid = af.getViewport().getSequenceSetId();
609 // make a second view of the alignment
610 af.newView_actionPerformed(null);
613 * remember representative and hidden sequences marked
616 Map<String, SequenceI> repSeqs = new HashMap<>();
617 Map<String, List<String>> hiddenSeqNames = new HashMap<>();
620 * mark sequence 2, 3, 4.. in panels 1, 2, 3...
621 * as reference sequence for itself and the preceding sequence
624 for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
626 AlignViewportI av = ap.getAlignViewport();
627 AlignmentI alignment = ap.getAlignment();
628 int repIndex = n % alignment.getHeight();
629 // ensure at least one preceding sequence i.e. index >= 1
630 repIndex = Math.max(repIndex, 1);
631 SequenceI repSeq = alignment.getSequenceAt(repIndex);
632 repSeqs.put(ap.getViewName(), repSeq);
633 List<String> hiddenNames = new ArrayList<>();
634 hiddenSeqNames.put(ap.getViewName(), hiddenNames);
637 * have rep sequence represent itself and the one before it
638 * this hides the group (except for the rep seq)
640 SequenceGroup sg = new SequenceGroup();
641 sg.addSequence(repSeq, false);
642 SequenceI precedingSeq = alignment.getSequenceAt(repIndex - 1);
643 sg.addSequence(precedingSeq, false);
644 sg.setSeqrep(repSeq);
645 assertTrue(sg.getSequences().contains(repSeq));
646 assertTrue(sg.getSequences().contains(precedingSeq));
647 av.setSelectionGroup(sg);
648 assertSame(repSeq, sg.getSeqrep());
651 * represent group with sequence adds to a map of hidden rep sequences
652 * (it does not create a group on the alignment)
654 ((AlignmentViewport) av).hideSequences(repSeq, true);
655 assertSame(repSeq, sg.getSeqrep());
656 assertTrue(sg.getSequences().contains(repSeq));
657 assertTrue(sg.getSequences().contains(precedingSeq));
658 assertTrue(alignment.getGroups().isEmpty(), "alignment has groups");
659 Map<SequenceI, SequenceCollectionI> hiddenRepSeqsMap = av
660 .getHiddenRepSequences();
661 assertNotNull(hiddenRepSeqsMap);
662 assertEquals(1, hiddenRepSeqsMap.size());
663 assertSame(sg, hiddenRepSeqsMap.get(repSeq));
664 assertTrue(alignment.getHiddenSequences().isHidden(precedingSeq));
665 assertFalse(alignment.getHiddenSequences().isHidden(repSeq));
666 hiddenNames.add(precedingSeq.getName());
671 .createTempFile("testStoreAndRecoverGroupReps", ".jvp");
674 new Jalview2XML(false).saveState(tfile);
675 } catch (Throwable e)
677 Assert.fail("Didn't save the expanded view state", e);
679 Desktop.instance.closeAll_actionPerformed(null);
680 if (Desktop.getAlignFrames() != null)
682 Assert.assertEquals(Desktop.getAlignFrames().length, 0);
685 af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
686 DataSourceType.FILE);
687 afid = af.getViewport().getSequenceSetId();
689 for (AlignmentViewPanel ap : Desktop.getAlignmentPanels(afid))
691 String viewName = ap.getViewName();
692 AlignViewportI av = ap.getAlignViewport();
693 AlignmentI alignment = ap.getAlignment();
694 List<SequenceGroup> groups = alignment.getGroups();
695 assertNotNull(groups);
696 assertTrue(groups.isEmpty(), "Alignment has groups");
697 Map<SequenceI, SequenceCollectionI> hiddenRepSeqsMap = av
698 .getHiddenRepSequences();
699 assertNotNull(hiddenRepSeqsMap, "No hidden represented sequences");
700 assertEquals(1, hiddenRepSeqsMap.size());
701 assertEquals(repSeqs.get(viewName).getDisplayId(true),
702 hiddenRepSeqsMap.keySet().iterator().next()
703 .getDisplayId(true));
706 * verify hidden sequences in restored panel
708 List<String> hidden = hiddenSeqNames.get(ap.getViewName());
709 HiddenSequences hs = alignment.getHiddenSequences();
713 "wrong number of restored hidden sequences in "
719 * Test save and reload of PDBEntry in Jalview project
723 @Test(groups = { "Functional" })
724 public void testStoreAndRecoverPDBEntry() throws Exception
726 Desktop.instance.closeAll_actionPerformed(null);
727 String exampleFile = "examples/3W5V.pdb";
728 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(exampleFile,
729 DataSourceType.FILE);
730 assertNotNull(af, "Didn't read in the example file correctly.");
731 String afid = af.getViewport().getSequenceSetId();
733 AlignmentPanel[] alignPanels = Desktop.getAlignmentPanels(afid);
734 System.out.println();
735 AlignmentViewPanel ap = alignPanels[0];
736 String tfileBase = new File(".").getAbsolutePath().replace(".", "");
737 String testFile = tfileBase + exampleFile;
738 AlignmentI alignment = ap.getAlignment();
739 System.out.println("blah");
740 SequenceI[] seqs = alignment.getSequencesArray();
741 Assert.assertNotNull(seqs[0]);
742 Assert.assertNotNull(seqs[1]);
743 Assert.assertNotNull(seqs[2]);
744 Assert.assertNotNull(seqs[3]);
745 Assert.assertNotNull(seqs[0].getDatasetSequence());
746 Assert.assertNotNull(seqs[1].getDatasetSequence());
747 Assert.assertNotNull(seqs[2].getDatasetSequence());
748 Assert.assertNotNull(seqs[3].getDatasetSequence());
749 PDBEntry[] pdbEntries = new PDBEntry[4];
750 pdbEntries[0] = new PDBEntry("3W5V", "A", Type.PDB, testFile);
751 pdbEntries[1] = new PDBEntry("3W5V", "B", Type.PDB, testFile);
752 pdbEntries[2] = new PDBEntry("3W5V", "C", Type.PDB, testFile);
753 pdbEntries[3] = new PDBEntry("3W5V", "D", Type.PDB, testFile);
754 Assert.assertEquals(seqs[0].getDatasetSequence().getAllPDBEntries()
755 .get(0), pdbEntries[0]);
756 Assert.assertEquals(seqs[1].getDatasetSequence().getAllPDBEntries()
757 .get(0), pdbEntries[1]);
758 Assert.assertEquals(seqs[2].getDatasetSequence().getAllPDBEntries()
759 .get(0), pdbEntries[2]);
760 Assert.assertEquals(seqs[3].getDatasetSequence().getAllPDBEntries()
761 .get(0), pdbEntries[3]);
763 File tfile = File.createTempFile("testStoreAndRecoverPDBEntry", ".jvp");
766 new Jalview2XML(false).saveState(tfile);
767 } catch (Throwable e)
769 Assert.fail("Didn't save the state", e);
771 Desktop.instance.closeAll_actionPerformed(null);
772 if (Desktop.getAlignFrames() != null)
774 Assert.assertEquals(Desktop.getAlignFrames().length, 0);
777 AlignFrame restoredFrame = new FileLoader().LoadFileWaitTillLoaded(
778 tfile.getAbsolutePath(), DataSourceType.FILE);
779 String rfid = restoredFrame.getViewport().getSequenceSetId();
780 AlignmentPanel[] rAlignPanels = Desktop.getAlignmentPanels(rfid);
781 AlignmentViewPanel rap = rAlignPanels[0];
782 AlignmentI rAlignment = rap.getAlignment();
783 System.out.println("blah");
784 SequenceI[] rseqs = rAlignment.getSequencesArray();
785 Assert.assertNotNull(rseqs[0]);
786 Assert.assertNotNull(rseqs[1]);
787 Assert.assertNotNull(rseqs[2]);
788 Assert.assertNotNull(rseqs[3]);
789 Assert.assertNotNull(rseqs[0].getDatasetSequence());
790 Assert.assertNotNull(rseqs[1].getDatasetSequence());
791 Assert.assertNotNull(rseqs[2].getDatasetSequence());
792 Assert.assertNotNull(rseqs[3].getDatasetSequence());
794 // The Asserts below are expected to fail until the PDB chainCode is
795 // recoverable from a Jalview projects
796 for (int chain = 0; chain < 4; chain++)
798 PDBEntry recov = rseqs[chain].getDatasetSequence().getAllPDBEntries()
800 PDBEntry expected = pdbEntries[chain];
801 Assert.assertEquals(recov.getId(), expected.getId(),
803 Assert.assertEquals(recov.getChainCode(), expected.getChainCode(),
805 Assert.assertEquals(recov.getType(), expected.getType(),
806 "Mismatch PDBEntry 'Type'");
807 Assert.assertNotNull(recov.getFile(),
808 "Recovered PDBEntry should have a non-null file entry");
813 * Configure an alignment and a sub-group each with distinct colour schemes,
814 * Conservation and PID thresholds, and confirm these are restored from the
817 * @throws IOException
819 @Test(groups = { "Functional" })
820 public void testStoreAndRecoverColourThresholds() throws IOException
822 Desktop.instance.closeAll_actionPerformed(null);
823 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
824 "examples/uniref50.fa", DataSourceType.FILE);
826 AlignViewport av = af.getViewport();
827 AlignmentI al = av.getAlignment();
830 * Colour alignment by Buried Index, Above 10% PID, By Conservation 20%
832 af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
833 assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
834 af.abovePIDThreshold_actionPerformed(true);
835 SliderPanel sp = SliderPanel.getSliderPanel();
836 assertFalse(sp.isForConservation());
838 af.conservationMenuItem_actionPerformed(true);
839 sp = SliderPanel.getSliderPanel();
840 assertTrue(sp.isForConservation());
842 ResidueShaderI rs = av.getResidueShading();
843 assertEquals(rs.getThreshold(), 10);
844 assertTrue(rs.conservationApplied());
845 assertEquals(rs.getConservationInc(), 20);
848 * create a group with Strand colouring, 30% Conservation
849 * and 40% PID threshold
851 SequenceGroup sg = new SequenceGroup();
852 sg.addSequence(al.getSequenceAt(0), false);
855 av.setSelectionGroup(sg);
856 PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
857 popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
859 assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
860 assertEquals(al.getGroups().size(), 1);
861 assertSame(al.getGroups().get(0), sg);
862 popupMenu.conservationMenuItem_actionPerformed(true);
863 sp = SliderPanel.getSliderPanel();
864 assertTrue(sp.isForConservation());
866 popupMenu.abovePIDColour_actionPerformed(true);
867 sp = SliderPanel.getSliderPanel();
868 assertFalse(sp.isForConservation());
870 assertTrue(sg.getGroupColourScheme().conservationApplied());
871 assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
872 assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
875 * save project, close windows, reload project, verify
877 File tfile = File.createTempFile("testStoreAndRecoverColourThresholds",
879 tfile.deleteOnExit();
880 new Jalview2XML(false).saveState(tfile);
881 Desktop.instance.closeAll_actionPerformed(null);
882 af = new FileLoader().LoadFileWaitTillLoaded(tfile.getAbsolutePath(),
883 DataSourceType.FILE);
884 Assert.assertNotNull(af, "Failed to reload project");
887 * verify alignment (background) colouring
889 rs = af.getViewport().getResidueShading();
890 assertTrue(rs.getColourScheme() instanceof BuriedColourScheme);
891 assertEquals(rs.getThreshold(), 10);
892 assertTrue(rs.conservationApplied());
893 assertEquals(rs.getConservationInc(), 20);
896 * verify group colouring
898 assertEquals(1, af.getViewport().getAlignment().getGroups().size(), 1);
899 rs = af.getViewport().getAlignment().getGroups().get(0)
900 .getGroupColourScheme();
901 assertTrue(rs.getColourScheme() instanceof StrandColourScheme);
902 assertEquals(rs.getThreshold(), 40);
903 assertTrue(rs.conservationApplied());
904 assertEquals(rs.getConservationInc(), 30);
908 * Test save and reload of feature colour schemes and filter settings
910 * @throws IOException
912 @Test(groups = { "Functional" })
913 public void testSaveLoadFeatureColoursAndFilters() throws IOException
915 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
916 ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
917 SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
920 * add some features to the sequence
923 addFeatures(seq1, "type1", score++);
924 addFeatures(seq1, "type2", score++);
925 addFeatures(seq1, "type3", score++);
926 addFeatures(seq1, "type4", score++);
927 addFeatures(seq1, "type5", score++);
930 * set colour schemes for features
932 FeatureRendererModel fr = af.getFeatureRenderer();
933 fr.findAllFeatures(true);
936 fr.setColour("type1", new FeatureColour(Color.red));
939 FeatureColourI byLabel = new FeatureColour();
940 byLabel.setColourByLabel(true);
941 fr.setColour("type2", byLabel);
943 // type3: by score above threshold
944 FeatureColourI byScore = new FeatureColour(Color.BLACK, Color.BLUE, 1,
946 byScore.setAboveThreshold(true);
947 byScore.setThreshold(2f);
948 fr.setColour("type3", byScore);
950 // type4: by attribute AF
951 FeatureColourI byAF = new FeatureColour();
952 byAF.setColourByLabel(true);
953 byAF.setAttributeName("AF");
954 fr.setColour("type4", byAF);
956 // type5: by attribute CSQ:PolyPhen below threshold
957 FeatureColourI byPolyPhen = new FeatureColour(Color.BLACK, Color.BLUE,
959 byPolyPhen.setBelowThreshold(true);
960 byPolyPhen.setThreshold(3f);
961 byPolyPhen.setAttributeName("CSQ", "PolyPhen");
962 fr.setColour("type5", byPolyPhen);
965 * set filters for feature types
968 // filter type1 features by (label contains "x")
969 FeatureMatcherSetI filterByX = new FeatureMatcherSet();
970 filterByX.and(FeatureMatcher.byLabel(Condition.Contains, "x"));
971 fr.setFeatureFilter("type1", filterByX);
973 // filter type2 features by (score <= 2.4 and score > 1.1)
974 FeatureMatcherSetI filterByScore = new FeatureMatcherSet();
975 filterByScore.and(FeatureMatcher.byScore(Condition.LE, "2.4"));
976 filterByScore.and(FeatureMatcher.byScore(Condition.GT, "1.1"));
977 fr.setFeatureFilter("type2", filterByScore);
979 // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
980 FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
982 .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
983 filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
985 fr.setFeatureFilter("type3", filterByXY);
988 * save as Jalview project
990 File tfile = File.createTempFile("JalviewTest", ".jvp");
991 tfile.deleteOnExit();
992 String filePath = tfile.getAbsolutePath();
993 assertTrue(af.saveAlignment(filePath, FileFormat.Jalview),
994 "Failed to store as a project.");
997 * close current alignment and load the saved project
999 af.closeMenuItem_actionPerformed(true);
1001 af = new FileLoader()
1002 .LoadFileWaitTillLoaded(filePath, DataSourceType.FILE);
1003 assertNotNull(af, "Failed to import new project");
1006 * verify restored feature colour schemes and filters
1008 fr = af.getFeatureRenderer();
1009 FeatureColourI fc = fr.getFeatureStyle("type1");
1010 assertTrue(fc.isSimpleColour());
1011 assertEquals(fc.getColour(), Color.red);
1012 fc = fr.getFeatureStyle("type2");
1013 assertTrue(fc.isColourByLabel());
1014 fc = fr.getFeatureStyle("type3");
1015 assertTrue(fc.isGraduatedColour());
1016 assertNull(fc.getAttributeName());
1017 assertTrue(fc.isAboveThreshold());
1018 assertEquals(fc.getThreshold(), 2f);
1019 fc = fr.getFeatureStyle("type4");
1020 assertTrue(fc.isColourByLabel());
1021 assertTrue(fc.isColourByAttribute());
1022 assertEquals(fc.getAttributeName(), new String[] { "AF" });
1023 fc = fr.getFeatureStyle("type5");
1024 assertTrue(fc.isGraduatedColour());
1025 assertTrue(fc.isColourByAttribute());
1026 assertEquals(fc.getAttributeName(), new String[] { "CSQ", "PolyPhen" });
1027 assertTrue(fc.isBelowThreshold());
1028 assertEquals(fc.getThreshold(), 3f);
1030 assertEquals(fr.getFeatureFilter("type1").toStableString(),
1031 "Label Contains x");
1032 assertEquals(fr.getFeatureFilter("type2").toStableString(),
1033 "(Score LE 2.4) AND (Score GT 1.1)");
1034 assertEquals(fr.getFeatureFilter("type3").toStableString(),
1035 "(AF Contains X) OR (CSQ:PolyPhen NE 0.0)");
1038 private void addFeature(SequenceI seq, String featureType, int score)
1040 SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
1042 sf.setValue("AF", score);
1043 sf.setValue("CSQ", new HashMap<String, String>()
1046 put("PolyPhen", Integer.toString(score));
1049 seq.addSequenceFeature(sf);
1053 * Adds two features of the given type to the given sequence, also setting the
1054 * score as the value of attribute "AF" and sub-attribute "CSQ:PolyPhen"
1057 * @param featureType
1060 private void addFeatures(SequenceI seq, String featureType, int score)
1062 addFeature(seq, featureType, score++);
1063 addFeature(seq, featureType, score);
1067 * Test save and reload of a PCA viewer
1069 * @throws IOException
1071 @Test(groups = { "Functional" })
1072 public void testSaveLoadPCA() throws IOException
1074 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
1075 "examples/uniref50.fa", DataSourceType.FILE);
1076 assertEquals(af.getViewport().getAlignment().getHeight(), 15);
1079 * calculate and open PCA (calculates in a separate thread)
1081 CalculationChooser chooser = new CalculationChooser(af);
1082 ((JRadioButton) PA.getValue(chooser, "pca")).setSelected(true);
1083 PA.invokeMethod(chooser, "calculate_actionPerformed()");
1084 PCAPanel pcaPanel = (PCAPanel) PA.getValue(chooser, "pcaPanel");
1085 assertNotNull(pcaPanel);
1086 waitFor(50); // let it get started!!
1087 while (pcaPanel.isWorking())
1091 PA.invokeMethod(chooser, "close_actionPerformed()");
1094 * rotate, zoom in, change background colour
1096 RotatableCanvas rc = (RotatableCanvas) PA.getValue(pcaPanel, "rc");
1097 PA.setValue(rc, "bgColour", Color.PINK);
1102 * save as Jalview project
1104 File tfile = File.createTempFile("JalviewTest", ".jvp");
1105 tfile.deleteOnExit();
1106 String filePath = tfile.getAbsolutePath();
1107 assertTrue(af.saveAlignment(filePath, FileFormat.Jalview),
1108 "Failed to store as a project.");
1111 * load the saved project and locate the restored PCA panel
1113 new FileLoader().LoadFileWaitTillLoaded(filePath, DataSourceType.FILE);
1114 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1115 PCAPanel pcaPanel2 = null;
1116 for (JInternalFrame frame : frames)
1118 if (frame instanceof PCAPanel && frame != pcaPanel)
1120 pcaPanel2 = (PCAPanel) frame;
1123 assertNotNull(pcaPanel2);
1126 * compare restored and original PCA
1128 PCAModel pcaModel = (PCAModel) PA.getValue(pcaPanel, "pcaModel");
1129 PCAModel pcaModel2 = (PCAModel) PA.getValue(pcaPanel2, "pcaModel");
1130 RotatableCanvas rc2 = (RotatableCanvas) PA.getValue(pcaPanel2, "rc");
1131 assertNotNull(pcaModel);
1132 assertNotNull(pcaModel2);
1134 assertEquals(rc2.getBackgroundColour(), Color.PINK);
1135 assertEquals(PA.getValue(rc2, "scaleFactor"), 1.9f);
1137 // original has input data
1138 assertNotNull(pcaModel.getInputData());
1139 // restored has no input data (JAL-2647 to do)
1140 assertNull(pcaModel2.getInputData());
1142 // verify sequence points are at the same positions
1143 List<SequencePoint> seqPts = pcaModel.getSequencePoints();
1144 List<SequencePoint> seqPts2 = pcaModel2.getSequencePoints();
1145 assertEquals(seqPts.size(), seqPts2.size());
1146 for (int i = 0; i < seqPts.size(); i++)
1148 SequencePoint sp = seqPts.get(i);
1149 SequencePoint sp2 = seqPts2.get(i);
1150 assertEquals(sp.getSequence().getName(), sp2.getSequence().getName());
1151 assertEquals(sp.coord, sp2.coord);
1154 // verify axis end points are at the same positions
1155 Point[] axes = (Point[]) PA.getValue(rc, "axisEndPoints");
1156 Point[] axes2 = (Point[]) PA.getValue(rc2, "axisEndPoints");
1157 assertEquals(axes.length, 3);
1158 assertEquals(axes2.length, 3);
1159 for (int i = 0; i < 3; i++)
1161 assertEquals(axes[i], axes2[i]);
1165 PCA pca = (PCA) PA.getValue(pcaModel, "pca");
1166 PCA pca2 = (PCA) PA.getValue(pcaModel2, "pca");
1168 assertNotNull(pca2);
1169 // same (BLOSUM62) score model (a singleton object)
1170 assertSame(PA.getValue(pca, "scoreModel"),
1171 PA.getValue(pca2, "scoreModel"));
1172 assertEquals(PA.getValue(pca, "similarityParams"),
1173 PA.getValue(pca2, "similarityParams"));
1174 MatrixTest.assertMatricesMatch(pca.getPairwiseScores(),
1175 pca2.getPairwiseScores());
1176 MatrixTest.assertMatricesMatch(pca.getTridiagonal(),
1177 pca2.getTridiagonal());
1178 MatrixTest.assertMatricesMatch(pca.getEigenmatrix(),
1179 pca2.getEigenmatrix());
1182 protected void waitFor(long t)
1189 } catch (InterruptedException e)
1191 fail(e.getMessage());