From c4cf3c01b3cbb89d98f29418444287ca8703a781 Mon Sep 17 00:00:00 2001 From: "cmzmasek@gmail.com" Date: Sun, 16 Sep 2012 01:48:31 +0000 Subject: [PATCH] in progress --- .../src/org/forester/application/decorator.java | 10 +++-- .../java/src/org/forester/application/pccx.java | 2 +- .../parsers/SymmetricalDistanceMatrixParser.java | 7 ++- forester/java/src/org/forester/test/Test.java | 1 + .../src/org/forester/tools/PhylogenyDecorator.java | 9 ++-- .../src/org/forester/util/BasicTableParser.java | 45 +++++++++++++------- 6 files changed, 49 insertions(+), 25 deletions(-) diff --git a/forester/java/src/org/forester/application/decorator.java b/forester/java/src/org/forester/application/decorator.java index d96d868..4f77bde 100644 --- a/forester/java/src/org/forester/application/decorator.java +++ b/forester/java/src/org/forester/application/decorator.java @@ -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=)" ); System.out.println( " -r= : 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 + "=: name for the phylogeny" ); System.out.println( " -" + TREE_ID_OPTION + "=: identifier for the phylogeny (in the form provider:value)" ); System.out.println( " -" + TREE_DESC_OPTION + "=: 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 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" ); } diff --git a/forester/java/src/org/forester/application/pccx.java b/forester/java/src/org/forester/application/pccx.java index e52c116..44839c9 100644 --- a/forester/java/src/org/forester/application/pccx.java +++ b/forester/java/src/org/forester/application/pccx.java @@ -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() + "]" ); diff --git a/forester/java/src/org/forester/io/parsers/SymmetricalDistanceMatrixParser.java b/forester/java/src/org/forester/io/parsers/SymmetricalDistanceMatrixParser.java index 991329b..538257c 100644 --- a/forester/java/src/org/forester/io/parsers/SymmetricalDistanceMatrixParser.java +++ b/forester/java/src/org/forester/io/parsers/SymmetricalDistanceMatrixParser.java @@ -98,7 +98,12 @@ public class SymmetricalDistanceMatrixParser { public DistanceMatrix[] parse( final Object source ) throws IOException { reset(); - final List> tables = BasicTableParser.parse( source, VALUE_SEPARATOR, false, COMMENT, true ); + final List> tables = BasicTableParser.parse( source, + VALUE_SEPARATOR, + false, + false, + COMMENT, + true ); final DistanceMatrix[] distance_matrices = new DistanceMatrix[ tables.size() ]; int i = 0; for( final BasicTable table : tables ) { diff --git a/forester/java/src/org/forester/test/Test.java b/forester/java/src/org/forester/test/Test.java index 9c67685..5961561 100644 --- a/forester/java/src/org/forester/test/Test.java +++ b/forester/java/src/org/forester/test/Test.java @@ -1733,6 +1733,7 @@ public final class Test { final List> tl = BasicTableParser.parse( source2.toString(), ";", false, + false, "comment:", false ); if ( tl.size() != 2 ) { diff --git a/forester/java/src/org/forester/tools/PhylogenyDecorator.java b/forester/java/src/org/forester/tools/PhylogenyDecorator.java index 891864b..3191201 100644 --- a/forester/java/src/org/forester/tools/PhylogenyDecorator.java +++ b/forester/java/src/org/forester/tools/PhylogenyDecorator.java @@ -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 intermediate_map, final String name ) { @@ -489,7 +490,7 @@ public final class PhylogenyDecorator { throws IOException { final Map> map = new HashMap>(); BasicTable 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 row_map = new HashMap(); String name = null; diff --git a/forester/java/src/org/forester/util/BasicTableParser.java b/forester/java/src/org/forester/util/BasicTableParser.java index 74dd567..bc369bf 100644 --- a/forester/java/src/org/forester/util/BasicTableParser.java +++ b/forester/java/src/org/forester/util/BasicTableParser.java @@ -39,15 +39,18 @@ public class BasicTableParser { } public static BasicTable 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 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> 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 ); -- 1.7.10.2