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     private final static boolean WRITE_EVALUATORS_AS_NHX = false;
54
55     public static void main( final String args[] ) {
56         ForesterUtil
57                 .printProgramInformation( count_support.PRG_NAME, count_support.PRG_VERSION, count_support.PRG_DATE );
58         if ( ( args.length < 3 ) || ( args.length > 7 ) ) {
59             System.out.println();
60             System.out.println( count_support.PRG_NAME + ": wrong number of arguments" );
61             System.out.println();
62             System.out
63                     .println( "Usage: \"count_support [options] <file containing phylogeny to be evaluated> <file with phylogenies to be used for evaluation> <outfile> [outfile for evaluator phylogenies, "
64                             + "always unstripped if -t=<d> option is used, otherwise strippedness is dependent on -s option]\"\n" );
65             System.out
66                     .println( " Options: -s strip external nodes from evaluator phylogenies not found in phylogeny to be evaluated" );
67             System.out.println( "        : -t=<d> threshold for similarity (0.0 to 1.0)" );
68             System.out.println( "        : -n no branch lengths in outfile for evaluator phylogenies" );
69             System.out.println();
70             System.exit( -1 );
71         }
72         CommandLineArguments cla = null;
73         try {
74             cla = new CommandLineArguments( args );
75         }
76         catch ( final Exception e ) {
77             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
78         }
79         final List<String> allowed_options = new ArrayList<String>();
80         allowed_options.add( "s" );
81         allowed_options.add( "t" );
82         allowed_options.add( "n" );
83         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
84         if ( dissallowed_options.length() > 0 ) {
85             ForesterUtil.fatalError( count_support.PRG_NAME, "Unknown option(s): " + dissallowed_options );
86         }
87         final File phylogeny_infile = cla.getFile( 0 );
88         final File evaluators_infile = cla.getFile( 1 );
89         final File phylogeny_outfile = cla.getFile( 2 );
90         File evaluators_outfile = null;
91         boolean branch_lengths_in_ev_out = true;
92         if ( cla.isOptionSet( "n" ) ) {
93             branch_lengths_in_ev_out = false;
94         }
95         if ( cla.getNumberOfNames() == 4 ) {
96             evaluators_outfile = cla.getFile( 3 );
97         }
98         else {
99             if ( !branch_lengths_in_ev_out ) {
100                 ForesterUtil.fatalError( count_support.PRG_NAME,
101                                          "Cannot use -n option if no outfile for evaluators specified" );
102             }
103         }
104         Phylogeny p = null;
105         Phylogeny[] ev = null;
106         try {
107             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
108             final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( phylogeny_infile, true );
109             p = factory.create( phylogeny_infile, pp )[ 0 ];
110         }
111         catch ( final Exception e ) {
112             ForesterUtil.fatalError( count_support.PRG_NAME,
113                                      "Could not read \"" + phylogeny_infile + "\" [" + e.getMessage() + "]" );
114         }
115         try {
116             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
117             final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( evaluators_infile, true );
118             ev = factory.create( evaluators_infile, pp );
119         }
120         catch ( final Exception e ) {
121             ForesterUtil.fatalError( count_support.PRG_NAME,
122                                      "Could not read \"" + evaluators_infile + "\" [" + e.getMessage() + "]" );
123         }
124         boolean strip = false;
125         if ( cla.isOptionSet( "s" ) ) {
126             strip = true;
127         }
128         double threshhold = -1.0;
129         if ( cla.isOptionSet( "t" ) ) {
130             try {
131                 threshhold = cla.getOptionValueAsDouble( "t" );
132             }
133             catch ( final Exception e ) {
134                 ForesterUtil.fatalError( count_support.PRG_NAME, "error in command line arguments: " + e.getMessage() );
135             }
136             if ( ( threshhold < 0 ) || ( threshhold > 1.0 ) ) {
137                 ForesterUtil.fatalError( count_support.PRG_NAME,
138                                          "support threshold has to be between 0.0 and 1.0 (inclusive)" );
139             }
140         }
141         List<Phylogeny> evaluator_phylogenies_above_threshold = null;
142         try {
143             if ( threshhold >= 0 ) {
144                 evaluator_phylogenies_above_threshold = SupportCount.count( p, ev, strip, threshhold, true );
145                 if ( evaluator_phylogenies_above_threshold.size() < 1 ) {
146                     ForesterUtil.fatalError( "count_support", "appears like threshold for similarity is set too high" );
147                 }
148             }
149             else {
150                 SupportCount.count( p, ev, strip, true );
151             }
152         }
153         catch ( final Exception e ) {
154             ForesterUtil.fatalError( count_support.PRG_NAME, "Failure during support counting: " + e.getMessage() );
155         }
156         if ( threshhold >= 0 ) {
157             count_support.normalizeSupport( p, 100, evaluator_phylogenies_above_threshold.size() );
158             System.out.println( evaluator_phylogenies_above_threshold.size() + " out of " + ev.length
159                     + " evaluator phylogenies are above threshold of " + threshhold );
160         }
161         try {
162             final PhylogenyWriter w = new PhylogenyWriter();
163             w.toPhyloXML( phylogeny_outfile, p, 1 );
164         }
165         catch ( final IOException e ) {
166             ForesterUtil.fatalError( count_support.PRG_NAME, "Failure to write output [" + e.getMessage() + "]" );
167         }
168         System.out.println();
169         System.out.println( "Wrote phylogeny with support values to: " + phylogeny_outfile );
170         if ( evaluators_outfile != null ) {
171             try {
172                 final PhylogenyWriter w = new PhylogenyWriter();
173                 if ( evaluator_phylogenies_above_threshold != null ) {
174                     System.out.println( "Writing " + evaluator_phylogenies_above_threshold.size()
175                             + " evaluator phylogenies above threshold of " + threshhold + " to: " + evaluators_outfile );
176                     if ( count_support.WRITE_EVALUATORS_AS_NHX ) {
177                         w.toNewHampshireX( evaluator_phylogenies_above_threshold, evaluators_outfile, ";"
178                                 + ForesterUtil.getLineSeparator() );
179                     }
180                     else {
181                         w.toNewHampshire( evaluator_phylogenies_above_threshold,
182                                           true,
183                                           branch_lengths_in_ev_out,
184                                           evaluators_outfile,
185                                           ";" + ForesterUtil.getLineSeparator() );
186                     }
187                 }
188                 else {
189                     System.out.println( "Writing " + ev.length + " evaluator phylogenies to :" + evaluators_outfile );
190                     if ( count_support.WRITE_EVALUATORS_AS_NHX ) {
191                         w.toNewHampshireX( Arrays.asList( ev ),
192                                            evaluators_outfile,
193                                            ";" + ForesterUtil.getLineSeparator() );
194                     }
195                     else {
196                         w.toNewHampshire( Arrays.asList( ev ), true, branch_lengths_in_ev_out, evaluators_outfile, ";"
197                                 + ForesterUtil.getLineSeparator() );
198                     }
199                 }
200             }
201             catch ( final IOException e ) {
202                 ForesterUtil.fatalError( count_support.PRG_NAME, "Failure to write output [" + e.getMessage() + "]" );
203             }
204         }
205         System.out.println();
206         System.out.println( "Done." );
207         System.out.println();
208     }
209
210     private static void normalizeSupport( final Phylogeny p, final double normalized_max, final int number_phylos ) {
211         double min = Double.MAX_VALUE;
212         double max = -Double.MAX_VALUE;
213         double sum = 0.0;
214         int n = 0;
215         for( final PhylogenyNodeIterator iter = p.iteratorPostorder(); iter.hasNext(); ) {
216             final PhylogenyNode node = iter.next();
217             if ( !node.isRoot() && !node.isExternal() ) {
218                 final double b = PhylogenyMethods.getConfidenceValue( node );
219                 if ( b > max ) {
220                     max = b;
221                 }
222                 if ( ( b >= 0 ) && ( b < min ) ) {
223                     min = b;
224                 }
225                 sum += b;
226                 ++n;
227             }
228         }
229         double av = sum / n;
230         System.out.println( "Max support before normalization is    : " + max );
231         System.out.println( "Min support before normalization is    : " + min );
232         System.out.println( "Average support before normalization is: " + av + " (=" + sum + "/" + n + ")" );
233         System.out.println( "Normalizing so that theoretical maximum support value is: " + normalized_max );
234         System.out.println( "Number of phylogenies used in support analysis: " + number_phylos );
235         final double f = normalized_max / number_phylos;
236         min = Double.MAX_VALUE;
237         max = -Double.MAX_VALUE;
238         sum = 0.0;
239         n = 0;
240         for( final PhylogenyNodeIterator iter = p.iteratorPostorder(); iter.hasNext(); ) {
241             final PhylogenyNode node = iter.next();
242             if ( node.isRoot() || node.isExternal() ) {
243                 PhylogenyMethods.setBootstrapConfidence( node, Confidence.CONFIDENCE_DEFAULT_VALUE );
244             }
245             else {
246                 double b = PhylogenyMethods.getConfidenceValue( node );
247                 b = f * b;
248                 PhylogenyMethods.setBootstrapConfidence( node, b );
249                 if ( b > max ) {
250                     max = b;
251                 }
252                 if ( ( b >= 0 ) && ( b < min ) ) {
253                     min = b;
254                 }
255                 sum += b;
256                 ++n;
257             }
258         }
259         av = sum / n;
260         System.out.println( "Max support after normalization is    : " + max );
261         System.out.println( "Min support after normalization is    : " + min );
262         System.out.println( "Average support after normalization is: " + av + " (=" + sum + "/" + n + ")" );
263     }
264 }