in progress...
[jalview.git] / forester / java / src / org / forester / application / cladinator.java
index 9e9c945..daf0224 100644 (file)
@@ -2,8 +2,8 @@
 // FORESTER -- software libraries and applications
 // for evolutionary biology research and applications.
 //
-// Copyright (C) 2008-2009 Christian M. Zmasek
-// Copyright (C) 2008-2009 Burnham Institute for Medical Research
+// Copyright (C) 2017 Christian M. Zmasek
+// Copyright (C) 2017 J. Craig Venter Institute
 // All rights reserved
 //
 // This library is free software; you can redistribute it and/or
 // License along with this library; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
-// Contact: phylosoft @ gmail . com
+// Contact: phyloxml @ gmail . com
 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.application;
 
 import java.io.File;
 import java.io.IOException;
+import java.text.DecimalFormat;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.forester.clade_analysis.AnalysisSingle;
+import org.forester.clade_analysis.ResultSingle;
 import org.forester.io.parsers.PhylogenyParser;
-import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
 import org.forester.io.parsers.util.ParserUtils;
-import org.forester.io.writers.PhylogenyWriter;
 import org.forester.phylogeny.Phylogeny;
-import org.forester.phylogeny.PhylogenyMethods;
-import org.forester.phylogeny.PhylogenyNode;
-import org.forester.phylogeny.data.Taxonomy;
 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
 import org.forester.phylogeny.factories.PhylogenyFactory;
 import org.forester.util.CommandLineArguments;
 import org.forester.util.ForesterUtil;
 
