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: https://sites.google.com/site/cmzmasek/home/software/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,
63                                               null,
64                                               PRG_VERSION,
65                                               PRG_DATE,
66                                               E_MAIL,
67                                               WWW,
68                                               ForesterUtil.getForesterLibraryInformation() );
69         CommandLineArguments cla = null;
70         try {
71             cla = new CommandLineArguments( args );
72         }
73         catch ( final Exception e ) {
74             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
75         }
76         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
77             printHelp();
78             System.exit( 0 );
79         }
80         if ( args.length < 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         if ( cla.getNumberOfNames() != 4 ) {
88             System.out.println();
89             System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
90             System.out.println();
91             printHelp();
92             System.exit( -1 );
93         }
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 );
102         }
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" );
109         }
110         if ( outfile.exists() ) {
111             ForesterUtil.fatalError( PRG_NAME, "[" + outfile + "] already exists" );
112         }
113         if ( !target_file.exists() ) {
114             ForesterUtil.fatalError( PRG_NAME, "target [" + target_file + "] does not exist" );
115         }
116         if ( !evaluators_file.exists() ) {
117             ForesterUtil.fatalError( PRG_NAME, "evaluators [" + evaluators_file + "] does not exist" );
118         }
119         boolean strict = false;
120         int first = 0;
121         int last = 0;
122         double norm = 0;
123         try {
124             if ( cla.isOptionSet( STRICT_OPTION ) ) {
125                 if ( cla.isOptionHasAValue( STRICT_OPTION ) ) {
126                     ForesterUtil.fatalError( PRG_NAME, "no value allowed for -" + STRICT_OPTION + " allowed" );
127                 }
128                 strict = true;
129             }
130             if ( cla.isOptionSet( FIRST_OPTION ) ) {
131                 first = cla.getOptionValueAsInt( FIRST_OPTION );
132             }
133             if ( cla.isOptionSet( LAST_OPTION ) ) {
134                 last = cla.getOptionValueAsInt( LAST_OPTION );
135             }
136             if ( cla.isOptionSet( NORMALIZE_OPTION ) ) {
137                 norm = cla.getOptionValueAsDouble( NORMALIZE_OPTION );
138             }
139         }
140         catch ( final Exception e ) {
141             ForesterUtil.fatalError( PRG_NAME, "error in command line: " + e.getLocalizedMessage() );
142         }
143         if ( ( first < 0 ) || ( last < 0 ) ) {
144             ForesterUtil
145             .fatalError( PRG_NAME,
146                          "attempt to set first or last evaluator topology to use to a number less than zero" );
147         }
148         if ( norm < 0 ) {
149             ForesterUtil.fatalError( PRG_NAME, "illegal value for normalizer [" + norm + "]" );
150         }
151         Phylogeny[] targets = null;
152         Phylogeny[] evaluators = null;
153         final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
154         try {
155             targets = factory.create( target_file, ParserUtils.createParserDependingOnFileType( target_file, true ) );
156         }
157         catch ( final IOException e ) {
158             ForesterUtil.fatalError( PRG_NAME,
159                                      "failed to read target phylogenies from [" + target_file + "]: "
160                                              + e.getLocalizedMessage() );
161         }
162         int counter = 0;
163         for( final Phylogeny target : targets ) {
164             try {
165                 checkUniquenessOfExternalNodes( target, "target " + counter );
166             }
167             catch ( final IllegalArgumentException e ) {
168                 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
169             }
170             counter++;
171         }
172         if ( targets.length == 1 ) {
173             ForesterUtil.programMessage( PRG_NAME, "read in one target" );
174         }
175         else {
176             ForesterUtil.programMessage( PRG_NAME, "read in a total of " + targets.length + " targets" );
177         }
178         try {
179             evaluators = factory.create( evaluators_file,
180                                          ParserUtils.createParserDependingOnFileType( evaluators_file, true ) );
181         }
182         catch ( final IOException e ) {
183             ForesterUtil.fatalError( PRG_NAME, "failed to read evaluator topologies from [" + evaluators_file + "]: "
184                     + e.getLocalizedMessage() );
185         }
186         counter = 0;
187         for( final Phylogeny evaluator : evaluators ) {
188             try {
189                 checkUniquenessOfExternalNodes( evaluator, "evaluator " + counter );
190             }
191             catch ( final IllegalArgumentException e ) {
192                 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
193             }
194             counter++;
195         }
196         ForesterUtil.programMessage( PRG_NAME, "read in a total of " + evaluators.length + " evaluator topologies" );
197         System.gc();
198         if ( last == 0 ) {
199             last = evaluators.length - 1;
200         }
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 + "]" );
204         }
205         double value = 1;
206         if ( norm > 0 ) {
207             value = norm / ( ( 1 + last ) - first );
208         }
209         ForesterUtil.programMessage( PRG_NAME, "first topology to use: " + first );
210         String is_last = "";
211         if ( last == ( evaluators.length - 1 ) ) {
212             is_last = " (corresponds to last topology in file)";
213         }
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 ) );
216         if ( norm > 0 ) {
217             ForesterUtil.programMessage( PRG_NAME, "normalizer: " + norm + " (" + ForesterUtil.round( value, 6 ) + ")" );
218         }
219         else {
220             ForesterUtil.programMessage( PRG_NAME, "normalizer: n/a" );
221         }
222         ForesterUtil.programMessage( PRG_NAME, "strict: " + strict );
223         for( final Phylogeny target : targets ) {
224             try {
225                 ConfidenceAssessor.evaluate( confidence_type, evaluators, target, strict, value, first, last );
226             }
227             catch ( final IllegalArgumentException e ) {
228                 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
229             }
230         }
231         try {
232             final PhylogenyWriter writer = new PhylogenyWriter();
233             writer.toPhyloXML( targets, 0, outfile, ForesterUtil.LINE_SEPARATOR );
234         }
235         catch ( final IOException e ) {
236             ForesterUtil.fatalError( PRG_NAME, "failed to write to [" + outfile + "]: " + e.getLocalizedMessage() );
237         }
238         ForesterUtil.programMessage( PRG_NAME, "wrote output to: [" + outfile + "]" );
239         ForesterUtil.programMessage( PRG_NAME, "OK" );
240         System.out.println();
241     }
242
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();
259     }
260
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" );
269             }
270             ext_nodes.add( node );
271         }
272     }
273 }