dbc4d507e46ae80d0b5da8d5c272bff050531e8c
[jalview.git] / forester / java / src / org / forester / application / confadd.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
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.
14 //
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.
19 //
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
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
26
27 package org.forester.application;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Set;
35
36 import org.forester.io.writers.PhylogenyWriter;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.PhylogenyNode;
39 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
40 import org.forester.phylogeny.factories.PhylogenyFactory;
41 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
42 import org.forester.tools.ConfidenceAssessor;
43 import org.forester.util.CommandLineArguments;
44 import org.forester.util.ForesterUtil;
45
46 public class confadd {
47
48     final static private String HELP_OPTION_1    = "help";
49     final static private String HELP_OPTION_2    = "h";
50     final static private String FIRST_OPTION     = "f";
51     final static private String LAST_OPTION      = "l";
52     final static private String STRICT_OPTION    = "s";
53     final static private String NORMALIZE_OPTION = "n";
54     final static private String PRG_NAME         = "confadd";
55     final static private String PRG_VERSION      = "1.01";
56     final static private String PRG_DATE         = "2010.10.26";
57     final static private String E_MAIL           = "czmasek@burnham.org";
58     final static private String WWW              = "www.phylosoft.org/forester/";
59
60     public static void main( final String args[] ) {
61         ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW );
62         CommandLineArguments cla = null;
63         try {
64             cla = new CommandLineArguments( args );
65         }
66         catch ( final Exception e ) {
67             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
68         }
69         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
70             printHelp();
71             System.exit( 0 );
72         }
73         if ( args.length < 4 ) {
74             System.out.println();
75             System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
76             System.out.println();
77             printHelp();
78             System.exit( -1 );
79         }
80         if ( cla.getNumberOfNames() != 4 ) {
81             System.out.println();
82             System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
83             System.out.println();
84             printHelp();
85             System.exit( -1 );
86         }
87         final List<String> allowed_options = new ArrayList<String>();
88         allowed_options.add( FIRST_OPTION );
89         allowed_options.add( LAST_OPTION );
90         allowed_options.add( STRICT_OPTION );
91         allowed_options.add( NORMALIZE_OPTION );
92         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
93         if ( dissallowed_options.length() > 0 ) {
94             ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
95         }
96         final String confidence_type = cla.getName( 0 );
97         final File target_file = cla.getFile( 1 );
98         final File evaluators_file = cla.getFile( 2 );
99         final File outfile = cla.getFile( 3 );
100         if ( ForesterUtil.isEmpty( confidence_type ) ) {
101             ForesterUtil.fatalError( PRG_NAME, "attempt to use empty confidence type" );
102         }
103         if ( outfile.exists() ) {
104             ForesterUtil.fatalError( PRG_NAME, "[" + outfile + "] already exists" );
105         }
106         if ( !target_file.exists() ) {
107             ForesterUtil.fatalError( PRG_NAME, "target [" + target_file + "] does not exist" );
108         }
109         if ( !evaluators_file.exists() ) {
110             ForesterUtil.fatalError( PRG_NAME, "evaluators [" + evaluators_file + "] does not exist" );
111         }
112         boolean strict = false;
113         int first = 0;
114         int last = 0;
115         double norm = 0;
116         try {
117             if ( cla.isOptionSet( STRICT_OPTION ) ) {
118                 if ( cla.isOptionHasAValue( STRICT_OPTION ) ) {
119                     ForesterUtil.fatalError( PRG_NAME, "no value allowed for -" + STRICT_OPTION + " allowed" );
120                 }
121                 strict = true;
122             }
123             if ( cla.isOptionSet( FIRST_OPTION ) ) {
124                 first = cla.getOptionValueAsInt( FIRST_OPTION );
125             }
126             if ( cla.isOptionSet( LAST_OPTION ) ) {
127                 last = cla.getOptionValueAsInt( LAST_OPTION );
128             }
129             if ( cla.isOptionSet( NORMALIZE_OPTION ) ) {
130                 norm = cla.getOptionValueAsDouble( NORMALIZE_OPTION );
131             }
132         }
133         catch ( final Exception e ) {
134             ForesterUtil.fatalError( PRG_NAME, "error in command line: " + e.getLocalizedMessage() );
135         }
136         if ( ( first < 0 ) || ( last < 0 ) ) {
137             ForesterUtil
138                     .fatalError( PRG_NAME,
139                                  "attempt to set first or last evaluator topology to use to a number less than zero" );
140         }
141         if ( norm < 0 ) {
142             ForesterUtil.fatalError( PRG_NAME, "illegal value for normalizer [" + norm + "]" );
143         }
144         Phylogeny[] targets = null;
145         Phylogeny[] evaluators = null;
146         final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
147         try {
148             targets = factory.create( target_file, ForesterUtil.createParserDependingOnFileType( target_file, true ) );
149         }
150         catch ( final IOException e ) {
151             ForesterUtil.fatalError( PRG_NAME,
152                                      "failed to read target phylogenies from [" + target_file + "]: "
153                                              + e.getLocalizedMessage() );
154         }
155         int counter = 0;
156         for( final Phylogeny target : targets ) {
157             try {
158                 checkUniquenessOfExternalNodes( target, "target " + counter );
159             }
160             catch ( final IllegalArgumentException e ) {
161                 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
162             }
163             counter++;
164         }
165         if ( targets.length == 1 ) {
166             ForesterUtil.programMessage( PRG_NAME, "read in one target" );
167         }
168         else {
169             ForesterUtil.programMessage( PRG_NAME, "read in a total of " + targets.length + " targets" );
170         }
171         try {
172             evaluators = factory.create( evaluators_file,
173                                          ForesterUtil.createParserDependingOnFileType( evaluators_file, true ) );
174         }
175         catch ( final IOException e ) {
176             ForesterUtil.fatalError( PRG_NAME, "failed to read evaluator topologies from [" + evaluators_file + "]: "
177                     + e.getLocalizedMessage() );
178         }
179         counter = 0;
180         for( final Phylogeny evaluator : evaluators ) {
181             try {
182                 checkUniquenessOfExternalNodes( evaluator, "evaluator " + counter );
183             }
184             catch ( final IllegalArgumentException e ) {
185                 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
186             }
187             counter++;
188         }
189         ForesterUtil.programMessage( PRG_NAME, "read in a total of " + evaluators.length + " evaluator topologies" );
190         System.gc();
191         if ( last == 0 ) {
192             last = evaluators.length - 1;
193         }
194         if ( ( last >= evaluators.length ) || ( last <= first ) ) {
195             ForesterUtil.fatalError( PRG_NAME, "illegal value for first or last evaluator topology to use [" + first
196                     + ", " + last + "]" );
197         }
198         double value = 1;
199         if ( norm > 0 ) {
200             value = norm / ( 1 + last - first );
201         }
202         ForesterUtil.programMessage( PRG_NAME, "first topology to use: " + first );
203         String is_last = "";
204         if ( last == ( evaluators.length - 1 ) ) {
205             is_last = " (corresponds to last topology in file)";
206         }
207         ForesterUtil.programMessage( PRG_NAME, "last topology to use : " + last + is_last );
208         ForesterUtil.programMessage( PRG_NAME, "sum of topologies used as evaluators: " + ( last - first + 1 ) );
209         if ( norm > 0 ) {
210             ForesterUtil.programMessage( PRG_NAME, "normalizer: " + norm + " (" + ForesterUtil.round( value, 6 ) + ")" );
211         }
212         else {
213             ForesterUtil.programMessage( PRG_NAME, "normalizer: n/a" );
214         }
215         ForesterUtil.programMessage( PRG_NAME, "strict: " + strict );
216         for( final Phylogeny target : targets ) {
217             try {
218                 ConfidenceAssessor.evaluate( confidence_type, evaluators, target, strict, value, first, last );
219             }
220             catch ( final IllegalArgumentException e ) {
221                 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
222             }
223         }
224         try {
225             final PhylogenyWriter writer = new PhylogenyWriter();
226             writer.toPhyloXML( targets, 0, outfile, ForesterUtil.LINE_SEPARATOR );
227         }
228         catch ( final IOException e ) {
229             ForesterUtil.fatalError( PRG_NAME, "failed to write to [" + outfile + "]: " + e.getLocalizedMessage() );
230         }
231         ForesterUtil.programMessage( PRG_NAME, "wrote output to: [" + outfile + "]" );
232         ForesterUtil.programMessage( PRG_NAME, "OK" );
233         System.out.println();
234     }
235
236     private static void printHelp() {
237         System.out.println( "Usage:" );
238         System.out.println();
239         System.out.println( PRG_NAME
240                 + " [options] <confidence type> <target tree file> <evaluators tree file> <outfile>" );
241         System.out.println();
242         System.out.println( "options:" );
243         System.out.println();
244         System.out.println( " -" + STRICT_OPTION
245                 + "    : strict [default: non-strict]: all nodes between 'target' and 'evaluators' must match" );
246         System.out.println( " -" + NORMALIZE_OPTION
247                 + "=<d>: normalize to this value (e.g. 100 for most bootstrap analyses) [default: no normalization]" );
248         System.out.println( " -" + FIRST_OPTION + "=<i>: first evaluator topology to use (0-based) [default: 0]" );
249         System.out.println( " -" + LAST_OPTION
250                 + "=<i>: last evaluator topology to use (0-based) [default: use all until final topology]" );
251         System.out.println();
252     }
253
254     private static void checkUniquenessOfExternalNodes( final Phylogeny phy, final String msg )
255             throws IllegalArgumentException {
256         final Set<PhylogenyNode> ext_nodes = new HashSet<PhylogenyNode>( phy.getNumberOfExternalNodes() );
257         for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
258             final PhylogenyNode node = it.next();
259             if ( ext_nodes.contains( node ) ) {
260                 throw new IllegalArgumentException( "external node [" + node.toString() + "] of " + msg
261                         + " is not unique" );
262             }
263             ext_nodes.add( node );
264         }
265     }
266 }