X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fapplication%2Frio.java;h=5c838f9ff4fc8555456cd7f8978002518eaa97f8;hb=18cc8f6f4f18effd5659b94bebf9308ae92eb642;hp=5bf670b80e7dc4047955bd1e61a332cc1e9d3aff;hpb=2b1c49af616eb01a99a69daf8f703f494b1a5a7e;p=jalview.git diff --git a/forester/java/src/org/forester/application/rio.java b/forester/java/src/org/forester/application/rio.java index 5bf670b..5c838f9 100644 --- a/forester/java/src/org/forester/application/rio.java +++ b/forester/java/src/org/forester/application/rio.java @@ -31,20 +31,19 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.SortedSet; -import java.util.TreeSet; import org.forester.datastructures.IntMatrix; import org.forester.io.parsers.phyloxml.PhyloXmlParser; import org.forester.phylogeny.Phylogeny; -import org.forester.phylogeny.PhylogenyNode; -import org.forester.phylogeny.data.Taxonomy; +import org.forester.phylogeny.PhylogenyMethods; import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory; import org.forester.phylogeny.factories.PhylogenyFactory; import org.forester.rio.RIO; +import org.forester.rio.RIO.REROOTING; import org.forester.rio.RIOException; -import org.forester.sdi.SDI.ALGORITHM; 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; @@ -52,12 +51,16 @@ import org.forester.util.ForesterUtil; public class rio { final static private String PRG_NAME = "rio"; - final static private String PRG_VERSION = "4.000 beta 1"; - final static private String PRG_DATE = "2012.12.11"; + 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 GT_FIRST = "f"; + final static private String GT_LAST = "l"; + final static private String REROOTING_OPT = "r"; + final static private String OUTGROUP = "o"; final static private String USE_SDIR = "b"; public static void main( final String[] args ) { @@ -78,13 +81,17 @@ public class rio { if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) { printHelp(); } - if ( ( args.length < 3 ) || ( args.length > 5 ) ) { + if ( ( args.length < 3 ) || ( args.length > 8 ) ) { System.out.println(); System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" ); System.out.println(); printHelp(); } final List allowed_options = new ArrayList(); + allowed_options.add( GT_FIRST ); + allowed_options.add( GT_LAST ); + allowed_options.add( REROOTING_OPT ); + allowed_options.add( OUTGROUP ); allowed_options.add( USE_SDIR ); final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options ); if ( dissallowed_options.length() > 0 ) { @@ -92,21 +99,78 @@ public class rio { } final File gene_trees_file = cla.getFile( 0 ); final File species_tree_file = cla.getFile( 1 ); - final File othology_outtable = cla.getFile( 2 ); + 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" ); + } } else { logfile = null; } + String outgroup = null; + if ( cla.isOptionSet( OUTGROUP ) ) { + if ( !cla.isOptionHasAValue( OUTGROUP ) ) { + ForesterUtil.fatalError( PRG_NAME, "no value for -" + OUTGROUP ); + } + outgroup = cla.getOptionValueAsCleanString( OUTGROUP ); + } + REROOTING rerooting = REROOTING.BY_ALGORITHM; + if ( cla.isOptionSet( REROOTING_OPT ) ) { + if ( !cla.isOptionHasAValue( REROOTING_OPT ) ) { + ForesterUtil.fatalError( PRG_NAME, "no value for -" + REROOTING_OPT ); + } + final String rerooting_str = cla.getOptionValueAsCleanString( REROOTING_OPT ).toLowerCase(); + if ( rerooting_str.equals( "none" ) ) { + rerooting = REROOTING.NONE; + } + else if ( rerooting_str.equals( "midpoint" ) ) { + rerooting = REROOTING.MIDPOINT; + } + else if ( rerooting_str.equals( "outgroup" ) ) { + rerooting = REROOTING.OUTGROUP; + } + else { + ForesterUtil.fatalError( PRG_NAME, "legal values for -" + REROOTING_OPT + + " are: none, midpoint, or outgroup (minizming duplications is default)" ); + } + } + int gt_first = -1; + int gt_last = -1; + if ( cla.isOptionSet( GT_FIRST ) ) { + if ( !cla.isOptionHasAValue( GT_FIRST ) ) { + ForesterUtil.fatalError( PRG_NAME, "no value for -" + GT_FIRST ); + } + try { + gt_first = cla.getOptionValueAsInt( GT_FIRST ); + } + catch ( IOException e ) { + ForesterUtil.fatalError( PRG_NAME, "could not parse integer for -" + GT_FIRST ); + } + } + if ( cla.isOptionSet( GT_LAST ) ) { + if ( !cla.isOptionHasAValue( GT_LAST ) ) { + ForesterUtil.fatalError( PRG_NAME, "no value for -" + GT_LAST ); + } + try { + gt_last = cla.getOptionValueAsInt( GT_LAST ); + } + catch ( IOException e ) { + ForesterUtil.fatalError( PRG_NAME, "could not parse integer for -" + GT_LAST ); + } + } ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, gene_trees_file ); ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, species_tree_file ); - if ( othology_outtable.exists() ) { - ForesterUtil.fatalError( PRG_NAME, "\"" + othology_outtable + "\" already exists" ); + if ( orthology_outtable.exists() ) { + ForesterUtil.fatalError( PRG_NAME, "\"" + orthology_outtable + "\" already exists" ); } boolean sdir = false; if ( cla.isOptionSet( USE_SDIR ) ) { + if ( cla.isOptionHasAValue( USE_SDIR ) ) { + ForesterUtil.fatalError( PRG_NAME, "no value allowed for -" + USE_SDIR ); + } sdir = true; if ( logfile != null ) { ForesterUtil.fatalError( PRG_NAME, "logfile output only for GSDIR algorithm" ); @@ -115,7 +179,7 @@ public class rio { 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: " + othology_outtable ); + System.out.println( "All vs all orthology table: " + orthology_outtable ); if ( !sdir ) { if ( logfile != null ) { System.out.println( "Logfile : " + logfile ); @@ -138,6 +202,15 @@ public class rio { if ( !species_tree.isRooted() ) { ForesterUtil.fatalError( PRG_NAME, "species tree is not rooted" ); } + 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; @@ -146,14 +219,38 @@ public class rio { algorithm = ALGORITHM.GSDIR; } try { - final RIO rio = new RIO( gene_trees_file, species_tree, algorithm, logfile != null ); + final RIO rio = RIO.executeAnalysis( gene_trees_file, + species_tree, + algorithm, + rerooting, + outgroup, + logfile != null, + true ); if ( algorithm == ALGORITHM.GSDIR ) { - System.out.println( "Taxonomy linking based on : " + rio.getGSDIRtaxCompBase() ); + ForesterUtil.programMessage( PRG_NAME, "taxonomy linking based on: " + rio.getGSDIRtaxCompBase() ); } - tableOutput( othology_outtable, rio ); - if ( ( algorithm == ALGORITHM.GSDIR ) && ( logfile != null ) ) { - writeLogFile( logfile, rio ); + 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() ); } + 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 RIOException e ) { ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() ); @@ -173,35 +270,34 @@ public class rio { System.exit( 0 ); } - private static void writeLogFile( final File logfile, final RIO rio ) throws IOException { - final EasyWriter out = ForesterUtil.createEasyWriter( logfile ); - out.println( "Species stripped from gene trees:" ); - final SortedSet rn = new TreeSet(); - for( final PhylogenyNode n : rio.getRemovedGeneTreeNodes() ) { - final Taxonomy t = n.getNodeData().getTaxonomy(); - switch ( rio.getGSDIRtaxCompBase() ) { - case CODE: { - rn.add( t.getTaxonomyCode() ); - break; - } - case ID: { - rn.add( t.getIdentifier().toString() ); - break; - } - case SCIENTIFIC_NAME: { - rn.add( t.getScientificName() ); - break; - } - } - } - for( final String s : rn ) { - out.println( s ); - } - out.println(); - out.println( "Some information about duplication numbers in gene trees:" ); - out.println( rio.getLog().toString() ); - out.close(); - ForesterUtil.programMessage( PRG_NAME, "wrote log to \"" + logfile + "\"" ); + private final static void printHelp() { + System.out.println( "Usage" ); + System.out.println(); + System.out + .println( PRG_NAME + + " [options] [logfile]" ); + System.out.println(); + System.out.println( " Options" ); + System.out.println( " -" + GT_FIRST + "= : to" ); + System.out.println( " -" + GT_LAST + "= : to" ); + System.out.println( " -" + REROOTING_OPT + " : to" ); + System.out.println( " -" + OUTGROUP + "=: tp" ); + 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( " 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 { @@ -209,6 +305,30 @@ public class rio { 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.###" ); @@ -223,13 +343,13 @@ public class rio { for( int y = 0; y < m.size(); ++y ) { w.print( "\t" ); if ( x == y ) { - if ( m.get( x, y ) != rio.getNumberOfSamples() ) { + 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.getNumberOfSamples() ) ); + w.print( df.format( ( ( double ) m.get( x, y ) ) / rio.getAnalyzedGeneTrees().length ) ); } } w.println(); @@ -237,30 +357,4 @@ public class rio { w.close(); ForesterUtil.programMessage( PRG_NAME, "wrote table to \"" + table_outfile + "\"" ); } - - private final static void printHelp() { - 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( " 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 ); - } }