in progress
[jalview.git] / forester / java / src / org / forester / application / rio.java
index 3815c6d..5564763 100644 (file)
@@ -29,199 +29,145 @@ package org.forester.application;
 
 import java.io.File;
 import java.io.FileWriter;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
-import java.util.Vector;
+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.PhylogenyNode;
 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
 import org.forester.phylogeny.factories.PhylogenyFactory;
-import org.forester.phylogeny.iterators.PreorderTreeIterator;
 import org.forester.sdi.RIO;
+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;
-    // For method getDistances -- calculation of distances.
-    final static private boolean MINIMIZE_COST                    = false;
-    // For method getDistances -- calculation of distances.
-    final static private boolean MINIMIZE_DUPS                    = true;
-    // For method getDistances -- calculation of distances.
-    final static private boolean MINIMIZE_HEIGHT                  = true;
-    final static private int     WARN_NO_ORTHOS_DEFAULT           = 2;
-    final static private int
-                                 // How many sd away from mean to root.
-                                 WARN_MORE_THAN_ONE_ORTHO_DEFAULT = 2;
-    // How many sd away from mean to LCA of orthos.
-    final static private double  THRESHOLD_ULTRA_PARALOGS_DEFAULT = 50;
-    // How many sd away from mean to LCA of orthos.
-    final static private double  WARN_ONE_ORTHO_DEFAULT           = 2;
+    final static private String PRG_NAME              = "rio";
+    final static private String PRG_VERSION           = "3.00 beta 1";
+    final static private String PRG_DATE              = "2012.11.27";
+    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";
 
