1 package jalview.ext.archaeopteryx;
3 import jalview.analysis.TreeBuilder;
4 import jalview.ext.forester.io.UtilityMethods;
5 import jalview.ext.treeviewer.LoadedTreeAssociationI;
6 import jalview.ext.treeviewer.LoadedTreeSequenceAssociation;
7 import jalview.ext.treeviewer.TreeBuilderI;
8 import jalview.ext.treeviewer.TreeFrameI;
9 import jalview.ext.treeviewer.TreeI;
10 import jalview.ext.treeviewer.TreeViewerUtils;
11 import jalview.gui.Desktop;
12 import jalview.gui.JvOptionPane;
13 import jalview.util.MessageManager;
14 import jalview.viewmodel.AlignmentViewport;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.MalformedURLException;
23 import org.forester.archaeopteryx.AptxUtil;
24 import org.forester.archaeopteryx.Configuration;
25 import org.forester.archaeopteryx.webservices.PhylogeniesWebserviceClient;
26 import org.forester.archaeopteryx.webservices.WebserviceUtil;
27 import org.forester.io.parsers.PhylogenyParser;
28 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
29 import org.forester.io.parsers.nhx.NHXParser;
30 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
31 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
32 import org.forester.io.parsers.tol.TolParser;
33 import org.forester.io.parsers.util.ParserUtils;
34 import org.forester.phylogeny.Phylogeny;
35 import org.forester.phylogeny.PhylogenyMethods;
36 import org.forester.phylogeny.data.Identifier;
37 import org.forester.util.ForesterUtil;
40 * Static class for creating Archaeopteryx tree viewer instances from calculated
41 * trees and letting them be bound to Jalview.
43 * @author kjvanderheide
46 public final class AptxInit
48 public final static InputStream CONFIG_LOC = AptxInit.class
50 .getResourceAsStream("/_aptx_jalview_configuration_file.txt");
52 public final static Configuration APTX_CONFIG = new Configuration(
57 // APTX_CONFIG.setBaseFontFamilyName(
58 // Desktop.instance.getFont().getFamily().replaceAll(" ", "_"));
59 // APTX_CONFIG.setBaseFontSize(Desktop.instance.getFont().getSize());
62 private final static boolean VALIDATE_PHYLOXML_XSD = APTX_CONFIG
63 .isValidatePhyloXmlAgainstSchema();
65 private final static boolean REPLACE_NHX_UNDERSCORES = APTX_CONFIG
66 .isReplaceUnderscoresInNhParsing();
68 private final static boolean INTERNAL_NUMBERS_AS_CONFIDENCE = APTX_CONFIG
69 .isInternalNumberAreConfidenceForNhParsing();
71 private final static boolean MIDPOINT_REROOT = APTX_CONFIG
74 private final static NHXParser.TAXONOMY_EXTRACTION TAXONOMY_EXTRACTION = APTX_CONFIG
75 .getTaxonomyExtraction();
82 public static TreeFrameI createInstanceFromCalculation(
83 final TreeBuilder calculatedTree)
85 TreeBuilderI aptxTreeBuilder = new AptxTreeBuilder(
88 TreeI aptxTree = aptxTreeBuilder.buildTree();
90 TreeFrameI aptxApp = createAptxFrame(aptxTree,
91 calculatedTree.getAvport(), null);
98 * Refactored from Archaeopteryx.main
103 * @throws IOException
104 * @throws FileNotFoundException
106 public static TreeFrameI[] createInstancesFromFile(
108 AlignmentViewport viewport)
109 throws FileNotFoundException, IOException
111 File treeFile = new File(filePath);
112 TreeFrameI[] aptxFrames = null;
113 if (UtilityMethods.canForesterReadFile(treeFile))
116 if (Desktop.instance != null)
118 Desktop.instance.startLoading(filePath);
120 boolean nhx_or_nexus = false;
121 final PhylogenyParser parser = ParserUtils
122 .createParserDependingOnFileType(
123 treeFile, VALIDATE_PHYLOXML_XSD);
124 if (parser instanceof NHXParser)
127 final NHXParser nhx = (NHXParser) parser;
128 nhx.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
129 nhx.setIgnoreQuotes(false);
130 nhx.setTaxonomyExtraction(TAXONOMY_EXTRACTION);
132 else if (parser instanceof NexusPhylogeniesParser)
135 final NexusPhylogeniesParser nex = (NexusPhylogeniesParser) parser;
136 nex.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
137 nex.setIgnoreQuotes(false);
139 else if (parser instanceof PhyloXmlParser)
141 if (VALIDATE_PHYLOXML_XSD == false)
143 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
144 MessageManager.getString("error.phyloxml_validation"),
145 MessageManager.getString("label.file_open_error"),
146 JvOptionPane.WARNING_MESSAGE );
149 Phylogeny[] trees = PhylogenyMethods.readPhylogenies(parser,
151 aptxFrames = new TreeFrameI[trees.length];
154 for (int i = 0; i < trees.length; i++)
156 Phylogeny aptxPhylogeny = trees[i];
158 if (nhx_or_nexus && INTERNAL_NUMBERS_AS_CONFIDENCE)
160 PhylogenyMethods.transferInternalNodeNamesToConfidence(aptxPhylogeny,
163 String treeTitle = treeFile.getName() + "[" + i + "]";
164 aptxPhylogeny.setName(treeTitle);
165 aptxFrames[i] = createAptxFrame(aptxPhylogeny, viewport, treeTitle);
168 if (Desktop.instance != null)
170 Desktop.instance.stopLoading();
177 public static TreeFrameI[] createInstancesFromUrl(URL treeUrl,
178 AlignmentViewport viewport)
179 throws FileNotFoundException, IOException, RuntimeException
182 String treeTitle = treeUrl.getFile();
183 if (Desktop.instance != null)
185 Desktop.instance.startLoading(treeTitle);
187 Phylogeny[] trees = AptxUtil.readPhylogeniesFromUrl(treeUrl,
188 VALIDATE_PHYLOXML_XSD,
189 REPLACE_NHX_UNDERSCORES, INTERNAL_NUMBERS_AS_CONFIDENCE,
190 TAXONOMY_EXTRACTION, MIDPOINT_REROOT);
192 TreeFrameI[] aptxFrames = new TreeFrameI[trees.length];
193 for (int i = 0; i < trees.length; i++)
195 Phylogeny aptxTree = trees[i];
196 aptxFrames[i] = createAptxFrame(aptxTree, viewport, treeTitle);
199 if (Desktop.instance != null)
201 Desktop.instance.stopLoading();
209 * Refactored from Forester's UrlTreeReader, this can be more efficient
211 * @param databaseIndex
215 public static TreeFrameI[] createInstancesFromDb(
216 PhylogeniesWebserviceClient treeDbClient, String identifier,
217 AlignmentViewport viewport)
221 Phylogeny[] trees = null;
222 TreeFrameI[] aptxFrames = null;
224 if ((identifier != null) && (identifier.trim().length() > 0))
226 if (Desktop.instance != null)
228 Desktop.instance.startLoading(identifier);
231 identifier = identifier.trim();
232 if (treeDbClient.isQueryInteger())
234 identifier = identifier.replaceAll("^\\D+", "");
239 id = Integer.parseInt(identifier);
240 } catch (final NumberFormatException e)
242 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
243 MessageManager.formatMessage(
244 "error.database_id_has_letters", new String[]
246 MessageManager.getString("label.invalid_url"),
247 JvOptionPane.ERROR_MESSAGE);
248 return new TreeFrameI[0];
250 identifier = id + "";
252 boolean exception = false;
255 String url_str = treeDbClient.getUrl();
256 url_str = url_str.replaceFirst(
257 PhylogeniesWebserviceClient.QUERY_PLACEHOLDER, identifier);
258 url = new URL(url_str);
259 PhylogenyParser parser = null;
260 switch (treeDbClient.getReturnFormat())
262 case TOL_XML_RESPONSE:
263 parser = new TolParser();
266 parser = new NexusPhylogeniesParser();
267 ((NexusPhylogeniesParser) parser).setReplaceUnderscores(true);
270 parser = new NexusPhylogeniesParser();
271 ((NexusPhylogeniesParser) parser).setReplaceUnderscores(true);
272 ((NexusPhylogeniesParser) parser)
273 .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
276 parser = new NexusPhylogeniesParser();
277 ((NexusPhylogeniesParser) parser).setReplaceUnderscores(true);
278 ((NexusPhylogeniesParser) parser)
279 .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
282 parser = new NHXParser();
284 .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
285 ((NHXParser) parser).setReplaceUnderscores(true);
286 ((NHXParser) parser).setGuessRootedness(true);
288 case NH_EXTRACT_TAXONOMY:
289 parser = new NHXParser();
290 ((NHXParser) parser).setTaxonomyExtraction(
291 NHXParser.TAXONOMY_EXTRACTION.AGGRESSIVE);
292 ((NHXParser) parser).setReplaceUnderscores(false);
293 ((NHXParser) parser).setGuessRootedness(true);
296 parser = new NHXParser();
297 ((NHXParser) parser).setTaxonomyExtraction(
298 NHXParser.TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT);
299 ((NHXParser) parser).setReplaceUnderscores(false);
300 ((NHXParser) parser).setGuessRootedness(true);
303 parser = new NHXParser();
305 .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
306 ((NHXParser) parser).setReplaceUnderscores(false);
307 ((NHXParser) parser).setGuessRootedness(true);
310 parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
313 throw new IllegalArgumentException(
314 "unknown format: " + treeDbClient.getReturnFormat());
317 // if (_main_frame.getMainPanel().getCurrentTreePanel() != null)
319 // _main_frame.getMainPanel().getCurrentTreePanel().setWaitCursor();
323 // _main_frame.getMainPanel().setWaitCursor();
325 trees = ForesterUtil.readPhylogeniesFromUrl(url, parser);
326 aptxFrames = new TreeFrameI[trees.length];
327 } catch (final MalformedURLException e)
330 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
331 MessageManager.formatMessage(
332 "exception.unable_to_launch_url", new String[]
334 MessageManager.getString("label.invalid_url"),
335 JvOptionPane.ERROR_MESSAGE);
336 System.err.println(e.getLocalizedMessage());
337 } catch (final IOException e)
340 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
341 "Could not read from " + url + "\n"
342 + e.getLocalizedMessage(),
343 "Failed to read tree from " + treeDbClient.getName() + " for "
345 JvOptionPane.ERROR_MESSAGE);
346 System.err.println(e.getLocalizedMessage());
347 } catch (final NumberFormatException e)
350 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
351 "Could not read from " + url + "\n"
352 + e.getLocalizedMessage(),
353 "Failed to read tree from " + treeDbClient.getName() + " for "
355 JvOptionPane.ERROR_MESSAGE);
356 System.err.println(e.getLocalizedMessage());
357 } catch (final Exception e)
361 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
362 e.getLocalizedMessage(), "Unexpected Exception",
363 JvOptionPane.ERROR_MESSAGE);
364 System.err.println(e.getLocalizedMessage());
366 if ((trees != null) && (trees.length > 0))
369 for (final Phylogeny aptxTree : trees)
371 if (!aptxTree.isEmpty())
373 if (treeDbClient.getName().equals(WebserviceUtil.TREE_FAM_NAME))
375 aptxTree.setRerootable(false);
376 aptxTree.setRooted(true);
378 if (treeDbClient.getProcessingInstructions() != null)
382 WebserviceUtil.processInstructions(treeDbClient,
384 } catch (final PhyloXmlDataFormatException e)
386 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
387 "Error:\n" + e.getLocalizedMessage(), "Error",
388 JvOptionPane.ERROR_MESSAGE);
391 if (treeDbClient.getNodeField() != null)
395 PhylogenyMethods.transferNodeNameToField(aptxTree,
396 treeDbClient.getNodeField(), false);
397 } catch (final PhyloXmlDataFormatException e)
399 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
400 "Error:\n" + e.getLocalizedMessage(), "Error",
401 JvOptionPane.ERROR_MESSAGE);
404 aptxTree.setIdentifier(
405 new Identifier(identifier, treeDbClient.getName()));
406 // _main_frame.getJMenuBar().remove(_main_frame.getHelpMenu());
407 // _main_frame.getMenuBarOfExternalTreeFrameI()
408 // .add(_main_frame.getHelpMenu());
409 // _main_frame.getMainPanel().addExternalTreeIInNewTab(ExternalTreeI,
410 // _main_frame.getConfiguration(),
411 // new File(url.getFile()).getName(), url.toString());
414 TreeFrameI aptxApp = createAptxFrame(aptxTree,
417 String my_name_for_file = "";
418 if (!ForesterUtil.isEmpty(aptxTree.getName()))
420 my_name_for_file = new String(aptxTree.getName())
421 .replaceAll(" ", "_");
423 else if (aptxTree.getIdentifier() != null)
425 final StringBuffer sb = new StringBuffer();
427 .isEmpty(aptxTree.getIdentifier().getProvider()))
429 sb.append(aptxTree.getIdentifier().getProvider());
432 sb.append(aptxTree.getIdentifier().getValue());
433 my_name_for_file = new String(
434 sb.toString().replaceAll(" ", "_"));
436 aptxApp.getTreePanel()
437 .setTreeFile(new File(my_name_for_file));
438 // AptxUtil.lookAtSomeTreePropertiesForAptxControlSettings(
439 // aptxTree, aptxApp.getMainPanel().getControlPanel(),
441 aptxApp.getTreeControls().displayEntireTree();
443 aptxApp.checkMultipleTrees();
444 aptxFrames[i++] = aptxApp;
448 else if (!exception) // ..what?
450 JvOptionPane.showMessageDialog(null,
451 ForesterUtil.wordWrap(
452 "Failed to read in tree(s) from [" + url + "]", 80),
453 "Error", JvOptionPane.ERROR_MESSAGE);
455 if ((trees != null) && (trees.length > 0))
459 JvOptionPane.showMessageDialog(null,
460 ForesterUtil.wordWrap("Successfully read in "
461 + trees.length + " tree(s) from [" + url + "]",
463 "Success", JvOptionPane.INFORMATION_MESSAGE);
464 } catch (final Exception e)
466 // Not important if this fails, do nothing.
468 // _main_frame.getContentPane().repaint();
475 if (Desktop.instance != null)
477 Desktop.instance.stopLoading();
486 public static TreeFrameI createAptxFrame(TreeI aptxTree,
487 AlignmentViewport jalviewAlignport, String treeTitle)
489 validateConfig(APTX_CONFIG);
490 TreeFrameI aptxApp = aptxTree
491 .createTreeViewerFromTree(treeTitle);
492 TreeI jalviewTree = aptxApp.getTree();
493 LoadedTreeAssociationI bindAptxNodes = new LoadedTreeSequenceAssociation(
494 jalviewAlignport.getAlignment().getSequencesArray(),
496 bindAptxNodes.associateLeavesToSequences();
498 TreeViewerUtils.associateNodesWithJalviewSequences(aptxApp, jalviewAlignport,
499 bindAptxNodes.getAlignmentWithNodes(),
500 bindAptxNodes.getNodesWithAlignment());
501 TreeViewerUtils.addTreeViewFrameToJalview(aptxApp);
503 // adaptAptxGui(aptxApp); //moved to AptxFrame
508 protected static TreeFrameI createAptxFrame(
509 final Phylogeny aptxTree,
510 final AlignmentViewport jalviewAlignport, String treeTitle)
512 validateConfig(APTX_CONFIG);
513 TreeFrameI aptxApp = new AptxFrame(aptxTree, APTX_CONFIG,
515 TreeI jalviewTree = aptxApp.getTree();
516 LoadedTreeAssociationI bindAptxNodes = new LoadedTreeSequenceAssociation(
517 jalviewAlignport.getAlignment().getSequencesArray(),
519 bindAptxNodes.associateLeavesToSequences();
521 TreeViewerUtils.associateNodesWithJalviewSequences(aptxApp, jalviewAlignport,
522 bindAptxNodes.getAlignmentWithNodes(),
523 bindAptxNodes.getNodesWithAlignment());
524 TreeViewerUtils.addTreeViewFrameToJalview(aptxApp);
526 // adaptAptxGui(aptxApp); //moved to AptxFrame
531 private static boolean validateConfig(Configuration aptxConfig)
533 if (aptxConfig == null || aptxConfig.isCouldReadConfigFile() == false)
535 int keepGoing = JvOptionPane.showConfirmDialog(Desktop.desktop,
536 MessageManager.getString("label.aptx_config_not_found"),
537 MessageManager.formatMessage("label.couldnt_locate",
539 { "_aptx_jalview_configuration_file" }),
540 JvOptionPane.YES_NO_CANCEL_OPTION);
542 if (keepGoing == JvOptionPane.CANCEL_OPTION
543 || keepGoing == JvOptionPane.CLOSED_OPTION
544 || keepGoing == JvOptionPane.NO_OPTION)