2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.application;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
36 import java.util.Map.Entry;
38 import org.forester.io.parsers.FastaParser;
39 import org.forester.io.parsers.PhylogenyParser;
40 import org.forester.io.parsers.util.ParserUtils;
41 import org.forester.io.writers.PhylogenyWriter;
42 import org.forester.phylogeny.Phylogeny;
43 import org.forester.phylogeny.PhylogenyMethods;
44 import org.forester.phylogeny.PhylogenyMethods.DESCENDANT_SORT_PRIORITY;
45 import org.forester.phylogeny.data.Identifier;
46 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
47 import org.forester.phylogeny.factories.PhylogenyFactory;
48 import org.forester.sequence.Sequence;
49 import org.forester.tools.PhylogenyDecorator;
50 import org.forester.tools.PhylogenyDecorator.FIELD;
51 import org.forester.util.BasicTable;
52 import org.forester.util.BasicTableParser;
53 import org.forester.util.CommandLineArguments;
54 import org.forester.util.ForesterUtil;
56 public final class decorator {
58 private static final String SEQUENCE_NAME_FIELD = "s";
59 private static final String MOL_SEQ = "m";
60 private static final String TAXONOMY_CODE_FIELD = "c";
61 private static final String TAXONOMY_SCIENTIFIC_NAME_FIELD = "sn";
62 private static final String DS_FILED = "d";
63 private static final String SEQUENCE_ANNOTATION_DESC = "a";
64 private static final String NODE_NAME_FIELD = "n";
65 final static private String PICKY_OPTION = "p";
66 final static private String FIELD_OPTION = "f";
67 final static private String TRIM_AFTER_TILDE_OPTION = "t";
68 final static private String VERBOSE_OPTION = "ve";
69 final static private String TREE_NAME_OPTION = "pn";
70 final static private String TREE_ID_OPTION = "pi";
71 final static private String TREE_DESC_OPTION = "pd";
72 final static private String MIDPOINT_ROOT_OPTION = "mp";
73 final static private String ORDER_TREE_OPTION = "or";
74 final static private String EXTRACT_BRACKETED_SCIENTIC_NAME_OPTION = "sn";
75 final static private String EXTRACT_BRACKETED_TAXONOMIC_CODE_OPTION = "tc";
76 final static private String PROCESS_NAME_INTELLIGENTLY_OPTION = "x";
77 final static private String PROCESS_SIMILAR_TO_OPTION = "xs";
78 final static private String CUT_NAME_AFTER_FIRST_SPACE_OPTION = "c";
79 final static private String ALLOW_REMOVAL_OF_CHARS_OPTION = "r";
80 final static private String ADVANCED_TABLE_OPTION = "table";
81 final static private String KEY_COLUMN = "k";
82 final static private String VALUE_COLUMN = "v";
83 final static private String MAPPING_FILE_SEPARATOR_OPTION = "s";
84 final static private char MAPPING_FILE_SEPARATOR_DEFAULT = '\t';
85 final static private String PRG_NAME = "decorator";
86 final static private String PRG_VERSION = "1.16";
87 final static private String PRG_DATE = "131113";
89 public static void main( final String args[] ) {
90 ForesterUtil.printProgramInformation( decorator.PRG_NAME, decorator.PRG_VERSION, decorator.PRG_DATE );
91 if ( ( args.length < 4 ) || ( args.length > 12 ) ) {
92 decorator.argumentsError();
94 CommandLineArguments cla = null;
96 cla = new CommandLineArguments( args );
98 catch ( final Exception e ) {
99 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
101 if ( ( cla.getNumberOfNames() < 3 ) || ( cla.getNumberOfNames() > 4 ) ) {
102 decorator.argumentsError();
104 final File phylogenies_infile = cla.getFile( 0 );
105 final File mapping_infile = cla.getFile( 1 );
106 final File phylogenies_outfile = cla.getFile( 2 );
107 if ( phylogenies_outfile.exists() ) {
108 ForesterUtil.fatalError( PRG_NAME, "[" + phylogenies_outfile + "] already exists" );
110 final List<String> allowed_options = new ArrayList<String>();
111 allowed_options.add( decorator.ADVANCED_TABLE_OPTION );
112 allowed_options.add( decorator.PICKY_OPTION );
113 allowed_options.add( decorator.FIELD_OPTION );
114 allowed_options.add( decorator.PROCESS_NAME_INTELLIGENTLY_OPTION );
115 allowed_options.add( decorator.PROCESS_SIMILAR_TO_OPTION );
116 allowed_options.add( decorator.CUT_NAME_AFTER_FIRST_SPACE_OPTION );
117 allowed_options.add( decorator.ALLOW_REMOVAL_OF_CHARS_OPTION );
118 allowed_options.add( decorator.KEY_COLUMN );
119 allowed_options.add( decorator.VALUE_COLUMN );
120 allowed_options.add( decorator.MAPPING_FILE_SEPARATOR_OPTION );
121 allowed_options.add( decorator.EXTRACT_BRACKETED_SCIENTIC_NAME_OPTION );
122 allowed_options.add( decorator.EXTRACT_BRACKETED_TAXONOMIC_CODE_OPTION );
123 allowed_options.add( decorator.TREE_NAME_OPTION );
124 allowed_options.add( decorator.TREE_ID_OPTION );
125 allowed_options.add( decorator.TREE_DESC_OPTION );
126 allowed_options.add( decorator.TRIM_AFTER_TILDE_OPTION );
127 allowed_options.add( decorator.ORDER_TREE_OPTION );
128 allowed_options.add( decorator.MIDPOINT_ROOT_OPTION );
129 allowed_options.add( decorator.VERBOSE_OPTION );
130 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
131 if ( dissallowed_options.length() > 0 ) {
132 ForesterUtil.fatalError( decorator.PRG_NAME, "unknown option(s): " + dissallowed_options );
134 final boolean advanced_table = cla.isOptionSet( decorator.ADVANCED_TABLE_OPTION );
135 if ( !advanced_table ) {
136 final List<String> mandatory_options = new ArrayList<String>();
137 mandatory_options.add( decorator.FIELD_OPTION );
138 final String missing_options = cla.validateMandatoryOptionsAsString( mandatory_options );
139 if ( missing_options.length() > 0 ) {
140 ForesterUtil.fatalError( decorator.PRG_NAME, "missing option(s): " + missing_options );
143 final boolean picky = cla.isOptionSet( decorator.PICKY_OPTION );
144 char separator = decorator.MAPPING_FILE_SEPARATOR_DEFAULT;
145 if ( cla.isOptionSet( decorator.MAPPING_FILE_SEPARATOR_OPTION ) ) {
146 if ( advanced_table ) {
149 separator = cla.getOptionValueAsChar( decorator.MAPPING_FILE_SEPARATOR_OPTION );
152 int value_column = 1;
153 String field_str = "";
154 FIELD field = FIELD.NODE_NAME;
155 int numbers_of_chars_allowed_to_remove_if_not_found_in_map = -1;
156 boolean cut_name_after_space = false;
157 boolean process_name_intelligently = false;
158 boolean process_similar_to = false;
159 boolean extract_bracketed_scientific_name = false;
160 boolean extract_bracketed_tax_code = false;
161 boolean trim_after_tilde = false;
162 boolean order_tree = false;
163 boolean midpoint_root = false;
164 boolean verbose = false;
165 String tree_name = "";
167 String tree_desc = "";
169 if ( cla.isOptionSet( decorator.TREE_NAME_OPTION ) ) {
170 tree_name = cla.getOptionValueAsCleanString( decorator.TREE_NAME_OPTION );
172 if ( cla.isOptionSet( decorator.TREE_ID_OPTION ) ) {
173 tree_id = cla.getOptionValueAsCleanString( decorator.TREE_ID_OPTION );
175 if ( cla.isOptionSet( decorator.TREE_DESC_OPTION ) ) {
176 tree_desc = cla.getOptionValueAsCleanString( decorator.TREE_DESC_OPTION );
178 if ( cla.isOptionSet( decorator.EXTRACT_BRACKETED_SCIENTIC_NAME_OPTION ) ) {
179 if ( advanced_table ) {
182 extract_bracketed_scientific_name = true;
184 if ( cla.isOptionSet( decorator.EXTRACT_BRACKETED_TAXONOMIC_CODE_OPTION ) ) {
185 if ( advanced_table ) {
188 extract_bracketed_tax_code = true;
190 if ( cla.isOptionSet( decorator.KEY_COLUMN ) ) {
191 if ( advanced_table ) {
194 key_column = cla.getOptionValueAsInt( decorator.KEY_COLUMN );
196 if ( cla.isOptionSet( decorator.VALUE_COLUMN ) ) {
197 if ( advanced_table ) {
200 value_column = cla.getOptionValueAsInt( decorator.VALUE_COLUMN );
202 if ( cla.isOptionSet( decorator.CUT_NAME_AFTER_FIRST_SPACE_OPTION ) ) {
203 if ( advanced_table ) {
206 cut_name_after_space = true;
208 if ( cla.isOptionSet( decorator.PROCESS_NAME_INTELLIGENTLY_OPTION ) ) {
209 if ( advanced_table ) {
212 process_name_intelligently = true;
214 if ( cla.isOptionSet( decorator.PROCESS_SIMILAR_TO_OPTION ) ) {
215 if ( advanced_table ) {
218 process_similar_to = true;
220 if ( cla.isOptionSet( decorator.TRIM_AFTER_TILDE_OPTION ) ) {
221 if ( advanced_table ) {
224 trim_after_tilde = true;
226 if ( cla.isOptionSet( decorator.ALLOW_REMOVAL_OF_CHARS_OPTION ) ) {
227 numbers_of_chars_allowed_to_remove_if_not_found_in_map = cla
228 .getOptionValueAsInt( decorator.ALLOW_REMOVAL_OF_CHARS_OPTION );
230 if ( cla.isOptionSet( decorator.MIDPOINT_ROOT_OPTION ) ) {
231 midpoint_root = true;
233 if ( cla.isOptionSet( decorator.ORDER_TREE_OPTION ) ) {
236 if ( cla.isOptionSet( decorator.VERBOSE_OPTION ) ) {
239 if ( cla.isOptionSet( decorator.FIELD_OPTION ) ) {
240 field_str = cla.getOptionValue( decorator.FIELD_OPTION );
241 if ( field_str.equals( NODE_NAME_FIELD ) ) {
242 field = FIELD.NODE_NAME;
244 else if ( field_str.equals( SEQUENCE_ANNOTATION_DESC ) ) {
245 field = FIELD.SEQUENCE_ANNOTATION_DESC;
247 else if ( field_str.equals( DS_FILED ) ) {
248 field = FIELD.DOMAIN_STRUCTURE;
249 extract_bracketed_scientific_name = false;
250 extract_bracketed_tax_code = false;
252 else if ( field_str.equals( TAXONOMY_CODE_FIELD ) ) {
253 field = FIELD.TAXONOMY_CODE;
255 else if ( field_str.equals( SEQUENCE_NAME_FIELD ) ) {
256 field = FIELD.SEQUENCE_NAME;
258 else if ( field_str.equals( MOL_SEQ ) ) {
259 field = FIELD.MOL_SEQ;
261 else if ( field_str.equals( TAXONOMY_SCIENTIFIC_NAME_FIELD ) ) {
262 field = FIELD.TAXONOMY_SCIENTIFIC_NAME;
263 extract_bracketed_scientific_name = false;
264 extract_bracketed_tax_code = false;
267 ForesterUtil.fatalError( decorator.PRG_NAME, "unknown value for \"" + decorator.FIELD_OPTION
268 + "\" option: \"" + field_str + "\"" );
272 catch ( final Exception e ) {
273 ForesterUtil.fatalError( decorator.PRG_NAME, "error in command line: " + e.getMessage() );
275 if ( ( field != FIELD.NODE_NAME ) && ( cut_name_after_space || process_name_intelligently ) ) {
276 ForesterUtil.fatalError( decorator.PRG_NAME, "attempt to use -x or -c option without -f=n" );
278 if ( ( field != FIELD.NODE_NAME ) && process_similar_to ) {
279 ForesterUtil.fatalError( decorator.PRG_NAME, "attempt to use -" + decorator.PROCESS_SIMILAR_TO_OPTION
280 + " option without -f=n" );
282 if ( cut_name_after_space && process_name_intelligently ) {
283 ForesterUtil.fatalError( decorator.PRG_NAME, "attempt to use -x and -c option together" );
285 if ( process_similar_to && process_name_intelligently ) {
286 ForesterUtil.fatalError( decorator.PRG_NAME, "attempt to use -" + decorator.PROCESS_SIMILAR_TO_OPTION
287 + " and -x option together" );
289 if ( process_similar_to && cut_name_after_space ) {
290 ForesterUtil.fatalError( decorator.PRG_NAME, "attempt to use -" + decorator.PROCESS_SIMILAR_TO_OPTION
291 + " and -c option together" );
293 if ( extract_bracketed_scientific_name && extract_bracketed_tax_code ) {
296 Phylogeny[] phylogenies = null;
298 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
299 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( phylogenies_infile, true );
300 phylogenies = factory.create( phylogenies_infile, pp );
302 catch ( final Exception e ) {
303 ForesterUtil.fatalError( decorator.PRG_NAME, "failed to read phylgenies from [" + phylogenies_infile
304 + "] [" + e.getMessage() + "]" );
306 Map<String, String> map = null;
307 if ( !advanced_table ) {
308 if ( field != FIELD.MOL_SEQ ) {
309 BasicTable<String> mapping_table = null;
311 mapping_table = BasicTableParser.parse( mapping_infile, separator, true, false );
313 catch ( final Exception e ) {
314 ForesterUtil.fatalError( decorator.PRG_NAME,
315 "failed to read [" + mapping_infile + "] [" + e.getMessage() + "]" );
317 if ( ( key_column < 0 ) || ( key_column >= mapping_table.getNumberOfColumns() ) ) {
318 ForesterUtil.fatalError( decorator.PRG_NAME, "illegal value for key column" );
320 if ( ( value_column < 0 ) || ( value_column >= mapping_table.getNumberOfColumns() ) ) {
321 ForesterUtil.fatalError( decorator.PRG_NAME, "illegal value for value column" );
323 if ( mapping_table.isEmpty() || ( mapping_table.getNumberOfColumns() < 1 ) ) {
324 ForesterUtil.fatalError( decorator.PRG_NAME, "mapping table is empty" );
326 if ( mapping_table.getNumberOfColumns() == 1 ) {
327 ForesterUtil.fatalError( decorator.PRG_NAME, "mapping table has only one column" );
329 map = mapping_table.getColumnsAsMap( key_column, value_column );
331 final Iterator<Entry<String, String>> iter = map.entrySet().iterator();
332 System.out.println();
333 while ( iter.hasNext() ) {
334 final Entry<String, String> e = iter.next();
335 System.out.println( e.getKey() + " => " + e.getValue() );
337 System.out.println();
341 map = readFastaFileIntoMap( mapping_infile, verbose );
344 if ( !ForesterUtil.isEmpty( tree_name ) || !ForesterUtil.isEmpty( tree_id )
345 || !ForesterUtil.isEmpty( tree_desc ) ) {
346 if ( ( phylogenies.length > 1 )
347 && ( !ForesterUtil.isEmpty( tree_name ) || !ForesterUtil.isEmpty( tree_id ) ) ) {
348 ForesterUtil.fatalError( decorator.PRG_NAME,
349 "attempt to set same name or id on more than one phylogeny" );
351 if ( !ForesterUtil.isEmpty( tree_name ) ) {
352 phylogenies[ 0 ].setName( tree_name );
354 if ( !ForesterUtil.isEmpty( tree_id ) ) {
355 final String[] s_ary = tree_id.split( ":" );
356 phylogenies[ 0 ].setIdentifier( new Identifier( s_ary[ 1 ], s_ary[ 0 ] ) );
358 if ( !ForesterUtil.isEmpty( tree_desc ) ) {
359 for( final Phylogeny phylogenie : phylogenies ) {
360 phylogenie.setDescription( tree_desc );
365 if ( advanced_table ) {
366 Map<String, Map<String, String>> table = null;
368 table = PhylogenyDecorator.parseMappingTable( mapping_infile );
370 catch ( final IOException e ) {
371 ForesterUtil.fatalError( decorator.PRG_NAME,
372 "failed to read \"" + mapping_infile + "\" [" + e.getMessage() + "]" );
374 PhylogenyDecorator.decorate( phylogenies,
377 numbers_of_chars_allowed_to_remove_if_not_found_in_map );
380 PhylogenyDecorator.decorate( phylogenies,
383 extract_bracketed_scientific_name,
384 extract_bracketed_tax_code,
386 cut_name_after_space,
387 process_name_intelligently,
389 numbers_of_chars_allowed_to_remove_if_not_found_in_map,
394 catch ( final NullPointerException e ) {
395 ForesterUtil.unexpectedFatalError( decorator.PRG_NAME, e );
397 catch ( final Exception e ) {
398 ForesterUtil.fatalError( decorator.PRG_NAME, e.getLocalizedMessage() );
400 if ( midpoint_root || order_tree ) {
401 for( final Phylogeny phy : phylogenies ) {
402 if ( midpoint_root ) {
403 PhylogenyMethods.midpointRoot( phy );
406 PhylogenyMethods.orderAppearance( phy.getRoot(), true, true, DESCENDANT_SORT_PRIORITY.TAXONOMY );
411 final PhylogenyWriter w = new PhylogenyWriter();
412 w.toPhyloXML( phylogenies, 0, phylogenies_outfile, ForesterUtil.getLineSeparator() );
414 catch ( final IOException e ) {
415 ForesterUtil.fatalError( decorator.PRG_NAME, "failed to write output [" + e.getMessage() + "]" );
417 System.out.println();
418 ForesterUtil.programMessage( PRG_NAME, "wrote: " + phylogenies_outfile );
419 ForesterUtil.programMessage( PRG_NAME, "OK." );
422 private static Map<String, String> readFastaFileIntoMap( final File mapping_infile, final boolean verbose ) {
423 List<Sequence> seqs = null;
425 seqs = FastaParser.parse( new FileInputStream( mapping_infile ) );
427 catch ( final IOException e ) {
428 ForesterUtil.fatalError( decorator.PRG_NAME, "failed to read fasta-file from [" + mapping_infile + "] ["
429 + e.getMessage() + "]" );
431 if ( ForesterUtil.isEmpty( seqs ) ) {
432 ForesterUtil.fatalError( decorator.PRG_NAME, "fasta-file [" + mapping_infile
433 + "] is devoid of fasta-formatted sequences" );
435 final Map<String, String> map = new HashMap<String, String>();
436 for( final Sequence seq : seqs ) {
437 if ( ForesterUtil.isEmpty( seq.getIdentifier() ) ) {
438 ForesterUtil.fatalError( decorator.PRG_NAME, "fasta-file [" + mapping_infile
439 + "] contains sequence with empty identifier" );
441 if ( map.containsKey( seq.getIdentifier() ) ) {
442 ForesterUtil.fatalError( decorator.PRG_NAME, "sequence identifier [" + seq.getIdentifier()
443 + "] is not unique" );
445 if ( seq.getLength() < 1 ) {
446 ForesterUtil.fatalError( decorator.PRG_NAME, "sequence [" + seq.getIdentifier() + "] is empty" );
448 map.put( seq.getIdentifier(), seq.getMolecularSequenceAsString() );
450 System.out.println( seq.getIdentifier() + " => " + seq.getMolecularSequenceAsString() );
456 private static void argumentsError() {
457 System.out.println();
458 System.out.println( decorator.PRG_NAME + " -" + ADVANCED_TABLE_OPTION + " | -f=<c> <phylogenies infile> "
459 + "[mapping table file|fasta-file] <phylogenies outfile>" );
460 System.out.println();
461 System.out.println( "options:" );
462 System.out.println();
463 System.out.println( " -" + ADVANCED_TABLE_OPTION + " : table instead of one to one map (-f=<c>)" );
464 System.out.println( " -r=<n> : allow to remove up to n characters from the end of the names" );
465 System.out.println( " in phylogenies infile if not found (in map) otherwise" );
466 System.out.println( " -p : picky, fails if node name not found in mapping table" );
467 System.out.println( " -" + TREE_NAME_OPTION + "=<s>: name for the phylogeny" );
468 System.out.println( " -" + TREE_ID_OPTION + "=<s>: identifier for the phylogeny (in the form provider:value)" );
469 System.out.println( " -" + TREE_DESC_OPTION + "=<s>: description for phylogenies" );
470 System.out.println();
471 System.out.println();
472 System.out.println( "advanced options, only available if -" + ADVANCED_TABLE_OPTION + " is not used:" );
473 System.out.println();
474 System.out.println( " -f=<c> : field to be replaced: " + NODE_NAME_FIELD + " : node name" );
475 System.out.println( " " + SEQUENCE_ANNOTATION_DESC
476 + " : sequence annotation description" );
477 System.out.println( " " + DS_FILED + " : domain structure" );
478 System.out.println( " " + TAXONOMY_CODE_FIELD + " : taxonomy code" );
479 System.out.println( " " + TAXONOMY_SCIENTIFIC_NAME_FIELD
480 + ": taxonomy scientific name" );
481 System.out.println( " " + SEQUENCE_NAME_FIELD + " : sequence name" );
482 System.out.println( " " + MOL_SEQ + " : molecular sequence" );
483 System.out.println( " -k=<n> : key column in mapping table (0 based)," );
484 System.out.println( " names of the node to be decorated - default is 0" );
485 System.out.println( " -v=<n> : value column in mapping table (0 based)," );
486 System.out.println( " data which with to decorate - default is 1" );
487 System.out.println( " -" + EXTRACT_BRACKETED_SCIENTIC_NAME_OPTION
488 + " : to extract bracketed scientific names, e.g. [Nematostella vectensis]" );
489 System.out.println( " -" + EXTRACT_BRACKETED_TAXONOMIC_CODE_OPTION
490 + " : to extract bracketed taxonomic codes, e.g. [NEMVE]" );
491 System.out.println( " -s=<c> : column separator in mapping file, default is tab" );
492 System.out.println( " -x : process name \"intelligently\" (only for -f=n)" );
493 System.out.println( " -" + decorator.PROCESS_SIMILAR_TO_OPTION
494 + " : process name \"intelligently\" and process information after \"similar to\" (only for -f=n)" );
495 System.out.println( " -c : cut name after first space (only for -f=n)" );
496 System.out.println( " -" + decorator.TRIM_AFTER_TILDE_OPTION
497 + " : trim node name to be replaced after tilde" );
498 System.out.println( " -" + decorator.MIDPOINT_ROOT_OPTION + " : to midpoint-root the tree" );
499 System.out.println( " -" + decorator.ORDER_TREE_OPTION + " : to order tree branches" );
500 System.out.println( " -" + decorator.VERBOSE_OPTION + " : verbose" );
501 System.out.println();