in progress
[jalview.git] / forester / java / src / org / forester / application / count_support.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.application;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33
34 import org.forester.io.parsers.PhylogenyParser;
35 import org.forester.io.parsers.util.ParserUtils;
36 import org.forester.io.writers.PhylogenyWriter;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.PhylogenyMethods;
39 import org.forester.phylogeny.PhylogenyNode;
40 import org.forester.phylogeny.data.Confidence;
41 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
42 import org.forester.phylogeny.factories.PhylogenyFactory;
43 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
44 import org.forester.tools.SupportCount;
45 import org.forester.util.CommandLineArguments;
46 import org.forester.util.ForesterUtil;
47
48 public class count_support {
49
50     final static private String  PRG_NAME                = "count_support";
51     final static private String  PRG_VERSION             = "1.0";
52     final static private String  PRG_DATE                = "2008.03.04";
53     final static private String E_MAIL        = "phylosoft@gmail.com";
54     final static private String WWW           = "www.phylosoft.org/forester/";
55     private final static boolean WRITE_EVALUATORS_AS_NHX = false;
56
57     public static void main( final String args[] ) {
58          ForesterUtil.printProgramInformation( PRG_NAME, null, PRG_VERSION, PRG_DATE, E_MAIL, WWW, ForesterUtil.getForesterLibraryInformation() );
59         
60         if ( ( args.length < 3 ) || ( args.length > 7 ) ) {
61             System.out.println();
62             System.out.println( count_support.PRG_NAME + ": wrong number of arguments" );
63             System.out.println();
64             System.out
65                     .println( "Usage: \"count_support [options] <file containing phylogeny to be evaluated> <file with phylogenies to be used for evaluation> <outfile> [outfile for evaluator phylogenies, "
66                             + "always unstripped if -t=<d> option is used, otherwise strippedness is dependent on -s option]\"\n" );
67             System.out
68                     .println( " Options: -s strip external nodes from evaluator phylogenies not found in phylogeny to be evaluated" );
69             System.out.println( "        : -t=<d> threshold for similarity (0.0 to 1.0)" );
70             System.out.println( "        : -n no branch lengths in outfile for evaluator phylogenies" );
71             System.out.println();
72             System.exit( -1 );
73         }
74         CommandLineArguments cla = null;
75         try {
76             cla = new CommandLineArguments( args );
77         }
78         catch ( final Exception e ) {
79             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
80         }
81         final List<String> allowed_options = new ArrayList<String>();
82         allowed_options.add( "s" );
83         allowed_options.add( "t" );
84         allowed_options.add( "n" );
85         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
86         if ( dissallowed_options.length() > 0 ) {
87             ForesterUtil.fatalError( count_support.PRG_NAME, "Unknown option(s): " + dissallowed_options );
88         }
89         final File phylogeny_infile = cla.getFile( 0 );
90         final File evaluators_infile = cla.getFile( 1 );
91         final File phylogeny_outfile = cla.getFile( 2 );
92         File evaluators_outfile = null;
93         boolean branch_lengths_in_ev_out = true;
94         if ( cla.isOptionSet( "n" ) ) {
95             branch_lengths_in_ev_out = false;
96         }
97         if ( cla.getNumberOfNames() == 4 ) {
98             evaluators_outfile = cla.getFile( 3 );
99         }
100         else {
101             if ( !branch_lengths_in_ev_out ) {
102                 ForesterUtil.fatalError( count_support.PRG_NAME,
103                                          "Cannot use -n option if no outfile for evaluators specified" );
104             }
105         }
106         Phylogeny p = null;
107         Phylogeny[] ev = null;
108         try {
109             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
110             final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( phylogeny_infile, true );
111             p = factory.create( phylogeny_infile, pp )[ 0 ];
112         }
113         catch ( final Exception e ) {
114             ForesterUtil.fatalError( count_support.PRG_NAME,
115                                      "Could not read \"" + phylogeny_infile + "\" [" + e.getMessage() + "]" );
116         }
117         try {
118             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
119             final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( evaluators_infile, true );
120             ev = factory.create( evaluators_infile, pp );
121         }
122         catch ( final Exception e ) {
123             ForesterUtil.fatalError( count_support.PRG_NAME,
124                                      "Could not read \"" + evaluators_infile + "\" [" + e.getMessage() + "]" );
125         }
126         boolean strip = false;
127         if ( cla.isOptionSet( "s" ) ) {
128             strip = true;
129         }
130         double threshhold = -1.0;
131         if ( cla.isOptionSet( "t" ) ) {
132             try {
133                 threshhold = cla.getOptionValueAsDouble( "t" );
134             }
135             catch ( final Exception e ) {
136                 ForesterUtil.fatalError( count_support.PRG_NAME, "error in command line arguments: " + e.getMessage() );
137             }
138             if ( ( threshhold < 0 ) || ( threshhold > 1.0 ) ) {
139                 ForesterUtil.fatalError( count_support.PRG_NAME,
140                                          "support threshold has to be between 0.0 and 1.0 (inclusive)" );
141             }
142         }
143         List<Phylogeny> evaluator_phylogenies_above_threshold = null;
144         try {
145             if ( threshhold >= 0 ) {
146                 evaluator_phylogenies_above_threshold = SupportCount.count( p, ev, strip, threshhold, true );
147                 if ( evaluator_phylogenies_above_threshold.size() < 1 ) {
148                     ForesterUtil.fatalError( "count_support", "appears like threshold for similarity is set too high" );
149                 }
150             }
151             else {
152                 SupportCount.count( p, ev, strip, true );
153             }
154         }
155         catch ( final Exception e ) {
156             ForesterUtil.fatalError( count_support.PRG_NAME, "Failure during support counting: " + e.getMessage() );
157         }
158         if ( threshhold >= 0 ) {
159             count_support.normalizeSupport( p, 100, evaluator_phylogenies_above_threshold.size() );
160             System.out.println( evaluator_phylogenies_above_threshold.size() + " out of " + ev.length
161                     + " evaluator phylogenies are above threshold of " + threshhold );
162         }
163         try {
164             final PhylogenyWriter w = new PhylogenyWriter();
165             w.toPhyloXML( phylogeny_outfile, p, 1 );
166         }
167         catch ( final IOException e ) {
168             ForesterUtil.fatalError( count_support.PRG_NAME, "Failure to write output [" + e.getMessage() + "]" );
169         }
170         System.out.println();
171         System.out.println( "Wrote phylogeny with support values to: " + phylogeny_outfile );
172         if ( evaluators_outfile != null ) {
173             try {
174                 final PhylogenyWriter w = new PhylogenyWriter();
175                 if ( evaluator_phylogenies_above_threshold != null ) {
176                     System.out.println( "Writing " + evaluator_phylogenies_above_threshold.size()
177                             + " evaluator phylogenies above threshold of " + threshhold + " to: " + evaluators_outfile );
178                     if ( count_support.WRITE_EVALUATORS_AS_NHX ) {
179                         w.toNewHampshireX( evaluator_phylogenies_above_threshold, evaluators_outfile, ";"
180                                 + ForesterUtil.getLineSeparator() );
181                     }
182                     else {
183                         w.toNewHampshire( evaluator_phylogenies_above_threshold,
184                                           true,
185                                           branch_lengths_in_ev_out,
186                                           evaluators_outfile,
187                                           ";" + ForesterUtil.getLineSeparator() );
188                     }
189                 }
190                 else {
191                     System.out.println( "Writing " + ev.length + " evaluator phylogenies to :" + evaluators_outfile );
192                     if ( count_support.WRITE_EVALUATORS_AS_NHX ) {
193                         w.toNewHampshireX( Arrays.asList( ev ),
194                                            evaluators_outfile,
195                                            ";" + ForesterUtil.getLineSeparator() );
196                     }
197                     else {
198                         w.toNewHampshire( Arrays.asList( ev ), true, branch_lengths_in_ev_out, evaluators_outfile, ";"
199                                 + ForesterUtil.getLineSeparator() );
200                     }
201                 }
202             }
203             catch ( final IOException e ) {
204                 ForesterUtil.fatalError( count_support.PRG_NAME, "Failure to write output [" + e.getMessage() + "]" );
205             }
206         }
207         System.out.println();
208         System.out.println( "Done." );
209         System.out.println();
210     }
211
212     private static void normalizeSupport( final Phylogeny p, final double normalized_max, final int number_phylos ) {
213         double min = Double.MAX_VALUE;
214         double max = -Double.MAX_VALUE;
215         double sum = 0.0;
216         int n = 0;
217         for( final PhylogenyNodeIterator iter = p.iteratorPostorder(); iter.hasNext(); ) {
218             final PhylogenyNode node = iter.next();
219             if ( !node.isRoot() && !node.isExternal() ) {
220                 final double b = PhylogenyMethods.getConfidenceValue( node );
221                 if ( b > max ) {
222                     max = b;
223                 }
224                 if ( ( b >= 0 ) && ( b < min ) ) {
225                     min = b;
226                 }
227                 sum += b;
228                 ++n;
229             }
230         }
231         double av = sum / n;
232         System.out.println( "Max support before normalization is    : " + max );
233         System.out.println( "Min support before normalization is    : " + min );
234         System.out.println( "Average support before normalization is: " + av + " (=" + sum + "/" + n + ")" );
235         System.out.println( "Normalizing so that theoretical maximum support value is: " + normalized_max );
236         System.out.println( "Number of phylogenies used in support analysis: " + number_phylos );
237         final double f = normalized_max / number_phylos;
238         min = Double.MAX_VALUE;
239         max = -Double.MAX_VALUE;
240         sum = 0.0;
241         n = 0;
242         for( final PhylogenyNodeIterator iter = p.iteratorPostorder(); iter.hasNext(); ) {
243             final PhylogenyNode node = iter.next();
244             if ( node.isRoot() || node.isExternal() ) {
245                 PhylogenyMethods.setBootstrapConfidence( node, Confidence.CONFIDENCE_DEFAULT_VALUE );
246             }
247             else {
248                 double b = PhylogenyMethods.getConfidenceValue( node );
249                 b = f * b;
250                 PhylogenyMethods.setBootstrapConfidence( node, b );
251                 if ( b > max ) {
252                     max = b;
253                 }
254                 if ( ( b >= 0 ) && ( b < min ) ) {
255                     min = b;
256                 }
257                 sum += b;
258                 ++n;
259             }
260         }
261         av = sum / n;
262         System.out.println( "Max support after normalization is    : " + max );
263         System.out.println( "Min support after normalization is    : " + min );
264         System.out.println( "Average support after normalization is: " + av + " (=" + sum + "/" + n + ")" );
265     }
266 }