in progress
authorcmzmasek@gmail.com <cmzmasek@gmail.com@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Sun, 16 Sep 2012 01:48:31 +0000 (01:48 +0000)
committercmzmasek@gmail.com <cmzmasek@gmail.com@ca865154-3058-d1c3-3e42-d8f55a55bdbd>
Sun, 16 Sep 2012 01:48:31 +0000 (01:48 +0000)
forester/java/src/org/forester/application/decorator.java
forester/java/src/org/forester/application/pccx.java
forester/java/src/org/forester/io/parsers/SymmetricalDistanceMatrixParser.java
forester/java/src/org/forester/test/Test.java
forester/java/src/org/forester/tools/PhylogenyDecorator.java
forester/java/src/org/forester/util/BasicTableParser.java

index d96d868..4f77bde 100644 (file)
@@ -73,7 +73,7 @@ public final class decorator {
     final static private boolean USE_FIRST_SEPARATOR_ONLY               = true;
     final static private String  PRG_NAME                               = "decorator";
     final static private String  PRG_VERSION                            = "1.11";
-    final static private String  PRG_DATE                               = "2012.08.08";
+    final static private String  PRG_DATE                               = "2012.09.15";
 
     private static void argumentsError() {
         System.out.println();
@@ -85,7 +85,7 @@ public final class decorator {
         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     : for picky, fails if node name not found in mapping table, default is off" );
+        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" );
@@ -113,7 +113,8 @@ public final class decorator {
         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 after tilde" );
+        System.out.println( " -" + decorator.TRIM_AFTER_TILDE_OPTION
+                + "     : trim node name to be replaced after tilde" );
         System.out.println();
         System.exit( -1 );
     }
@@ -313,12 +314,13 @@ public final class decorator {
         if ( !advanced_table ) {
             BasicTable<String> mapping_table = null;
             try {
-                mapping_table = BasicTableParser.parse( mapping_infile, separator, decorator.USE_FIRST_SEPARATOR_ONLY );
+                mapping_table = BasicTableParser.parse( mapping_infile, separator, false, true );
             }
             catch ( final Exception e ) {
                 ForesterUtil.fatalError( decorator.PRG_NAME,
                                          "failed to read [" + mapping_infile + "] [" + e.getMessage() + "]" );
             }
+            System.out.println( mapping_table.toString() );
             if ( ( key_column < 0 ) || ( key_column >= mapping_table.getNumberOfColumns() ) ) {
                 ForesterUtil.fatalError( decorator.PRG_NAME, "illegal value for key column" );
             }
index e52c116..44839c9 100644 (file)
@@ -176,7 +176,7 @@ public class pccx {
                 if ( !ForesterUtil.isEmpty( error ) ) {
                     ForesterUtil.fatalError( pccx.PRG_NAME, error );
                 }
-                intable = BasicTableParser.parse( infile, " ", false );
+                intable = BasicTableParser.parse( infile, " ", false, false );
             }
             catch ( final IOException e ) {
                 ForesterUtil.fatalError( pccx.PRG_NAME, "failed to read \"" + infile + "\" [" + e.getMessage() + "]" );
index 991329b..538257c 100644 (file)
@@ -98,7 +98,12 @@ public class SymmetricalDistanceMatrixParser {
 
     public DistanceMatrix[] parse( final Object source ) throws IOException {
         reset();
-        final List<BasicTable<String>> tables = BasicTableParser.parse( source, VALUE_SEPARATOR, false, COMMENT, true );
+        final List<BasicTable<String>> tables = BasicTableParser.parse( source,
+                                                                        VALUE_SEPARATOR,
+                                                                        false,
+                                                                        false,
+                                                                        COMMENT,
+                                                                        true );
         final DistanceMatrix[] distance_matrices = new DistanceMatrix[ tables.size() ];
         int i = 0;
         for( final BasicTable<String> table : tables ) {
index 9c67685..5961561 100644 (file)
@@ -1733,6 +1733,7 @@ public final class Test {
             final List<BasicTable<String>> tl = BasicTableParser.parse( source2.toString(),
                                                                         ";",
                                                                         false,
+                                                                        false,
                                                                         "comment:",
                                                                         false );
             if ( tl.size() != 2 ) {
index 891864b..3191201 100644 (file)
@@ -234,7 +234,7 @@ public final class PhylogenyDecorator {
                                  final boolean trim_after_tilde ) throws IllegalArgumentException,
             PhyloXmlDataFormatException {
         if ( extract_bracketed_scientific_name && ( field == FIELD.TAXONOMY_SCIENTIFIC_NAME ) ) {
-            throw new IllegalArgumentException( "Attempt to extract bracketed scientific name together with data field pointing to scientific name" );
+            throw new IllegalArgumentException( "attempt to extract bracketed scientific name together with data field pointing to scientific name" );
         }
         for( final PhylogenyNodeIterator iter = phylogeny.iteratorPostorder(); iter.hasNext(); ) {
             final PhylogenyNode node = iter.next();
@@ -276,7 +276,7 @@ public final class PhylogenyDecorator {
                         new_value = new_value.trim();
                         new_value.replaceAll( "/\\s+/", " " );
                         if ( extract_bracketed_scientific_name && new_value.endsWith( "]" ) ) {
-                            extractBracketedScientificNames( node, new_value );
+                            new_value = extractBracketedScientificNames( node, new_value );
                         }
                         switch ( field ) {
                             case SEQUENCE_ANNOTATION_DESC:
@@ -445,11 +445,12 @@ public final class PhylogenyDecorator {
         return name;
     }
 
-    private static void extractBracketedScientificNames( final PhylogenyNode node, final String new_value ) {
+    private static String extractBracketedScientificNames( final PhylogenyNode node, final String new_value ) {
         final int i = new_value.lastIndexOf( "[" );
         final String scientific_name = new_value.substring( i + 1, new_value.length() - 1 );
         AptxUtil.ensurePresenceOfTaxonomy( node );
         node.getNodeData().getTaxonomy().setScientificName( scientific_name );
+        return new_value.substring( 0, i - 1 ).trim();
     }
 
     private static String extractIntermediate( final Map<String, String> intermediate_map, final String name ) {
@@ -489,7 +490,7 @@ public final class PhylogenyDecorator {
             throws IOException {
         final Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
         BasicTable<String> mapping_table = null;
-        mapping_table = BasicTableParser.parse( mapping_table_file, "\t", false );
+        mapping_table = BasicTableParser.parse( mapping_table_file, "\t", false, false );
         for( int row = 0; row < mapping_table.getNumberOfRows(); ++row ) {
             final Map<String, String> row_map = new HashMap<String, String>();
             String name = null;
index 74dd567..bc369bf 100644 (file)
@@ -39,15 +39,18 @@ public class BasicTableParser {
     }
 
     public static BasicTable<String> parse( final Object source, final String column_delimiter ) throws IOException {
-        return BasicTableParser.parse( source, column_delimiter, false, START_OF_COMMENT_LINE_DEFAULT, false ).get( 0 );
+        return BasicTableParser.parse( source, column_delimiter, false, false, START_OF_COMMENT_LINE_DEFAULT, false )
+                .get( 0 );
     }
 
     public static BasicTable<String> parse( final Object source,
                                             final String column_delimiter,
-                                            final boolean use_first_separator_only ) throws IOException {
+                                            final boolean use_first_separator_only,
+                                            final boolean use_last_separator_only ) throws IOException {
         return BasicTableParser.parse( source,
                                        column_delimiter,
                                        use_first_separator_only,
+                                       use_last_separator_only,
                                        START_OF_COMMENT_LINE_DEFAULT,
                                        false ).get( 0 );
     }
@@ -55,6 +58,7 @@ public class BasicTableParser {
     public static List<BasicTable<String>> parse( final Object source,
                                                   final String column_delimiter,
                                                   final boolean use_first_separator_only,
+                                                  final boolean use_last_separator_only,
                                                   final String start_of_comment_line,
                                                   final boolean tables_separated_by_single_string_line )
             throws IOException {
@@ -85,25 +89,36 @@ public class BasicTableParser {
             else if ( !ForesterUtil.isEmpty( line )
                     && ( !use_start_of_comment_line || !line.startsWith( start_of_comment_line ) ) ) {
                 saw_first_table = true;
-                final StringTokenizer st = new StringTokenizer( line, column_delimiter );
-                int col = 0;
-                if ( st.hasMoreTokens() ) {
-                    table.setValue( col++, row, st.nextToken().trim() );
-                }
-                if ( !use_first_separator_only ) {
-                    while ( st.hasMoreTokens() ) {
-                        table.setValue( col++, row, st.nextToken().trim() );
+                if ( use_last_separator_only ) {
+                    String e[] = line.split( column_delimiter );
+                    final StringBuffer rest = new StringBuffer();
+                    for( int i = 0; i < e.length - 1; ++i ) {
+                        rest.append( e[ i ].trim() );
                     }
+                    table.setValue( 0, row, e[ e.length - 1 ] );
+                    table.setValue( 1, row, rest.toString() );
                 }
                 else {
-                    final StringBuffer rest = new StringBuffer();
-                    while ( st.hasMoreTokens() ) {
-                        rest.append( st.nextToken() );
+                    final StringTokenizer st = new StringTokenizer( line, column_delimiter );
+                    int col = 0;
+                    if ( st.hasMoreTokens() ) {
+                        table.setValue( col++, row, st.nextToken().trim() );
+                    }
+                    if ( use_first_separator_only ) {
+                        final StringBuffer rest = new StringBuffer();
+                        while ( st.hasMoreTokens() ) {
+                            rest.append( st.nextToken() );
+                        }
+                        table.setValue( col++, row, rest.toString() );
+                    }
+                    else {
+                        while ( st.hasMoreTokens() ) {
+                            table.setValue( col++, row, st.nextToken().trim() );
+                        }
                     }
-                    table.setValue( col++, row, rest.toString().trim() );
                 }
-                ++row;
             }
+            ++row;
         }
         if ( !table.isEmpty() ) {
             tables.add( table );