public static Phylogeny generateRandomTree(int size, boolean randomNames) { Random random = new Random(); List<PhylogenyNode> nodes = new ArrayList<>(); for (int i = 0; i < size; i++) { PhylogenyNode newNode = new PhylogenyNode(); newNode.setName(i + ""); nodes.add(newNode); } while (nodes.size() > 1) { int i = random.nextInt(nodes.size()); PhylogenyNode node1 = nodes.get(i); nodes.remove(i); int j = random.nextInt(nodes.size()); PhylogenyNode node2 = nodes.get(j); nodes.remove(j); PhylogenyNode newNode = new PhylogenyNode(); newNode.setChild1(node1); newNode.setChild2(node2); nodes.add(newNode); newNode.setName(newNode.getId() + ""); } Phylogeny tree = new Phylogeny(); tree.setRoot(nodes.get(0)); if (!randomNames) { renameTreeLeavesLeftToRight(tree); } return tree; }
public static Phylogeny generatePerfectTree(int size, boolean randomNames) { Phylogeny tree = new Phylogeny(); int currentTreeSize = 0; Queue<PhylogenyNode> currentLeaves = new LinkedList<>(); PhylogenyNode root = new PhylogenyNode(); tree.setRoot(root); currentLeaves.add(root); currentTreeSize++; while (currentTreeSize < size) { PhylogenyNode currentNode = currentLeaves.poll(); PhylogenyNode child1 = new PhylogenyNode(); PhylogenyNode child2 = new PhylogenyNode(); currentNode.setChild1(child1); currentNode.setChild2(child2); currentLeaves.add(child1); currentLeaves.add(child2); currentTreeSize++; } if (randomNames) renameTreeLeavesRandomly(tree); else renameTreeLeavesLeftToRight(tree); return tree; }
public static Phylogeny generateBaseCaseTree(int size, boolean randomNames) { Random random = new Random(); List<PhylogenyNode> nodes = new ArrayList<>(); for (int i = 0; i < size; i++) { PhylogenyNode newNode = new PhylogenyNode(); newNode.setName(i + ""); nodes.add(newNode); } Phylogeny tree = new Phylogeny(); PhylogenyNode root = new PhylogenyNode(); tree.setRoot(root); PhylogenyNode currentNode = root; while (nodes.size() > 2) { int i = random.nextInt(nodes.size()); PhylogenyNode leaf = nodes.get(i); nodes.remove(i); currentNode.setChild1(leaf); PhylogenyNode newInternalNode = new PhylogenyNode(); currentNode.setChild2(newInternalNode); currentNode = newInternalNode; } currentNode.setChild1(nodes.get(0)); currentNode.setChild2(nodes.get(1)); if (!randomNames) { renameTreeLeavesLeftToRight(tree); } return tree; }
public final Phylogeny execute(final BasicSymmetricalDistanceMatrix distance) { reset(distance); final Phylogeny phylogeny = new Phylogeny(); while (_n > 2) { // Calculates the minimal distance. // If more than one minimal distances, always the first found is used updateM(); final int otu1 = _min_i; final int otu2 = _min_j; // System.out.println( _min_i + " " + _min_j ); // It is a condition that otu1 < otu2. final PhylogenyNode node = new PhylogenyNode(); final double d = _d_values[_mappings[otu1]][_mappings[otu2]]; final double d1 = (d / 2) + ((_r[otu1] - _r[otu2]) / (2 * (_n - 2))); final double d2 = d - d1; if (_df == null) { getExternalPhylogenyNode(otu1).setDistanceToParent(d1); getExternalPhylogenyNode(otu2).setDistanceToParent(d2); } else { // yes, yes, slow but only grows with n (and not n^2 or worse)... getExternalPhylogenyNode(otu1).setDistanceToParent(Double.parseDouble(_df.format(d1))); getExternalPhylogenyNode(otu2).setDistanceToParent(Double.parseDouble(_df.format(d2))); } node.addAsChild(getExternalPhylogenyNode(otu1)); node.addAsChild(getExternalPhylogenyNode(otu2)); if (_verbose) { printProgress(otu1, otu2); } calculateDistancesFromNewNode(otu1, otu2, d); _external_nodes[_mappings[otu1]] = node; updateMappings(otu2); --_n; } final double d = _d_values[_mappings[0]][_mappings[1]] / 2; if (_df == null) { getExternalPhylogenyNode(0).setDistanceToParent(d); getExternalPhylogenyNode(1).setDistanceToParent(d); } else { final double dd = Double.parseDouble(_df.format(d)); getExternalPhylogenyNode(0).setDistanceToParent(dd); getExternalPhylogenyNode(1).setDistanceToParent(dd); } final PhylogenyNode root = new PhylogenyNode(); root.addAsChild(getExternalPhylogenyNode(0)); root.addAsChild(getExternalPhylogenyNode(1)); if (_verbose) { printProgress(0, 1); } phylogeny.setRoot(root); phylogeny.setRooted(false); return phylogeny; }
public static Pair<Phylogeny, Phylogeny> generateIdenticalRandomTrees( int size, boolean randomNames) { Random random = new Random(); List<PhylogenyNode> t1Nodes = new ArrayList<>(); List<PhylogenyNode> t2Nodes = new ArrayList<>(); for (int i = 0; i < size; i++) { PhylogenyNode t1NewNode = new PhylogenyNode(); PhylogenyNode t2NewNode = new PhylogenyNode(); t1NewNode.setName(i + ""); t2NewNode.setName(i + ""); t1Nodes.add(t1NewNode); t2Nodes.add(t2NewNode); } while (t1Nodes.size() > 1) { int i = random.nextInt(t1Nodes.size()); PhylogenyNode t1Child1 = t1Nodes.get(i); PhylogenyNode t2Child1 = t2Nodes.get(i); t1Nodes.remove(i); t2Nodes.remove(i); int j = random.nextInt(t1Nodes.size()); PhylogenyNode t1Child2 = t1Nodes.get(j); PhylogenyNode t2Child2 = t2Nodes.get(j); t1Nodes.remove(j); t2Nodes.remove(j); PhylogenyNode t1NewNode = new PhylogenyNode(); PhylogenyNode t2NewNode = new PhylogenyNode(); t1NewNode.setChild1(t1Child1); t1NewNode.setChild2(t1Child2); t2NewNode.setChild1(t2Child1); t2NewNode.setChild2(t2Child2); t1Nodes.add(t1NewNode); t2Nodes.add(t2NewNode); t1NewNode.setName(t1NewNode.getId() + ""); t2NewNode.setName(t2NewNode.getId() + ""); } Phylogeny tree1 = new Phylogeny(); Phylogeny tree2 = new Phylogeny(); tree1.setRoot(t1Nodes.get(0)); tree2.setRoot(t2Nodes.get(0)); if (!randomNames) { renameTreeLeavesLeftToRight(tree1); renameTreeLeavesLeftToRight(tree2); } return new Pair<>(tree1, tree2); }
static SortedMap<PhylogenyNode, Double> setUpExternalCoverageHashMap(final Phylogeny phylogeny) { final SortedMap<PhylogenyNode, Double> external_node_coverage = new TreeMap<PhylogenyNode, Double>(); for (final PhylogenyNodeIterator iter = phylogeny.iteratorExternalForward(); iter.hasNext(); ) { external_node_coverage.put(iter.next(), 0.0); } return external_node_coverage; }
public static void renameTreeLeavesRightToLeft(Phylogeny tree) { List<PhylogenyNode> leaves = tree.getExternalNodes(); int j = 0; for (int i = leaves.size() - 1; i >= 0; i--) { PhylogenyNode currentLeaf = leaves.get(i); currentLeaf.setName(j + ""); j++; } }
private static void renameTreeLeavesLeftToRight(Phylogeny tree) { PhylogenyNodeIterator iterator = tree.iteratorPreorder(); int i = 0; while (iterator.hasNext()) { PhylogenyNode currentNode = iterator.next(); if (currentNode.isExternal()) { currentNode.setName(i + ""); i++; } } }
public static Phylogeny generateTreeExampleH() { Phylogeny tree = new Phylogeny(); PhylogenyNode root = new PhylogenyNode(); PhylogenyNode[] leaves = new PhylogenyNode[9]; for (int i = 1; i < leaves.length; i++) { PhylogenyNode leaf = new PhylogenyNode(); leaf.setName(i + ""); leaves[i] = leaf; } PhylogenyNode NodeI = new PhylogenyNode(); PhylogenyNode NodeJ = new PhylogenyNode(); PhylogenyNode NodeL = new PhylogenyNode(); PhylogenyNode NodeM = new PhylogenyNode(); PhylogenyNode NodeN = new PhylogenyNode(); PhylogenyNode NodeF = new PhylogenyNode(); NodeJ.setChild1(leaves[1]); NodeJ.setChild2(leaves[8]); NodeL.setChild1(leaves[5]); NodeL.setChild2(leaves[6]); NodeN.setChild1(leaves[2]); NodeN.setChild2(leaves[7]); NodeF.setChild1(leaves[3]); NodeF.setChild2(leaves[4]); NodeI.setChild1(NodeJ); NodeI.setChild2(NodeL); NodeM.setChild1(NodeN); NodeM.setChild2(NodeF); root.setChild1(NodeI); root.setChild2(NodeM); tree.setRoot(root); return tree; }
public static Phylogeny generateTreeExampleA() { Phylogeny tree = new Phylogeny(); PhylogenyNode root = new PhylogenyNode(); PhylogenyNode[] leaves = new PhylogenyNode[9]; for (int i = 1; i < leaves.length; i++) { PhylogenyNode leaf = new PhylogenyNode(); leaf.setName(i + ""); leaves[i] = leaf; } PhylogenyNode NodeB = new PhylogenyNode(); PhylogenyNode NodeC = new PhylogenyNode(); PhylogenyNode NodeD = new PhylogenyNode(); PhylogenyNode NodeE = new PhylogenyNode(); PhylogenyNode NodeF = new PhylogenyNode(); PhylogenyNode NodeG = new PhylogenyNode(); NodeC.setChild1(leaves[1]); NodeC.setChild2(leaves[2]); NodeD.setChild1(leaves[3]); NodeD.setChild2(leaves[4]); NodeF.setChild1(leaves[5]); NodeF.setChild2(leaves[6]); NodeG.setChild1(leaves[7]); NodeG.setChild2(leaves[8]); NodeB.setChild1(NodeC); NodeB.setChild2(NodeD); NodeE.setChild1(NodeF); NodeE.setChild2(NodeG); root.setChild1(NodeB); root.setChild2(NodeE); tree.setRoot(root); return tree; }
public static void renameTreeLeavesRandomly(Phylogeny tree) { Random random = new Random(); List<PhylogenyNode> leaves = new ArrayList(); PhylogenyNodeIterator iterator = tree.iteratorPostorder(); while (iterator.hasNext()) { PhylogenyNode node = iterator.next(); if (node.isExternal()) { leaves.add(node); } } int i = 0; while (!leaves.isEmpty()) { int leafIndex = random.nextInt(leaves.size()); PhylogenyNode currentLeaf = leaves.get(leafIndex); currentLeaf.setName(i + ""); leaves.remove(leafIndex); i++; } }
synchronized void readPhylogeniesFromWebservice() { final long start_time = new Date().getTime(); URL url = null; Phylogeny[] trees = null; final WebservicesManager webservices_manager = WebservicesManager.getInstance(); final PhylogeniesWebserviceClient client = webservices_manager.getAvailablePhylogeniesWebserviceClient(_webservice_client_index); String identifier = JOptionPane.showInputDialog( _main_frame, client.getInstructions() + "\n(Reference: " + client.getReference() + ")", client.getDescription(), JOptionPane.QUESTION_MESSAGE); if ((identifier != null) && (identifier.trim().length() > 0)) { identifier = identifier.trim(); if (client.isQueryInteger()) { int id = -1; try { id = Integer.parseInt(identifier); } catch (final NumberFormatException e) { id = -1; } if (id < 1) { JOptionPane.showMessageDialog( _main_frame, "Identifier is expected to be a number", "Can not open URL", JOptionPane.ERROR_MESSAGE); return; } identifier = id + ""; } try { String url_str = client.getUrl(); url_str = url_str.replaceFirst(PhylogeniesWebserviceClient.QUERY_PLACEHOLDER, identifier); url = new URL(url_str); PhylogenyParser parser = null; switch (client.getReturnFormat()) { case TOL_XML_RESPONSE: parser = new TolParser(); break; case NEXUS: parser = new NexusPhylogeniesParser(); ((NexusPhylogeniesParser) parser).setReplaceUnderscores(true); break; case NH: parser = new NHXParser(); ((NHXParser) parser).setTaxonomyExtraction(TAXONOMY_EXTRACTION.NO); ((NHXParser) parser).setReplaceUnderscores(true); ((NHXParser) parser).setGuessRootedness(true); break; case NH_EXTRACT_TAXONOMY: parser = new NHXParser(); ((NHXParser) parser).setTaxonomyExtraction(TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY); ((NHXParser) parser).setReplaceUnderscores(false); ((NHXParser) parser).setGuessRootedness(true); break; case PFAM: parser = new NHXParser(); ((NHXParser) parser).setTaxonomyExtraction(TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY); ((NHXParser) parser).setReplaceUnderscores(false); ((NHXParser) parser).setGuessRootedness(true); break; case NHX: parser = new NHXParser(); ((NHXParser) parser).setTaxonomyExtraction(TAXONOMY_EXTRACTION.NO); ((NHXParser) parser).setReplaceUnderscores(false); ((NHXParser) parser).setGuessRootedness(true); break; case PHYLOXML: parser = new PhyloXmlParser(); break; default: throw new IllegalArgumentException("unknown format: " + client.getReturnFormat()); } if (_main_frame.getMainPanel().getCurrentTreePanel() != null) { _main_frame.getMainPanel().getCurrentTreePanel().setWaitCursor(); } else { _main_frame.getMainPanel().setWaitCursor(); } final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); trees = factory.create(url.openStream(), parser); } catch (final MalformedURLException e) { JOptionPane.showMessageDialog( _main_frame, "Malformed URL: " + url + "\n" + e.getLocalizedMessage(), "Malformed URL", JOptionPane.ERROR_MESSAGE); } catch (final IOException e) { JOptionPane.showMessageDialog( _main_frame, "Could not read from " + url + "\n" + e.getLocalizedMessage(), "Failed to read tree from " + client.getName() + " for " + identifier, JOptionPane.ERROR_MESSAGE); } catch (final NumberFormatException e) { JOptionPane.showMessageDialog( _main_frame, "Could not read from " + url + "\n" + e.getLocalizedMessage(), "Failed to read tree from " + client.getName() + " for " + identifier, JOptionPane.ERROR_MESSAGE); } catch (final Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog( _main_frame, e.getLocalizedMessage(), "Unexpected Exception", JOptionPane.ERROR_MESSAGE); } finally { if (_main_frame.getCurrentTreePanel() != null) { _main_frame.getCurrentTreePanel().setArrowCursor(); } else { _main_frame.getMainPanel().setArrowCursor(); } } if ((trees != null) && (trees.length > 0)) { for (final Phylogeny phylogeny : trees) { if (client.getName().equals(WebserviceUtil.TREE_FAM_NAME)) { phylogeny.setRerootable(false); } if (client.getName().equals(WebserviceUtil.PFAM_NAME)) { phylogeny.setRerootable(false); ForesterUtil.transferInternalNodeNamesToConfidence(phylogeny); } if (client.getProcessingInstructions() != null) { WebserviceUtil.processInstructions(client, phylogeny); } if (client.getNodeField() != null) { ForesterUtil.transferNodeNameToField(phylogeny, client.getNodeField()); } phylogeny.setIdentifier(new Identifier(identifier, client.getName())); _main_frame.getJMenuBar().remove(_main_frame.getHelpMenu()); _main_frame.getMenuBarOfMainFrame().add(_main_frame.getHelpMenu()); _main_frame .getMainPanel() .addPhylogenyInNewTab( phylogeny, _main_frame.getConfiguration(), new File(url.getFile()).getName(), url.toString()); String my_name_for_file = ""; if (!ForesterUtil.isEmpty(phylogeny.getName())) { my_name_for_file = new String(phylogeny.getName()).replaceAll(" ", "_"); } else if (phylogeny.getIdentifier() != null) { final StringBuffer sb = new StringBuffer(); if (!ForesterUtil.isEmpty(phylogeny.getIdentifier().getProvider())) { sb.append(phylogeny.getIdentifier().getProvider()); sb.append("_"); } sb.append(phylogeny.getIdentifier().getValue()); my_name_for_file = new String(sb.toString().replaceAll(" ", "_")); } _main_frame.getMainPanel().getCurrentTreePanel().setTreeFile(new File(my_name_for_file)); Util.lookAtSomeTreePropertiesForAptxControlSettings( phylogeny, _main_frame.getMainPanel().getControlPanel(), _main_frame.getConfiguration()); _main_frame.getMainPanel().getControlPanel().showWhole(); } } _main_frame.getContentPane().repaint(); if (((trees != null) && (trees.length > 0)) && ((new Date().getTime() - start_time) > 20000)) { try { JOptionPane.showMessageDialog( null, ForesterUtil.wordWrap( "Successfully read in " + trees.length + " evolutionry tree(s) from [" + url + "]", 80), "Success", JOptionPane.INFORMATION_MESSAGE); } catch (final Exception e) { // Not important if this fails, do nothing. } _main_frame.getContentPane().repaint(); } } _main_frame.activateSaveAllIfNeeded(); System.gc(); }
public static void main(final String args[]) { ForesterUtil.printProgramInformation(PRG_NAME, PRG_VERSION, PRG_DATE); CommandLineArguments cla = null; try { cla = new CommandLineArguments(args); } catch (final Exception e) { ForesterUtil.fatalError(PRG_NAME, e.getMessage()); } if (cla.isOptionSet(HELP_OPTION_1) || cla.isOptionSet(HELP_OPTION_2)) { System.out.println(); print_help(); System.exit(0); } else if ((args.length < 2) || (cla.getNumberOfNames() < 2) || (cla.getNumberOfNames() > 3)) { System.out.println(); System.out.println("Wrong number of arguments."); System.out.println(); print_help(); System.exit(-1); } final List<String> allowed_options = new ArrayList<String>(); allowed_options.add(STRIP_OPTION); allowed_options.add(SDISE_OPTION); allowed_options.add(MOST_PARSIMONIOUS_OPTION); final String dissallowed_options = cla.validateAllowedOptionsAsString(allowed_options); if (dissallowed_options.length() > 0) { ForesterUtil.fatalError(PRG_NAME, "unknown option(s): " + dissallowed_options); } boolean use_sdise = false; boolean strip = false; boolean most_parsimonous_duplication_model = false; if (cla.isOptionSet(STRIP_OPTION)) { strip = true; } if (cla.isOptionSet(SDISE_OPTION)) { use_sdise = true; } if (cla.isOptionSet(MOST_PARSIMONIOUS_OPTION)) { if (use_sdise) { ForesterUtil.fatalError( PRG_NAME, "Can only use most parsimonious duplication mode with GSDI"); } most_parsimonous_duplication_model = true; } Phylogeny species_tree = null; Phylogeny gene_tree = null; File gene_tree_file = null; File species_tree_file = null; File out_file = null; try { gene_tree_file = cla.getFile(0); species_tree_file = cla.getFile(1); if (cla.getNumberOfNames() == 3) { out_file = cla.getFile(2); } else { out_file = new File(DEFAULT_OUTFILE); } } catch (final IllegalArgumentException e) { ForesterUtil.fatalError(PRG_NAME, "error in command line: " + e.getMessage()); } if (ForesterUtil.isReadableFile(gene_tree_file) != null) { ForesterUtil.fatalError(PRG_NAME, ForesterUtil.isReadableFile(gene_tree_file)); } if (ForesterUtil.isReadableFile(species_tree_file) != null) { ForesterUtil.fatalError(PRG_NAME, ForesterUtil.isReadableFile(species_tree_file)); } if (ForesterUtil.isWritableFile(out_file) != null) { ForesterUtil.fatalError(PRG_NAME, ForesterUtil.isWritableFile(out_file)); } try { final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); final PhylogenyParser pp = ForesterUtil.createParserDependingOnFileType(species_tree_file, true); species_tree = factory.create(species_tree_file, pp)[0]; } catch (final IOException e) { ForesterUtil.fatalError( PRG_NAME, "Failed to read species tree from \"" + gene_tree_file + "\" [" + e.getMessage() + "]"); } try { final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); final PhylogenyParser pp = ForesterUtil.createParserDependingOnFileType(gene_tree_file, true); gene_tree = factory.create(gene_tree_file, pp)[0]; } catch (final IOException e) { ForesterUtil.fatalError( PRG_NAME, "Failed to read gene tree from \"" + gene_tree_file + "\" [" + e.getMessage() + "]"); } gene_tree.setRooted(true); species_tree.setRooted(true); if (!gene_tree.isCompletelyBinary()) { ForesterUtil.fatalError(PRG_NAME, "gene tree is not completely binary."); } if (use_sdise) { if (!species_tree.isCompletelyBinary()) { ForesterUtil.fatalError(PRG_NAME, "species tree is not completely binary."); } } // For timing. // gene_tree = Helper.createBalancedTree( 10 ); // species_tree = Helper.createBalancedTree( 13 ); // species_tree = Helper.createUnbalancedTree( 1024 ); // gene_tree = Helper.createUnbalancedTree( 8192 ); // species_tree = gene_tree.copyTree(); // gene_tree = species_tree.copyTree(); // Helper.numberSpeciesInOrder( species_tree ); // Helper.numberSpeciesInOrder( gene_tree ); // Helper.randomizeSpecies( 1, 8192, gene_tree ); // Helper.intervalNumberSpecies( gene_tree, 4096 ); // Helper.numberSpeciesInDescOrder( gene_tree ); System.out.println(); System.out.println("Strip species tree: " + strip); SDI sdi = null; final long start_time = new Date().getTime(); try { if (use_sdise) { System.out.println(); System.out.println("Using SDIse algorithm."); sdi = new SDIse(gene_tree, species_tree); } else { System.out.println(); System.out.println("Using GSDI algorithm."); System.out.println(); System.out.println( "Use most parsimonous duplication model: " + most_parsimonous_duplication_model); sdi = new GSDI(gene_tree, species_tree, most_parsimonous_duplication_model); } } catch (final Exception e) { ForesterUtil.unexpectedFatalError(PRG_NAME, e); } System.out.println(); System.out.println( "Running time (excluding I/O): " + (new Date().getTime() - start_time) + "ms"); try { final PhylogenyWriter writer = new PhylogenyWriter(); writer.toPhyloXML(out_file, gene_tree, 1); } catch (final IOException e) { ForesterUtil.fatalError( PRG_NAME, "Failed to write to \"" + out_file + "\" [" + e.getMessage() + "]"); } System.out.println(); System.out.println("Successfully wrote resulting gene tree to: " + out_file); System.out.println(); // if ( use_sdise ) { // computeMappingCostL(); // System.out.println( "Mapping cost : " + computeMappingCostL() // ); // } // System.out.println( "Number of duplications : " + getDuplicationsSum() ); if (!use_sdise && !most_parsimonous_duplication_model) { System.out.println( "Number of potential duplications: " + ((GSDI) sdi).getSpeciationOrDuplicationEventsSum()); } if (!use_sdise) { System.out.println("Number speciations : " + ((GSDI) sdi).getSpeciationsSum()); } System.out.println(); } // main( final String args[] )
public static void main(final String args[]) { ForesterUtil.printProgramInformation(PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW); CommandLineArguments cla = null; try { cla = new CommandLineArguments(args); } catch (final Exception e) { ForesterUtil.fatalError(PRG_NAME, e.getMessage()); } if (cla.isOptionSet(HELP_OPTION_1) || cla.isOptionSet(HELP_OPTION_2) || (args.length == 0)) { printHelp(); System.exit(0); } if ((args.length < 3) || (cla.getNumberOfNames() != 2)) { System.out.println(); System.out.println("[" + PRG_NAME + "] incorrect number of arguments"); System.out.println(); printHelp(); System.exit(-1); } final List<String> allowed_options = new ArrayList<String>(); allowed_options.add(MIN_MAPPING_COST_OPTION); allowed_options.add(MIN_DUPS_OPTION); allowed_options.add(MIN_HEIGHT_OPTION); final String dissallowed_options = cla.validateAllowedOptionsAsString(allowed_options); if (dissallowed_options.length() > 0) { ForesterUtil.fatalError(PRG_NAME, "unknown option(s): " + dissallowed_options); } final File outfile = new File("sdir_outfile.xml"); if (outfile.exists()) { ForesterUtil.fatalError(PRG_NAME, "outfile \"" + outfile + "\" already exists"); } final File gene_tree_file = cla.getFile(0); final File species_tree_file = cla.getFile(1); boolean minimize_cost = false; if (cla.isOptionSet(MIN_MAPPING_COST_OPTION)) { minimize_cost = true; } boolean minimize_sum_of_dup = false; if (cla.isOptionSet(MIN_DUPS_OPTION)) { minimize_sum_of_dup = true; } boolean minimize_height = false; if (cla.isOptionSet(MIN_HEIGHT_OPTION)) { minimize_height = true; } int r = 0; Phylogeny[] gene_trees = null; Phylogeny species_tree = null; if (minimize_cost && minimize_sum_of_dup) { minimize_sum_of_dup = false; } final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); try { final PhylogenyParser pp = new PhyloXmlParser(); species_tree = factory.create(species_tree_file, pp)[0]; } catch (final IOException e) { ForesterUtil.fatalError( PRG_NAME, "failed to read species tree [" + species_tree_file + "]: " + e.getLocalizedMessage()); } if (!species_tree.isRooted()) { ForesterUtil.fatalError(PRG_NAME, "species tree [" + species_tree_file + "] is not rooted"); } try { final PhylogenyParser pp = new PhyloXmlParser(); gene_trees = factory.create(gene_tree_file, pp); } catch (final IOException e) { ForesterUtil.fatalError( PRG_NAME, "failed to read gene trees [" + gene_tree_file + "]: " + e.getLocalizedMessage()); } // Removes from gene_tree all species not found in species_tree. int gene_tree_counter = 0; final List<Phylogeny> all_result_trees = new ArrayList<Phylogeny>(); for (final Phylogeny gene_tree : gene_trees) { r = PhylogenyMethods.taxonomyBasedDeletionOfExternalNodes(species_tree, gene_tree); ForesterUtil.programMessage(PRG_NAME, "Removed " + r + " external nodes from gene tree"); final SDIR sdiunrooted = new SDIR(); final long start_time = new Date().getTime(); Phylogeny[] result_trees = null; try { result_trees = sdiunrooted.infer( gene_tree, species_tree, minimize_cost, minimize_sum_of_dup, minimize_height, true, sdi_r.TREES_TO_RETURN); } catch (final Exception e) { ForesterUtil.fatalError(PRG_NAME, e.getLocalizedMessage()); } final long time_req = new Date().getTime() - start_time; if (minimize_cost) { ForesterUtil.programMessage(PRG_NAME, "Rooted by minimizing mapping cost L"); if (minimize_height) { ForesterUtil.programMessage( PRG_NAME, "Selected tree(s) with minimal height out of resulting trees"); } ForesterUtil.programMessage( PRG_NAME, "Number differently rooted trees minimizing criterion : " + sdiunrooted.getCount()); ForesterUtil.programMessage( PRG_NAME, "Minimal cost : " + sdiunrooted.getMinimalMappingCost()); ForesterUtil.programMessage( PRG_NAME, "Minimal duplications : " + sdiunrooted.getMinimalDuplications()); if (minimize_height) { ForesterUtil.programMessage( PRG_NAME, "Phylogeny height : " + ForesterUtil.FORMATTER_06.format(sdiunrooted.getMinimalTreeHeight())); ForesterUtil.programMessage( PRG_NAME, "Difference in subtree heights : " + ForesterUtil.FORMATTER_06.format(sdiunrooted.getMinimalDiffInSubTreeHeights())); } } else if (minimize_sum_of_dup) { ForesterUtil.programMessage(PRG_NAME, "Rooted by minimizing sum of duplications"); if (minimize_height) { ForesterUtil.programMessage( PRG_NAME, "Selected tree(s) with minimal height out of resulting trees"); } ForesterUtil.programMessage( PRG_NAME, "Number differently rooted trees minimizing criterion : " + sdiunrooted.getCount()); ForesterUtil.programMessage( PRG_NAME, "Minimal duplications : " + sdiunrooted.getMinimalDuplications()); if (minimize_height) { ForesterUtil.programMessage( PRG_NAME, "Phylogeny height : " + ForesterUtil.FORMATTER_06.format(sdiunrooted.getMinimalTreeHeight())); ForesterUtil.programMessage( PRG_NAME, "Difference in subtree heights : " + ForesterUtil.FORMATTER_06.format(sdiunrooted.getMinimalDiffInSubTreeHeights())); } } else if (minimize_height) { ForesterUtil.programMessage( PRG_NAME, "Rooted by minimizing tree height (midpoint rooting)."); ForesterUtil.programMessage( PRG_NAME, "Minimal tree height : " + ForesterUtil.FORMATTER_06.format(sdiunrooted.getMinimalTreeHeight())); ForesterUtil.programMessage( PRG_NAME, "Minimal difference in subtree heights: " + ForesterUtil.FORMATTER_06.format(sdiunrooted.getMinimalDiffInSubTreeHeights())); ForesterUtil.programMessage( PRG_NAME, "Duplications in midpoint rooted tree : " + sdiunrooted.getMinimalDuplications()); } else { ForesterUtil.programMessage(PRG_NAME, "No (re) rooting was performed."); ForesterUtil.programMessage( PRG_NAME, "Duplications in tree: " + sdiunrooted.getMinimalDuplications()); } ForesterUtil.programMessage( PRG_NAME, "Time requirement (minus I/O) : " + time_req + "ms"); for (int i = 0; i < result_trees.length; ++i) { final String name = result_trees[i].getName(); if (ForesterUtil.isEmpty(name)) { result_trees[i].setName("SDIR result [gene tree + " + gene_tree_counter + "]" + " " + i); } else { result_trees[i].setName( name + " SDIR result [gene tree + " + gene_tree_counter + "]" + " " + i); } all_result_trees.add(result_trees[i]); } ++gene_tree_counter; } // for( final Phylogeny gene_tree : gene_trees ) try { final PhylogenyWriter w = new PhylogenyWriter(); w.toPhyloXML(outfile, all_result_trees, 0, ForesterUtil.LINE_SEPARATOR); } catch (final IOException e) { ForesterUtil.fatalError( PRG_NAME, "failure to write output to [" + outfile + "]: " + e.getLocalizedMessage()); } ForesterUtil.programMessage(PRG_NAME, "Wrote: " + outfile); ForesterUtil.programMessage(PRG_NAME, "OK."); }