3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
27 package org.forester.application;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashSet;
33 import java.util.List;
36 import org.forester.io.parsers.util.ParserUtils;
37 import org.forester.io.writers.PhylogenyWriter;
38 import org.forester.phylogeny.Phylogeny;
39 import org.forester.phylogeny.PhylogenyNode;
40 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
41 import org.forester.phylogeny.factories.PhylogenyFactory;
42 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
43 import org.forester.tools.ConfidenceAssessor;
44 import org.forester.util.CommandLineArguments;
45 import org.forester.util.ForesterUtil;
47 public class confadd {
49 final static private String HELP_OPTION_1 = "help";
50 final static private String HELP_OPTION_2 = "h";
51 final static private String FIRST_OPTION = "f";
52 final static private String LAST_OPTION = "l";
53 final static private String STRICT_OPTION = "s";
54 final static private String NORMALIZE_OPTION = "n";
55 final static private String PRG_NAME = "confadd";
56 final static private String PRG_VERSION = "1.01";
57 final static private String PRG_DATE = "2010.10.26";
58 final static private String E_MAIL = "phylosoft@gmail.com";
59 final static private String WWW = "www.phylosoft.org/forester/";
61 public static void main( final String args[] ) {
62 ForesterUtil.printProgramInformation( PRG_NAME,
68 ForesterUtil.getForesterLibraryInformation() );
69 CommandLineArguments cla = null;
71 cla = new CommandLineArguments( args );
73 catch ( final Exception e ) {
74 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
76 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
80 if ( args.length < 4 ) {
82 System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
87 if ( cla.getNumberOfNames() != 4 ) {
89 System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
94 final List<String> allowed_options = new ArrayList<String>();
95 allowed_options.add( FIRST_OPTION );
96 allowed_options.add( LAST_OPTION );
97 allowed_options.add( STRICT_OPTION );
98 allowed_options.add( NORMALIZE_OPTION );
99 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
100 if ( dissallowed_options.length() > 0 ) {
101 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
103 final String confidence_type = cla.getName( 0 );
104 final File target_file = cla.getFile( 1 );
105 final File evaluators_file = cla.getFile( 2 );
106 final File outfile = cla.getFile( 3 );
107 if ( ForesterUtil.isEmpty( confidence_type ) ) {
108 ForesterUtil.fatalError( PRG_NAME, "attempt to use empty confidence type" );
110 if ( outfile.exists() ) {
111 ForesterUtil.fatalError( PRG_NAME, "[" + outfile + "] already exists" );
113 if ( !target_file.exists() ) {
114 ForesterUtil.fatalError( PRG_NAME, "target [" + target_file + "] does not exist" );
116 if ( !evaluators_file.exists() ) {
117 ForesterUtil.fatalError( PRG_NAME, "evaluators [" + evaluators_file + "] does not exist" );
119 boolean strict = false;
124 if ( cla.isOptionSet( STRICT_OPTION ) ) {
125 if ( cla.isOptionHasAValue( STRICT_OPTION ) ) {
126 ForesterUtil.fatalError( PRG_NAME, "no value allowed for -" + STRICT_OPTION + " allowed" );
130 if ( cla.isOptionSet( FIRST_OPTION ) ) {
131 first = cla.getOptionValueAsInt( FIRST_OPTION );
133 if ( cla.isOptionSet( LAST_OPTION ) ) {
134 last = cla.getOptionValueAsInt( LAST_OPTION );
136 if ( cla.isOptionSet( NORMALIZE_OPTION ) ) {
137 norm = cla.getOptionValueAsDouble( NORMALIZE_OPTION );
140 catch ( final Exception e ) {
141 ForesterUtil.fatalError( PRG_NAME, "error in command line: " + e.getLocalizedMessage() );
143 if ( ( first < 0 ) || ( last < 0 ) ) {
145 .fatalError( PRG_NAME,
146 "attempt to set first or last evaluator topology to use to a number less than zero" );
149 ForesterUtil.fatalError( PRG_NAME, "illegal value for normalizer [" + norm + "]" );
151 Phylogeny[] targets = null;
152 Phylogeny[] evaluators = null;
153 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
155 targets = factory.create( target_file, ParserUtils.createParserDependingOnFileType( target_file, true ) );
157 catch ( final IOException e ) {
158 ForesterUtil.fatalError( PRG_NAME,
159 "failed to read target phylogenies from [" + target_file + "]: "
160 + e.getLocalizedMessage() );
163 for( final Phylogeny target : targets ) {
165 checkUniquenessOfExternalNodes( target, "target " + counter );
167 catch ( final IllegalArgumentException e ) {
168 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
172 if ( targets.length == 1 ) {
173 ForesterUtil.programMessage( PRG_NAME, "read in one target" );
176 ForesterUtil.programMessage( PRG_NAME, "read in a total of " + targets.length + " targets" );
179 evaluators = factory.create( evaluators_file,
180 ParserUtils.createParserDependingOnFileType( evaluators_file, true ) );
182 catch ( final IOException e ) {
183 ForesterUtil.fatalError( PRG_NAME, "failed to read evaluator topologies from [" + evaluators_file + "]: "
184 + e.getLocalizedMessage() );
187 for( final Phylogeny evaluator : evaluators ) {
189 checkUniquenessOfExternalNodes( evaluator, "evaluator " + counter );
191 catch ( final IllegalArgumentException e ) {
192 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
196 ForesterUtil.programMessage( PRG_NAME, "read in a total of " + evaluators.length + " evaluator topologies" );
199 last = evaluators.length - 1;
201 if ( ( last >= evaluators.length ) || ( last <= first ) ) {
202 ForesterUtil.fatalError( PRG_NAME, "illegal value for first or last evaluator topology to use [" + first
203 + ", " + last + "]" );
207 value = norm / ( ( 1 + last ) - first );
209 ForesterUtil.programMessage( PRG_NAME, "first topology to use: " + first );
211 if ( last == ( evaluators.length - 1 ) ) {
212 is_last = " (corresponds to last topology in file)";
214 ForesterUtil.programMessage( PRG_NAME, "last topology to use : " + last + is_last );
215 ForesterUtil.programMessage( PRG_NAME, "sum of topologies used as evaluators: " + ( ( last - first ) + 1 ) );
217 ForesterUtil.programMessage( PRG_NAME, "normalizer: " + norm + " (" + ForesterUtil.round( value, 6 ) + ")" );
220 ForesterUtil.programMessage( PRG_NAME, "normalizer: n/a" );
222 ForesterUtil.programMessage( PRG_NAME, "strict: " + strict );
223 for( final Phylogeny target : targets ) {
225 ConfidenceAssessor.evaluate( confidence_type, evaluators, target, strict, value, first, last );
227 catch ( final IllegalArgumentException e ) {
228 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
232 final PhylogenyWriter writer = new PhylogenyWriter();
233 writer.toPhyloXML( targets, 0, outfile, ForesterUtil.LINE_SEPARATOR );
235 catch ( final IOException e ) {
236 ForesterUtil.fatalError( PRG_NAME, "failed to write to [" + outfile + "]: " + e.getLocalizedMessage() );
238 ForesterUtil.programMessage( PRG_NAME, "wrote output to: [" + outfile + "]" );
239 ForesterUtil.programMessage( PRG_NAME, "OK" );
240 System.out.println();
243 private static void printHelp() {
244 System.out.println( "Usage:" );
245 System.out.println();
246 System.out.println( PRG_NAME
247 + " [options] <confidence type> <target tree file> <evaluators tree file> <outfile>" );
248 System.out.println();
249 System.out.println( "options:" );
250 System.out.println();
251 System.out.println( " -" + STRICT_OPTION
252 + " : strict [default: non-strict]: all nodes between 'target' and 'evaluators' must match" );
253 System.out.println( " -" + NORMALIZE_OPTION
254 + "=<d>: normalize to this value (e.g. 100 for most bootstrap analyses) [default: no normalization]" );
255 System.out.println( " -" + FIRST_OPTION + "=<i>: first evaluator topology to use (0-based) [default: 0]" );
256 System.out.println( " -" + LAST_OPTION
257 + "=<i>: last evaluator topology to use (0-based) [default: use all until final topology]" );
258 System.out.println();
261 private static void checkUniquenessOfExternalNodes( final Phylogeny phy, final String msg )
262 throws IllegalArgumentException {
263 final Set<PhylogenyNode> ext_nodes = new HashSet<PhylogenyNode>( phy.getNumberOfExternalNodes() );
264 for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
265 final PhylogenyNode node = it.next();
266 if ( ext_nodes.contains( node ) ) {
267 throw new IllegalArgumentException( "external node [" + node.toString() + "] of " + msg
268 + " is not unique" );
270 ext_nodes.add( node );