-    // Factor between the two distances to their LCA
-    // (larger/smaller).
-    // Factor between the two distances to their LCA
-    // (larger/smaller).
-    /**
-     * Calculates the mean and standard deviation of all nodes of Phylogeny t
-     * which have a bootstrap values zero or more. Returns null in case of
-     * failure (e.g t has no bootstrap values, or just one).
-     * <p>
-     * 
-     * @param t
-     *            reference to a tree with bootstrap values
-     * @return Array of doubles, [0] is the mean, [1] the standard deviation
-     */
-    private static double[] calculateMeanBoostrapValue( final Phylogeny t ) {
-        double b = 0;
-        int n = 0;
-        long sum = 0;
-        double x = 0.0, mean = 0.0;
-        final double[] da = new double[ 2 ];
-        final Vector<Double> bv = new Vector<Double>();
-        PhylogenyNode node = null;
-        PreorderTreeIterator i = null;
-        i = new PreorderTreeIterator( t );
-        // Calculates the mean.
-        while ( i.hasNext() ) {
-            node = i.next();
-            if ( !( ( node.getParent() != null ) && node.getParent().isRoot()
-                    && ( PhylogenyMethods.getConfidenceValue( node.getParent().getChildNode1() ) > 0 )
-                    && ( PhylogenyMethods.getConfidenceValue( node.getParent().getChildNode2() ) > 0 ) && ( node
-                    .getParent().getChildNode2() == node ) ) ) {
-                b = PhylogenyMethods.getConfidenceValue( node );
-                if ( b > 0 ) {
-                    sum += b;
-                    bv.addElement( new Double( b ) );
-                    n++;
-                }
-            }
-            // i.next();
+    public static void main( final String[] args ) {
+        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 );
         }
-        if ( n < 2 ) {
-            return null;
+        catch ( final Exception e ) {
+            ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
         }
-        mean = ( double ) sum / n;
-        // Calculates the standard deviation.
-        sum = 0;
-        for( int j = 0; j < n; ++j ) {
-            b = ( bv.elementAt( j ) ).intValue();
-            x = b - mean;
-            sum += ( x * x );
+        if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
+            printHelp();
         }
-        da[ 0 ] = mean;
-        da[ 1 ] = java.lang.Math.sqrt( sum / ( n - 1.0 ) );
-        return da;
-    }
-
-    private final static void errorInCommandLine() {
-        System.out.println( "\nrio: Error in command line.\n" );
-        printHelp();
-        System.exit( -1 );
-    }
-
-    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;
-        ArrayList<String> orthologs_al_for_dc = null;
-        double t_orthologs = 0.0;
-        double t_orthologs_dc = 0.0;
-        double threshold_ultra_paralogs = 0.0;
-        double[] bs_mean_sd = null;
-        int sort = 13;
-        Phylogeny species_tree = null;
-        RIO rio_instance = null;
-        PrintWriter out = null;
-        long time = 0;
-        if ( args.length < 2 ) {
+        if ( ( args.length < 2 ) || ( args.length > 10 ) ) {
+            System.out.println();
+            System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
+            System.out.println();
             printHelp();
-            System.exit( 0 );
         }
-        else if ( ( args.length < 3 ) || ( args.length > 18 ) ) {
-            errorInCommandLine();
+        final List<String> allowed_options = new ArrayList<String>();
+        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 );
         }
-        for( final String arg2 : args ) {
-            if ( arg2.trim().charAt( 0 ) != 'p' ) {
-                if ( arg2.trim().length() < 3 ) {
-                    errorInCommandLine();
-                }
-                else {
-                    arg = arg2.trim().substring( 2 );
-                }
+        final File gene_trees_file = cla.getFile( 0 );
+        final File species_tree_file = cla.getFile( 1 );
+        File outfile = null;
+        if ( cla.getNumberOfNames() > 2 ) {
+            outfile = cla.getFile( 2 );
+        }
+        ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, gene_trees_file );
+        ForesterUtil.fatalErrorIfFileNotReadable( PRG_NAME, species_tree_file );
+        if ( outfile.exists() ) {
+            ForesterUtil.fatalError( PRG_NAME, "[" + outfile + "] already exists" );
+        }
+        String query = null;
+        if ( cla.isOptionSet( QUERY_OPTION ) ) {
+            query = 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, "[" + table_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 cutoff_for_orthologs = 50;
+        double cutoff_for_ultra_paralogs = 50;
+        int sort = 2;
+        try {
+            if ( cla.isOptionSet( CUTOFF_ORTHO_OPTION ) ) {
+                cutoff_for_orthologs = cla.getOptionValueAsDouble( CUTOFF_ORTHO_OPTION );
+            }
+            if ( cla.isOptionSet( CUTOFF_ULTRA_P_OPTION ) ) {
+                cutoff_for_ultra_paralogs = cla.getOptionValueAsDouble( CUTOFF_ULTRA_P_OPTION );
+                if ( !output_ultraparalogs ) {
+                    printHelp();
                 }
             }
-            catch ( final Exception e ) {
-                errorInCommandLine();
+            if ( cla.isOptionSet( SORT_OPTION ) ) {
+                sort = cla.getOptionValueAsInt( SORT_OPTION );
             }
         }
-        if ( ( seq_name == "" ) || ( species_tree_file == null ) || ( multiple_trees_file == null )
-                || ( outfile == null ) ) {
-            errorInCommandLine();
+        catch ( final Exception e ) {
+            ForesterUtil.fatalError( PRG_NAME, "error in command line: " + e.getLocalizedMessage() );
+        }
+        if ( ( cutoff_for_orthologs < 0 ) || ( cutoff_for_ultra_paralogs < 0 ) || ( sort < 0 ) || ( sort > 2 ) ) {
+            printHelp();
+        }
+        if ( ( ( query == null ) && ( outfile != null ) ) || ( ( query != null ) && ( outfile == null ) ) ) {
+            printHelp();
         }
-        if ( ( sort < 0 ) || ( sort > 2 ) ) {
-            errorInCommandLine();
+        if ( output_ultraparalogs && ( outfile == null ) ) {
+            printHelp();
         }
-        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 );
+        long time = 0;
+        System.out.println( "Gene trees                : " + gene_trees_file );
+        System.out.println( "Species tree              : " + species_tree_file );
+        if ( query != null ) {
+            System.out.println( "Query                     : " + query );
+            System.out.println( "Outfile                   : " + outfile );
+            System.out.println( "Sort                      : " + sort );
+            System.out.println( "Cutoff for  orthologs     : " + cutoff_for_orthologs );
             if ( output_ultraparalogs ) {
-                System.out.println( "Threshold ultra paralogs:                     " + threshold_ultra_paralogs );
+                System.out.println( "Cutoff for ultra paralogs : " + cutoff_for_ultra_paralogs );
             }
         }
-        if ( TIME && VERBOSE ) {
-            time = System.currentTimeMillis();
+        if ( table_outfile != null ) {
+            System.out.println( "Table output              : " + table_outfile );
         }
+        System.out.println();
+        time = System.currentTimeMillis();
+        Phylogeny species_tree = null;
         try {
             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
             species_tree = factory.create( species_tree_file, new PhyloXmlParser() )[ 0 ];
@@ -231,55 +177,116 @@ public class rio {
             System.exit( -1 );
         }
         if ( !species_tree.isRooted() ) {
-            ForesterUtil.printErrorMessage( PRG_NAME, "Species tree is not rooted" );
+            ForesterUtil.printErrorMessage( PRG_NAME, "species tree is not rooted" );
             System.exit( -1 );
         }
         if ( !species_tree.isCompletelyBinary() ) {
-            ForesterUtil.printErrorMessage( PRG_NAME, "Species tree is not completely binary" );
+            ForesterUtil.printErrorMessage( PRG_NAME, "species tree is not completely binary" );
             System.exit( -1 );
         }
-        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 ) );
+            RIO rio;
+            if ( ForesterUtil.isEmpty( query ) ) {
+                rio = new RIO( gene_trees_file, species_tree );
+            }
+            else {
+                rio = new RIO( gene_trees_file, species_tree, query );
+            }
+            if ( outfile != null ) {
+                final StringBuilder output = new StringBuilder();
+                output.append( rio.inferredOrthologsToString( query, sort, cutoff_for_orthologs ) );
+                if ( output_ultraparalogs ) {
+                    output.append( "\n\nUltra paralogs:\n" );
+                    output.append( rio.inferredUltraParalogsToString( query, cutoff_for_ultra_paralogs ) );
+                }
+                output.append( "\n\nSort priority: " + RIO.getOrder( sort ) );
+                output.append( "\nExt nodes    : " + rio.getExtNodesOfAnalyzedGeneTrees() );
+                output.append( "\nSamples      : " + rio.getNumberOfSamples() + "\n" );
+                final PrintWriter out = new PrintWriter( new FileWriter( outfile ), true );
+                out.println( output );
+                out.close();
+            }
+            if ( table_outfile != null ) {
+                tableOutput( table_outfile, rio );
             }
-            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 );
         }
         catch ( final Exception e ) {
             ForesterUtil.printErrorMessage( PRG_NAME, e.getLocalizedMessage() );
             e.printStackTrace();
             System.exit( -1 );
         }
-        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" );
+        if ( outfile != null ) {
+            ForesterUtil.programMessage( PRG_NAME, "wrote results to \"" + outfile + "\"" );
         }
-        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 static void tableOutput( final File table_outfile, final RIO rio ) throws IOException {
+        final IntMatrix m = RIO.calculateOrthologTable( rio.getAnalyzedGeneTrees() );
+        writeTable( table_outfile, rio, m );
+    }
+
+    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 );
+        w.print( "\t" );
+        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 ) );
+            w.print( "\t" );
+            for( int y = 0; y < m.size(); ++y ) {
+                w.print( "\t" );
+                if ( x == y ) {
+                    if ( m.get( x, y ) != rio.getNumberOfSamples() ) {
+                        ForesterUtil.unexpectedFatalError( PRG_NAME, "diagonal value is off" );
+                    }
+                    w.print( "-" );
+                }
+                else {
+                    w.print( df.format( ( ( double ) m.get( x, y ) ) / rio.getNumberOfSamples() ) );
+                }
+            }
+            w.println();
+        }
+        w.close();
+        ForesterUtil.programMessage( PRG_NAME, "wrote table to \"" + table_outfile + "\"" );
+    }
+
     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] <gene trees file> <species tree file> [outfile]" );
+        System.out.println();
+        System.out.println( " Options" );
+        System.out.println( "  -" + CUTOFF_ORTHO_OPTION + " : cutoff for ortholog output (default: 50)" );
+        System.out.println( "  -" + TABLE_OUTPUT_OPTION + "  : file-name for output table" );
+        System.out.println( "  -" + QUERY_OPTION + "  : name for query (sequence/node)" );
+        System.out.println( "  -" + SORT_OPTION + "  : sort (default: 2)" );
+        System.out.println( "  -" + OUTPUT_ULTRA_P_OPTION
+                + "  : to output ultra-paralogs (species specific expansions/paralogs)" );
+        System.out.println( "  -" + CUTOFF_ULTRA_P_OPTION + " : cutoff for ultra-paralog output (default: 50)" );
         System.out.println();
+        System.out.println( " Sort" );
+        System.out.println( RIO.getOrderHelp().toString() );
+        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 outfile -q=BCL2_HUMAN -t=outtable -u -cu=60 -co=60\"" );
+        System.out.println( "  \"rio gene_trees.nh species.xml -t=outtable\"" );
         System.out.println();
+        System.exit( -1 );
     }
 }