X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fapplication%2Frio.java;h=13b2ade45566f2885ef6f756fb13c5a7a447632c;hb=89f36e42fb462f8d7b59def3ead995fb16a87b59;hp=b9a15738056982413b4205a966b60b3a9a5bae53;hpb=4e8cb17afec5430c00ff6a026d1528c188e91cda;p=jalview.git diff --git a/forester/java/src/org/forester/application/rio.java b/forester/java/src/org/forester/application/rio.java index b9a1573..13b2ade 100644 --- a/forester/java/src/org/forester/application/rio.java +++ b/forester/java/src/org/forester/application/rio.java @@ -28,123 +28,108 @@ package org.forester.application; import java.io.File; -import java.io.FileWriter; -import java.io.PrintWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.forester.datastructures.IntMatrix; import org.forester.io.parsers.phyloxml.PhyloXmlParser; import org.forester.phylogeny.Phylogeny; +import org.forester.phylogeny.PhylogenyMethods; import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory; import org.forester.phylogeny.factories.PhylogenyFactory; -import org.forester.sdi.RIO; +import org.forester.rio.RIO; +import org.forester.rio.RIO.REROOTING; +import org.forester.rio.RIOException; +import org.forester.sdi.SDIException; +import org.forester.sdi.SDIutil.ALGORITHM; +import org.forester.util.BasicDescriptiveStatistics; +import org.forester.util.CommandLineArguments; +import org.forester.util.EasyWriter; import org.forester.util.ForesterUtil; public class rio { - final static private String PRG_NAME = "RIO"; - final static private String PRG_VERSION = "2.03 ALPHA"; - final static private String PRG_DATE = "2010.01.15"; - final static private String E_MAIL = "czmasek@burnham.org"; - final static private String WWW = "www.phylosoft.org/forester/"; - final static private boolean TIME = true; - final static private boolean VERBOSE = true; - - private final static void errorInCommandLine() { - System.out.println( "\nrio: Error in command line.\n" ); - printHelp(); - System.exit( -1 ); - } + final static private String PRG_NAME = "rio"; + final static private String PRG_VERSION = "4.000 beta 3"; + final static private String PRG_DATE = "2012.12.17"; + final static private String E_MAIL = "czmasek@burnham.org"; + final static private String WWW = "www.phylosoft.org/forester/"; + final static private String HELP_OPTION_1 = "help"; + final static private String HELP_OPTION_2 = "h"; + final static private String USE_SDIR = "b"; public static void main( final String[] args ) { - ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW ); - File species_tree_file = null; - File multiple_trees_file = null; - File outfile = null; - String seq_name = ""; - String arg = ""; - boolean output_ultraparalogs = false; - double t_orthologs = 0.0; - double t_orthologs_dc = 0.0; - double threshold_ultra_paralogs = 0.0; - int sort = 13; - Phylogeny species_tree = null; - RIO rio_instance = null; - PrintWriter out = null; - long time = 0; - if ( args.length < 2 ) { + ForesterUtil.printProgramInformation( PRG_NAME, + "resampled inference of orthologs", + PRG_VERSION, + PRG_DATE, + E_MAIL, + WWW, + ForesterUtil.getForesterLibraryInformation() ); + 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 ); } - else if ( ( args.length < 3 ) || ( args.length > 18 ) ) { - errorInCommandLine(); + if ( ( args.length < 3 ) || ( args.length > 8 ) ) { + System.out.println(); + System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" ); + System.out.println(); + printHelp(); } - for( final String arg2 : args ) { - if ( arg2.trim().charAt( 0 ) != 'p' ) { - if ( arg2.trim().length() < 3 ) { - errorInCommandLine(); - } - else { - arg = arg2.trim().substring( 2 ); - } - } - try { - switch ( arg2.trim().charAt( 0 ) ) { - case 'M': - multiple_trees_file = new File( arg ); - break; - case 'N': - seq_name = arg; - break; - case 'S': - species_tree_file = new File( arg ); - break; - case 'O': - outfile = new File( arg ); - break; - case 'p': - output_ultraparalogs = true; - break; - case 'P': - sort = Integer.parseInt( arg ); - if ( ( sort < 0 ) || ( sort > 17 ) ) { - errorInCommandLine(); - } - break; - case 'L': - t_orthologs = Double.parseDouble( arg ); - break; - case 'v': - threshold_ultra_paralogs = Double.parseDouble( arg ); - break; - default: - errorInCommandLine(); - } + final List allowed_options = new ArrayList(); + allowed_options.add( USE_SDIR ); + final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options ); + if ( dissallowed_options.length() > 0 ) { + ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options ); + } + final File gene_trees_file = cla.getFile( 0 ); + final File species_tree_file = cla.getFile( 1 ); + final File orthology_outtable = cla.getFile( 2 ); + final File logfile; + if ( cla.getNumberOfNames() > 3 ) { + logfile = cla.getFile( 3 ); + if ( logfile.exists() ) { + ForesterUtil.fatalError( PRG_NAME, "\"" + logfile + "\" already exists" ); } - catch ( final Exception e ) { - errorInCommandLine(); + } + else { + logfile = null; + } + final String outgroup = ""; + ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, gene_trees_file ); + ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, species_tree_file ); + if ( orthology_outtable.exists() ) { + ForesterUtil.fatalError( PRG_NAME, "\"" + orthology_outtable + "\" already exists" ); + } + boolean sdir = false; + if ( cla.isOptionSet( USE_SDIR ) ) { + sdir = true; + if ( logfile != null ) { + ForesterUtil.fatalError( PRG_NAME, "logfile output only for GSDIR algorithm" ); } } - if ( ( seq_name == "" ) || ( species_tree_file == null ) || ( multiple_trees_file == null ) - || ( outfile == null ) ) { - errorInCommandLine(); - } - if ( ( sort < 0 ) || ( sort > 2 ) ) { - errorInCommandLine(); - } - if ( VERBOSE ) { - System.out.println( "\nMultiple trees file: " + multiple_trees_file ); - System.out.println( "Seq name: " + seq_name ); - System.out.println( "Species tree file: " + species_tree_file ); - System.out.println( "Outfile: " + outfile ); - System.out.println( "Sort: " + sort ); - System.out.println( "Threshold orthologs: " + t_orthologs ); - System.out.println( "Threshold orthologs for distance calc.: " + t_orthologs_dc ); - if ( output_ultraparalogs ) { - System.out.println( "Threshold ultra paralogs: " + threshold_ultra_paralogs ); + long time = 0; + System.out.println( "Gene trees : " + gene_trees_file ); + System.out.println( "Species tree : " + species_tree_file ); + System.out.println( "All vs all orthology table: " + orthology_outtable ); + if ( !sdir ) { + if ( logfile != null ) { + System.out.println( "Logfile : " + logfile ); } + System.out.println( "Non binary species tree : allowed (GSDIR algorithm)" ); } - if ( TIME && VERBOSE ) { - time = System.currentTimeMillis(); + else { + System.out.println( "Non binary species tree : disallowed (SDIR algorithm)" ); } + time = System.currentTimeMillis(); + Phylogeny species_tree = null; try { final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ]; @@ -154,55 +139,157 @@ public class rio { System.exit( -1 ); } if ( !species_tree.isRooted() ) { - ForesterUtil.printErrorMessage( PRG_NAME, "Species tree is not rooted" ); - System.exit( -1 ); + ForesterUtil.fatalError( PRG_NAME, "species tree is not rooted" ); } - if ( !species_tree.isCompletelyBinary() ) { - ForesterUtil.printErrorMessage( PRG_NAME, "Species tree is not completely binary" ); - System.exit( -1 ); + final int o = PhylogenyMethods.countNumberOfOneDescendantNodes( species_tree ); + if ( o > 0 ) { + ForesterUtil.printWarningMessage( PRG_NAME, "species tree has " + o + + " internal nodes with only one descendent! Going to strip them." ); + PhylogenyMethods.deleteInternalNodesWithOnlyOneDescendent( species_tree ); + if ( PhylogenyMethods.countNumberOfOneDescendantNodes( species_tree ) > 0 ) { + ForesterUtil.unexpectedFatalError( PRG_NAME, "stripping of one-desc nodes failed" ); + } + } + final ALGORITHM algorithm; + if ( sdir ) { + algorithm = ALGORITHM.SDIR; + } + else { + algorithm = ALGORITHM.GSDIR; } - rio_instance = new RIO(); - final StringBuffer output = new StringBuffer(); try { - rio_instance.inferOrthologs( multiple_trees_file, species_tree.copy(), seq_name ); - output.append( rio_instance.inferredOrthologsToString( seq_name, sort, t_orthologs ) ); - if ( output_ultraparalogs ) { - output.append( "\n\nUltra paralogs:\n" ); - output.append( rio_instance.inferredUltraParalogsToString( seq_name, threshold_ultra_paralogs ) ); + final RIO rio = RIO.executeAnalysis( gene_trees_file, + species_tree, + algorithm, + REROOTING.BY_ALGORITHM, + outgroup, + logfile != null, + true ); + if ( algorithm == ALGORITHM.GSDIR ) { + ForesterUtil.programMessage( PRG_NAME, "taxonomy linking based on: " + rio.getGSDIRtaxCompBase() ); + } + tableOutput( orthology_outtable, rio ); + if ( ( algorithm != ALGORITHM.SDIR ) && ( logfile != null ) ) { + writeLogFile( logfile, + rio, + species_tree_file, + gene_trees_file, + orthology_outtable, + PRG_NAME, + PRG_VERSION, + PRG_DATE, + ForesterUtil.getForesterLibraryInformation() ); } - output.append( "\n\nSort priority: " + RIO.getOrder( sort ) ); - output.append( "\nExt nodes : " + rio_instance.getExtNodesOfAnalyzedGeneTrees() ); - output.append( "\nSamples : " + rio_instance.getNumberOfSamples() + "\n" ); - out = new PrintWriter( new FileWriter( outfile ), true ); + final BasicDescriptiveStatistics stats = rio.getDuplicationsStatistics(); + final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.#" ); + ForesterUtil.programMessage( PRG_NAME, + "Mean number of duplications : " + df.format( stats.arithmeticMean() ) + + " (sd: " + df.format( stats.sampleStandardDeviation() ) + ")" ); + if ( stats.getN() > 3 ) { + ForesterUtil.programMessage( PRG_NAME, "Median number of duplications: " + df.format( stats.median() ) ); + } + ForesterUtil.programMessage( PRG_NAME, "Minimum duplications : " + ( int ) stats.getMin() ); + ForesterUtil.programMessage( PRG_NAME, "Maximum duplications : " + ( int ) stats.getMax() ); } - catch ( final Exception e ) { - ForesterUtil.printErrorMessage( PRG_NAME, e.getLocalizedMessage() ); - e.printStackTrace(); - System.exit( -1 ); + catch ( final RIOException e ) { + ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() ); } - out.println( output ); - out.close(); - ForesterUtil.programMessage( PRG_NAME, "wrote results to \"" + outfile + "\"" ); - if ( TIME && VERBOSE ) { - time = System.currentTimeMillis() - time; - ForesterUtil.programMessage( PRG_NAME, "time: " + time + "ms" ); + catch ( final SDIException e ) { + ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() ); + } + catch ( final IOException e ) { + ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() ); + } + catch ( final Exception e ) { + ForesterUtil.unexpectedFatalError( PRG_NAME, e ); } - ForesterUtil.programMessage( PRG_NAME, "OK." ); + time = System.currentTimeMillis() - time; + ForesterUtil.programMessage( PRG_NAME, "time: " + time + "ms" ); + ForesterUtil.programMessage( PRG_NAME, "OK" ); System.exit( 0 ); } private final static void printHelp() { - System.out.println( "M= (String) Multiple gene tree file (mandatory)" ); - System.out.println( "N= (String) Query sequence name (mandatory)" ); - System.out.println( "S= (String) Species tree file (mandatory)" ); - System.out.println( "O= (String) Output file name -- overwritten without warning! (mandatory)" ); - System.out.println( "P= (int) Sort priority" ); - System.out.println( "L= (double) Threshold orthologs for output" ); - System.out.println( " Sort priority (\"P=\"):" ); - System.out.println( RIO.getOrderHelp().toString() ); + System.out.println( "Usage" ); + System.out.println(); + System.out + .println( PRG_NAME + + " [options] [logfile]" ); + System.out.println(); + System.out.println( " Options" ); + System.out.println( " -" + USE_SDIR + + " : to use SDIR instead of GSDIR (faster, but non-binary species trees are disallowed)" ); System.out.println(); + System.out.println( " Formats" ); + System.out.println( " The species tree is expected to be in phyloXML format." ); System.out - .println( " Example: \"rio M=gene_trees.xml N=bcl2_NEMVE S=species_tree.xml D=distances P=13 p O=out\"" ); + .println( " The gene trees ideally are in phyloXML as well, but can also be in New Hamphshire (Newick)" ); + System.out.println( " or Nexus format as long as species information can be extracted from the gene names" ); + System.out.println( " (e.g. \"HUMAN\" from \"BCL2_HUMAN\")." ); System.out.println(); + System.out.println( " Examples" ); + System.out.println( " \"rio gene_trees.nh species.xml outtable.tsv log.txt\"" ); + System.out.println(); + System.out.println( " More information: http://code.google.com/p/forester/wiki/RIO" ); + System.out.println(); + System.exit( -1 ); + } + + private static void tableOutput( final File table_outfile, final RIO rio ) throws IOException, RIOException { + final IntMatrix m = RIO.calculateOrthologTable( rio.getAnalyzedGeneTrees(), true ); + writeTable( table_outfile, rio, m ); + } + + private static void writeLogFile( final File logfile, + final RIO rio, + final File species_tree_file, + final File gene_trees_file, + final File outtable, + final String prg_name, + final String prg_v, + final String prg_date, + final String f ) throws IOException { + final EasyWriter out = ForesterUtil.createEasyWriter( logfile ); + out.println( prg_name ); + out.println( "version : " + prg_v ); + out.println( "date : " + prg_date ); + out.println( "based on: " + f ); + out.println( "----------------------------------" ); + out.println( "Gene trees : " + gene_trees_file ); + out.println( "Species tree : " + species_tree_file ); + out.println( "All vs all orthology table : " + outtable ); + out.flush(); + out.println( rio.getLog().toString() ); + out.close(); + ForesterUtil.programMessage( PRG_NAME, "wrote log to \"" + logfile + "\"" ); + } + + private static void writeTable( final File table_outfile, final RIO rio, final IntMatrix m ) throws IOException { + final EasyWriter w = ForesterUtil.createEasyWriter( table_outfile ); + final java.text.DecimalFormat df = new java.text.DecimalFormat( "0.###" ); + df.setDecimalSeparatorAlwaysShown( false ); + for( int i = 0; i < m.size(); ++i ) { + w.print( "\t" ); + w.print( m.getLabel( i ) ); + } + w.println(); + for( int x = 0; x < m.size(); ++x ) { + w.print( m.getLabel( x ) ); + for( int y = 0; y < m.size(); ++y ) { + w.print( "\t" ); + if ( x == y ) { + if ( m.get( x, y ) != rio.getAnalyzedGeneTrees().length ) { + ForesterUtil.unexpectedFatalError( PRG_NAME, "diagonal value is off" ); + } + w.print( "-" ); + } + else { + w.print( df.format( ( ( double ) m.get( x, y ) ) / rio.getAnalyzedGeneTrees().length ) ); + } + } + w.println(); + } + w.close(); + ForesterUtil.programMessage( PRG_NAME, "wrote table to \"" + table_outfile + "\"" ); } }