From 9fe1833d6a0e8275886edf3c7a43c73346dbab45 Mon Sep 17 00:00:00 2001 From: kjvdheide Date: Mon, 9 Oct 2017 12:38:30 +0100 Subject: [PATCH] Fully separated tree GUI from calculation --- src/jalview/analysis/TreeBuilder.java | 4 +- src/jalview/analysis/TreeCalculator.java | 57 ++++++++- src/jalview/analysis/TreeModel.java | 7 +- .../AlignConvertForArchaeopteryx.java | 6 - .../AlignToArchaeopteryxConverter.java | 6 + ...ewerI.java => AlignToTreeViewerConverterI.java} | 2 +- .../archaeopteryx/ArchaeopteryxTreeBuilder.java | 4 +- src/jalview/gui/AlignFrame.java | 22 ++-- src/jalview/gui/CalculationChooser.java | 21 ++-- src/jalview/gui/TreePanel.java | 122 +++++++++++--------- 10 files changed, 165 insertions(+), 86 deletions(-) delete mode 100644 src/jalview/ext/archaeopteryx/AlignConvertForArchaeopteryx.java create mode 100644 src/jalview/ext/archaeopteryx/AlignToArchaeopteryxConverter.java rename src/jalview/ext/archaeopteryx/{AlignmentToTreeViewerI.java => AlignToTreeViewerConverterI.java} (96%) diff --git a/src/jalview/analysis/TreeBuilder.java b/src/jalview/analysis/TreeBuilder.java index 0601dd9..414ca39 100644 --- a/src/jalview/analysis/TreeBuilder.java +++ b/src/jalview/analysis/TreeBuilder.java @@ -364,7 +364,7 @@ public abstract class TreeBuilder protected void init(AlignmentView seqView, int start, int end) { - this.node = new Vector(); + this.node = new Vector<>(); if (seqView != null) { this.seqData = seqView; @@ -458,7 +458,7 @@ public abstract class TreeBuilder */ void makeLeaves() { - clusters = new Vector(); + clusters = new Vector<>(); for (int i = 0; i < noseqs; i++) { diff --git a/src/jalview/analysis/TreeCalculator.java b/src/jalview/analysis/TreeCalculator.java index 7ccee92..49c66bf 100644 --- a/src/jalview/analysis/TreeCalculator.java +++ b/src/jalview/analysis/TreeCalculator.java @@ -20,15 +20,22 @@ */ package jalview.analysis; +import jalview.analysis.scoremodels.ScoreModels; +import jalview.api.analysis.ScoreModelI; import jalview.api.analysis.SimilarityParamsI; +import jalview.gui.AlignViewport; /** * @author kjvanderheide * */ -public class TreeCalculator +public class TreeCalculator extends Thread// add threading { + SimilarityParamsI similarityParams; + String treeType; + + String scoreModelName; // if tree computed /** * * @param treeAlgo @@ -38,9 +45,55 @@ public class TreeCalculator public TreeCalculator(String treeAlgo, String substitutionMatrix, SimilarityParamsI calculateParams) { - // TODO Auto-generated constructor stub + this.treeType = treeAlgo; + this.scoreModelName = substitutionMatrix; + this.similarityParams = calculateParams; + } + + public TreeBuilder makeTree(AlignViewport alignViewport) + { + ScoreModelI sm = ScoreModels.getInstance().getScoreModel(scoreModelName, + alignViewport.getAlignPanel()); + + TreeBuilder builtTree = treeType.equals(TreeBuilder.NEIGHBOUR_JOINING) + ? new NJTree(alignViewport, sm, similarityParams) + : new AverageDistanceTree(alignViewport, sm, similarityParams); + + return builtTree; + + } + + public SimilarityParamsI getSimilarityParams() + { + return similarityParams; + } + + public void setSimilarityParams(SimilarityParamsI similarityParams) + { + this.similarityParams = similarityParams; } + public String getTreeType() + { + return treeType; + } + + public void setTreeType(String treeType) + { + this.treeType = treeType; + } + + public String getScoreModelName() + { + return scoreModelName; + } + + public void setScoreModelName(String scoreModelName) + { + this.scoreModelName = scoreModelName; + } + + } diff --git a/src/jalview/analysis/TreeModel.java b/src/jalview/analysis/TreeModel.java index 6ad181d..b6c9caa 100644 --- a/src/jalview/analysis/TreeModel.java +++ b/src/jalview/analysis/TreeModel.java @@ -97,6 +97,7 @@ public class TreeModel this(tree.getSequences(), tree.getTopNode(), tree.hasDistances(), tree.hasBootstrap(), tree.hasRootDistance()); seqData = tree.getOriginalData(); + } /** @@ -136,7 +137,7 @@ public class TreeModel SequenceNode j; SequenceI nam; String realnam; - Vector one2many = new Vector(); + Vector one2many = new Vector<>(); // int countOne2Many = 0; while (i < leaves.size()) { @@ -295,7 +296,7 @@ public class TreeModel */ public Vector findLeaves(SequenceNode nd) { - Vector leaves = new Vector(); + Vector leaves = new Vector<>(); findLeaves(nd, leaves); return leaves; } @@ -389,7 +390,7 @@ public class TreeModel */ public List groupNodes(float threshold) { - List groups = new ArrayList(); + List groups = new ArrayList<>(); _groupNodes(groups, getTopNode(), threshold); return groups; } diff --git a/src/jalview/ext/archaeopteryx/AlignConvertForArchaeopteryx.java b/src/jalview/ext/archaeopteryx/AlignConvertForArchaeopteryx.java deleted file mode 100644 index 22aec97..0000000 --- a/src/jalview/ext/archaeopteryx/AlignConvertForArchaeopteryx.java +++ /dev/null @@ -1,6 +0,0 @@ -package jalview.ext.archaeopteryx; - -public class AlignConvertForArchaeopteryx -{ - -} diff --git a/src/jalview/ext/archaeopteryx/AlignToArchaeopteryxConverter.java b/src/jalview/ext/archaeopteryx/AlignToArchaeopteryxConverter.java new file mode 100644 index 0000000..b90cf4b --- /dev/null +++ b/src/jalview/ext/archaeopteryx/AlignToArchaeopteryxConverter.java @@ -0,0 +1,6 @@ +package jalview.ext.archaeopteryx; + +public class AlignToArchaeopteryxConverter +{ + +} diff --git a/src/jalview/ext/archaeopteryx/AlignmentToTreeViewerI.java b/src/jalview/ext/archaeopteryx/AlignToTreeViewerConverterI.java similarity index 96% rename from src/jalview/ext/archaeopteryx/AlignmentToTreeViewerI.java rename to src/jalview/ext/archaeopteryx/AlignToTreeViewerConverterI.java index 0dddbf1..60a4a1f 100644 --- a/src/jalview/ext/archaeopteryx/AlignmentToTreeViewerI.java +++ b/src/jalview/ext/archaeopteryx/AlignToTreeViewerConverterI.java @@ -22,7 +22,7 @@ package jalview.ext.archaeopteryx; import java.util.Collection; -interface AlignmentToTreeViewerI +interface AlignToTreeViewerConverterI { public void rootNode(Object node); diff --git a/src/jalview/ext/archaeopteryx/ArchaeopteryxTreeBuilder.java b/src/jalview/ext/archaeopteryx/ArchaeopteryxTreeBuilder.java index e8c5109..18ac083 100644 --- a/src/jalview/ext/archaeopteryx/ArchaeopteryxTreeBuilder.java +++ b/src/jalview/ext/archaeopteryx/ArchaeopteryxTreeBuilder.java @@ -1,6 +1,8 @@ package jalview.ext.archaeopteryx; -public class ArchaeopteryxTreeBuilder +import org.forester.phylogeny.Phylogeny; + +public class ArchaeopteryxTreeBuilder extends Phylogeny { } diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 6f0bd11..d740303 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -26,6 +26,7 @@ import jalview.analysis.CrossRef; import jalview.analysis.Dna; import jalview.analysis.ParseProperties; import jalview.analysis.SequenceIdMatcher; +import jalview.analysis.TreeModel; import jalview.api.AlignExportSettingI; import jalview.api.AlignViewControllerGuiI; import jalview.api.AlignViewControllerI; @@ -3557,15 +3558,15 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, /** * Constructs a tree panel and adds it to the desktop * - * @param type - * tree type (NJ or AV) - * @param modelName - * name of score model used to compute the tree - * @param options - * parameters for the distance or similarity calculation + * @param params + * @param treeAlgo + * + * @param tree + * + * */ - void newTreePanel(String type, String modelName, - SimilarityParamsI options) + void newTreePanel(TreeModel tree, String treeAlgo, + SimilarityParamsI params) { String frameTitle = ""; TreePanel tp; @@ -3601,7 +3602,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, } } - tp = new TreePanel(alignPanel, type, modelName, options); + tp = new TreePanel(alignPanel, tree, treeAlgo, params); frameTitle = tp.getPanelTitle() + (onSelection ? " on region" : ""); frameTitle += " from "; @@ -4032,6 +4033,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, final JMenu seqsrchmenu = new JMenu("Sequence Database Search"); final JMenu analymenu = new JMenu("Analysis"); final JMenu dismenu = new JMenu("Protein Disorder"); + final JMenu phylogenmenu = new JMenu("Phylogenetic inference"); + // JAL-940 - only show secondary structure prediction services from // the legacy server if (// Cache.getDefault("SHOW_JWS1_SERVICES", true) @@ -4072,6 +4075,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, wsmenu.add(secstrmenu); wsmenu.add(dismenu); wsmenu.add(analymenu); + wsmenu.add(phylogenmenu); // No search services yet // wsmenu.add(seqsrchmenu); diff --git a/src/jalview/gui/CalculationChooser.java b/src/jalview/gui/CalculationChooser.java index bd6c5ee..811957b 100644 --- a/src/jalview/gui/CalculationChooser.java +++ b/src/jalview/gui/CalculationChooser.java @@ -21,6 +21,8 @@ package jalview.gui; import jalview.analysis.TreeBuilder; +import jalview.analysis.TreeCalculator; +import jalview.analysis.TreeModel; import jalview.analysis.scoremodels.ScoreModels; import jalview.analysis.scoremodels.SimilarityParams; import jalview.api.analysis.ScoreModelI; @@ -471,10 +473,14 @@ public class CalculationChooser extends JPanel { String treeAlgo = determineTreeAlgo(); - // TreeCalculator calculateTree = new TreeCalculator(treeAlgo, - // substitutionMatrix, params); + TreeCalculator treeCalculator = new TreeCalculator(treeAlgo, + substitutionMatrix, params); + TreeBuilder calculatedTree = treeCalculator + .makeTree(af.getViewport()); + TreeModel tree = new TreeModel(calculatedTree); + openTreePanel(tree, treeAlgo, params); + - openTreePanel(treeAlgo, substitutionMatrix, params); } // closeFrame(); @@ -508,8 +514,11 @@ public class CalculationChooser extends JPanel /** * Open a new Tree panel on the desktop * + * @param tree + * @param params + * @param treeAlgo */ - protected void openTreePanel(String treeType, String substitutionMatrix, + protected void openTreePanel(TreeModel tree, String treeAlgo, SimilarityParamsI params) { /* @@ -518,9 +527,7 @@ public class CalculationChooser extends JPanel */ checkEnoughSequences(af.getViewport()); - - - af.newTreePanel(treeType, substitutionMatrix, params); + af.newTreePanel(tree, treeAlgo, params); } /** diff --git a/src/jalview/gui/TreePanel.java b/src/jalview/gui/TreePanel.java index ed6eae0..dbd5752 100755 --- a/src/jalview/gui/TreePanel.java +++ b/src/jalview/gui/TreePanel.java @@ -21,12 +21,7 @@ package jalview.gui; import jalview.analysis.AlignmentSorter; -import jalview.analysis.AverageDistanceTree; -import jalview.analysis.NJTree; -import jalview.analysis.TreeBuilder; import jalview.analysis.TreeModel; -import jalview.analysis.scoremodels.ScoreModels; -import jalview.api.analysis.ScoreModelI; import jalview.api.analysis.SimilarityParamsI; import jalview.bin.Cache; import jalview.commands.CommandI; @@ -75,14 +70,15 @@ import org.jibble.epsgraphics.EpsGraphics2D; */ public class TreePanel extends GTreePanel { + + SimilarityParamsI similarityParams; + String treeType; String scoreModelName; // if tree computed String treeTitle; // if tree loaded - SimilarityParamsI similarityParams; - TreeCanvas treeCanvas; TreeModel tree; @@ -93,28 +89,37 @@ public class TreePanel extends GTreePanel * Creates a new TreePanel object. * * @param ap - * @param type - * @param modelName - * @param options + * @param tree */ - public TreePanel(AlignmentPanel ap, String type, String modelName, - SimilarityParamsI options) + public TreePanel(AlignmentPanel ap, TreeModel tree, String treeType, + SimilarityParamsI params) { super(); - this.similarityParams = options; - initTreePanel(ap, type, modelName, null, null); + this.treeType = treeType; + this.similarityParams = params; + this.tree = tree; + initNewTreePanel(ap, tree); // We know this tree has distances. JBPNote TODO: prolly should add this as // a userdefined default // showDistances(true); } + /** + * Creates a new TreePanel object. + * + * @param alignPanel + * @param newtree + * @param theTitle + * @param inputData + * @param viewport + */ public TreePanel(AlignmentPanel alignPanel, NewickFile newtree, String theTitle, AlignmentView inputData) { super(); this.treeTitle = theTitle; - initTreePanel(alignPanel, null, null, newtree, inputData); + initLoadedTreePanel(alignPanel, newtree, inputData); } public AlignmentI getAlignment() @@ -127,17 +132,30 @@ public class TreePanel extends GTreePanel return treeCanvas.av; } - void initTreePanel(AlignmentPanel ap, String type, String modelName, + void initNewTreePanel(AlignmentPanel ap, TreeModel tree) + { + buildTreeCanvas(ap); + + TreeLoader tl = new TreeLoader(null, null); + tl.start(); + + } + void initLoadedTreePanel(AlignmentPanel ap, NewickFile newTree, AlignmentView inputData) { + buildTreeCanvas(ap); - av = ap.av; - this.treeType = type; - this.scoreModelName = modelName; + TreeLoader tl = new TreeLoader(newTree, inputData); + tl.start(); + } + +public void buildTreeCanvas(AlignmentPanel ap) { + av = ap.av; treeCanvas = new TreeCanvas(this, ap, scrollPane); scrollPane.setViewportView(treeCanvas); + PaintRefresher.Register(this, ap.av.getSequenceSetId()); buildAssociatedViewMenu(); @@ -172,8 +190,7 @@ public class TreePanel extends GTreePanel }); - TreeLoader tl = new TreeLoader(newTree, inputData); - tl.start(); + } @@ -281,22 +298,12 @@ public class TreePanel extends GTreePanel } - else - { - ScoreModelI sm = ScoreModels.getInstance() - .getScoreModel(scoreModelName, treeCanvas.ap); - TreeBuilder njtree = treeType.equals(TreeBuilder.NEIGHBOUR_JOINING) - ? new NJTree(av, sm, similarityParams) - : new AverageDistanceTree(av, sm, similarityParams); - tree = new TreeModel(njtree); - showDistances(true); - - - - } - ArchaeopteryxNewickInit archae = new ArchaeopteryxNewickInit(tree); - archae.startArchaeopteryx(); + showTree(tree); + } + + public void showTree(TreeModel tree) + { tree.reCount(tree.getTopNode()); tree.findHeight(tree.getTopNode()); treeCanvas.setTree(tree); @@ -307,6 +314,9 @@ public class TreePanel extends GTreePanel { sortByTree_actionPerformed(); } + + ArchaeopteryxNewickInit archae = new ArchaeopteryxNewickInit(tree); + archae.startArchaeopteryx(); } } @@ -841,29 +851,31 @@ public class TreePanel extends GTreePanel * * @return */ - public String getPanelTitle() + public String getPanelTitle() // to be moved/fixed { if (treeTitle != null) { return treeTitle; } - - /* - * i18n description of Neighbour Joining or Average Distance method - */ - String treecalcnm = MessageManager - .getString("label.tree_calc_" + treeType.toLowerCase()); - - /* - * short score model name (long description can be too long) - */ - String smn = scoreModelName; - - /* - * put them together as Using - */ - final String ttl = MessageManager.formatMessage("label.treecalc_title", - treecalcnm, smn); - return ttl; + else + { + /* + * i18n description of Neighbour Joining or Average Distance method + */ + String treecalcnm = MessageManager + .getString("label.tree_calc_" + treeType.toLowerCase()); + + /* + * short score model name (long description can be too long) + */ + String smn = scoreModelName; + + /* + * put them together as Using + */ + final String ttl = MessageManager + .formatMessage("label.treecalc_title", treecalcnm, smn); + return ttl; + } } } -- 1.7.10.2