2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2017 Christian M. Zmasek
6 // Copyright (C) 2017 J. Craig Venter Institute
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: phyloxml @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.application;
29 import java.io.IOException;
30 import java.text.DecimalFormat;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.SortedMap;
34 import java.util.regex.Pattern;
35 import java.util.regex.PatternSyntaxException;
37 import org.forester.clade_analysis.AnalysisMulti;
38 import org.forester.clade_analysis.Prefix;
39 import org.forester.clade_analysis.ResultMulti;
40 import org.forester.io.parsers.PhylogenyParser;
41 import org.forester.io.parsers.util.ParserUtils;
42 import org.forester.phylogeny.Phylogeny;
43 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
44 import org.forester.phylogeny.factories.PhylogenyFactory;
45 import org.forester.util.BasicTable;
46 import org.forester.util.BasicTableParser;
47 import org.forester.util.CommandLineArguments;
48 import org.forester.util.EasyWriter;
49 import org.forester.util.ForesterUtil;
50 import org.forester.util.UserException;
52 public final class cladinator {
54 final static private String PRG_NAME = "cladinator";
55 final static private String PRG_VERSION = "1.04";
56 final static private String PRG_DATE = "170915";
57 final static private String PRG_DESC = "clades within clades of annotated labels -- analysis of pplacer-type outputs";
58 final static private String E_MAIL = "phyloxml@gmail.com";
59 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
60 final static private String HELP_OPTION_1 = "help";
61 final static private String HELP_OPTION_2 = "h";
62 final static private String SEP_OPTION = "s";
63 final static private String QUERY_PATTERN_OPTION = "q";
64 final static private String SPECIFICS_CUTOFF_OPTION = "c";
65 final static private String MAPPING_FILE_OPTION = "m";
66 final static private String EXTRA_PROCESSING_OPTION1 = "x";
67 final static private String EXTRA_PROCESSING1_SEP_OPTION = "xs";
68 final static private String EXTRA_PROCESSING1_KEEP_EXTRA_OPTION = "xk";
69 final static private String QUIET_OPTION = "Q";
70 final static private String VERBOSE_OPTION = "v";
71 final static private double SPECIFICS_CUTOFF_DEFAULT = 0.8;
72 final static private String SEP_DEFAULT = ".";
73 final static private Pattern QUERY_PATTERN_DEFAULT = AnalysisMulti.DEFAULT_QUERY_PATTERN_FOR_PPLACER_TYPE;
74 final static private String EXTRA_PROCESSING1_SEP_DEFAULT = "|";
75 final static private boolean EXTRA_PROCESSING1_KEEP_EXTRA_DEFAULT = false;
76 private final static DecimalFormat df = new DecimalFormat( "0.0###" );
78 public static void main( final String args[] ) {
80 ForesterUtil.printProgramInformation( PRG_NAME,
86 ForesterUtil.getForesterLibraryInformation() );
87 CommandLineArguments cla = null;
89 cla = new CommandLineArguments( args );
91 catch ( final Exception e ) {
92 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
94 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
99 if ( ( cla.getNumberOfNames() != 1 ) && ( cla.getNumberOfNames() != 2 ) ) {
103 final List<String> allowed_options = new ArrayList<>();
104 allowed_options.add( SEP_OPTION );
105 allowed_options.add( QUERY_PATTERN_OPTION );
106 allowed_options.add( SPECIFICS_CUTOFF_OPTION );
107 allowed_options.add( MAPPING_FILE_OPTION );
108 allowed_options.add( EXTRA_PROCESSING_OPTION1 );
109 allowed_options.add( EXTRA_PROCESSING1_SEP_OPTION );
110 allowed_options.add( EXTRA_PROCESSING1_KEEP_EXTRA_OPTION );
111 allowed_options.add( VERBOSE_OPTION );
112 allowed_options.add( QUIET_OPTION );
113 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
114 if ( dissallowed_options.length() > 0 ) {
115 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
117 double cutoff_specifics = SPECIFICS_CUTOFF_DEFAULT;
118 if ( cla.isOptionSet( SPECIFICS_CUTOFF_OPTION ) ) {
119 if ( cla.isOptionValueSet( SPECIFICS_CUTOFF_OPTION ) ) {
120 cutoff_specifics = cla.getOptionValueAsDouble( SPECIFICS_CUTOFF_OPTION );
121 if ( cutoff_specifics < 0 ) {
122 ForesterUtil.fatalError( PRG_NAME, "cutoff cannot be negative" );
126 ForesterUtil.fatalError( PRG_NAME, "no value for cutoff for specifics" );
129 String separator = SEP_DEFAULT;
130 if ( cla.isOptionSet( SEP_OPTION ) ) {
131 if ( cla.isOptionValueSet( SEP_OPTION ) ) {
132 separator = cla.getOptionValue( SEP_OPTION );
135 ForesterUtil.fatalError( PRG_NAME, "no value for separator option" );
138 Pattern compiled_query_str = null;
139 if ( cla.isOptionSet( QUERY_PATTERN_OPTION ) ) {
140 if ( cla.isOptionValueSet( QUERY_PATTERN_OPTION ) ) {
141 final String query_str = cla.getOptionValue( QUERY_PATTERN_OPTION );
143 compiled_query_str = Pattern.compile( query_str );
145 catch ( final PatternSyntaxException e ) {
146 ForesterUtil.fatalError( PRG_NAME, "error in regular expression: " + e.getMessage() );
150 ForesterUtil.fatalError( PRG_NAME, "no value for query pattern option" );
153 File mapping_file = null;
154 if ( cla.isOptionSet( MAPPING_FILE_OPTION ) ) {
155 if ( cla.isOptionValueSet( MAPPING_FILE_OPTION ) ) {
156 final String mapping_file_str = cla.getOptionValue( MAPPING_FILE_OPTION );
157 final String error = ForesterUtil.isReadableFile( mapping_file_str );
158 if ( !ForesterUtil.isEmpty( error ) ) {
159 ForesterUtil.fatalError( PRG_NAME, error );
161 mapping_file = new File( mapping_file_str );
164 ForesterUtil.fatalError( PRG_NAME, "no value for mapping file" );
167 final Pattern pattern = ( compiled_query_str != null ) ? compiled_query_str : QUERY_PATTERN_DEFAULT;
168 final File intreefile = cla.getFile( 0 );
169 final String error_intreefile = ForesterUtil.isReadableFile( intreefile );
170 if ( !ForesterUtil.isEmpty( error_intreefile ) ) {
171 ForesterUtil.fatalError( PRG_NAME, error_intreefile );
173 final File outtablefile;
174 if ( cla.getNumberOfNames() > 1 ) {
175 outtablefile = cla.getFile( 1 );
176 final String error_outtablefile = ForesterUtil.isWritableFile( outtablefile );
177 if ( !ForesterUtil.isEmpty( error_outtablefile ) ) {
178 ForesterUtil.fatalError( PRG_NAME, error_outtablefile );
184 final BasicTable<String> t;
185 final SortedMap<String, String> map;
186 if ( mapping_file != null ) {
187 t = BasicTableParser.parse( mapping_file, '\t' );
188 if ( t.getNumberOfColumns() != 2 ) {
189 ForesterUtil.fatalError( PRG_NAME,
190 "mapping file needs to have 2 tab-separated columns, not "
191 + t.getNumberOfColumns() );
193 map = t.getColumnsAsMap( 0, 1 );
199 final boolean extra_processing1;
200 if ( cla.isOptionSet( EXTRA_PROCESSING_OPTION1 ) ) {
201 extra_processing1 = true;
204 extra_processing1 = false;
206 String extra_processing1_sep = EXTRA_PROCESSING1_SEP_DEFAULT;
207 if ( cla.isOptionSet( EXTRA_PROCESSING1_SEP_OPTION ) ) {
208 if ( !extra_processing1 ) {
209 ForesterUtil.fatalError( PRG_NAME,
210 "extra processing is not enabled, cannot set -"
211 + EXTRA_PROCESSING1_SEP_OPTION + " option" );
213 if ( cla.isOptionValueSet( EXTRA_PROCESSING1_SEP_OPTION ) ) {
214 extra_processing1_sep = cla.getOptionValue( EXTRA_PROCESSING1_SEP_OPTION );
217 ForesterUtil.fatalError( PRG_NAME, "no value for extra processing separator" );
220 if ( ( extra_processing1_sep != null ) && extra_processing1_sep.equals( separator ) ) {
221 ForesterUtil.fatalError( PRG_NAME,
222 "extra processing separator must not be the same the annotation-separator" );
224 boolean extra_processing1_keep = EXTRA_PROCESSING1_KEEP_EXTRA_DEFAULT;
225 if ( cla.isOptionSet( EXTRA_PROCESSING1_KEEP_EXTRA_OPTION ) ) {
226 if ( !extra_processing1 ) {
227 ForesterUtil.fatalError( PRG_NAME,
228 "extra processing is not enabled, cannot set -"
229 + EXTRA_PROCESSING1_KEEP_EXTRA_OPTION + " option" );
231 extra_processing1_keep = true;
233 final boolean verbose;
234 if ( cla.isOptionSet( VERBOSE_OPTION ) ) {
241 if ( cla.isOptionSet( QUIET_OPTION ) ) {
247 System.out.println( "Input tree : " + intreefile );
248 System.out.println( "Specific-hit support cutoff: " + cutoff_specifics );
249 if ( mapping_file != null ) {
250 System.out.println( "Mapping file : " + mapping_file + " (" + t.getNumberOfRows()
253 System.out.println( "Annotation-separator : " + separator );
254 System.out.println( "Query pattern : " + pattern );
255 System.out.println( "Extra processing : " + extra_processing1 );
256 if ( extra_processing1 ) {
257 System.out.println( "Extra processing separator : " + extra_processing1_sep );
258 System.out.println( "Keep extra annotations : " + extra_processing1_keep );
260 if ( outtablefile != null ) {
261 System.out.println( "Output table : " + outtablefile );
263 Phylogeny phys[] = null;
265 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
266 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( intreefile, true );
267 phys = factory.create( intreefile, pp );
269 catch ( final IOException e ) {
270 ForesterUtil.fatalError( PRG_NAME, "Could not read \"" + intreefile + "\" [" + e.getMessage() + "]" );
272 if ( phys.length == 0 ) {
273 ForesterUtil.fatalError( PRG_NAME, "\"" + intreefile + "\" does not contain any trees" );
275 System.out.println( "Number of input trees : " + phys.length );
276 if ( phys.length == 1 ) {
277 System.out.println( "Ext. nodes in input tree : " + phys[ 0 ].getNumberOfExternalNodes() );
280 System.out.println( "Ext. nodes in input tree 1 : " + phys[ 0 ].getNumberOfExternalNodes() );
282 final EasyWriter outtable_writer;
283 if ( outtablefile != null ) {
284 outtable_writer = ForesterUtil.createEasyWriter( outtablefile );
285 outtable_writer.print( "#" + PRG_NAME + " " + PRG_VERSION + " " + PRG_DATE );
286 outtable_writer.print( " Input tree: " + intreefile );
287 outtable_writer.println( " Specific-hit support cutoff: " + cutoff_specifics );
290 outtable_writer = null;
293 for( final Phylogeny phy : phys ) {
295 AnalysisMulti.performMapping( pattern, map, phy, verbose );
297 if ( extra_processing1 ) {
298 AnalysisMulti.performExtraProcessing1( pattern,
300 extra_processing1_sep,
301 extra_processing1_keep,
305 final ResultMulti res = AnalysisMulti.execute( phy, pattern, separator, cutoff_specifics );
307 if ( phys.length == 1 ) {
308 printResult( res, -1 );
311 printResult( res, counter );
314 if ( outtable_writer != null ) {
315 writeResultToTable( res, outtable_writer );
316 outtable_writer.flush();
320 if ( outtable_writer != null ) {
321 outtable_writer.flush();
322 outtable_writer.close();
325 catch ( final UserException e ) {
326 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
328 catch ( final IOException e ) {
329 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
331 catch ( final Exception e ) {
333 ForesterUtil.fatalError( PRG_NAME, "Unexpected errror!" );
337 private final static void printResult( final ResultMulti res, final int counter ) {
338 System.out.println();
339 if ( counter == -1 ) {
340 System.out.println( "Result for " + res.getQueryNamePrefix() );
343 System.out.println( "Result for " + res.getQueryNamePrefix() + " [tree " + counter + "]" );
345 if ( ( res.getAllMultiHitPrefixes() == null ) | ( res.getAllMultiHitPrefixes().size() < 1 ) ) {
346 System.out.println( " No match to query pattern!" );
349 System.out.println( " Matching Clade(s):" );
350 for( final Prefix prefix : res.getCollapsedMultiHitPrefixes() ) {
351 System.out.println( " " + prefix );
353 if ( res.isHasSpecificMultiHitsPrefixes() ) {
354 System.out.println();
355 System.out.println( " Specific-hit(s):" );
356 for( final Prefix prefix : res.getSpecificMultiHitPrefixes() ) {
357 System.out.println( " " + prefix );
359 System.out.println();
360 System.out.println( " Matching Clade(s) with Specific-hit(s):" );
361 for( final Prefix prefix : res.getCollapsedMultiHitPrefixes() ) {
362 System.out.println( " " + prefix );
363 for( final Prefix spec : res.getSpecificMultiHitPrefixes() ) {
364 if ( spec.getPrefix().startsWith( prefix.getPrefix() ) ) {
365 System.out.println( " " + spec );
370 if ( !ForesterUtil.isEmpty( res.getAllMultiHitPrefixesDown() ) ) {
371 System.out.println();
372 System.out.println( " Matching Down-tree Bracketing Clade(s):" );
373 for( final Prefix prefix : res.getCollapsedMultiHitPrefixesDown() ) {
374 System.out.println( " " + prefix );
377 if ( !ForesterUtil.isEmpty( res.getAllMultiHitPrefixesUp() ) ) {
378 System.out.println();
379 System.out.println( " Matching Up-tree Bracketing Clade(s):" );
380 for( final Prefix prefix : res.getCollapsedMultiHitPrefixesUp() ) {
381 System.out.println( " " + prefix );
384 System.out.println();
385 System.out.println( " Total Number of Matches: " + res.getNumberOfMatches() + "/"
386 + res.getReferenceTreeNumberOfExternalNodes() );
388 System.out.println();
391 private final static void writeResultToTable( final ResultMulti res, final EasyWriter w ) throws IOException {
392 if ( ( res.getAllMultiHitPrefixes() == null ) | ( res.getAllMultiHitPrefixes().size() < 1 ) ) {
393 w.print( res.getQueryNamePrefix() );
395 w.println( "No match to query pattern!" );
398 for( final Prefix prefix : res.getCollapsedMultiHitPrefixes() ) {
399 w.print( res.getQueryNamePrefix() );
401 w.print( "Matching Clades" );
403 w.print( prefix.getPrefix() );
405 w.print( df.format( prefix.getConfidence() ) );
407 w.print( String.valueOf( res.getNumberOfMatches() ) );
409 w.print( String.valueOf( res.getReferenceTreeNumberOfExternalNodes() ) );
412 if ( res.isHasSpecificMultiHitsPrefixes() ) {
413 for( final Prefix prefix : res.getSpecificMultiHitPrefixes() ) {
414 w.print( res.getQueryNamePrefix() );
416 w.print( "Specific-hits" );
418 w.print( prefix.getPrefix() );
420 w.print( df.format( prefix.getConfidence() ) );
422 w.print( String.valueOf( res.getNumberOfMatches() ) );
424 w.print( String.valueOf( res.getReferenceTreeNumberOfExternalNodes() ) );
428 if ( !ForesterUtil.isEmpty( res.getAllMultiHitPrefixesDown() ) ) {
429 for( final Prefix prefix : res.getCollapsedMultiHitPrefixesDown() ) {
430 w.print( res.getQueryNamePrefix() );
432 w.print( "Matching Down-tree Bracketing Clades" );
434 w.print( prefix.getPrefix() );
436 w.print( df.format( prefix.getConfidence() ) );
438 w.print( String.valueOf( res.getNumberOfMatches() ) );
440 w.print( String.valueOf( res.getReferenceTreeNumberOfExternalNodes() ) );
444 if ( !ForesterUtil.isEmpty( res.getAllMultiHitPrefixesUp() ) ) {
445 for( final Prefix prefix : res.getCollapsedMultiHitPrefixesUp() ) {
446 w.print( res.getQueryNamePrefix() );
448 w.print( "Matching Up-tree Bracketing Clades" );
450 w.print( prefix.getPrefix() );
452 w.print( df.format( prefix.getConfidence() ) );
454 w.print( String.valueOf( res.getNumberOfMatches() ) );
456 w.print( String.valueOf( res.getReferenceTreeNumberOfExternalNodes() ) );
463 private final static void print_help() {
464 System.out.println( "Usage:" );
465 System.out.println();
466 System.out.println( PRG_NAME + " [options] <input tree(s) file> [output table file]" );
467 System.out.println();
468 System.out.println( " options:" );
469 System.out.println( " -" + SPECIFICS_CUTOFF_OPTION
470 + "=<double> : the minimal confidence value for \"specific-hits\" to be reported (default: "
471 + SPECIFICS_CUTOFF_DEFAULT + ")" );
472 System.out.println( " -" + SEP_OPTION + "=<separator> : the annotation-separator to be used (default: "
473 + SEP_DEFAULT + ")" );
474 System.out.println( " -" + MAPPING_FILE_OPTION
475 + "=<mapping table> : to map node names to appropriate annotations (tab-separated, two columns) (default: no mapping)" );
476 System.out.println( " -" + EXTRA_PROCESSING_OPTION1
477 + " : to enable extra processing of annotations (e.g. \"Q16611|A.1.1\" becomes \"A.1.1\")" );
478 System.out.println( " -" + EXTRA_PROCESSING1_SEP_OPTION
479 + "=<separator> : the separator for extra annotations (default: \"" + EXTRA_PROCESSING1_SEP_DEFAULT
481 System.out.println( " -" + EXTRA_PROCESSING1_KEEP_EXTRA_OPTION
482 + " : to keep extra annotations (e.g. \"Q16611|A.1.1\" becomes \"A.1.1.Q16611\")" );
483 System.out.println( " -" + VERBOSE_OPTION + " : verbose" );
484 System.out.println( " --" + QUERY_PATTERN_OPTION
485 + "=<query pattern>: the regular expression for the query (default: \"" + QUERY_PATTERN_DEFAULT
486 + "\" for pplacer output)" );
487 System.out.println();
488 System.out.println( "Examples:" );
489 System.out.println();
490 System.out.println( " " + PRG_NAME + " pp_out_tree.sing.tre result.tsv" );
491 System.out.println( " " + PRG_NAME + " -c=0.5 -s=. pp_out_tree.sing.tre result.tsv" );
492 System.out.println( " " + PRG_NAME + " -c=0.9 -s=_ -m=map.tsv pp_out_trees.sing.tre result.tsv" );
493 System.out.println( " " + PRG_NAME + " -x -xs=& -xk pp_out_trees.sing.tre result.tsv" );
494 System.out.println( " " + PRG_NAME + " -x -xs=\"|\" pp_out_trees.sing.tre result.tsv" );
495 System.out.println( " " + PRG_NAME + " -x -xk -m=map.tsv pp_out_trees.sing.tre result.tsv" );
496 System.out.println();