1 package jalview.ext.archaeopteryx;
3 import jalview.ext.forester.io.UtilityMethods;
4 import jalview.ext.treeviewer.LoadedTreeAssociationI;
5 import jalview.ext.treeviewer.LoadedTreeSequenceAssociation;
6 import jalview.ext.treeviewer.TreeFrameI;
7 import jalview.ext.treeviewer.TreeI;
8 import jalview.ext.treeviewer.TreeViewerUtils;
9 import jalview.gui.Desktop;
10 import jalview.gui.JvOptionPane;
11 import jalview.util.MessageManager;
12 import jalview.viewmodel.AlignmentViewport;
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.net.MalformedURLException;
21 import org.forester.archaeopteryx.AptxUtil;
22 import org.forester.archaeopteryx.Configuration;
23 import org.forester.archaeopteryx.webservices.PhylogeniesWebserviceClient;
24 import org.forester.archaeopteryx.webservices.WebserviceUtil;
25 import org.forester.io.parsers.PhylogenyParser;
26 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
27 import org.forester.io.parsers.nhx.NHXParser;
28 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
29 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
30 import org.forester.io.parsers.tol.TolParser;
31 import org.forester.io.parsers.util.ParserUtils;
32 import org.forester.phylogeny.Phylogeny;
33 import org.forester.phylogeny.PhylogenyMethods;
34 import org.forester.phylogeny.data.Identifier;
35 import org.forester.util.ForesterUtil;
38 * Static class for creating Archaeopteryx tree viewer instances from calculated
39 * trees and letting them be bound to Jalview.
41 * @author kjvanderheide
44 public final class AptxInit
46 public final static InputStream CONFIG_LOC = AptxInit.class
48 .getResourceAsStream("/_aptx_jalview_configuration_file.txt");
50 public final static Configuration APTX_CONFIG = new Configuration(
55 APTX_CONFIG.setBaseFontSize(Desktop.instance.getFont().getSize());
57 .setBaseFontFamilyName(Desktop.instance.getFont().getFamily());
60 private final static boolean VALIDATE_PHYLOXML_XSD = APTX_CONFIG
61 .isValidatePhyloXmlAgainstSchema();
63 private final static boolean REPLACE_NHX_UNDERSCORES = APTX_CONFIG
64 .isReplaceUnderscoresInNhParsing();
66 private final static boolean INTERNAL_NUMBERS_AS_CONFIDENCE = APTX_CONFIG
67 .isInternalNumberAreConfidenceForNhParsing();
69 private final static boolean MIDPOINT_REROOT = APTX_CONFIG
72 private final static NHXParser.TAXONOMY_EXTRACTION TAXONOMY_EXTRACTION = APTX_CONFIG
73 .getTaxonomyExtraction();
76 public static TreeFrameI createInstanceFromNhx(String treeTitle,
77 String nhxString, AlignmentViewport viewport)
80 if (Desktop.instance != null)
82 Desktop.instance.startLoading(treeTitle);
86 Phylogeny aptxPhylogeny = Phylogeny
87 .createInstanceFromNhxString(nhxString);
88 aptxPhylogeny.setName(treeTitle);
90 TreeFrameI aptxFrame = createAptxFrame(aptxPhylogeny, viewport,
93 if (Desktop.instance != null)
95 Desktop.instance.stopLoading();
103 * Refactored from Archaeopteryx.main
108 * @throws IOException
109 * @throws FileNotFoundException
111 public static TreeFrameI[] createInstancesFromFile(File treeFile,
112 AlignmentViewport viewport)
113 throws FileNotFoundException, IOException
115 TreeFrameI[] aptxFrames = null;
116 if (UtilityMethods.canForesterReadFile(treeFile))
119 if (Desktop.instance != null)
121 Desktop.instance.startLoading(treeFile.getCanonicalPath());
123 boolean nhx_or_nexus = false;
124 final PhylogenyParser parser = ParserUtils
125 .createParserDependingOnFileType(treeFile,
126 VALIDATE_PHYLOXML_XSD);
127 if (parser instanceof NHXParser)
130 final NHXParser nhx = (NHXParser) parser;
131 nhx.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
132 nhx.setIgnoreQuotes(false);
133 nhx.setTaxonomyExtraction(TAXONOMY_EXTRACTION);
135 else if (parser instanceof NexusPhylogeniesParser)
138 final NexusPhylogeniesParser nex = (NexusPhylogeniesParser) parser;
139 nex.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
140 nex.setIgnoreQuotes(false);
142 else if (parser instanceof PhyloXmlParser)
144 if (VALIDATE_PHYLOXML_XSD == false)
146 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
147 MessageManager.getString("error.phyloxml_validation"),
148 MessageManager.getString("label.file_open_error"),
149 JvOptionPane.WARNING_MESSAGE);
152 Phylogeny[] trees = PhylogenyMethods.readPhylogenies(parser,
154 aptxFrames = new TreeFrameI[trees.length];
156 for (int i = 0; i < trees.length; i++)
158 Phylogeny aptxPhylogeny = trees[i];
160 if (nhx_or_nexus && INTERNAL_NUMBERS_AS_CONFIDENCE)
163 .transferInternalNodeNamesToConfidence(aptxPhylogeny, "");
165 String treeTitle = treeFile.getName() + "[" + i + "]";
166 aptxPhylogeny.setName(treeTitle);
167 aptxFrames[i] = createAptxFrame(aptxPhylogeny, viewport, treeTitle);
170 if (Desktop.instance != null)
172 Desktop.instance.stopLoading();
179 public static TreeFrameI[] createInstancesFromFile(
181 AlignmentViewport viewport)
182 throws FileNotFoundException, IOException
184 File treeFile = new File(filePath);
185 return createInstancesFromFile(treeFile, viewport);
189 public static TreeFrameI[] createInstancesFromUrl(URL treeUrl,
190 AlignmentViewport viewport)
191 throws FileNotFoundException, IOException, RuntimeException
194 String treeTitle = treeUrl.getFile();
195 if (Desktop.instance != null)
197 Desktop.instance.startLoading(treeTitle);
199 Phylogeny[] trees = AptxUtil.readPhylogeniesFromUrl(treeUrl,
200 VALIDATE_PHYLOXML_XSD,
201 REPLACE_NHX_UNDERSCORES, INTERNAL_NUMBERS_AS_CONFIDENCE,
202 TAXONOMY_EXTRACTION, MIDPOINT_REROOT);
204 TreeFrameI[] aptxFrames = new TreeFrameI[trees.length];
205 for (int i = 0; i < trees.length; i++)
207 Phylogeny aptxTree = trees[i];
208 aptxFrames[i] = createAptxFrame(aptxTree, viewport, treeTitle);
211 if (Desktop.instance != null)
213 Desktop.instance.stopLoading();
221 * Refactored from Forester's UrlTreeReader, this can be more efficient
223 * @param databaseIndex
227 public static TreeFrameI[] createInstancesFromDb(
228 PhylogeniesWebserviceClient treeDbClient, String identifier,
229 AlignmentViewport viewport)
233 Phylogeny[] trees = null;
234 TreeFrameI[] aptxFrames = null;
236 if ((identifier != null) && (identifier.trim().length() > 0))
238 if (Desktop.instance != null)
240 Desktop.instance.startLoading(identifier);
243 identifier = identifier.trim();
244 if (treeDbClient.isQueryInteger())
246 identifier = identifier.replaceAll("^\\D+", "");
251 id = Integer.parseInt(identifier);
252 } catch (final NumberFormatException e)
254 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
255 MessageManager.formatMessage(
256 "error.database_id_has_letters", new String[]
258 MessageManager.getString("label.invalid_url"),
259 JvOptionPane.ERROR_MESSAGE);
260 return new TreeFrameI[0];
262 identifier = id + "";
264 boolean exception = false;
267 String url_str = treeDbClient.getUrl();
268 url_str = url_str.replaceFirst(
269 PhylogeniesWebserviceClient.QUERY_PLACEHOLDER, identifier);
270 url = new URL(url_str);
271 PhylogenyParser parser = null;
272 switch (treeDbClient.getReturnFormat())
274 case TOL_XML_RESPONSE:
275 parser = new TolParser();
278 parser = new NexusPhylogeniesParser();
279 ((NexusPhylogeniesParser) parser).setReplaceUnderscores(true);
282 parser = new NexusPhylogeniesParser();
283 ((NexusPhylogeniesParser) parser).setReplaceUnderscores(true);
284 ((NexusPhylogeniesParser) parser)
285 .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
288 parser = new NexusPhylogeniesParser();
289 ((NexusPhylogeniesParser) parser).setReplaceUnderscores(true);
290 ((NexusPhylogeniesParser) parser)
291 .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
294 parser = new NHXParser();
296 .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
297 ((NHXParser) parser).setReplaceUnderscores(true);
298 ((NHXParser) parser).setGuessRootedness(true);
300 case NH_EXTRACT_TAXONOMY:
301 parser = new NHXParser();
302 ((NHXParser) parser).setTaxonomyExtraction(
303 NHXParser.TAXONOMY_EXTRACTION.AGGRESSIVE);
304 ((NHXParser) parser).setReplaceUnderscores(false);
305 ((NHXParser) parser).setGuessRootedness(true);
308 parser = new NHXParser();
309 ((NHXParser) parser).setTaxonomyExtraction(
310 NHXParser.TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT);
311 ((NHXParser) parser).setReplaceUnderscores(false);
312 ((NHXParser) parser).setGuessRootedness(true);
315 parser = new NHXParser();
317 .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
318 ((NHXParser) parser).setReplaceUnderscores(false);
319 ((NHXParser) parser).setGuessRootedness(true);
322 parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
325 throw new IllegalArgumentException(
326 "unknown format: " + treeDbClient.getReturnFormat());
329 // if (_main_frame.getMainPanel().getCurrentTreePanel() != null)
331 // _main_frame.getMainPanel().getCurrentTreePanel().setWaitCursor();
335 // _main_frame.getMainPanel().setWaitCursor();
337 trees = ForesterUtil.readPhylogeniesFromUrl(url, parser);
338 aptxFrames = new TreeFrameI[trees.length];
339 } catch (final MalformedURLException e)
342 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
343 MessageManager.formatMessage(
344 "exception.unable_to_launch_url", new String[]
346 MessageManager.getString("label.invalid_url"),
347 JvOptionPane.ERROR_MESSAGE);
348 System.err.println(e.getLocalizedMessage());
349 } catch (final IOException e)
352 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
353 "Could not read from " + url + "\n"
354 + e.getLocalizedMessage(),
355 "Failed to read tree from " + treeDbClient.getName() + " for "
357 JvOptionPane.ERROR_MESSAGE);
358 System.err.println(e.getLocalizedMessage());
359 } catch (final NumberFormatException e)
362 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
363 "Could not read from " + url + "\n"
364 + e.getLocalizedMessage(),
365 "Failed to read tree from " + treeDbClient.getName() + " for "
367 JvOptionPane.ERROR_MESSAGE);
368 System.err.println(e.getLocalizedMessage());
369 } catch (final Exception e)
373 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
374 e.getLocalizedMessage(), "Unexpected Exception",
375 JvOptionPane.ERROR_MESSAGE);
376 System.err.println(e.getLocalizedMessage());
378 if ((trees != null) && (trees.length > 0))
381 for (final Phylogeny aptxTree : trees)
383 if (!aptxTree.isEmpty())
385 if (treeDbClient.getName().equals(WebserviceUtil.TREE_FAM_NAME))
387 aptxTree.setRerootable(false);
388 aptxTree.setRooted(true);
390 if (treeDbClient.getProcessingInstructions() != null)
394 WebserviceUtil.processInstructions(treeDbClient,
396 } catch (final PhyloXmlDataFormatException e)
398 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
399 "Error:\n" + e.getLocalizedMessage(), "Error",
400 JvOptionPane.ERROR_MESSAGE);
403 if (treeDbClient.getNodeField() != null)
407 PhylogenyMethods.transferNodeNameToField(aptxTree,
408 treeDbClient.getNodeField(), false);
409 } catch (final PhyloXmlDataFormatException e)
411 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
412 "Error:\n" + e.getLocalizedMessage(), "Error",
413 JvOptionPane.ERROR_MESSAGE);
416 aptxTree.setIdentifier(
417 new Identifier(identifier, treeDbClient.getName()));
418 // _main_frame.getJMenuBar().remove(_main_frame.getHelpMenu());
419 // _main_frame.getMenuBarOfExternalTreeFrameI()
420 // .add(_main_frame.getHelpMenu());
421 // _main_frame.getMainPanel().addExternalTreeIInNewTab(ExternalTreeI,
422 // _main_frame.getConfiguration(),
423 // new File(url.getFile()).getName(), url.toString());
426 TreeFrameI aptxApp = createAptxFrame(aptxTree,
429 String my_name_for_file = "";
430 if (!ForesterUtil.isEmpty(aptxTree.getName()))
432 my_name_for_file = new String(aptxTree.getName())
433 .replaceAll(" ", "_");
435 else if (aptxTree.getIdentifier() != null)
437 final StringBuffer sb = new StringBuffer();
439 .isEmpty(aptxTree.getIdentifier().getProvider()))
441 sb.append(aptxTree.getIdentifier().getProvider());
444 sb.append(aptxTree.getIdentifier().getValue());
445 my_name_for_file = new String(
446 sb.toString().replaceAll(" ", "_"));
448 aptxApp.getTreePanel()
449 .setTreeFile(new File(my_name_for_file));
450 // AptxUtil.lookAtSomeTreePropertiesForAptxControlSettings(
451 // aptxTree, aptxApp.getMainPanel().getControlPanel(),
453 aptxApp.getTreeControls().displayEntireTree();
455 aptxApp.enableMultipleTrees();
456 aptxFrames[i++] = aptxApp;
460 else if (!exception) // ..what?
462 JvOptionPane.showMessageDialog(null,
463 ForesterUtil.wordWrap(
464 "Failed to read in tree(s) from [" + url + "]", 80),
465 "Error", JvOptionPane.ERROR_MESSAGE);
467 if ((trees != null) && (trees.length > 0))
471 JvOptionPane.showMessageDialog(null,
472 ForesterUtil.wordWrap("Successfully read in "
473 + trees.length + " tree(s) from [" + url + "]",
475 "Success", JvOptionPane.INFORMATION_MESSAGE);
476 } catch (final Exception e)
478 // Not important if this fails, do nothing.
484 if (Desktop.instance != null)
486 Desktop.instance.stopLoading();
495 public static TreeFrameI createAptxFrame(TreeI aptxTree,
496 AlignmentViewport jalviewAlignport, String treeTitle)
498 validateConfig(APTX_CONFIG);
499 TreeFrameI aptxApp = aptxTree
500 .createTreeViewerFromTree(treeTitle);
501 TreeI jalviewTree = aptxApp.getTree();
502 LoadedTreeAssociationI bindAptxNodes = new LoadedTreeSequenceAssociation(
503 jalviewAlignport.getAlignment().getSequencesArray(),
505 bindAptxNodes.associateNodesToSequences();
507 TreeViewerUtils.associateNodesWithJalviewSequences(aptxApp, jalviewAlignport,
508 bindAptxNodes.getAlignmentWithNodes(),
509 bindAptxNodes.getNodesWithAlignment());
510 TreeViewerUtils.addTreeViewFrameToJalview(aptxApp, 400, 500);
512 // adaptAptxGui(aptxApp); //moved to AptxFrame
517 protected static TreeFrameI createAptxFrame(
518 final Phylogeny aptxTree,
519 final AlignmentViewport jalviewAlignport, String treeTitle)
521 validateConfig(APTX_CONFIG);
522 TreeFrameI aptxApp = new AptxFrame(aptxTree, APTX_CONFIG,
524 TreeI jalviewTree = aptxApp.getTree();
525 LoadedTreeAssociationI bindAptxNodes = new LoadedTreeSequenceAssociation(
526 jalviewAlignport.getAlignment().getSequencesArray(),
528 bindAptxNodes.associateNodesToSequences();
530 TreeViewerUtils.associateNodesWithJalviewSequences(aptxApp, jalviewAlignport,
531 bindAptxNodes.getAlignmentWithNodes(),
532 bindAptxNodes.getNodesWithAlignment());
533 TreeViewerUtils.addTreeViewFrameToJalview(aptxApp, 400, 500);
539 private static boolean validateConfig(Configuration aptxConfig)
541 if (aptxConfig == null || aptxConfig.isCouldReadConfigFile() == false)
543 int keepGoing = JvOptionPane.showConfirmDialog(Desktop.desktop,
544 MessageManager.getString("label.aptx_config_not_found"),
545 MessageManager.formatMessage("label.couldnt_locate",
547 { "_aptx_jalview_configuration_file" }),
548 JvOptionPane.YES_NO_CANCEL_OPTION);
550 if (keepGoing == JvOptionPane.CANCEL_OPTION
551 || keepGoing == JvOptionPane.CLOSED_OPTION
552 || keepGoing == JvOptionPane.NO_OPTION)