X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fext%2Farchaeopteryx%2FAptxInit.java;h=b9a2266f49651437ac5f250513c4246e232b66b3;hb=ccbd3e0bfd34533d8e98b7ef9a6cb48d9684b3da;hp=732e85a873307a6525e5e5a173e43654a6cc931a;hpb=3d860d173dda8b7f8e0527c4a19da16aef4c804d;p=jalview.git diff --git a/src/jalview/ext/archaeopteryx/AptxInit.java b/src/jalview/ext/archaeopteryx/AptxInit.java index 732e85a..b9a2266 100644 --- a/src/jalview/ext/archaeopteryx/AptxInit.java +++ b/src/jalview/ext/archaeopteryx/AptxInit.java @@ -15,6 +15,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.util.HashMap; import java.util.Map; import org.forester.archaeopteryx.AptxUtil; @@ -23,7 +24,6 @@ import org.forester.archaeopteryx.Configuration; import org.forester.archaeopteryx.MainFrame; import org.forester.archaeopteryx.webservices.PhylogeniesWebserviceClient; import org.forester.archaeopteryx.webservices.WebserviceUtil; -import org.forester.archaeopteryx.webservices.WebservicesManager; import org.forester.io.parsers.PhylogenyParser; import org.forester.io.parsers.nexus.NexusPhylogeniesParser; import org.forester.io.parsers.nhx.NHXParser; @@ -46,9 +46,9 @@ import org.forester.util.ForesterUtil; */ public final class AptxInit { - private final static Configuration APTX_CONFIG = new Configuration( - "_aptx_jalview_configuration_file", false, false, false); + "_aptx_jalview_configuration_file", + false, false, false); private final static boolean VALIDATE_PHYLOXML_XSD = APTX_CONFIG .isValidatePhyloXmlAgainstSchema(); @@ -65,6 +65,8 @@ public final class AptxInit private final static NHXParser.TAXONOMY_EXTRACTION TAXONOMY_EXTRACTION = APTX_CONFIG .getTaxonomyExtraction(); + private static Map activeAptx = new HashMap<>(); + public static MainFrame createInstanceFromCalculation( @@ -108,41 +110,49 @@ public final class AptxInit Desktop.instance.startLoading(filePath); } boolean nhx_or_nexus = false; - final PhylogenyParser p = ParserUtils.createParserDependingOnFileType( + final PhylogenyParser parser = ParserUtils.createParserDependingOnFileType( treeFile, VALIDATE_PHYLOXML_XSD); - if (p instanceof NHXParser) + if (parser instanceof NHXParser) { nhx_or_nexus = true; - final NHXParser nhx = (NHXParser) p; + final NHXParser nhx = (NHXParser) parser; nhx.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES); nhx.setIgnoreQuotes(false); nhx.setTaxonomyExtraction(TAXONOMY_EXTRACTION); } - else if (p instanceof NexusPhylogeniesParser) + else if (parser instanceof NexusPhylogeniesParser) { nhx_or_nexus = true; - final NexusPhylogeniesParser nex = (NexusPhylogeniesParser) p; + final NexusPhylogeniesParser nex = (NexusPhylogeniesParser) parser; nex.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES); nex.setIgnoreQuotes(false); } -// else if (p instanceof PhyloXmlParser) -// { -// MainFrameApplication.warnIfNotPhyloXmlValidation(APTX_CONFIG); -// } - Phylogeny[] phylogenies = PhylogenyMethods.readPhylogenies(p, treeFile); - MainFrame[] aptxFrames = new MainFrame[phylogenies.length]; - String treeTitle = treeFile.getName(); - - for (int i = 0; i < phylogenies.length; i++) + else if (parser instanceof PhyloXmlParser) + { + if (VALIDATE_PHYLOXML_XSD == false) + { + JvOptionPane.showInternalMessageDialog(Desktop.desktop, + MessageManager.getString("error.phyloxml_validation"), + MessageManager.getString("label.file_open_error"), + JvOptionPane.WARNING_MESSAGE ); + } + } + Phylogeny[] trees = PhylogenyMethods.readPhylogenies(parser, treeFile); + MainFrame[] aptxFrames = new MainFrame[trees.length]; + + + for (int i = 0; i < trees.length; i++) { - Phylogeny tree = phylogenies[i]; + Phylogeny tree = trees[i]; if (nhx_or_nexus && INTERNAL_NUMBERS_AS_CONFIDENCE) { PhylogenyMethods.transferInternalNodeNamesToConfidence(tree, ""); } - + String treeTitle = treeFile.getName() + "[" + i + "]"; + tree.setName(treeTitle); aptxFrames[i] = createAptxFrame(tree, viewport, treeTitle); + } if (Desktop.instance != null) { @@ -184,50 +194,44 @@ public final class AptxInit } /** - * Refactored from Forester's UrlTreeReader + * Refactored from Forester's UrlTreeReader, this can be more efficient * * @param databaseIndex * @param viewport * @return */ - public static MainFrame[] createInstancesFromDb(int databaseIndex, + public static MainFrame[] createInstancesFromDb( + PhylogeniesWebserviceClient treeDbClient, String identifier, AlignmentViewport viewport) { - if (Desktop.desktop != null) - { - // Desktop.desktop.startLoading(treeTitle); - } URL url = null; Phylogeny[] trees = null; - final WebservicesManager webservices_manager = WebservicesManager - .getInstance(); - final PhylogeniesWebserviceClient client = webservices_manager - .getAvailablePhylogeniesWebserviceClient(databaseIndex); - String identifier = JvOptionPane.showInternalInputDialog( - Desktop.desktop, - client.getInstructions() + "\n(Reference: " - + client.getReference() + ")", - client.getDescription(), JvOptionPane.QUESTION_MESSAGE); + if ((identifier != null) && (identifier.trim().length() > 0)) { + if (Desktop.instance != null) + { + Desktop.instance.startLoading(identifier); + } + identifier = identifier.trim(); - if (client.isQueryInteger()) + if (treeDbClient.isQueryInteger()) { identifier = identifier.replaceAll("^\\D+", ""); - int id = -1; + + int id; try { id = Integer.parseInt(identifier); } catch (final NumberFormatException e) { - id = -1; - } - if (id < 1) - { JvOptionPane.showInternalMessageDialog(Desktop.desktop, - "Identifier is expected to be a number", - "Can not open URL", JvOptionPane.ERROR_MESSAGE); + MessageManager.formatMessage( + "error.database_id_has_letters", new String[] + { identifier }), + MessageManager.getString("label.invalid_url"), + JvOptionPane.ERROR_MESSAGE); return new MainFrame[0]; } identifier = id + ""; @@ -235,12 +239,12 @@ public final class AptxInit boolean exception = false; try { - String url_str = client.getUrl(); + String url_str = treeDbClient.getUrl(); url_str = url_str.replaceFirst( PhylogeniesWebserviceClient.QUERY_PLACEHOLDER, identifier); url = new URL(url_str); PhylogenyParser parser = null; - switch (client.getReturnFormat()) + switch (treeDbClient.getReturnFormat()) { case TOL_XML_RESPONSE: parser = new TolParser(); @@ -294,7 +298,7 @@ public final class AptxInit break; default: throw new IllegalArgumentException( - "unknown format: " + client.getReturnFormat()); + "unknown format: " + treeDbClient.getReturnFormat()); } // // if (_main_frame.getMainPanel().getCurrentTreePanel() != null) @@ -310,26 +314,32 @@ public final class AptxInit { exception = true; JvOptionPane.showInternalMessageDialog(Desktop.desktop, - "Malformed URL: " + url + "\n" + e.getLocalizedMessage(), - "Malformed URL", JvOptionPane.ERROR_MESSAGE); + MessageManager.formatMessage( + "exception.unable_to_launch_url", new String[] + { url.toString() }), + MessageManager.getString("label.invalid_url"), + JvOptionPane.ERROR_MESSAGE); + System.err.println(e.getLocalizedMessage()); } catch (final IOException e) { exception = true; JvOptionPane.showInternalMessageDialog(Desktop.desktop, "Could not read from " + url + "\n" + e.getLocalizedMessage(), - "Failed to read tree from " + client.getName() + " for " + "Failed to read tree from " + treeDbClient.getName() + " for " + identifier, JvOptionPane.ERROR_MESSAGE); + System.err.println(e.getLocalizedMessage()); } catch (final NumberFormatException e) { exception = true; JvOptionPane.showInternalMessageDialog(Desktop.desktop, "Could not read from " + url + "\n" + e.getLocalizedMessage(), - "Failed to read tree from " + client.getName() + " for " + "Failed to read tree from " + treeDbClient.getName() + " for " + identifier, JvOptionPane.ERROR_MESSAGE); + System.err.println(e.getLocalizedMessage()); } catch (final Exception e) { exception = true; @@ -337,16 +347,7 @@ public final class AptxInit JvOptionPane.showInternalMessageDialog(Desktop.desktop, e.getLocalizedMessage(), "Unexpected Exception", JvOptionPane.ERROR_MESSAGE); - } finally - { - // if (_main_frame.getCurrentTreePanel() != null) - // { - // _main_frame.getCurrentTreePanel().setArrowCursor(); - // } - // else - // { - // _main_frame.getMainPanel().setArrowCursor(); - // } + System.err.println(e.getLocalizedMessage()); } if ((trees != null) && (trees.length > 0)) { @@ -354,16 +355,16 @@ public final class AptxInit { if (!phylogeny.isEmpty()) { - if (client.getName().equals(WebserviceUtil.TREE_FAM_NAME)) + if (treeDbClient.getName().equals(WebserviceUtil.TREE_FAM_NAME)) { phylogeny.setRerootable(false); phylogeny.setRooted(true); } - if (client.getProcessingInstructions() != null) + if (treeDbClient.getProcessingInstructions() != null) { try { - WebserviceUtil.processInstructions(client, phylogeny); + WebserviceUtil.processInstructions(treeDbClient, phylogeny); } catch (final PhyloXmlDataFormatException e) { JvOptionPane.showInternalMessageDialog(Desktop.desktop, @@ -371,12 +372,12 @@ public final class AptxInit JvOptionPane.ERROR_MESSAGE); } } - if (client.getNodeField() != null) + if (treeDbClient.getNodeField() != null) { try { PhylogenyMethods.transferNodeNameToField(phylogeny, - client.getNodeField(), false); + treeDbClient.getNodeField(), false); } catch (final PhyloXmlDataFormatException e) { JvOptionPane.showInternalMessageDialog(Desktop.desktop, @@ -385,7 +386,7 @@ public final class AptxInit } } phylogeny.setIdentifier( - new Identifier(identifier, client.getName())); + new Identifier(identifier, treeDbClient.getName())); // _main_frame.getJMenuBar().remove(_main_frame.getHelpMenu()); // _main_frame.getMenuBarOfMainFrame() // .add(_main_frame.getHelpMenu()); @@ -414,8 +415,8 @@ public final class AptxInit my_name_for_file = new String( sb.toString().replaceAll(" ", "_")); } - // _main_frame.getMainPanel().getCurrentTreePanel() - // .setTreeFile(new File(my_name_for_file)); + aptxApp.getMainPanel().getCurrentTreePanel() + .setTreeFile(new File(my_name_for_file)); AptxUtil.lookAtSomeTreePropertiesForAptxControlSettings( phylogeny, aptxApp.getMainPanel().getControlPanel(), APTX_CONFIG); @@ -425,7 +426,7 @@ public final class AptxInit } } } - else if (!exception) + else if (!exception) // ..what? { JvOptionPane.showMessageDialog(null, ForesterUtil.wordWrap( @@ -469,26 +470,38 @@ public final class AptxInit final Phylogeny aptxTree, final AlignmentViewport jalviewAlignport, String treeTitle) { + if (APTX_CONFIG == null || APTX_CONFIG.isCouldReadConfigFile() == false) + { + int keepGoing = JvOptionPane.showConfirmDialog(Desktop.desktop, + MessageManager.getString("label.aptx_config_not_found"), + MessageManager.formatMessage("label.couldnt_locate", + new String[] + { "_aptx_jalview_configuration_file" }), + JvOptionPane.YES_NO_CANCEL_OPTION); + + if (keepGoing == JvOptionPane.CANCEL_OPTION + || keepGoing == JvOptionPane.CLOSED_OPTION + || keepGoing == JvOptionPane.NO_OPTION) + { + return null; + } + + } MainFrame aptxApp = Archaeopteryx.createApplication(aptxTree, APTX_CONFIG, treeTitle); - // addPartitioningSlider(aptxApp); - LoadedTreeAssociation bindAptxNodes = new LoadedTreeAssociation( - jalviewAlignport.getAlignment().getSequencesArray(), aptxTree); + LoadedTreeSequenceAssociation bindAptxNodes = new LoadedTreeSequenceAssociation( + jalviewAlignport.getAlignment().getSequencesArray(), aptxTree); bindAptxNodes.associateLeavesToSequences(); + bindNodesToJalviewSequences(aptxApp, jalviewAlignport, bindAptxNodes.getAlignmentWithNodes(), bindAptxNodes.getNodesWithAlignment()); - bindFrameToJalview(aptxApp); + bindTreeViewFrameToJalview(aptxApp); + return aptxApp; } - // private static void addPartitioningSlider(MainFrame aptxFrame) - // { - // JSlider slider = new JSlider(); - // - // - // } public static ExternalTreeViewerBindingI bindNodesToJalviewSequences( final MainFrame aptxApp, @@ -496,26 +509,44 @@ public final class AptxInit final Map alignMappedToNodes, final Map nodesMappedToAlign) { - return new JalviewBinding(aptxApp, jalviewAlignViewport, + JalviewBinding treeBinding = new JalviewBinding(aptxApp, + jalviewAlignViewport, alignMappedToNodes, nodesMappedToAlign); + activeAptx.put(aptxApp, treeBinding); + return treeBinding; } - public static MainFrame bindFrameToJalview(final MainFrame aptxApp) + public static MainFrame bindTreeViewFrameToJalview( + final MainFrame aptxApp) { int width = 400; int height = 550; aptxApp.setMinimumSize(new Dimension(width, height)); // aptxApp.setFont(Desktop.instance.getFont()); // aptxApp.getMainPanel().setFont(Desktop.instance.getFont()); - - Desktop.addInternalFrame(aptxApp, "Archaeopteryx Tree View", true, - width, height, true, true); - + String frameTitle = MessageManager.getString("label.aptx_title"); + File treeFile = aptxApp.getMainPanel().getCurrentTreePanel() + .getTreeFile(); + if (treeFile != null) + { + frameTitle += MessageManager.formatMessage("label.aptx_title_append", + new String[] + { treeFile.getAbsolutePath() }); + } + Desktop.addInternalFrame(aptxApp, frameTitle, true, width, height, true, + true); return aptxApp; } + public static Map getAllAptxFrames() + { + return activeAptx; + } + private static void disableRedundantGuiMenus() + { + } }