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.regex.Pattern;
34 import java.util.regex.PatternSyntaxException;
36 import org.forester.clade_analysis.AnalysisMulti;
37 import org.forester.clade_analysis.Prefix;
38 import org.forester.clade_analysis.ResultMulti;
39 import org.forester.io.parsers.PhylogenyParser;
40 import org.forester.io.parsers.util.ParserUtils;
41 import org.forester.phylogeny.Phylogeny;
42 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
43 import org.forester.phylogeny.factories.PhylogenyFactory;
44 import org.forester.util.CommandLineArguments;
45 import org.forester.util.EasyWriter;
46 import org.forester.util.ForesterUtil;
48 public final class cladinator {
50 final static private String PRG_NAME = "cladinator";
51 final static private String PRG_VERSION = "1.00";
52 final static private String PRG_DATE = "170902";
53 final static private String PRG_DESC = "clades within clades of annotated labels -- analysis of pplacer-type outputs";
54 final static private String E_MAIL = "phyloxml@gmail.com";
55 final static private String WWW = "https://sites.google.com/site/cmzmasek/home/software/forester";
56 final static private String HELP_OPTION_1 = "help";
57 final static private String HELP_OPTION_2 = "h";
58 final static private String SEP_OPTION = "s";
59 final static private String QUERY_PATTERN_OPTION = "q";
60 final static private String SPECIFICS_CUTOFF_OPTION = "c";
61 final static private double SPECIFICS_CUTOFF_DEFAULT = 0.8;
62 final static private String SEP_DEFAULT = ".";
63 final static private Pattern QUERY_PATTERN_DEFAULT = AnalysisMulti.DEFAULT_QUERY_PATTERN_FOR_PPLACER_TYPE;
64 private final static DecimalFormat df = new DecimalFormat( "0.0#######" );
66 public static void main( final String args[] ) {
68 ForesterUtil.printProgramInformation( PRG_NAME,
74 ForesterUtil.getForesterLibraryInformation() );
75 CommandLineArguments cla = null;
77 cla = new CommandLineArguments( args );
79 catch ( final Exception e ) {
80 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
82 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
87 if ( ( cla.getNumberOfNames() != 1 ) && ( cla.getNumberOfNames() != 2 ) ) {
91 final List<String> allowed_options = new ArrayList<>();
92 allowed_options.add( SEP_OPTION );
93 allowed_options.add( QUERY_PATTERN_OPTION );
94 allowed_options.add( SPECIFICS_CUTOFF_OPTION );
95 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
96 if ( dissallowed_options.length() > 0 ) {
97 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
99 double cutoff_specifics = SPECIFICS_CUTOFF_DEFAULT;
100 if ( cla.isOptionSet( SPECIFICS_CUTOFF_OPTION ) ) {
101 if ( cla.isOptionValueSet( SPECIFICS_CUTOFF_OPTION ) ) {
102 cutoff_specifics = cla.getOptionValueAsDouble( SPECIFICS_CUTOFF_OPTION );
103 if ( cutoff_specifics < 0 ) {
104 ForesterUtil.fatalError( PRG_NAME, "cutoff cannot be negative" );
108 ForesterUtil.fatalError( PRG_NAME, "no value for cutoff for specifics" );
111 String separator = SEP_DEFAULT;
112 if ( cla.isOptionSet( SEP_OPTION ) ) {
113 if ( cla.isOptionValueSet( SEP_OPTION ) ) {
114 separator = cla.getOptionValue( SEP_OPTION );
117 ForesterUtil.fatalError( PRG_NAME, "no value for separator option" );
120 Pattern compiled_query_str = null;
121 if ( cla.isOptionSet( QUERY_PATTERN_OPTION ) ) {
122 if ( cla.isOptionValueSet( QUERY_PATTERN_OPTION ) ) {
123 final String query_str = cla.getOptionValue( QUERY_PATTERN_OPTION );
125 compiled_query_str = Pattern.compile( query_str );
127 catch ( final PatternSyntaxException e ) {
128 ForesterUtil.fatalError( PRG_NAME, "error in regular expression: " + e.getMessage() );
132 ForesterUtil.fatalError( PRG_NAME, "no value for query pattern option" );
135 final Pattern pattern = ( compiled_query_str != null ) ? compiled_query_str : QUERY_PATTERN_DEFAULT;
136 final File intreefile = cla.getFile( 0 );
137 final String error_intreefile = ForesterUtil.isReadableFile( intreefile );
138 if ( !ForesterUtil.isEmpty( error_intreefile ) ) {
139 ForesterUtil.fatalError( PRG_NAME, error_intreefile );
141 final File outtablefile;
142 if ( cla.getNumberOfNames() > 1 ) {
143 outtablefile = cla.getFile( 1 );
144 final String error_outtablefile = ForesterUtil.isWritableFile( outtablefile );
145 if ( !ForesterUtil.isEmpty( error_outtablefile ) ) {
146 ForesterUtil.fatalError( PRG_NAME, error_outtablefile );
152 System.out.println( "Input tree : " + intreefile );
153 System.out.println( "Specific-hit support cutoff: " + cutoff_specifics );
154 System.out.println( "Annotation-separator : " + separator );
155 System.out.println( "Query pattern : " + pattern );
156 if ( outtablefile != null ) {
157 System.out.println( "Output table : " + outtablefile );
161 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
162 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( intreefile, true );
163 p = factory.create( intreefile, pp )[ 0 ];
165 catch ( final IOException e ) {
166 ForesterUtil.fatalError( PRG_NAME, "Could not read \"" + intreefile + "\" [" + e.getMessage() + "]" );
169 System.out.println( "Ext. nodes in input tree : " + p.getNumberOfExternalNodes() );
170 final ResultMulti res = AnalysisMulti.execute( p, pattern, separator, cutoff_specifics );
172 if ( outtablefile != null ) {
173 writeResultToTable( res, outtablefile );
176 catch ( final IllegalArgumentException e ) {
177 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
179 catch ( final IOException e ) {
180 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
182 catch ( final Exception e ) {
184 ForesterUtil.fatalError( PRG_NAME, "Unexpected errror!" );
188 private final static void printResult( final ResultMulti res ) {
189 System.out.println();
190 System.out.println( "Result:" );
191 System.out.println();
192 if ( ( res.getAllMultiHitPrefixes() == null ) | ( res.getAllMultiHitPrefixes().size() < 1 ) ) {
193 System.out.println( "No match to query pattern!" );
196 System.out.println( "Matching Clade(s):" );
197 for( final Prefix prefix : res.getCollapsedMultiHitPrefixes() ) {
198 System.out.println( prefix );
200 if ( res.isHasSpecificMultiHitsPrefixes() ) {
201 System.out.println();
202 System.out.println( "Specific-hit(s):" );
203 for( final Prefix prefix : res.getSpecificMultiHitPrefixes() ) {
204 System.out.println( prefix );
206 System.out.println();
207 System.out.println( "Matching Clade(s) with Specific-hit(s):" );
208 for( final Prefix prefix : res.getCollapsedMultiHitPrefixes() ) {
209 System.out.println( prefix );
210 for( final Prefix spec : res.getSpecificMultiHitPrefixes() ) {
211 if ( spec.getPrefix().startsWith( prefix.getPrefix() ) ) {
212 System.out.println( " " + spec );
217 if ( !ForesterUtil.isEmpty( res.getAllMultiHitPrefixesDown() ) ) {
218 System.out.println();
219 System.out.println( "Matching Down-tree Bracketing Clade(s):" );
220 for( final Prefix prefix : res.getCollapsedMultiHitPrefixesDown() ) {
221 System.out.println( prefix );
224 if ( !ForesterUtil.isEmpty( res.getAllMultiHitPrefixesUp() ) ) {
225 System.out.println();
226 System.out.println( "Matching Up-tree Bracketing Clade(s):" );
227 for( final Prefix prefix : res.getCollapsedMultiHitPrefixesUp() ) {
228 System.out.println( prefix );
232 System.out.println();
235 private final static void writeResultToTable( final ResultMulti res, final File outtablefile ) throws IOException {
236 final EasyWriter w = ForesterUtil.createEasyWriter( outtablefile );
237 if ( ( res.getAllMultiHitPrefixes() == null ) | ( res.getAllMultiHitPrefixes().size() < 1 ) ) {
238 w.println( "No match to query pattern!" );
241 for( final Prefix prefix : res.getCollapsedMultiHitPrefixes() ) {
242 w.print( "Matching Clades" );
244 w.print( prefix.getPrefix() );
246 w.print( df.format( prefix.getConfidence() ) );
249 if ( res.isHasSpecificMultiHitsPrefixes() ) {
250 for( final Prefix prefix : res.getSpecificMultiHitPrefixes() ) {
251 w.print( "Specific-hits" );
253 w.print( prefix.getPrefix() );
255 w.print( df.format( prefix.getConfidence() ) );
259 if ( !ForesterUtil.isEmpty( res.getAllMultiHitPrefixesDown() ) ) {
260 for( final Prefix prefix : res.getCollapsedMultiHitPrefixesDown() ) {
261 w.print( "Matching Down-tree Bracketing Clades" );
263 w.print( prefix.getPrefix() );
265 w.print( df.format( prefix.getConfidence() ) );
269 if ( !ForesterUtil.isEmpty( res.getAllMultiHitPrefixesUp() ) ) {
270 for( final Prefix prefix : res.getCollapsedMultiHitPrefixesUp() ) {
271 w.print( "Matching Up-tree Bracketing Clades" );
273 w.print( prefix.getPrefix() );
275 w.print( df.format( prefix.getConfidence() ) );
284 private final static void print_help() {
285 System.out.println( "Usage:" );
286 System.out.println();
287 System.out.println( PRG_NAME + " [options] <input tree file> [output table file]" );
288 System.out.println();
289 System.out.println( " options:" );
290 System.out.println( " -" + SPECIFICS_CUTOFF_OPTION
291 + "=<double>: the cutoff for \"specific-hit\" support values (default: " + SPECIFICS_CUTOFF_DEFAULT
293 System.out.println( " -" + SEP_OPTION + "=<separator>: the annotation-separator to be used (default: "
294 + SEP_DEFAULT + ")" );
295 System.out.println( " -" + QUERY_PATTERN_OPTION
296 + "=<query pattern>: the regular expression for the query (default: \"" + QUERY_PATTERN_DEFAULT
297 + "\" for pplacer output)" );
298 System.out.println();
299 System.out.println( "Example:" );
300 System.out.println();
301 System.out.println( " " + PRG_NAME + " -c=0.5 -s=. my_tree.nh result.tsv" );
302 System.out.println();