From 2c3ff4023f0603d9ae13aa5cbbea737e5f4cc092 Mon Sep 17 00:00:00 2001 From: kjvdheide Date: Fri, 17 Nov 2017 15:48:51 +0000 Subject: [PATCH] JAL-2805 code refactor (actionPerformed cleaned up) & moved tree loading --- src/jalview/ext/archaeopteryx/AptxInit.java | 91 ++++++ .../ext/forester/io/SupportedTreeFileFilter.java | 3 +- src/jalview/ext/forester/io/TreeParser.java | 300 +++++++++++--------- src/jalview/gui/AlignFrame.java | 75 ++--- 4 files changed, 287 insertions(+), 182 deletions(-) diff --git a/src/jalview/ext/archaeopteryx/AptxInit.java b/src/jalview/ext/archaeopteryx/AptxInit.java index 2363e4c..817fef3 100644 --- a/src/jalview/ext/archaeopteryx/AptxInit.java +++ b/src/jalview/ext/archaeopteryx/AptxInit.java @@ -93,6 +93,97 @@ public final class AptxInit return bindFrameToJalview(aptxApp); } + public static MainFrame createInstanceFromUrl() + { + + return null; + // void readPhylogeniesFromURL() { + // URL url = null; + // Phylogeny[] phys = null; + // final String message = "Please enter a complete URL, for example + // \"http://purl.org/phylo/treebase/phylows/study/TB2:S15480?format=nexus\""; + // final String url_string = JOptionPane + // .showInputDialog( this, + // message, + // "Use URL/webservice to obtain a phylogeny", + // JOptionPane.QUESTION_MESSAGE ); + // boolean nhx_or_nexus = false; + // if ( ( url_string != null ) && ( url_string.length() > 0 ) ) { + // try { + // url = new URL( url_string ); + // PhylogenyParser parser = null; + // if ( url.getHost().toLowerCase().indexOf( "tolweb" ) >= 0 ) { + // parser = new TolParser(); + // } + // else { + // parser = ParserUtils + // .createParserDependingOnUrlContents( url, + // getConfiguration().isValidatePhyloXmlAgainstSchema() ); + // } + // if ( parser instanceof NexusPhylogeniesParser ) { + // nhx_or_nexus = true; + // } + // else if ( parser instanceof NHXParser ) { + // nhx_or_nexus = true; + // } + // if ( _mainpanel.getCurrentTreePanel() != null ) { + // _mainpanel.getCurrentTreePanel().setWaitCursor(); + // } + // else { + // _mainpanel.setWaitCursor(); + // } + // final PhylogenyFactory factory = + // ParserBasedPhylogenyFactory.getInstance(); + // phys = factory.create( url.openStream(), parser ); + // } + // catch ( final MalformedURLException e ) { + // JOptionPane.showMessageDialog( this, + // "Malformed URL: " + url + "\n" + e.getLocalizedMessage(), + // "Malformed URL", + // JOptionPane.ERROR_MESSAGE ); + // } + // catch ( final IOException e ) { + // JOptionPane.showMessageDialog( this, + // "Could not read from " + url + "\n" + // + ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ), + // "Failed to read URL", + // JOptionPane.ERROR_MESSAGE ); + // } + // catch ( final Exception e ) { + // JOptionPane.showMessageDialog( this, + // ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ), + // "Unexpected Exception", + // JOptionPane.ERROR_MESSAGE ); + // } + // finally { + // if ( _mainpanel.getCurrentTreePanel() != null ) { + // _mainpanel.getCurrentTreePanel().setArrowCursor(); + // } + // else { + // _mainpanel.setArrowCursor(); + // } + // } + // if ( ( phys != null ) && ( phys.length > 0 ) ) { + // if ( nhx_or_nexus && + // getOptions().isInternalNumberAreConfidenceForNhParsing() ) { + // for( final Phylogeny phy : phys ) { + // PhylogenyMethods.transferInternalNodeNamesToConfidence( phy, "" ); + // } + // } + // AptxUtil.addPhylogeniesToTabs( phys, + // new File( url.getFile() ).getName(), + // new File( url.getFile() ).toString(), + // getConfiguration(), + // getMainPanel() ); + // _mainpanel.getControlPanel().showWhole(); + // } + // } + // activateSaveAllIfNeeded(); + // System.gc(); + // } + + } + public static MainFrame createAptxFrame( diff --git a/src/jalview/ext/forester/io/SupportedTreeFileFilter.java b/src/jalview/ext/forester/io/SupportedTreeFileFilter.java index b13c92a..b077485 100644 --- a/src/jalview/ext/forester/io/SupportedTreeFileFilter.java +++ b/src/jalview/ext/forester/io/SupportedTreeFileFilter.java @@ -10,7 +10,8 @@ public enum SupportedTreeFileFilter { NHFILTER(MainFrame.nhfilter), NHXFILTER(MainFrame.nhxfilter), XMLFILTER(MainFrame.xmlfilter), TOLFILTER(MainFrame.tolfilter), - NEXUSFILTER(MainFrame.nexusfilter); + NEXUSFILTER(MainFrame.nexusfilter), + DEFAULTFILTER(MainFrame.defaultfilter); private final FileFilter treeFilter; diff --git a/src/jalview/ext/forester/io/TreeParser.java b/src/jalview/ext/forester/io/TreeParser.java index b2f36b8..96b5770 100644 --- a/src/jalview/ext/forester/io/TreeParser.java +++ b/src/jalview/ext/forester/io/TreeParser.java @@ -1,56 +1,84 @@ -// package jalview.ext.forester.io; -// -// import jalview.ext.archaeopteryx.AptxInit; -// import jalview.ext.archaeopteryx.JalviewBinding; -// import jalview.ext.archaeopteryx.LoadedTreeAssociation; -// import jalview.ext.treeviewer.ExternalTreeParserI; -// import jalview.gui.Desktop; -// import jalview.gui.JvOptionPane; -// import jalview.util.MessageManager; -// import jalview.viewmodel.AlignmentViewport; -// -// import java.io.File; -// import java.io.IOException; -// -// import org.forester.archaeopteryx.Archaeopteryx; -// import org.forester.archaeopteryx.MainFrame; -// import org.forester.util.ForesterUtil; -// -// public class TreeParser implements ExternalTreeParserI -// { -// private final String filePath; -// -// private final File file; -// -// public TreeParser(final String treeFilePath) -// { -// final String possibleError = ForesterUtil.isReadableFile(treeFilePath); -// if (possibleError != null) -// { -// JvOptionPane.showMessageDialog(Desktop.desktop, possibleError, -// MessageManager.getString("label.problem_reading_tree_file"), -// JvOptionPane.WARNING_MESSAGE); -// -// } -// filePath = treeFilePath; -// file = new File(filePath); -// -// -// } -// -// public TreeParser(final File treeFile) throws IOException -// { -// final String possibleError = ForesterUtil.isReadableFile(treeFile); -// if (possibleError != null) -// { -// JvOptionPane.showMessageDialog(Desktop.desktop, possibleError, -// MessageManager.getString("label.problem_reading_tree_file"), -// JvOptionPane.WARNING_MESSAGE); -// -// } -// file = treeFile; -// filePath = file.getCanonicalPath(); -// } +package jalview.ext.forester.io; + +import jalview.ext.archaeopteryx.AptxInit; +import jalview.gui.AlignViewport; +//import jalview.ext.treeviewer.ExternalTreeParserI; +import jalview.gui.Desktop; +import jalview.gui.JvOptionPane; +import jalview.io.DataSourceType; +import jalview.io.NewickFile; +import jalview.util.MessageManager; + +import java.io.File; +import java.io.IOException; + +import org.forester.util.ForesterUtil; + +public class TreeParser // implements ExternalTreeParserI +{ + private final String filePath; + + private final File file; + + public TreeParser(final String treeFilePath) + { + final String possibleError = ForesterUtil.isReadableFile(treeFilePath); + if (possibleError != null) + { + JvOptionPane.showMessageDialog(Desktop.desktop, possibleError, + MessageManager.getString("label.problem_reading_tree_file"), + JvOptionPane.WARNING_MESSAGE); + + } + filePath = treeFilePath; + file = new File(filePath); + + } + + public TreeParser(final File treeFile) throws IOException + { + final String possibleError = ForesterUtil.isReadableFile(treeFile); + if (possibleError != null) + { + JvOptionPane.showMessageDialog(Desktop.desktop, possibleError, + MessageManager.getString("label.problem_reading_tree_file"), + JvOptionPane.WARNING_MESSAGE); + + } + file = treeFile; + filePath = file.getCanonicalPath(); + } + + public void loadTree(AlignViewport viewport) + { + + NewickFile fin = null; + try + { + AptxInit.createInstanceFromFile(filePath, viewport); + + fin = new NewickFile(filePath, DataSourceType.FILE); + viewport.setCurrentTree(viewport.getAlignPanel().alignFrame + .showNewickTree(fin, filePath).getTree()); + } catch (Exception ex) + { + JvOptionPane.showMessageDialog(Desktop.desktop, ex.getMessage(), + MessageManager.getString("label.problem_reading_tree_file"), + JvOptionPane.WARNING_MESSAGE); + ex.printStackTrace(); + } + if (fin != null && fin.hasWarningMessage()) + { + JvOptionPane.showMessageDialog(Desktop.desktop, + fin.getWarningMessage(), + MessageManager + .getString("label.possible_problem_with_tree_file"), + JvOptionPane.WARNING_MESSAGE); + } + } + } + + // // @Override // public MainFrame loadTreeFile(AlignmentViewport viewport) @@ -75,90 +103,90 @@ // // } // // -// // void readPhylogeniesFromURL() { -// // URL url = null; -// // Phylogeny[] phys = null; -// // final String message = "Please enter a complete URL, for example -// // \"http://purl.org/phylo/treebase/phylows/study/TB2:S15480?format=nexus\""; -// // final String url_string = JOptionPane -// // .showInputDialog( this, -// // message, -// // "Use URL/webservice to obtain a phylogeny", -// // JOptionPane.QUESTION_MESSAGE ); -// // boolean nhx_or_nexus = false; -// // if ( ( url_string != null ) && ( url_string.length() > 0 ) ) { -// // try { -// // url = new URL( url_string ); -// // PhylogenyParser parser = null; -// // if ( url.getHost().toLowerCase().indexOf( "tolweb" ) >= 0 ) { -// // parser = new TolParser(); -// // } -// // else { -// // parser = ParserUtils -// // .createParserDependingOnUrlContents( url, -// // getConfiguration().isValidatePhyloXmlAgainstSchema() ); -// // } -// // if ( parser instanceof NexusPhylogeniesParser ) { -// // nhx_or_nexus = true; -// // } -// // else if ( parser instanceof NHXParser ) { -// // nhx_or_nexus = true; -// // } -// // if ( _mainpanel.getCurrentTreePanel() != null ) { -// // _mainpanel.getCurrentTreePanel().setWaitCursor(); -// // } -// // else { -// // _mainpanel.setWaitCursor(); -// // } -// // final PhylogenyFactory factory = +// void readPhylogeniesFromURL() { +// URL url = null; +// Phylogeny[] phys = null; +// final String message = "Please enter a complete URL, for example +// \"http://purl.org/phylo/treebase/phylows/study/TB2:S15480?format=nexus\""; +// final String url_string = JOptionPane +// .showInputDialog( this, +// message, +// "Use URL/webservice to obtain a phylogeny", +// JOptionPane.QUESTION_MESSAGE ); +// boolean nhx_or_nexus = false; +// if ( ( url_string != null ) && ( url_string.length() > 0 ) ) { +// try { +// url = new URL( url_string ); +// PhylogenyParser parser = null; +// if ( url.getHost().toLowerCase().indexOf( "tolweb" ) >= 0 ) { +// parser = new TolParser(); +// } +// else { +// parser = ParserUtils +// .createParserDependingOnUrlContents( url, +// getConfiguration().isValidatePhyloXmlAgainstSchema() ); +// } +// if ( parser instanceof NexusPhylogeniesParser ) { +// nhx_or_nexus = true; +// } +// else if ( parser instanceof NHXParser ) { +// nhx_or_nexus = true; +// } +// if ( _mainpanel.getCurrentTreePanel() != null ) { +// _mainpanel.getCurrentTreePanel().setWaitCursor(); +// } +// else { +// _mainpanel.setWaitCursor(); +// } +// final PhylogenyFactory factory = // ParserBasedPhylogenyFactory.getInstance(); -// // phys = factory.create( url.openStream(), parser ); -// // } -// // catch ( final MalformedURLException e ) { -// // JOptionPane.showMessageDialog( this, -// // "Malformed URL: " + url + "\n" + e.getLocalizedMessage(), -// // "Malformed URL", -// // JOptionPane.ERROR_MESSAGE ); -// // } -// // catch ( final IOException e ) { -// // JOptionPane.showMessageDialog( this, -// // "Could not read from " + url + "\n" -// // + ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ), -// // "Failed to read URL", -// // JOptionPane.ERROR_MESSAGE ); -// // } -// // catch ( final Exception e ) { -// // JOptionPane.showMessageDialog( this, -// // ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ), -// // "Unexpected Exception", -// // JOptionPane.ERROR_MESSAGE ); -// // } -// // finally { -// // if ( _mainpanel.getCurrentTreePanel() != null ) { -// // _mainpanel.getCurrentTreePanel().setArrowCursor(); -// // } -// // else { -// // _mainpanel.setArrowCursor(); -// // } -// // } -// // if ( ( phys != null ) && ( phys.length > 0 ) ) { -// // if ( nhx_or_nexus && -// // getOptions().isInternalNumberAreConfidenceForNhParsing() ) { -// // for( final Phylogeny phy : phys ) { -// // PhylogenyMethods.transferInternalNodeNamesToConfidence( phy, "" ); -// // } -// // } -// // AptxUtil.addPhylogeniesToTabs( phys, -// // new File( url.getFile() ).getName(), -// // new File( url.getFile() ).toString(), -// // getConfiguration(), -// // getMainPanel() ); -// // _mainpanel.getControlPanel().showWhole(); -// // } -// // } -// // activateSaveAllIfNeeded(); -// // System.gc(); -// // } +// phys = factory.create( url.openStream(), parser ); +// } +// catch ( final MalformedURLException e ) { +// JOptionPane.showMessageDialog( this, +// "Malformed URL: " + url + "\n" + e.getLocalizedMessage(), +// "Malformed URL", +// JOptionPane.ERROR_MESSAGE ); +// } +// catch ( final IOException e ) { +// JOptionPane.showMessageDialog( this, +// "Could not read from " + url + "\n" +// + ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ), +// "Failed to read URL", +// JOptionPane.ERROR_MESSAGE ); +// } +// catch ( final Exception e ) { +// JOptionPane.showMessageDialog( this, +// ForesterUtil.wordWrap( e.getLocalizedMessage(), 80 ), +// "Unexpected Exception", +// JOptionPane.ERROR_MESSAGE ); +// } +// finally { +// if ( _mainpanel.getCurrentTreePanel() != null ) { +// _mainpanel.getCurrentTreePanel().setArrowCursor(); +// } +// else { +// _mainpanel.setArrowCursor(); +// } +// } +// if ( ( phys != null ) && ( phys.length > 0 ) ) { +// if ( nhx_or_nexus && +// getOptions().isInternalNumberAreConfidenceForNhParsing() ) { +// for( final Phylogeny phy : phys ) { +// PhylogenyMethods.transferInternalNodeNamesToConfidence( phy, "" ); +// } +// } +// AptxUtil.addPhylogeniesToTabs( phys, +// new File( url.getFile() ).getName(), +// new File( url.getFile() ).toString(), +// getConfiguration(), +// getMainPanel() ); +// _mainpanel.getControlPanel().showWhole(); +// } +// } +// activateSaveAllIfNeeded(); +// System.gc(); +// } // // } // diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index b9bf23e..4bd20fc 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -62,6 +62,7 @@ import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.ext.archaeopteryx.AptxInit; import jalview.ext.forester.io.SupportedTreeFileFilter; +import jalview.ext.forester.io.TreeParser; import jalview.gui.ColourMenuHelper.ColourChangeListener; import jalview.gui.ViewSelectionMenu.ViewSetProvider; import jalview.io.AlignmentProperties; @@ -3870,6 +3871,33 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, } return seqs; } +public void chooseTreeFile() { + // Pick the tree file + JalviewFileChooser chooser = new JalviewFileChooser( + jalview.bin.Cache.getProperty("LAST_DIRECTORY")); + chooser.setFileView(new JalviewFileView()); + chooser.setDialogTitle( + MessageManager.getString("label.select_newick_like_tree_file")); // modify + chooser.setToolTipText( + MessageManager.getString("label.load_tree_file")); + for (SupportedTreeFileFilter treeFormat : SupportedTreeFileFilter + .values()) + { + chooser.setFileFilter(treeFormat.getTreeFilter()); + } + + int value = chooser.showOpenDialog(null); + + if (value == JalviewFileChooser.APPROVE_OPTION) + { + String filePath = chooser.getSelectedFile().getPath(); + Cache.setProperty("LAST_DIRECTORY", filePath); + + TreeParser treeParser = new TreeParser(filePath); + treeParser.loadTree(viewport); + + } + } /** * DOCUMENT ME! @@ -3880,56 +3908,13 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, @Override protected void loadTreeMenuItem_actionPerformed(ActionEvent e) { - // Pick the tree file - JalviewFileChooser chooser = new JalviewFileChooser( - jalview.bin.Cache.getProperty("LAST_DIRECTORY")); - chooser.setFileView(new JalviewFileView()); - chooser.setDialogTitle( - MessageManager.getString("label.select_newick_like_tree_file")); // modify - chooser.setToolTipText( - MessageManager.getString("label.load_tree_file")); - for (SupportedTreeFileFilter treeFormat : SupportedTreeFileFilter.values()) - { - chooser.setFileFilter(treeFormat.getTreeFilter()); - } - - int value = chooser.showOpenDialog(null); - - if (value == JalviewFileChooser.APPROVE_OPTION) - { - String filePath = chooser.getSelectedFile().getPath(); - Cache.setProperty("LAST_DIRECTORY", filePath); - - NewickFile fin = null; - try - { - AptxInit.createInstanceFromFile(filePath, getViewport()); - - - - fin = new NewickFile(filePath, DataSourceType.FILE); - viewport.setCurrentTree(showNewickTree(fin, filePath).getTree()); - } catch (Exception ex) - { - JvOptionPane.showMessageDialog(Desktop.desktop, ex.getMessage(), - MessageManager.getString("label.problem_reading_tree_file"), - JvOptionPane.WARNING_MESSAGE); - ex.printStackTrace(); - } - if (fin != null && fin.hasWarningMessage()) - { - JvOptionPane.showMessageDialog(Desktop.desktop, - fin.getWarningMessage(), - MessageManager - .getString("label.possible_problem_with_tree_file"), - JvOptionPane.WARNING_MESSAGE); - } - } + chooseTreeFile(); } @Override protected void loadTreeUrlItem_actionPerformed(ActionEvent e) { + AptxInit.createInstanceFromUrl(); } -- 1.7.10.2