From 1542f37267c30e690b840d4552234f1f70598a46 Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Tue, 27 Nov 2012 02:47:13 +0000 Subject: [PATCH] "rio" work --- .../java/src/org/forester/application/rio.java | 236 +++++++++++--------- .../org/forester/phylogeny/PhylogenyMethods.java | 12 +- forester/java/src/org/forester/sdi/RIO.java | 7 +- .../java/src/org/forester/util/ForesterUtil.java | 10 + 4 files changed, 147 insertions(+), 118 deletions(-) diff --git a/forester/java/src/org/forester/application/rio.java b/forester/java/src/org/forester/application/rio.java index b9a1573..18fa912 100644 --- a/forester/java/src/org/forester/application/rio.java +++ b/forester/java/src/org/forester/application/rio.java @@ -30,121 +30,130 @@ package org.forester.application; import java.io.File; import java.io.FileWriter; import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; import org.forester.io.parsers.phyloxml.PhyloXmlParser; import org.forester.phylogeny.Phylogeny; import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory; import org.forester.phylogeny.factories.PhylogenyFactory; import org.forester.sdi.RIO; +import org.forester.util.CommandLineArguments; 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 = "3.00 beta 1"; + 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 String HELP_OPTION_1 = "help"; + final static private String HELP_OPTION_2 = "h"; + final static private String QUERY_OPTION = "q"; + final static private String SORT_OPTION = "s"; + final static private String OUTPUT_ULTRA_P_OPTION = "u"; + final static private String CUTOFF_ULTRA_P_OPTION = "cu"; + final static private String CUTOFF_ORTHO_OPTION = "co"; + final static private String TABLE_OUTPUT_OPTION = "t"; 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(); - } - for( final String arg2 : args ) { - if ( arg2.trim().charAt( 0 ) != 'p' ) { - if ( arg2.trim().length() < 3 ) { - errorInCommandLine(); - } - else { - arg = arg2.trim().substring( 2 ); - } + if ( ( args.length < 3 ) || ( args.length > 10 ) ) { + System.out.println(); + System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" ); + System.out.println(); + printHelp(); + System.exit( -1 ); + } + final List allowed_options = new ArrayList(); + allowed_options.add( QUERY_OPTION ); + allowed_options.add( SORT_OPTION ); + allowed_options.add( CUTOFF_ULTRA_P_OPTION ); + allowed_options.add( CUTOFF_ORTHO_OPTION ); + allowed_options.add( TABLE_OUTPUT_OPTION ); + allowed_options.add( OUTPUT_ULTRA_P_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 multiple_trees_file = cla.getFile( 0 ); + final File species_tree_file = cla.getFile( 1 ); + final File outfile = cla.getFile( 2 ); + ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, multiple_trees_file ); + ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, species_tree_file ); + if ( outfile.exists() ) { + ForesterUtil.fatalError( PRG_NAME, "[" + outfile + "] already exists" ); + } + String seq_name = null; + if ( cla.isOptionSet( QUERY_OPTION ) ) { + seq_name = cla.getOptionValue( QUERY_OPTION ); + } + File table_outfile = null; + if ( cla.isOptionSet( TABLE_OUTPUT_OPTION ) ) { + table_outfile = new File( cla.getOptionValue( TABLE_OUTPUT_OPTION ) ); + if ( table_outfile.exists() ) { + ForesterUtil.fatalError( PRG_NAME, "[" + outfile + "] already exists" ); } - 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(); - } + } + boolean output_ultraparalogs = false; + if ( cla.isOptionSet( OUTPUT_ULTRA_P_OPTION ) ) { + output_ultraparalogs = true; + } + double t_orthologs = 0.0; + double threshold_ultra_paralogs = 0.0; + int sort = 2; + try { + if ( cla.isOptionSet( CUTOFF_ORTHO_OPTION ) ) { + t_orthologs = cla.getOptionValueAsDouble( CUTOFF_ORTHO_OPTION ); } - catch ( final Exception e ) { - errorInCommandLine(); + if ( cla.isOptionSet( CUTOFF_ULTRA_P_OPTION ) ) { + threshold_ultra_paralogs = cla.getOptionValueAsDouble( CUTOFF_ULTRA_P_OPTION ); } - } - 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 ); + if ( cla.isOptionSet( SORT_OPTION ) ) { + sort = cla.getOptionValueAsInt( SORT_OPTION ); } } - if ( TIME && VERBOSE ) { - time = System.currentTimeMillis(); + catch ( final Exception e ) { + ForesterUtil.fatalError( PRG_NAME, "error in command line: " + e.getLocalizedMessage() ); + } + if ( sort < 0 ) { + sort = 0; } + else if ( sort > 2 ) { + sort = 2; + } + long time = 0; + System.out.println( "\n" ); + System.out.println( "Gene trees: " + multiple_trees_file ); + System.out.println( "Species tree: " + species_tree_file ); + System.out.println( "Query: " + seq_name ); + System.out.println( "Outfile: " + outfile ); + System.out.println( "Outfile: " + table_outfile ); + System.out.println( "Sort: " + sort ); + System.out.println( "Threshold orthologs: " + t_orthologs ); + if ( output_ultraparalogs ) { + System.out.println( "Threshold ultra paralogs: " + threshold_ultra_paralogs ); + } + time = System.currentTimeMillis(); + Phylogeny species_tree = null; try { final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance(); species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ]; @@ -161,8 +170,9 @@ public class rio { ForesterUtil.printErrorMessage( PRG_NAME, "Species tree is not completely binary" ); System.exit( -1 ); } - rio_instance = new RIO(); + final RIO rio_instance = new RIO(); final StringBuffer output = new StringBuffer(); + PrintWriter out = null; try { rio_instance.inferOrthologs( multiple_trees_file, species_tree.copy(), seq_name ); output.append( rio_instance.inferredOrthologsToString( seq_name, sort, t_orthologs ) ); @@ -183,26 +193,38 @@ public class rio { 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" ); - } + 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( "Usage:" ); + System.out.println(); + System.out.println( PRG_NAME + " [options] [outfile]" ); + System.out.println(); + System.out.println( "options:" ); + System.out.println(); + // System.out.println( " -" + STRICT_OPTION + // + " : strict [default: non-strict]: all nodes between 'target' and 'evaluators' must match" ); + // System.out.println( " -" + NORMALIZE_OPTION + // + "=: normalize to this value (e.g. 100 for most bootstrap analyses) [default: no normalization]" ); + // System.out.println( " -" + FIRST_OPTION + "=: first evaluator topology to use (0-based) [default: 0]" ); + // System.out.println( " -" + LAST_OPTION + // + "=: last evaluator topology to use (0-based) [default: use all until final topology]" ); + // System.out.println(); + // 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(); System.out - .println( " Example: \"rio M=gene_trees.xml N=bcl2_NEMVE S=species_tree.xml D=distances P=13 p O=out\"" ); + .println( " Example: \"rio -q=D_NEMVE -s=1 -t=out -u Bcl-2_e1_20_mafft_05_40_fme.mlt species.xml out\"" ); System.out.println(); } } diff --git a/forester/java/src/org/forester/phylogeny/PhylogenyMethods.java b/forester/java/src/org/forester/phylogeny/PhylogenyMethods.java index c881906..3c33301 100644 --- a/forester/java/src/org/forester/phylogeny/PhylogenyMethods.java +++ b/forester/java/src/org/forester/phylogeny/PhylogenyMethods.java @@ -583,16 +583,14 @@ public class PhylogenyMethods { return PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT; } - // Helper for getUltraParalogousNodes( PhylogenyNode ). - public static boolean areAllChildrenDuplications( final PhylogenyNode n ) { + public final static boolean isAllDecendentsAreDuplications( final PhylogenyNode n ) { if ( n.isExternal() ) { - return false; + return true; } else { if ( n.isDuplication() ) { - //FIXME test me! for( final PhylogenyNode desc : n.getDescendants() ) { - if ( !areAllChildrenDuplications( desc ) ) { + if ( !isAllDecendentsAreDuplications( desc ) ) { return false; } } @@ -1148,9 +1146,9 @@ public class PhylogenyMethods { // FIXME test me PhylogenyNode node = n; if ( !node.isExternal() ) { - return null; + throw new IllegalArgumentException( "attempt to get ultra-paralogous nodes of internal node" ); } - while ( !node.isRoot() && node.getParent().isDuplication() && areAllChildrenDuplications( node.getParent() ) ) { + while ( !node.isRoot() && node.getParent().isDuplication() && isAllDecendentsAreDuplications( node.getParent() ) ) { node = node.getParent(); } final List nodes = node.getAllExternalDescendants(); diff --git a/forester/java/src/org/forester/sdi/RIO.java b/forester/java/src/org/forester/sdi/RIO.java index 62b2c21..3c2c298 100644 --- a/forester/java/src/org/forester/sdi/RIO.java +++ b/forester/java/src/org/forester/sdi/RIO.java @@ -53,9 +53,8 @@ import org.forester.util.ForesterUtil; */ public final class RIO { - private final static boolean ROOT_BY_MINIMIZING_MAPPING_COST = false; - private final static boolean ROOT_BY_MINIMIZING_SUM_OF_DUPS = true; - private final static boolean ROOT_BY_MINIMIZING_TREE_HEIGHT = true; + private final static boolean ROOT_BY_MINIMIZING_SUM_OF_DUPS = true; + private final static boolean ROOT_BY_MINIMIZING_TREE_HEIGHT = true; private HashMap> _o_hash_maps; private HashMap> _so_hash_maps; private HashMap> _up_hash_maps; @@ -289,7 +288,7 @@ public final class RIO { List ultra_paralogs = null; assigned_tree = sdiunrooted.infer( gene_tree, species_tree, - RIO.ROOT_BY_MINIMIZING_MAPPING_COST, + false, RIO.ROOT_BY_MINIMIZING_SUM_OF_DUPS, RIO.ROOT_BY_MINIMIZING_TREE_HEIGHT, true, diff --git a/forester/java/src/org/forester/util/ForesterUtil.java b/forester/java/src/org/forester/util/ForesterUtil.java index 08c3b9e..9d9570d 100644 --- a/forester/java/src/org/forester/util/ForesterUtil.java +++ b/forester/java/src/org/forester/util/ForesterUtil.java @@ -335,6 +335,16 @@ public final class ForesterUtil { System.exit( -1 ); } + public static void fatalErrorIfFileNotReadable( final String prg_name, final File file ) { + final String error = isReadableFile( file ); + if ( !isEmpty( error ) ) { + System.err.println(); + System.err.println( "[" + prg_name + "] > " + error ); + System.err.println(); + System.exit( -1 ); + } + } + public static String[] file2array( final File file ) throws IOException { final List list = file2list( file ); final String[] ary = new String[ list.size() ]; -- 1.7.10.2