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