in progress...
[jalview.git] / forester / java / src / org / forester / application / cladinator.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2017 Christian M. Zmasek
6 // Copyright (C) 2017 J. Craig Venter Institute
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: phyloxml @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.application;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.text.DecimalFormat;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import org.forester.clade_analysis.AnalysisMulti;
35 import org.forester.clade_analysis.AnalysisSingle;
36 import org.forester.clade_analysis.Prefix;
37 import org.forester.clade_analysis.ResultMulti;
38 import org.forester.clade_analysis.ResultSingle;
39 import org.forester.io.parsers.PhylogenyParser;
40 import org.forester.io.parsers.util.ParserUtils;
41 import org.forester.phylogeny.Phylogeny;
42 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
43 import org.forester.phylogeny.factories.PhylogenyFactory;
44 import org.forester.util.CommandLineArguments;
45 import org.forester.util.ForesterUtil;
46
47 public final class cladinator {
48
49     final static private String        PRG_NAME      = "cladinator";
50     final static private String        PRG_VERSION   = "0.100";
51     final static private String        PRG_DATE      = "170823";
52     final static private String        PRG_DESC      = "clades within clades -- analysis of pplacer type outputs";
53     final static private String        E_MAIL        = "phyloxml@gmail.com";
54     final static private String        WWW           = "https://sites.google.com/site/cmzmasek/home/software/forester";
55     final static private String        HELP_OPTION_1 = "help";
56     final static private String        HELP_OPTION_2 = "h";
57     final static private String        SEP_OPTION    = "s";
58     private final static DecimalFormat df2           = new DecimalFormat( "0.0#" );
59
60     public static void main( final String args[] ) {
61         try {
62             ForesterUtil.printProgramInformation( PRG_NAME,
63                                                   PRG_DESC,
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 ) ) {
77                 System.out.println();
78                 print_help();
79                 System.exit( 0 );
80             }
81             else if ( ( args.length != 2 && args.length != 3 ) ) {
82                 System.out.println();
83                 System.out.println( "Wrong number of arguments." );
84                 System.out.println();
85                 print_help();
86                 System.exit( -1 );
87             }
88             final List<String> allowed_options = new ArrayList<String>();
89             allowed_options.add( SEP_OPTION );
90             final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
91             if ( dissallowed_options.length() > 0 ) {
92                 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
93             }
94             final String separator;
95             if ( cla.isOptionSet( SEP_OPTION ) ) {
96                 separator = cla.getOptionValue( SEP_OPTION );
97             }
98             else {
99                 separator = null;
100             }
101             final File intreefile = cla.getFile( 0 );
102             final String query = cla.getName( 1 );
103             System.out.println( "Input tree: " + intreefile );
104             System.out.println( "Query     : " + query );
105             if ( !ForesterUtil.isEmpty( separator ) ) {
106                 System.out.println( "Separator : " + separator );
107             }
108             else {
109                 System.out.println( "Separator : none" );
110             }
111             Phylogeny p = null;
112             try {
113                 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
114                 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( intreefile, true );
115                 p = factory.create( intreefile, pp )[ 0 ];
116             }
117             catch ( final IOException e ) {
118                 System.out.println( "\nCould not read \"" + intreefile + "\" [" + e.getMessage() + "]\n" );
119                 System.exit( -1 );
120             }
121           
122             final ResultMulti res = AnalysisMulti.execute( p, query, separator, 0.5 );
123             
124             System.out.println();
125             System.out.println( "Result:" );
126             System.out.println( "Query                        : " + query );
127             
128             ///////////////////
129             
130          
131          
132             System.out.println( "Collapsed:" );
133           
134               for( final Prefix prefix : res.getCollapsedMultiHitPrefixes() ) {
135                   System.out.println( prefix );
136               }
137               if ( _has_specifics ) {
138                  
139                   System.out.println( "Specifics:" );
140                  
141                   for( final Prefix prefix : _cleaned_spec ) {
142                       System.out.println( prefix );
143                      
144                   }
145                   
146                   System.out.println( "Collapsed With Specifics:" );
147                  
148                   for( final Prefix prefix : _collapsed ) {
149                       System.out.println( prefix );
150                       
151                       for( final Prefix spec : _cleaned_spec ) {
152                           if ( spec.getPrefix().startsWith( prefix.getPrefix() ) ) {
153                               System.out.println( "    " + spec );
154                              
155                           }
156                       }
157                   }
158               }
159               if ( !ForesterUtil.isEmpty( _all_down ) ) {
160                   
161                   System.out.println( "Collapsed Down:" );
162                   
163                   for( final Prefix prefix : _collapsed_down ) {
164                       System.out.println( prefix );
165                       
166                   }
167               
168               }
169               if ( !ForesterUtil.isEmpty( _all_up ) ) {
170                   
171            
172                   System.out.println( "Collapsed Up:" );
173                  
174                   for( final Prefix prefix : _collapsed_up ) {
175                       System.out.println( prefix );
176                      
177                   }
178              
179               }
180             
181             ///////////////////
182             
183             
184             System.out.print( "Greatest Common Prefix       : " + res.getGreatestCommonPrefix() );
185             if ( !ForesterUtil.isEmpty( res.getGreatestCommonPrefix() )
186                     && !ForesterUtil.isEmpty( res.getGreatestCommonCladeSubtreeConfidence() ) ) {
187                 System.out.println( "\t(" + res.getGreatestCommonCladeSubtreeConfidence() + ")" );
188             }
189             else {
190                 System.out.println();
191             }
192             System.out.print( "Greatest Common Prefix Up    : " + res.getGreatestCommonPrefixUp() );
193             if ( !ForesterUtil.isEmpty( res.getGreatestCommonPrefixUp() )
194                     && !ForesterUtil.isEmpty( res.getGreatestCommonCladeUpSubtreeConfidence() ) ) {
195                 System.out.println( "\t(" + res.getGreatestCommonCladeUpSubtreeConfidence() + ")" );
196             }
197             else {
198                 System.out.println();
199             }
200             System.out.print( "Greatest Common Prefix Down  : " + res.getGreatestCommonPrefixDown() );
201             if ( !ForesterUtil.isEmpty( res.getGreatestCommonPrefixDown() )
202                     && !ForesterUtil.isEmpty( res.getGreatestCommonCladeDownSubtreeConfidence() ) ) {
203                 System.out.println( "\t(" + res.getGreatestCommonCladeDownSubtreeConfidence() + ")" );
204             }
205             else {
206                 System.out.println();
207             }
208             System.out.println( "Least Encompassing Clade size: " + res.getLeastEncompassingCladeSize()
209                     + " external nodes" );
210             final double lec_ratio = ( 100.0 * res.getLeastEncompassingCladeSize() ) / res.getTreeSize();
211             System.out.println( "Least Encompassing Clade size: " + df2.format( lec_ratio ) + "%" );
212             System.out.println( "Total tree size              : " + res.getTreeSize() + " external nodes" );
213             if ( res.getWarnings().size() > 0 ) {
214                 System.out.println( "Warnings:" );
215                 for( final String s : res.getWarnings() ) {
216                     System.out.println( s );
217                 }
218             }
219             System.out.println();
220         }
221         catch ( final IllegalArgumentException e ) {
222             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
223         }
224         catch ( final Exception e ) {
225             e.printStackTrace();
226             ForesterUtil.fatalError( PRG_NAME, "Unexpected errror!" );
227         }
228     }
229
230     private final static void print_help() {
231         System.out.println( "Usage:" );
232         System.out.println();
233         System.out.println( PRG_NAME + " [options] <gene tree file> <query>" );
234         System.out.println();
235         System.out.println( " options:" );
236         System.out.println( "  -" + SEP_OPTION + "=<separator>: the separator to be used" );
237         System.out.println();
238         System.out.println( "Example:" );
239         System.out.println();
240         System.out.println( " " + PRG_NAME + " -s=. my_tree.xml A.1.1.1" );
241         System.out.println();
242     }
243 }