initial commit
[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, "failed to read target phylogenies from [" + target_file + "]: "
152                     + e.getLocalizedMessage() );
153         }
154         int counter = 0;
155         for( final Phylogeny target : targets ) {
156             try {
157                 checkUniquenessOfExternalNodes( target, "target " + counter );
158             }
159             catch ( final IllegalArgumentException e ) {
160                 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
161             }
162             counter++;
163         }
164         if ( targets.length == 1 ) {
165             ForesterUtil.programMessage( PRG_NAME, "read in one target" );
166         }
167         else {
168             ForesterUtil.programMessage( PRG_NAME, "read in a total of " + targets.length + " targets" );
169         }
170         try {
171             evaluators = factory.create( evaluators_file, ForesterUtil
172                     .createParserDependingOnFileType( evaluators_file, true ) );
173         }
174         catch ( final IOException e ) {
175             ForesterUtil.fatalError( PRG_NAME, "failed to read evaluator topologies from [" + evaluators_file + "]: "
176                     + e.getLocalizedMessage() );
177         }
178         counter = 0;
179         for( final Phylogeny evaluator : evaluators ) {
180             try {
181                 checkUniquenessOfExternalNodes( evaluator, "evaluator " + counter );
182             }
183             catch ( final IllegalArgumentException e ) {
184                 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
185             }
186             counter++;
187         }
188         ForesterUtil.programMessage( PRG_NAME, "read in a total of " + evaluators.length + " evaluator topologies" );
189         System.gc();
190         if ( last == 0 ) {
191             last = evaluators.length - 1;
192         }
193         if ( ( last >= evaluators.length ) || ( last <= first ) ) {
194             ForesterUtil.fatalError( PRG_NAME, "illegal value for first or last evaluator topology to use [" + first
195                     + ", " + last + "]" );
196         }
197         double value = 1;
198         if ( norm > 0 ) {
199             value = norm / ( 1 + last - first );
200         }
201         ForesterUtil.programMessage( PRG_NAME, "first topology to use: " + first );
202         String is_last = "";
203         if ( last == ( evaluators.length - 1 ) ) {
204             is_last = " (corresponds to last topology in file)";
205         }
206         ForesterUtil.programMessage( PRG_NAME, "last topology to use : " + last + is_last );
207         ForesterUtil.programMessage( PRG_NAME, "sum of topologies used as evaluators: " + ( last - first + 1 ) );
208         if ( norm > 0 ) {
209             ForesterUtil.programMessage( PRG_NAME, "normalizer: " + norm + " (" + ForesterUtil.round( value, 6 ) + ")" );
210         }
211         else {
212             ForesterUtil.programMessage( PRG_NAME, "normalizer: n/a" );
213         }
214         ForesterUtil.programMessage( PRG_NAME, "strict: " + strict );
215         for( final Phylogeny target : targets ) {
216             try {
217                 ConfidenceAssessor.evaluate( confidence_type, evaluators, target, strict, value, first, last );
218             }
219             catch ( final IllegalArgumentException e ) {
220                 ForesterUtil.fatalError( PRG_NAME, e.getLocalizedMessage() );
221             }
222         }
223         try {
224             final PhylogenyWriter writer = new PhylogenyWriter();
225             writer.toPhyloXML( targets, 0, outfile, ForesterUtil.LINE_SEPARATOR );
226         }
227         catch ( final IOException e ) {
228             ForesterUtil.fatalError( PRG_NAME, "failed to write to [" + outfile + "]: " + e.getLocalizedMessage() );
229         }
230         ForesterUtil.programMessage( PRG_NAME, "wrote output to: [" + outfile + "]" );
231         ForesterUtil.programMessage( PRG_NAME, "OK" );
232         System.out.println();
233     }
234
235     private static void printHelp() {
236         System.out.println( "Usage:" );
237         System.out.println();
238         System.out.println( PRG_NAME
239                 + " [options] <confidence type> <target tree file> <evaluators tree file> <outfile>" );
240         System.out.println();
241         System.out.println( "options:" );
242         System.out.println();
243         System.out.println( " -" + STRICT_OPTION
244                 + "    : strict [default: non-strict]: all nodes between 'target' and 'evaluators' must match" );
245         System.out.println( " -" + NORMALIZE_OPTION
246                 + "=<d>: normalize to this value (e.g. 100 for most bootstrap analyses) [default: no normalization]" );
247         System.out.println( " -" + FIRST_OPTION + "=<i>: first evaluator topology to use (0-based) [default: 0]" );
248         System.out.println( " -" + LAST_OPTION
249                 + "=<i>: last evaluator topology to use (0-based) [default: use all until final topology]" );
250         System.out.println();
251     }
252
253     private static void checkUniquenessOfExternalNodes( final Phylogeny phy, final String msg )
254             throws IllegalArgumentException {
255         final Set<PhylogenyNode> ext_nodes = new HashSet<PhylogenyNode>( phy.getNumberOfExternalNodes() );
256         for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
257             final PhylogenyNode node = it.next();
258             if ( ext_nodes.contains( node ) ) {
259                 throw new IllegalArgumentException( "external node [" + node.toString() + "] of " + msg
260                         + " is not unique" );
261             }
262             ext_nodes.add( node );
263         }
264     }
265 }