in progress
[jalview.git] / forester / java / src / org / forester / application / decorator.java
index acf8c50..8d04470 100644 (file)
@@ -28,13 +28,17 @@ package org.forester.application;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 
 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 +63,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";
@@ -71,8 +77,8 @@ public final class decorator {
     final static private String MAPPING_FILE_SEPARATOR_OPTION           = "s";
     final static private char   MAPPING_FILE_SEPARATOR_DEFAULT          = '\t';
     final static private String PRG_NAME                                = "decorator";
-    final static private String PRG_VERSION                             = "1.13";
-    final static private String PRG_DATE                                = "2013.01.19";
+    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 );
@@ -112,6 +118,8 @@ public final class decorator {
         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 );
@@ -144,6 +152,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 = "";
@@ -209,6 +219,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 ) ) {
@@ -289,7 +305,20 @@ public final class decorator {
             if ( ( value_column < 0 ) || ( value_column >= mapping_table.getNumberOfColumns() ) ) {
                 ForesterUtil.fatalError( decorator.PRG_NAME, "illegal value for value column" );
             }
+            if ( mapping_table.isEmpty() || mapping_table.getNumberOfColumns() < 1 ) {
+                ForesterUtil.fatalError( decorator.PRG_NAME, "mapping table is empty" );
+            }
+            if ( mapping_table.getNumberOfColumns() == 1 ) {
+                ForesterUtil.fatalError( decorator.PRG_NAME, "mapping table has only one column" );
+            }
             map = mapping_table.getColumnsAsMap( key_column, value_column );
+            Iterator<Entry<String, String>> iter = map.entrySet().iterator();
+            System.out.println();
+            while ( iter.hasNext() ) {
+                Entry<String, String> e = iter.next();
+                System.out.println( e.getKey() + " => " + e.getValue() );
+            }
+            System.out.println();
         }
         if ( !ForesterUtil.isEmpty( tree_name ) || !ForesterUtil.isEmpty( tree_id )
                 || !ForesterUtil.isEmpty( tree_desc ) ) {
@@ -346,6 +375,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() );
@@ -400,6 +439,8 @@ public final class decorator {
         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 );
     }