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.setBaseFontFamilyName(
56 // Desktop.instance.getFont().getFamily().replaceAll(" ", "_"));
57 // APTX_CONFIG.setBaseFontSize(Desktop.instance.getFont().getSize());
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);
85 Phylogeny aptxPhylogeny = Phylogeny
86 .createInstanceFromNhxString(nhxString);
87 aptxPhylogeny.setName(treeTitle);
89 TreeFrameI aptxFrame = createAptxFrame(aptxPhylogeny, viewport,
92 if (Desktop.instance != null)
94 Desktop.instance.stopLoading();
102 * Refactored from Archaeopteryx.main
107 * @throws IOException
108 * @throws FileNotFoundException
110 public static TreeFrameI[] createInstancesFromFile(File treeFile,
111 AlignmentViewport viewport)
112 throws FileNotFoundException, IOException
114 TreeFrameI[] aptxFrames = null;
115 if (UtilityMethods.canForesterReadFile(treeFile))
118 if (Desktop.instance != null)
120 Desktop.instance.startLoading(treeFile.getCanonicalPath());
122 boolean nhx_or_nexus = false;
123 final PhylogenyParser parser = ParserUtils
124 .createParserDependingOnFileType(treeFile,
125 VALIDATE_PHYLOXML_XSD);
126 if (parser instanceof NHXParser)
129 final NHXParser nhx = (NHXParser) parser;
130 nhx.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
131 nhx.setIgnoreQuotes(false);
132 nhx.setTaxonomyExtraction(TAXONOMY_EXTRACTION);
134 else if (parser instanceof NexusPhylogeniesParser)
137 final NexusPhylogeniesParser nex = (NexusPhylogeniesParser) parser;
138 nex.setReplaceUnderscores(REPLACE_NHX_UNDERSCORES);
139 nex.setIgnoreQuotes(false);
141 else if (parser instanceof PhyloXmlParser)
143 if (VALIDATE_PHYLOXML_XSD == false)
145 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
146 MessageManager.getString("error.phyloxml_validation"),
147 MessageManager.getString("label.file_open_error"),
148 JvOptionPane.WARNING_MESSAGE);
151 Phylogeny[] trees = PhylogenyMethods.readPhylogenies(parser,
153 aptxFrames = new TreeFrameI[trees.length];
155 for (int i = 0; i < trees.length; i++)
157 Phylogeny aptxPhylogeny = trees[i];
159 if (nhx_or_nexus && INTERNAL_NUMBERS_AS_CONFIDENCE)
162 .transferInternalNodeNamesToConfidence(aptxPhylogeny, "");
164 String treeTitle = treeFile.getName() + "[" + i + "]";
165 aptxPhylogeny.setName(treeTitle);
166 aptxFrames[i] = createAptxFrame(aptxPhylogeny, viewport, treeTitle);
169 if (Desktop.instance != null)
171 Desktop.instance.stopLoading();
178 public static TreeFrameI[] createInstancesFromFile(
180 AlignmentViewport viewport)
181 throws FileNotFoundException, IOException
183 File treeFile = new File(filePath);
184 return createInstancesFromFile(treeFile, viewport);
188 public static TreeFrameI[] createInstancesFromUrl(URL treeUrl,
189 AlignmentViewport viewport)
190 throws FileNotFoundException, IOException, RuntimeException
193 String treeTitle = treeUrl.getFile();
194 if (Desktop.instance != null)
196 Desktop.instance.startLoading(treeTitle);
198 Phylogeny[] trees = AptxUtil.readPhylogeniesFromUrl(treeUrl,
199 VALIDATE_PHYLOXML_XSD,
200 REPLACE_NHX_UNDERSCORES, INTERNAL_NUMBERS_AS_CONFIDENCE,
201 TAXONOMY_EXTRACTION, MIDPOINT_REROOT);
203 TreeFrameI[] aptxFrames = new TreeFrameI[trees.length];
204 for (int i = 0; i < trees.length; i++)
206 Phylogeny aptxTree = trees[i];
207 aptxFrames[i] = createAptxFrame(aptxTree, viewport, treeTitle);
210 if (Desktop.instance != null)
212 Desktop.instance.stopLoading();
220 * Refactored from Forester's UrlTreeReader, this can be more efficient
222 * @param databaseIndex
226 public static TreeFrameI[] createInstancesFromDb(
227 PhylogeniesWebserviceClient treeDbClient, String identifier,
228 AlignmentViewport viewport)
232 Phylogeny[] trees = null;
233 TreeFrameI[] aptxFrames = null;
235 if ((identifier != null) && (identifier.trim().length() > 0))
237 if (Desktop.instance != null)
239 Desktop.instance.startLoading(identifier);
242 identifier = identifier.trim();
243 if (treeDbClient.isQueryInteger())
245 identifier = identifier.replaceAll("^\\D+", "");
250 id = Integer.parseInt(identifier);
251 } catch (final NumberFormatException e)
253 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
254 MessageManager.formatMessage(
255 "error.database_id_has_letters", new String[]
257 MessageManager.getString("label.invalid_url"),
258 JvOptionPane.ERROR_MESSAGE);
259 return new TreeFrameI[0];
261 identifier = id + "";
263 boolean exception = false;
266 String url_str = treeDbClient.getUrl();
267 url_str = url_str.replaceFirst(
268 PhylogeniesWebserviceClient.QUERY_PLACEHOLDER, identifier);
269 url = new URL(url_str);
270 PhylogenyParser parser = null;
271 switch (treeDbClient.getReturnFormat())
273 case TOL_XML_RESPONSE:
274 parser = new TolParser();
277 parser = new NexusPhylogeniesParser();
278 ((NexusPhylogeniesParser) parser).setReplaceUnderscores(true);
281 parser = new NexusPhylogeniesParser();
282 ((NexusPhylogeniesParser) parser).setReplaceUnderscores(true);
283 ((NexusPhylogeniesParser) parser)
284 .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
287 parser = new NexusPhylogeniesParser();
288 ((NexusPhylogeniesParser) parser).setReplaceUnderscores(true);
289 ((NexusPhylogeniesParser) parser)
290 .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
293 parser = new NHXParser();
295 .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
296 ((NHXParser) parser).setReplaceUnderscores(true);
297 ((NHXParser) parser).setGuessRootedness(true);
299 case NH_EXTRACT_TAXONOMY:
300 parser = new NHXParser();
301 ((NHXParser) parser).setTaxonomyExtraction(
302 NHXParser.TAXONOMY_EXTRACTION.AGGRESSIVE);
303 ((NHXParser) parser).setReplaceUnderscores(false);
304 ((NHXParser) parser).setGuessRootedness(true);
307 parser = new NHXParser();
308 ((NHXParser) parser).setTaxonomyExtraction(
309 NHXParser.TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT);
310 ((NHXParser) parser).setReplaceUnderscores(false);
311 ((NHXParser) parser).setGuessRootedness(true);
314 parser = new NHXParser();
316 .setTaxonomyExtraction(NHXParser.TAXONOMY_EXTRACTION.NO);
317 ((NHXParser) parser).setReplaceUnderscores(false);
318 ((NHXParser) parser).setGuessRootedness(true);
321 parser = PhyloXmlParser.createPhyloXmlParserXsdValidating();
324 throw new IllegalArgumentException(
325 "unknown format: " + treeDbClient.getReturnFormat());
328 // if (_main_frame.getMainPanel().getCurrentTreePanel() != null)
330 // _main_frame.getMainPanel().getCurrentTreePanel().setWaitCursor();
334 // _main_frame.getMainPanel().setWaitCursor();
336 trees = ForesterUtil.readPhylogeniesFromUrl(url, parser);
337 aptxFrames = new TreeFrameI[trees.length];
338 } catch (final MalformedURLException e)
341 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
342 MessageManager.formatMessage(
343 "exception.unable_to_launch_url", new String[]
345 MessageManager.getString("label.invalid_url"),
346 JvOptionPane.ERROR_MESSAGE);
347 System.err.println(e.getLocalizedMessage());
348 } catch (final IOException e)
351 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
352 "Could not read from " + url + "\n"
353 + e.getLocalizedMessage(),
354 "Failed to read tree from " + treeDbClient.getName() + " for "
356 JvOptionPane.ERROR_MESSAGE);
357 System.err.println(e.getLocalizedMessage());
358 } catch (final NumberFormatException e)
361 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
362 "Could not read from " + url + "\n"
363 + e.getLocalizedMessage(),
364 "Failed to read tree from " + treeDbClient.getName() + " for "
366 JvOptionPane.ERROR_MESSAGE);
367 System.err.println(e.getLocalizedMessage());
368 } catch (final Exception e)
372 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
373 e.getLocalizedMessage(), "Unexpected Exception",
374 JvOptionPane.ERROR_MESSAGE);
375 System.err.println(e.getLocalizedMessage());
377 if ((trees != null) && (trees.length > 0))
380 for (final Phylogeny aptxTree : trees)
382 if (!aptxTree.isEmpty())
384 if (treeDbClient.getName().equals(WebserviceUtil.TREE_FAM_NAME))
386 aptxTree.setRerootable(false);
387 aptxTree.setRooted(true);
389 if (treeDbClient.getProcessingInstructions() != null)
393 WebserviceUtil.processInstructions(treeDbClient,
395 } catch (final PhyloXmlDataFormatException e)
397 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
398 "Error:\n" + e.getLocalizedMessage(), "Error",
399 JvOptionPane.ERROR_MESSAGE);
402 if (treeDbClient.getNodeField() != null)
406 PhylogenyMethods.transferNodeNameToField(aptxTree,
407 treeDbClient.getNodeField(), false);
408 } catch (final PhyloXmlDataFormatException e)
410 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
411 "Error:\n" + e.getLocalizedMessage(), "Error",
412 JvOptionPane.ERROR_MESSAGE);
415 aptxTree.setIdentifier(
416 new Identifier(identifier, treeDbClient.getName()));
417 // _main_frame.getJMenuBar().remove(_main_frame.getHelpMenu());
418 // _main_frame.getMenuBarOfExternalTreeFrameI()
419 // .add(_main_frame.getHelpMenu());
420 // _main_frame.getMainPanel().addExternalTreeIInNewTab(ExternalTreeI,
421 // _main_frame.getConfiguration(),
422 // new File(url.getFile()).getName(), url.toString());
425 TreeFrameI aptxApp = createAptxFrame(aptxTree,
428 String my_name_for_file = "";
429 if (!ForesterUtil.isEmpty(aptxTree.getName()))
431 my_name_for_file = new String(aptxTree.getName())
432 .replaceAll(" ", "_");
434 else if (aptxTree.getIdentifier() != null)
436 final StringBuffer sb = new StringBuffer();
438 .isEmpty(aptxTree.getIdentifier().getProvider()))
440 sb.append(aptxTree.getIdentifier().getProvider());
443 sb.append(aptxTree.getIdentifier().getValue());
444 my_name_for_file = new String(
445 sb.toString().replaceAll(" ", "_"));
447 aptxApp.getTreePanel()
448 .setTreeFile(new File(my_name_for_file));
449 // AptxUtil.lookAtSomeTreePropertiesForAptxControlSettings(
450 // aptxTree, aptxApp.getMainPanel().getControlPanel(),
452 aptxApp.getTreeControls().displayEntireTree();
454 aptxApp.enableMultipleTrees();
455 aptxFrames[i++] = aptxApp;
459 else if (!exception) // ..what?
461 JvOptionPane.showMessageDialog(null,
462 ForesterUtil.wordWrap(
463 "Failed to read in tree(s) from [" + url + "]", 80),
464 "Error", JvOptionPane.ERROR_MESSAGE);
466 if ((trees != null) && (trees.length > 0))
470 JvOptionPane.showMessageDialog(null,
471 ForesterUtil.wordWrap("Successfully read in "
472 + trees.length + " tree(s) from [" + url + "]",
474 "Success", JvOptionPane.INFORMATION_MESSAGE);
475 } catch (final Exception e)
477 // Not important if this fails, do nothing.
483 if (Desktop.instance != null)
485 Desktop.instance.stopLoading();
494 public static TreeFrameI createAptxFrame(TreeI aptxTree,
495 AlignmentViewport jalviewAlignport, String treeTitle)
497 validateConfig(APTX_CONFIG);
498 TreeFrameI aptxApp = aptxTree
499 .createTreeViewerFromTree(treeTitle);
500 TreeI jalviewTree = aptxApp.getTree();
501 LoadedTreeAssociationI bindAptxNodes = new LoadedTreeSequenceAssociation(
502 jalviewAlignport.getAlignment().getSequencesArray(),
504 bindAptxNodes.associateNodesToSequences();
506 TreeViewerUtils.associateNodesWithJalviewSequences(aptxApp, jalviewAlignport,
507 bindAptxNodes.getAlignmentWithNodes(),
508 bindAptxNodes.getNodesWithAlignment());
509 TreeViewerUtils.addTreeViewFrameToJalview(aptxApp);
511 // adaptAptxGui(aptxApp); //moved to AptxFrame
516 protected static TreeFrameI createAptxFrame(
517 final Phylogeny aptxTree,
518 final AlignmentViewport jalviewAlignport, String treeTitle)
520 validateConfig(APTX_CONFIG);
521 TreeFrameI aptxApp = new AptxFrame(aptxTree, APTX_CONFIG,
523 TreeI jalviewTree = aptxApp.getTree();
524 LoadedTreeAssociationI bindAptxNodes = new LoadedTreeSequenceAssociation(
525 jalviewAlignport.getAlignment().getSequencesArray(),
527 bindAptxNodes.associateNodesToSequences();
529 TreeViewerUtils.associateNodesWithJalviewSequences(aptxApp, jalviewAlignport,
530 bindAptxNodes.getAlignmentWithNodes(),
531 bindAptxNodes.getNodesWithAlignment());
532 TreeViewerUtils.addTreeViewFrameToJalview(aptxApp);
538 private static boolean validateConfig(Configuration aptxConfig)
540 if (aptxConfig == null || aptxConfig.isCouldReadConfigFile() == false)
542 int keepGoing = JvOptionPane.showConfirmDialog(Desktop.desktop,
543 MessageManager.getString("label.aptx_config_not_found"),
544 MessageManager.formatMessage("label.couldnt_locate",
546 { "_aptx_jalview_configuration_file" }),
547 JvOptionPane.YES_NO_CANCEL_OPTION);
549 if (keepGoing == JvOptionPane.CANCEL_OPTION
550 || keepGoing == JvOptionPane.CLOSED_OPTION
551 || keepGoing == JvOptionPane.NO_OPTION)