-public class cladinator {
+public final class cladinator {
 
-    final static private String PRG_NAME      = "cladinator";
-    final static private String PRG_VERSION   = "0.100";
-    final static private String PRG_DATE      = "170721";
-    final static private String PRG_DESC      = "clades within clades";
-    final static private String E_MAIL        = "phyloxml@gmail.com";
-    final static private String WWW           = "https://sites.google.com/site/cmzmasek/home/software/forester";
-    final static private String HELP_OPTION_1 = "help";
-    final static private String HELP_OPTION_2 = "h";
+    final static private String        PRG_NAME      = "cladinator";
+    final static private String        PRG_VERSION   = "0.101";
+    final static private String        PRG_DATE      = "170810";
+    final static private String        PRG_DESC      = "clades within clades -- analysis of pplacer type outputs";
+    final static private String        E_MAIL        = "phyloxml@gmail.com";
+    final static private String        WWW           = "https://sites.google.com/site/cmzmasek/home/software/forester";
+    final static private String        HELP_OPTION_1 = "help";
+    final static private String        HELP_OPTION_2 = "h";
+    final static private String        SEP_OPTION    = "s";
+    private final static DecimalFormat df2           = new DecimalFormat( ".##" );
 
     public static void main( final String args[] ) {
         try {
@@ -75,7 +75,7 @@ public class cladinator {
                 print_help();
                 System.exit( 0 );
             }
-            else if ( ( args.length != 2 ) ) {
+            else if ( ( args.length != 2 && args.length != 3 ) ) {
                 System.out.println();
                 System.out.println( "Wrong number of arguments." );
                 System.out.println();
@@ -83,71 +83,99 @@ public class cladinator {
                 System.exit( -1 );
             }
             final List<String> allowed_options = new ArrayList<String>();
+            allowed_options.add( SEP_OPTION );
+            final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
+            if ( dissallowed_options.length() > 0 ) {
+                ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
+            }
+            final String separator;
+            if ( cla.isOptionSet( SEP_OPTION ) ) {
+                separator = cla.getOptionValue( SEP_OPTION );
+            }
+            else {
+                separator = null;
+            }
             final File intreefile = cla.getFile( 0 );
             final String query = cla.getName( 1 );
             System.out.println( "Input tree: " + intreefile );
-            System.out.println( "Query:      " + query );
+            System.out.println( "Query     : " + query );
+            if ( !ForesterUtil.isEmpty( separator ) ) {
+                System.out.println( "Separator : " + separator );
+            }
+            else {
+                System.out.println( "Separator : none" );
+            }
             Phylogeny p = null;
             try {
                 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
                 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( intreefile, true );
                 p = factory.create( intreefile, pp )[ 0 ];
             }
-            catch ( final Exception e ) {
+            catch ( final IOException e ) {
                 System.out.println( "\nCould not read \"" + intreefile + "\" [" + e.getMessage() + "]\n" );
                 System.exit( -1 );
             }
-            execute( p, query );
-        }
-        catch ( final Exception e ) {
-            ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
-        }
-    }
-
-    private static void execute( final Phylogeny p, final String query ) {
-        final PhylogenyNode qnode = p.getNode( query );
-        if ( qnode.isRoot() ) {
-            throw new IllegalStateException( "Unexpected error: Query " + query
-                    + " is root. This should have never happened" );
-        }
-        if ( qnode.getParent().isRoot() ) {
-            throw new IllegalStateException( "Unexpected error: Parent of query " + query
-                    + " is root. This should have never happened" );
-        }
-        final PhylogenyNode qnode_pp = qnode.getParent().getParent();
-        final List<PhylogenyNode> qnode_ext_nodes = qnode_pp.getAllExternalDescendants();
-        final int lec_ext_nodes = qnode_ext_nodes.size() - 1;
-        final int p_ext_nodes = p.getNumberOfExternalNodes() - 1;
-        final double lec_ratio = ( 100.0 * lec_ext_nodes ) / p_ext_nodes;
-        final List<String> qnode_ext_nodes_names = new ArrayList<String>();
-        for( final PhylogenyNode qnode_ext_node : qnode_ext_nodes ) {
-            String name = qnode_ext_node.getName();
-            if ( ForesterUtil.isEmptyTrimmed( name ) ) {
-                throw new IllegalArgumentException( "external node(s) with empty names found" );
-            }
-            name = name.trim();
-            if ( !name.equals( query ) ) {
-                qnode_ext_nodes_names.add( name );
+            final ResultSingle res = AnalysisSingle.execute( p, query, separator );
+            System.out.println();
+            System.out.println( "Result:" );
+            System.out.println( "Query                        : " + query );
+            System.out.print( "Greatest Common Prefix       : " + res.getGreatestCommonPrefix() );
+            if ( !ForesterUtil.isEmpty( res.getGreatestCommonPrefix() )
+                    && !ForesterUtil.isEmpty( res.getGreatestCommonCladeSubtreeConfidence() ) ) {
+                System.out.println( "\t(" + res.getGreatestCommonCladeSubtreeConfidence() + ")" );
             }
+            else {
+                System.out.println();
+            }
+            System.out.print( "Greatest Common Prefix Up    : " + res.getGreatestCommonPrefixUp() );
+            if ( !ForesterUtil.isEmpty( res.getGreatestCommonPrefixUp() )
+                    && !ForesterUtil.isEmpty( res.getGreatestCommonCladeUpSubtreeConfidence() ) ) {
+                System.out.println( "\t(" + res.getGreatestCommonCladeUpSubtreeConfidence() + ")" );
+            }
+            else {
+                System.out.println();
+            }
+            System.out.print( "Greatest Common Prefix Down  : " + res.getGreatestCommonPrefixDown() );
+            if ( !ForesterUtil.isEmpty( res.getGreatestCommonPrefixDown() )
+                    && !ForesterUtil.isEmpty( res.getGreatestCommonCladeDownSubtreeConfidence() ) ) {
+                System.out.println( "\t(" + res.getGreatestCommonCladeDownSubtreeConfidence() + ")" );
+            }
+            else {
+                System.out.println();
+            }
+            System.out.println( "Least Encompassing Clade size: " + res.getLeastEncompassingCladeSize()
+                    + " external nodes" );
+            final double lec_ratio = ( 100.0 * res.getLeastEncompassingCladeSize() ) / res.getTreeSize();
+            System.out.println( "Least Encompassing Clade size: " + df2.format( lec_ratio ) + "%" );
+            System.out.println( "Total tree size              : " + res.getTreeSize() + " external nodes" );
+            if ( res.getWarnings().size() > 0 ) {
+                System.out.println( "Warnings:" );
+                for( final String s : res.getWarnings() ) {
+                    System.out.println( s );
+                }
+            }
+            System.out.println();
         }
-        final String greatest_common_prefix = ForesterUtil.greatestCommonPrefix( qnode_ext_nodes_names );
-        System.out.println( );
-        System.out.println( "Results:");
-        if ( greatest_common_prefix.length() < 1 ) {
-            System.out.println( "WARNING: No greatest common prefix" );
-        }
-        else {
-            System.out.println( "Greatest common prefix: " + greatest_common_prefix );
+        catch ( final IllegalArgumentException e ) {
+            ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
         }
-        if ( qnode_pp.isRoot() ) {
-            System.out.println( "WARNING: Least Encompassing Clade is entire tree" );
+        catch ( final Exception e ) {
+            e.printStackTrace();
+            ForesterUtil.fatalError( PRG_NAME, "Unexpected errror!" );
         }
-        System.out.println( "Least Encompassing Clade has " + lec_ext_nodes + " external nodes (" +lec_ratio + "% of a total of "+ p_ext_nodes +")" );
     }
 
     private final static void print_help() {
-        System.out.println( "Usage: " + PRG_NAME
-                + " <gene tree file> <query>" );
+        System.out.println( "Usage:" );
+        System.out.println();
+        System.out.println( PRG_NAME + " [options] <gene tree file> <query>" );
+        System.out.println();
+        System.out.println( " options:" );
+        System.out.println( "  -" + SEP_OPTION + "=<separator>: the separator to be used" );
+        System.out.println();
+        System.out.println( "Example:" );
+        System.out.println();
+        System.out.println( " " + PRG_NAME + " -s=. my_tree.xml A.1.1.1" );
         System.out.println();
     }
 }