inprogress
[jalview.git] / forester / java / src / org / forester / application / decorator.java
index 18966af..d4bc1e4 100644 (file)
@@ -21,7 +21,7 @@
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/forester
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.application;
 
@@ -35,6 +35,8 @@ import org.forester.io.parsers.PhylogenyParser;
 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.PhylogenyMethods.DESCENDANT_SORT_PRIORITY;
 import org.forester.phylogeny.data.Identifier;
 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
 import org.forester.phylogeny.factories.PhylogenyFactory;
@@ -59,6 +61,8 @@ public final class decorator {
     final static private String TREE_NAME_OPTION                        = "pn";
     final static private String TREE_ID_OPTION                          = "pi";
     final static private String TREE_DESC_OPTION                        = "pd";
+    final static private String MIDPOINT_ROOT_OPTION                    = "mp";
+    final static private String ORDER_TREE_OPTION                       = "or";
     final static private String EXTRACT_BRACKETED_SCIENTIC_NAME_OPTION  = "sn";
     final static private String EXTRACT_BRACKETED_TAXONOMIC_CODE_OPTION = "tc";
     final static private String PROCESS_NAME_INTELLIGENTLY_OPTION       = "x";
@@ -69,56 +73,10 @@ public final class decorator {
     final static private String KEY_COLUMN                              = "k";
     final static private String VALUE_COLUMN                            = "v";
     final static private String MAPPING_FILE_SEPARATOR_OPTION           = "s";
-    final static private String MAPPING_FILE_SEPARATOR_DEFAULT          = ": ";
+    final static private char   MAPPING_FILE_SEPARATOR_DEFAULT          = '\t';
     final static private String PRG_NAME                                = "decorator";
-    final static private String PRG_VERSION                             = "1.11";
-    final static private String PRG_DATE                                = "2012.09.15";
-
-    private static void argumentsError() {
-        System.out.println();
-        System.out.println( decorator.PRG_NAME + " -" + ADVANCED_TABLE_OPTION + " | -f=<c> <phylogenies infile> "
-                + "[mapping table file] <phylogenies outfile>" );
-        System.out.println();
-        System.out.println( "options:" );
-        System.out.println();
-        System.out.println( " -" + ADVANCED_TABLE_OPTION + " : table instead of one to one map (-f=<c>)" );
-        System.out.println( " -r=<n> : allow to remove up to n characters from the end of the names" );
-        System.out.println( "          in phylogenies infile if not found (in map) otherwise" );
-        System.out.println( " -p     : picky, fails if node name not found in mapping table" );
-        System.out.println( " -" + TREE_NAME_OPTION + "=<s>: name for the phylogeny" );
-        System.out.println( " -" + TREE_ID_OPTION + "=<s>: identifier for the phylogeny (in the form provider:value)" );
-        System.out.println( " -" + TREE_DESC_OPTION + "=<s>: description for phylogenies" );
-        System.out.println();
-        System.out.println();
-        System.out.println( "advanced options, only available if -" + ADVANCED_TABLE_OPTION + " is not used:" );
-        System.out.println();
-        System.out.println( " -f=<c> : field to be replaced: " + NODE_NAME_FIELD + " : node name" );
-        System.out.println( "                                " + SEQUENCE_ANNOTATION_DESC
-                + " : sequence annotation description" );
-        System.out.println( "                                " + DS_FILED + " : domain structure" );
-        System.out.println( "                                " + TAXONOMY_CODE_FIELD + " : taxonomy code" );
-        System.out.println( "                                " + TAXONOMY_SCIENTIFIC_NAME_FIELD
-                + ": taxonomy scientific name" );
-        System.out.println( "                                " + SEQUENCE_NAME_FIELD + " : sequence name" );
-        System.out.println( " -k=<n> : key column in mapping table (0 based)," );
-        System.out.println( "          names of the node to be decorated - default is 0" );
-        System.out.println( " -v=<n> : value column in mapping table (0 based)," );
-        System.out.println( "          data which with to decorate - default is 1" );
-        System.out.println( " -" + EXTRACT_BRACKETED_SCIENTIC_NAME_OPTION
-                + "    : to extract bracketed scientific names, e.g. [Nematostella vectensis]" );
-        System.out.println( " -" + EXTRACT_BRACKETED_TAXONOMIC_CODE_OPTION
-                + "    : to extract bracketed taxonomic codes, e.g. [NEMVE]" );
-        System.out.println( " -s=<c> : column separator in mapping file, default is \""
-                + decorator.MAPPING_FILE_SEPARATOR_DEFAULT + "\"" );
-        System.out.println( " -x     : process name \"intelligently\" (only for -f=n)" );
-        System.out.println( " -" + decorator.PROCESS_SIMILAR_TO_OPTION
-                + "    : process name \"intelligently\" and process information after \"similar to\" (only for -f=n)" );
-        System.out.println( " -c     : cut name after first space (only for -f=n)" );
-        System.out.println( " -" + decorator.TRIM_AFTER_TILDE_OPTION
-                + "     : trim node name to be replaced after tilde" );
-        System.out.println();
-        System.exit( -1 );
-    }
+    final static private String PRG_VERSION                             = "1.14";
+    final static private String PRG_DATE                                = "130426";
 
     public static void main( final String args[] ) {
         ForesterUtil.printProgramInformation( decorator.PRG_NAME, decorator.PRG_VERSION, decorator.PRG_DATE );
@@ -153,10 +111,13 @@ public final class decorator {
         allowed_options.add( decorator.VALUE_COLUMN );
         allowed_options.add( decorator.MAPPING_FILE_SEPARATOR_OPTION );
         allowed_options.add( decorator.EXTRACT_BRACKETED_SCIENTIC_NAME_OPTION );
+        allowed_options.add( decorator.EXTRACT_BRACKETED_TAXONOMIC_CODE_OPTION );
         allowed_options.add( decorator.TREE_NAME_OPTION );
         allowed_options.add( decorator.TREE_ID_OPTION );
         allowed_options.add( decorator.TREE_DESC_OPTION );
         allowed_options.add( decorator.TRIM_AFTER_TILDE_OPTION );
+        allowed_options.add( decorator.ORDER_TREE_OPTION );
+        allowed_options.add( decorator.MIDPOINT_ROOT_OPTION );
         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
         if ( dissallowed_options.length() > 0 ) {
             ForesterUtil.fatalError( decorator.PRG_NAME, "unknown option(s): " + dissallowed_options );
@@ -171,12 +132,12 @@ public final class decorator {
             }
         }
         final boolean picky = cla.isOptionSet( decorator.PICKY_OPTION );
-        String separator = decorator.MAPPING_FILE_SEPARATOR_DEFAULT;
+        char separator = decorator.MAPPING_FILE_SEPARATOR_DEFAULT;
         if ( cla.isOptionSet( decorator.MAPPING_FILE_SEPARATOR_OPTION ) ) {
             if ( advanced_table ) {
                 argumentsError();
             }
-            separator = cla.getOptionValue( decorator.MAPPING_FILE_SEPARATOR_OPTION );
+            separator = cla.getOptionValueAsChar( decorator.MAPPING_FILE_SEPARATOR_OPTION );
         }
         int key_column = 0;
         int value_column = 1;
@@ -189,6 +150,8 @@ public final class decorator {
         boolean extract_bracketed_scientific_name = false;
         boolean extract_bracketed_tax_code = false;
         boolean trim_after_tilde = false;
+        boolean order_tree = false;
+        boolean midpoint_root = false;
         String tree_name = "";
         String tree_id = "";
         String tree_desc = "";
@@ -254,6 +217,12 @@ public final class decorator {
                 numbers_of_chars_allowed_to_remove_if_not_found_in_map = cla
                         .getOptionValueAsInt( decorator.ALLOW_REMOVAL_OF_CHARS_OPTION );
             }
+            if ( cla.isOptionSet( decorator.MIDPOINT_ROOT_OPTION ) ) {
+                midpoint_root = true;
+            }
+            if ( cla.isOptionSet( decorator.ORDER_TREE_OPTION ) ) {
+                order_tree = true;
+            }
             if ( cla.isOptionSet( decorator.FIELD_OPTION ) ) {
                 field_str = cla.getOptionValue( decorator.FIELD_OPTION );
                 if ( field_str.equals( NODE_NAME_FIELD ) ) {
@@ -322,7 +291,7 @@ public final class decorator {
         if ( !advanced_table ) {
             BasicTable<String> mapping_table = null;
             try {
-                mapping_table = BasicTableParser.parse( mapping_infile, separator, false, true );
+                mapping_table = BasicTableParser.parse( mapping_infile, separator, true, false );
             }
             catch ( final Exception e ) {
                 ForesterUtil.fatalError( decorator.PRG_NAME,
@@ -391,6 +360,16 @@ public final class decorator {
         catch ( final Exception e ) {
             ForesterUtil.fatalError( decorator.PRG_NAME, e.getLocalizedMessage() );
         }
+        if ( midpoint_root || order_tree ) {
+            for( final Phylogeny phy : phylogenies ) {
+                if ( midpoint_root ) {
+                    PhylogenyMethods.midpointRoot( phy );
+                }
+                if ( order_tree ) {
+                    PhylogenyMethods.orderAppearance( phy.getRoot(), true, true, DESCENDANT_SORT_PRIORITY.TAXONOMY );
+                }
+            }
+        }
         try {
             final PhylogenyWriter w = new PhylogenyWriter();
             w.toPhyloXML( phylogenies, 0, phylogenies_outfile, ForesterUtil.getLineSeparator() );
@@ -402,4 +381,52 @@ public final class decorator {
         ForesterUtil.programMessage( PRG_NAME, "wrote: " + phylogenies_outfile );
         ForesterUtil.programMessage( PRG_NAME, "OK." );
     }
+
+    private static void argumentsError() {
+        System.out.println();
+        System.out.println( decorator.PRG_NAME + " -" + ADVANCED_TABLE_OPTION + " | -f=<c> <phylogenies infile> "
+                + "[mapping table file] <phylogenies outfile>" );
+        System.out.println();
+        System.out.println( "options:" );
+        System.out.println();
+        System.out.println( " -" + ADVANCED_TABLE_OPTION + " : table instead of one to one map (-f=<c>)" );
+        System.out.println( " -r=<n> : allow to remove up to n characters from the end of the names" );
+        System.out.println( "          in phylogenies infile if not found (in map) otherwise" );
+        System.out.println( " -p     : picky, fails if node name not found in mapping table" );
+        System.out.println( " -" + TREE_NAME_OPTION + "=<s>: name for the phylogeny" );
+        System.out.println( " -" + TREE_ID_OPTION + "=<s>: identifier for the phylogeny (in the form provider:value)" );
+        System.out.println( " -" + TREE_DESC_OPTION + "=<s>: description for phylogenies" );
+        System.out.println();
+        System.out.println();
+        System.out.println( "advanced options, only available if -" + ADVANCED_TABLE_OPTION + " is not used:" );
+        System.out.println();
+        System.out.println( " -f=<c> : field to be replaced: " + NODE_NAME_FIELD + " : node name" );
+        System.out.println( "                                " + SEQUENCE_ANNOTATION_DESC
+                + " : sequence annotation description" );
+        System.out.println( "                                " + DS_FILED + " : domain structure" );
+        System.out.println( "                                " + TAXONOMY_CODE_FIELD + " : taxonomy code" );
+        System.out.println( "                                " + TAXONOMY_SCIENTIFIC_NAME_FIELD
+                + ": taxonomy scientific name" );
+        System.out.println( "                                " + SEQUENCE_NAME_FIELD + " : sequence name" );
+        System.out.println( " -k=<n> : key column in mapping table (0 based)," );
+        System.out.println( "          names of the node to be decorated - default is 0" );
+        System.out.println( " -v=<n> : value column in mapping table (0 based)," );
+        System.out.println( "          data which with to decorate - default is 1" );
+        System.out.println( " -" + EXTRACT_BRACKETED_SCIENTIC_NAME_OPTION
+                + "    : to extract bracketed scientific names, e.g. [Nematostella vectensis]" );
+        System.out.println( " -" + EXTRACT_BRACKETED_TAXONOMIC_CODE_OPTION
+                + "    : to extract bracketed taxonomic codes, e.g. [NEMVE]" );
+        System.out.println( " -s=<c> : column separator in mapping file, default is \""
+                + decorator.MAPPING_FILE_SEPARATOR_DEFAULT + "\"" );
+        System.out.println( " -x     : process name \"intelligently\" (only for -f=n)" );
+        System.out.println( " -" + decorator.PROCESS_SIMILAR_TO_OPTION
+                + "    : process name \"intelligently\" and process information after \"similar to\" (only for -f=n)" );
+        System.out.println( " -c     : cut name after first space (only for -f=n)" );
+        System.out.println( " -" + decorator.TRIM_AFTER_TILDE_OPTION
+                + "     : trim node name to be replaced after tilde" );
+        System.out.println( " -" + decorator.MIDPOINT_ROOT_OPTION + "     : to midpoint-root the tree" );
+        System.out.println( " -" + decorator.ORDER_TREE_OPTION + "     : to order tree branches" );
+        System.out.println();
+        System.exit( -1 );
+    }
 }