in progress...
[jalview.git] / forester / java / src / org / forester / application / cladinator.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2017 Christian M. Zmasek
6 // Copyright (C) 2017 J. Craig Venter Institute
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phyloxml @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.application;
27
28 import java.io.File;
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;
35
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;
47
48 public final class cladinator {
49
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#######" );
65
66     public static void main( final String args[] ) {
67         try {
68             ForesterUtil.printProgramInformation( PRG_NAME,
69                                                   PRG_DESC,
70                                                   PRG_VERSION,
71                                                   PRG_DATE,
72                                                   E_MAIL,
73                                                   WWW,
74                                                   ForesterUtil.getForesterLibraryInformation() );
75             CommandLineArguments cla = null;
76             try {
77                 cla = new CommandLineArguments( args );
78             }
79             catch ( final Exception e ) {
80                 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
81             }
82             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
83                 System.out.println();
84                 print_help();
85                 System.exit( 0 );
86             }
87             if ( ( cla.getNumberOfNames() != 1 ) && ( cla.getNumberOfNames() != 2 ) ) {
88                 print_help();
89                 System.exit( -1 );
90             }
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 );
98             }
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" );
105                     }
106                 }
107                 else {
108                     ForesterUtil.fatalError( PRG_NAME, "no value for cutoff for specifics" );
109                 }
110             }
111             String separator = SEP_DEFAULT;
112             if ( cla.isOptionSet( SEP_OPTION ) ) {
113                 if ( cla.isOptionValueSet( SEP_OPTION ) ) {
114                     separator = cla.getOptionValue( SEP_OPTION );
115                 }
116                 else {
117                     ForesterUtil.fatalError( PRG_NAME, "no value for separator option" );
118                 }
119             }
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 );
124                     try {
125                         compiled_query_str = Pattern.compile( query_str );
126                     }
127                     catch ( final PatternSyntaxException e ) {
128                         ForesterUtil.fatalError( PRG_NAME, "error in regular expression: " + e.getMessage() );
129                     }
130                 }
131                 else {
132                     ForesterUtil.fatalError( PRG_NAME, "no value for query pattern option" );
133                 }
134             }
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 );
140             }
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 );
147                 }
148             }
149             else {
150                 outtablefile = null;
151             }
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 );
158             }
159             Phylogeny p = null;
160             try {
161                 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
162                 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( intreefile, true );
163                 p = factory.create( intreefile, pp )[ 0 ];
164             }
165             catch ( final IOException e ) {
166                 ForesterUtil.fatalError( PRG_NAME, "Could not read \"" + intreefile + "\" [" + e.getMessage() + "]" );
167                 System.exit( -1 );
168             }
169             System.out.println( "Ext. nodes in input tree   : " + p.getNumberOfExternalNodes() );
170             final ResultMulti res = AnalysisMulti.execute( p, pattern, separator, cutoff_specifics );
171             printResult( res );
172             if ( outtablefile != null ) {
173                 writeResultToTable( res, outtablefile );
174             }
175         }
176         catch ( final IllegalArgumentException e ) {
177             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
178         }
179         catch ( final IOException e ) {
180             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
181         }
182         catch ( final Exception e ) {
183             e.printStackTrace();
184             ForesterUtil.fatalError( PRG_NAME, "Unexpected errror!" );
185         }
186     }
187
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!" );
194         }
195         else {
196             System.out.println( "Matching Clade(s):" );
197             for( final Prefix prefix : res.getCollapsedMultiHitPrefixes() ) {
198                 System.out.println( prefix );
199             }
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 );
205                 }
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 );
213                         }
214                     }
215                 }
216             }
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 );
222                 }
223             }
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 );
229                 }
230             }
231         }
232         System.out.println();
233     }
234
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!" );
239         }
240         else {
241             for( final Prefix prefix : res.getCollapsedMultiHitPrefixes() ) {
242                 w.print( "Matching Clades" );
243                 w.print( "\t" );
244                 w.print( prefix.getPrefix() );
245                 w.print( "\t" );
246                 w.print( df.format( prefix.getConfidence() ) );
247                 w.println();
248             }
249             if ( res.isHasSpecificMultiHitsPrefixes() ) {
250                 for( final Prefix prefix : res.getSpecificMultiHitPrefixes() ) {
251                     w.print( "Specific-hits" );
252                     w.print( "\t" );
253                     w.print( prefix.getPrefix() );
254                     w.print( "\t" );
255                     w.print( df.format( prefix.getConfidence() ) );
256                     w.println();
257                 }
258             }
259             if ( !ForesterUtil.isEmpty( res.getAllMultiHitPrefixesDown() ) ) {
260                 for( final Prefix prefix : res.getCollapsedMultiHitPrefixesDown() ) {
261                     w.print( "Matching Down-tree Bracketing Clades" );
262                     w.print( "\t" );
263                     w.print( prefix.getPrefix() );
264                     w.print( "\t" );
265                     w.print( df.format( prefix.getConfidence() ) );
266                     w.println();
267                 }
268             }
269             if ( !ForesterUtil.isEmpty( res.getAllMultiHitPrefixesUp() ) ) {
270                 for( final Prefix prefix : res.getCollapsedMultiHitPrefixesUp() ) {
271                     w.print( "Matching Up-tree Bracketing Clades" );
272                     w.print( "\t" );
273                     w.print( prefix.getPrefix() );
274                     w.print( "\t" );
275                     w.print( df.format( prefix.getConfidence() ) );
276                     w.println();
277                 }
278             }
279         }
280         w.flush();
281         w.close();
282     }
283
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
292                 + ")" );
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();
303     }
304 }