From 90ae01c1c1504e0ae42817f78116f1d35b317c47 Mon Sep 17 00:00:00 2001 From: kjvdheide Date: Fri, 29 Sep 2017 16:56:49 +0100 Subject: [PATCH] Begin separation of GUI and tree calculation logic --- .../ext/archaeopteryx/AlignmentToTreeViewerI.java | 7 ++ .../ext/archaeopteryx/ArchaeopteryxInit.java | 109 ++++++++++++++++++++ .../ext/archaeopteryx/TreeViewerBindingI.java | 6 ++ src/jalview/gui/AlignFrame.java | 1 + src/jalview/gui/CalculationChooser.java | 53 ++++++---- src/jalview/gui/TreePanel.java | 24 ++--- 6 files changed, 167 insertions(+), 33 deletions(-) create mode 100644 src/jalview/ext/archaeopteryx/AlignmentToTreeViewerI.java create mode 100644 src/jalview/ext/archaeopteryx/ArchaeopteryxInit.java create mode 100644 src/jalview/ext/archaeopteryx/TreeViewerBindingI.java diff --git a/src/jalview/ext/archaeopteryx/AlignmentToTreeViewerI.java b/src/jalview/ext/archaeopteryx/AlignmentToTreeViewerI.java new file mode 100644 index 0000000..85ee750 --- /dev/null +++ b/src/jalview/ext/archaeopteryx/AlignmentToTreeViewerI.java @@ -0,0 +1,7 @@ +package jalview.ext.archaeopteryx; + + +interface AlignmentToTreeViewerI +{ + +} diff --git a/src/jalview/ext/archaeopteryx/ArchaeopteryxInit.java b/src/jalview/ext/archaeopteryx/ArchaeopteryxInit.java new file mode 100644 index 0000000..5089112 --- /dev/null +++ b/src/jalview/ext/archaeopteryx/ArchaeopteryxInit.java @@ -0,0 +1,109 @@ +package jalview.ext.archaeopteryx; + +import jalview.analysis.TreeModel; +import jalview.io.NewickFile; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintWriter; + +import org.forester.archaeopteryx.Archaeopteryx; + +public class ArchaeopteryxInit +{ + private String filePath = "/tmp/jalviewtree.nwk"; + + private TreeModel tree; + + private NewickFile newickTree; + + public ArchaeopteryxInit(TreeModel jalviewTreeModel) + { + this.tree = jalviewTreeModel; + this.newickTree = treeToNewick(tree); + } + + public ArchaeopteryxInit(NewickFile newickTreeModel) + { + this.newickTree = newickTreeModel; + } + + public void startArchaeopteryx() + { + String newickOutput = newickTree.print(newickTree.hasBootstrap(), + newickTree.hasDistances(), newickTree.hasRootDistance()); + + File newickFile = new File(filePath); + PrintWriter writer; + + try + { + writer = new PrintWriter(newickFile); + writer.println(newickOutput); + + // System.out.println(writer.checkError()); + writer.close(); + + String[] commandLineArgs = { "-open", newickFile.getCanonicalPath() }; + Archaeopteryx.main(commandLineArgs); + + } catch (FileNotFoundException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + } + + + public String getFilePath() + { + return filePath; + } + + public void setFilePath(String newFilePath) + { + this.filePath = newFilePath; + } + + public TreeModel getTree() + { + return tree; + } + + public void setTree(TreeModel newTree) + { + this.tree = newTree; + } + + public NewickFile getNewickTree() + { + return newickTree; + } + + public void setNewickTree(NewickFile newNewickTree) + { + this.newickTree = newNewickTree; + } + + public static NewickFile treeToNewick(TreeModel tree) + { + NewickFile newickTree = new NewickFile(tree.getTopNode(), + tree.hasBootstrap(), tree.hasDistances(), + tree.hasRootDistance()); + /* System.out.println(newickTree.print(tree.hasBootstrap(), + tree.hasDistances(), tree.hasRootDistance())); + + System.out.println(newickTree.print(newickTree.hasBootstrap(), + newickTree.hasDistances(), newickTree.hasRootDistance()));*/ + + return newickTree; + } + +} \ No newline at end of file diff --git a/src/jalview/ext/archaeopteryx/TreeViewerBindingI.java b/src/jalview/ext/archaeopteryx/TreeViewerBindingI.java new file mode 100644 index 0000000..4bb0ff2 --- /dev/null +++ b/src/jalview/ext/archaeopteryx/TreeViewerBindingI.java @@ -0,0 +1,6 @@ +package jalview.ext.archaeopteryx; + +interface TreeViewerBindingI +{ + +} diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 13b715e..6f0bd11 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -3553,6 +3553,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, viewport.followSelection = listenToViewSelections.isSelected(); } + /** * Constructs a tree panel and adds it to the desktop * diff --git a/src/jalview/gui/CalculationChooser.java b/src/jalview/gui/CalculationChooser.java index a9f3966..f2d65e5 100644 --- a/src/jalview/gui/CalculationChooser.java +++ b/src/jalview/gui/CalculationChooser.java @@ -103,7 +103,7 @@ public class CalculationChooser extends JPanel final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer(); - List tips = new ArrayList(); + List tips = new ArrayList<>(); /** * Constructor @@ -370,7 +370,7 @@ public class CalculationChooser extends JPanel */ protected JComboBox buildModelOptionsList() { - final JComboBox scoreModelsCombo = new JComboBox(); + final JComboBox scoreModelsCombo = new JComboBox<>(); scoreModelsCombo.setRenderer(renderer); /* @@ -413,7 +413,7 @@ public class CalculationChooser extends JPanel { Object curSel = comboBox.getSelectedItem(); toolTips.clear(); - DefaultComboBoxModel model = new DefaultComboBoxModel(); + DefaultComboBoxModel model = new DefaultComboBoxModel<>(); /* * now we can actually add entries to the combobox, @@ -469,25 +469,29 @@ public class CalculationChooser extends JPanel } else { + calculateTree(); openTreePanel(modelName, params); } // closeFrame(); } - /** - * Open a new Tree panel on the desktop - * - * @param modelName - * @param params - */ - protected void openTreePanel(String modelName, SimilarityParamsI params) + protected void calculateTree() + { + + } + protected String determineTreeAlgo() // to be modified & expanded + { + String treeAlgorithm = neighbourJoining.isSelected() + ? TreeBuilder.NEIGHBOUR_JOINING + : TreeBuilder.AVERAGE_DISTANCE; + + return treeAlgorithm; + + } + + protected void checkEnoughSequences(AlignViewport viewport) { - /* - * gui validation shouldn't allow insufficient sequences here, but leave - * this check in in case this method gets exposed programmatically in future - */ - AlignViewport viewport = af.getViewport(); SequenceGroup sg = viewport.getSelectionGroup(); if (sg != null && sg.getSize() < MIN_TREE_SELECTION) { @@ -499,10 +503,22 @@ public class CalculationChooser extends JPanel JvOptionPane.WARNING_MESSAGE); return; } + } + + /** + * Open a new Tree panel on the desktop + * + */ + protected void openTreePanel(String modelName, SimilarityParamsI params) + { + /* + * gui validation shouldn't allow insufficient sequences here, but leave + * this check in in case this method gets exposed programmatically in future + */ + checkEnoughSequences(af.getViewport()); + + String treeType = determineTreeAlgo(); - String treeType = neighbourJoining.isSelected() - ? TreeBuilder.NEIGHBOUR_JOINING - : TreeBuilder.AVERAGE_DISTANCE; af.newTreePanel(treeType, modelName, params); } @@ -576,6 +592,7 @@ public class CalculationChooser extends JPanel */ boolean matchGap = doPCA ? false : treeMatchGaps; + return new SimilarityParams(includeGapGap, matchGap, includeGapResidue, matchOnShortestLength); } diff --git a/src/jalview/gui/TreePanel.java b/src/jalview/gui/TreePanel.java index 3af174a..a4521cc 100755 --- a/src/jalview/gui/TreePanel.java +++ b/src/jalview/gui/TreePanel.java @@ -41,7 +41,7 @@ import jalview.datamodel.NodeTransformI; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceI; import jalview.datamodel.SequenceNode; -import jalview.ext.archaeopteryx.Testo; +import jalview.ext.archaeopteryx.ArchaeopteryxInit; import jalview.io.JalviewFileChooser; import jalview.io.JalviewFileView; import jalview.io.NewickFile; @@ -56,9 +56,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.beans.PropertyChangeEvent; -import java.io.File; import java.io.FileOutputStream; -import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -279,6 +277,10 @@ public class TreePanel extends GTreePanel { originalSeqData.setVisible(false); } + + ArchaeopteryxInit archae = new ArchaeopteryxInit(newtree); + archae.startArchaeopteryx(); + } else { @@ -289,6 +291,10 @@ public class TreePanel extends GTreePanel : new AverageDistanceTree(av, sm, similarityParams); tree = new TreeModel(njtree); showDistances(true); + + ArchaeopteryxInit archae = new ArchaeopteryxInit(tree); + archae.startArchaeopteryx(); + } tree.reCount(tree.getTopNode()); @@ -296,18 +302,6 @@ public class TreePanel extends GTreePanel treeCanvas.setTree(tree); treeCanvas.repaint(); - Testo test = new Testo(); - NewickFile newickTree = test.treeToNewick(tree); - File newickAsFile = test.newickToArchaeopteryx(newickTree); - try - { - String[] archaeCommandlineArgs = { - "-open", newickAsFile.getCanonicalPath() }; - test.startArchaeopteryx(archaeCommandlineArgs); - } catch (IOException e) - { - e.printStackTrace(); - } av.setCurrentTree(tree); if (av.getSortByTree()) { -- 1.7.10.2