public static void main(final String args[]) { ForesterUtil.printProgramInformation(nhx_too.PRG_NAME, nhx_too.PRG_VERSION, nhx_too.PRG_DATE); if ((args.length < 3) || (args.length > 3)) { System.out.println(); System.out.println(nhx_too.PRG_NAME + ": wrong number of arguments"); System.out.println(); System.out.println("Usage: \"" + nhx_too.PRG_NAME + " [options] <infile> <outfile>\n"); System.out.println( " Options: -" + nhx_too.INT_NODE_NAME_IS_SUPPORT + ": internal node names are support values (i.e. MrBayes output)"); System.out.println(); System.exit(-1); } CommandLineArguments cla = null; try { cla = new CommandLineArguments(args); } catch (final Exception e) { ForesterUtil.fatalError(PRG_NAME, e.getMessage()); } final List<String> allowed_options = new ArrayList<String>(); allowed_options.add(nhx_too.INT_NODE_NAME_IS_SUPPORT); final String dissallowed_options = cla.validateAllowedOptionsAsString(allowed_options); if (dissallowed_options.length() > 0) { ForesterUtil.fatalError(nhx_too.PRG_NAME, "Unknown option(s): " + dissallowed_options); } final File phylogeny_infile = cla.getFile(0); final File phylogeny_outfile = cla.getFile(1); boolean int_node_name_is_support = false; if (cla.isOptionSet(nhx_too.INT_NODE_NAME_IS_SUPPORT)) { int_node_name_is_support = true; } Phylogeny p = null; try { final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); final PhylogenyParser pp = ForesterUtil.createParserDependingOnFileType(phylogeny_infile, true); p = factory.create(phylogeny_infile, pp)[0]; } catch (final Exception e) { ForesterUtil.fatalError( nhx_too.PRG_NAME, "Could not read \"" + phylogeny_infile + "\" [" + e.getMessage() + "]"); } if (int_node_name_is_support) { try { ForesterUtil.transferInternalNodeNamesToConfidence(p); } catch (final Exception e) { ForesterUtil.unexpectedFatalError( nhx_too.PRG_NAME, "Failure during moving of internal names to support values [" + e.getMessage() + "]"); } } try { final PhylogenyWriter w = new PhylogenyWriter(); w.toNewHampshireX(p, phylogeny_outfile); } catch (final IOException e) { ForesterUtil.fatalError(nhx_too.PRG_NAME, "Failure to write output [" + e.getMessage() + "]"); } System.out.println(); System.out.println("Done [wrote \"" + phylogeny_outfile + "\"]."); System.out.println(); }
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(); }
private static boolean testExternalNodeBasedCoverage() { try { final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); final String ps1 = "((((A:0.1,B:0.7):0.2,C:1.0):2.0,D:1.7):1.3,((E:0.3,F:0.4):1.1,(G:0.5,H:0.6):1.2):1.4,X:2.0)"; final Phylogeny p1 = factory.create(ps1, new NHXParser())[0]; final List<Phylogeny> phylogenies = new ArrayList<Phylogeny>(); final List<String> names = new ArrayList<String>(); phylogenies.add(p1); names.add("A"); names.add("A"); final CoverageCalculationOptions options = new ExternalNodeBasedCoverageMethodOptions( "org.forester.pccx.BranchCountingBasedScoringMethod"); final CoverageCalculator cc = CoverageCalculator.getInstance(new ExternalNodeBasedCoverageMethod(), options); Coverage cov = cc.calculateCoverage(phylogenies, names, false); if (!TestPccx.isEqual( cov.getScore(), (1.0 + 1.0 / 2 + 1.0 / 3 + 1.0 / 4 + 1.0 / 7 + 1.0 / 7 + 1.0 / 7 + 1.0 / 7 + 1.0 / 5) / 9)) { return false; } names.add("B"); names.add("B"); cov = cc.calculateCoverage(phylogenies, names, false); if (!TestPccx.isEqual( cov.getScore(), (1.0 + 1.0 + 1.0 / 3 + 1.0 / 4 + 1.0 / 7 + 1.0 / 7 + 1.0 / 7 + 1.0 / 7 + 1.0 / 5) / 9)) { return false; } names.add("G"); cov = cc.calculateCoverage(phylogenies, names, false); if (!TestPccx.isEqual( cov.getScore(), (1.0 + 1.0 + 1.0 / 3 + 1.0 / 4 + 1.0 / 4 + 1.0 / 4 + 1.0 + 1.0 / 2 + 1.0 / 4) / 9)) { return false; } names.add("E"); cov = cc.calculateCoverage(phylogenies, names, false); if (!TestPccx.isEqual( cov.getScore(), (1.0 + 1.0 + 1.0 / 3 + 1.0 / 4 + 1.0 + 1.0 / 2 + 1.0 + 1.0 / 2 + 1.0 / 4) / 9)) { return false; } names.add("X"); cov = cc.calculateCoverage(phylogenies, names, false); if (!TestPccx.isEqual( cov.getScore(), (1.0 + 1.0 + 1.0 / 3 + 1.0 / 3 + 1.0 + 1.0 / 2 + 1.0 + 1.0 / 2 + 1.0) / 9)) { return false; } names.add("C"); names.add("C"); names.add("C"); cov = cc.calculateCoverage(phylogenies, names, false); if (!TestPccx.isEqual( cov.getScore(), (1.0 + 1.0 + 1.0 + 1.0 / 3 + 1.0 + 1.0 / 2 + 1.0 + 1.0 / 2 + 1.0) / 9)) { return false; } names.add("D"); cov = cc.calculateCoverage(phylogenies, names, false); if (!TestPccx.isEqual( cov.getScore(), (1.0 + 1.0 + 1.0 + 1.0 + 1.0 + 1.0 / 2 + 1.0 + 1.0 / 2 + 1.0) / 9)) { return false; } names.add("F"); cov = cc.calculateCoverage(phylogenies, names, false); if (!TestPccx.isEqual( cov.getScore(), (1.0 + 1.0 + 1.0 + 1.0 + 1.0 + 1.0 + 1.0 + 1.0 / 2 + 1.0) / 9)) { return false; } names.add("H"); cov = cc.calculateCoverage(phylogenies, names, false); if (!TestPccx.isEqual( cov.getScore(), (1.0 + 1.0 + 1.0 + 1.0 + 1.0 + 1.0 + 1.0 + 1.0 + 1.0) / 9)) { return false; } final CoverageExtender ce = new BasicExternalNodeBasedCoverageExtender(); List<String> l = ce.find( phylogenies, null, 0, new ExternalNodeBasedCoverageMethodOptions( "org.forester.pccx.BranchCountingBasedScoringMethod"), null); if (!l.get(0).equals("X")) { return false; } if (!l.get(1).equals("A")) { return false; } if (!l.get(2).equals("E")) { return false; } if (!l.get(3).equals("G")) { return false; } if (!l.get(4).equals("C")) { return false; } if (!l.get(5).equals("D")) { return false; } if (!l.get(6).equals("B")) { return false; } if (!l.get(7).equals("F")) { return false; } if (!l.get(8).equals("H")) { return false; } final List<String> already_covered = new ArrayList<String>(); already_covered.add("A"); already_covered.add("X"); already_covered.add("H"); already_covered.add("C"); l = ce.find( phylogenies, already_covered, 0, new ExternalNodeBasedCoverageMethodOptions( "org.forester.pccx.BranchCountingBasedScoringMethod"), null); if (!l.get(0).equals("E")) { return false; } if (!l.get(1).equals("D")) { return false; } if (!l.get(2).equals("B")) { return false; } if (!l.get(3).equals("F")) { return false; } if (!l.get(4).equals("G")) { return false; } final String ps2 = "((((A:0.1,B:0.7):0.2,C:1.0):2.0,D:1.7):1.3,((E:0.3,F:0.4):1.1,(G:0.5,H:0.6):1.2):1.4,X:2.0)"; final String ps3 = "((((A:0.1,B:0.1):0.2,C:1.0):2.0,D:1.7):1.3,((E:0.3,F:0.4):1.1,(G:0.5,H:0.6):1.2):1.4,X:2.0)"; final String ps4 = "((((A:0.1,B:0.05):0.2,C:1.0):2.0,D:1.7):1.3,((E:0.3,F:0.4):1.1,(G:0.5,H:0.6):1.2):1.4,X:2.0)"; final Phylogeny p2 = factory.create(ps2, new NHXParser())[0]; final Phylogeny p3 = factory.create(ps3, new NHXParser())[0]; final Phylogeny p4 = factory.create(ps4, new NHXParser())[0]; final List<Phylogeny> phylogenies2 = new ArrayList<Phylogeny>(); final List<String> names2 = new ArrayList<String>(); phylogenies2.add(p2); phylogenies2.add(p3); phylogenies2.add(p4); names2.add("A"); names2.add("A"); final CoverageCalculationOptions options2 = new ExternalNodeBasedCoverageMethodOptions( "org.forester.pccx.BranchLengthBasedScoringMethod"); final CoverageCalculator cc2 = CoverageCalculator.getInstance(new ExternalNodeBasedCoverageMethod(), options2); Coverage cov2 = cc2.calculateCoverage(phylogenies2, names2, false); final double nf = 1 / (1 / 0.1 + 1 / 0.7 + 1 / 1.0 + 1 / 1.7 + 1 / 0.3 + 1 / 0.4 + 1 / 0.5 + 1 / 0.6 + 1 / 2.0); if (!TestPccx.isEqual( cov2.getScore(), (1 / 0.1 + (1 / 0.8 + 1 / 0.2 + 1 / 0.15) / 3 + 1 / 1.3 + 1 / 4.0 + 1 / 6.4 + 1 / 6.5 + 1 / 6.7 + 1 / 6.8 + 1 / 5.6) * nf)) { return false; } names2.add("C"); cov2 = cc2.calculateCoverage(phylogenies2, names2, false); if (!TestPccx.isEqual( cov2.getScore(), (1 / 0.1 + (1 / 0.8 + 1 / 0.2 + 1 / 0.15) / 3 + 1 / 1.0 + 1 / 4.0 + 1 / 6.4 + 1 / 6.5 + 1 / 6.7 + 1 / 6.8 + 1 / 5.6) * nf)) { return false; } names2.add("E"); cov2 = cc2.calculateCoverage(phylogenies2, names2, false); if (!TestPccx.isEqual( cov2.getScore(), (1 / 0.1 + (1 / 0.8 + 1 / 0.2 + 1 / 0.15) / 3 + 1 / 1.0 + +1 / 4.0 + 1 / 0.3 + 1 / 0.7 + 1 / 3.1 + 1 / 3.2 + 1 / 4.8) * nf)) { return false; } final CoverageCalculationOptions options_log = new ExternalNodeBasedCoverageMethodOptions( "org.forester.pccx.LogBranchLengthBasedScoringMethod"); final CoverageCalculator cclog = CoverageCalculator.getInstance(new ExternalNodeBasedCoverageMethod(), options_log); final Coverage cov_log = cclog.calculateCoverage(phylogenies2, names2, false); if (!TestPccx.isEqual(cov_log.getScore(), 0.8534252108361485)) { return false; } final String ps10 = "((((A:0.1,B:0.7):0.2,C:1.0):2.0,D:1.7):1.3,((E:0.3,F:0.4):1.1,(G:0.5,H:0.6):1.2):1.4,((((I:0.1,J:0.7):0.2,K:1.0):2.0,L:1.7):1.3,((M:0.3,N:0.4,O:0.1,P:0.2):1.1,(Q:0.5,R:0.6):1.2):1.4,S:2.0):2.0)"; final Phylogeny p10 = factory.create(ps10, new NHXParser())[0]; final List<Phylogeny> phylogenies10 = new ArrayList<Phylogeny>(); final List<String> names10 = new ArrayList<String>(); phylogenies10.add(p10); names10.add("A"); names10.add("B"); names10.add("N"); names10.add("O"); final CoverageCalculationOptions options10 = new ExternalNodeBasedCoverageMethodOptions( "org.forester.pccx.BranchCountingBasedScoringMethod"); final CoverageCalculator cc10 = CoverageCalculator.getInstance(new ExternalNodeBasedCoverageMethod(), options10); cc10.calculateCoverage(phylogenies10, names10, true); } catch (final Exception e) { e.printStackTrace(System.out); return false; } return true; }
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."); }