ddc1aefc09fd7a751a98295afdd158e181643f32
[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.Analysis;
35 import org.forester.clade_analysis.Result;
36 import org.forester.io.parsers.PhylogenyParser;
37 import org.forester.io.parsers.util.ParserUtils;
38 import org.forester.phylogeny.Phylogeny;
39 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
40 import org.forester.phylogeny.factories.PhylogenyFactory;
41 import org.forester.util.CommandLineArguments;
42 import org.forester.util.ForesterUtil;
43
44 public final class cladinator {
45
46     final static private String        PRG_NAME      = "cladinator";
47     final static private String        PRG_VERSION   = "0.101";
48     final static private String        PRG_DATE      = "170810";
49     final static private String        PRG_DESC      = "clades within clades -- analysis of pplacer type outputs";
50     final static private String        E_MAIL        = "phyloxml@gmail.com";
51     final static private String        WWW           = "https://sites.google.com/site/cmzmasek/home/software/forester";
52     final static private String        HELP_OPTION_1 = "help";
53     final static private String        HELP_OPTION_2 = "h";
54     final static private String        SEP_OPTION    = "s";
55     private final static DecimalFormat df2           = new DecimalFormat( ".##" );
56
57     public static void main( final String args[] ) {
58         try {
59             ForesterUtil.printProgramInformation( PRG_NAME,
60                                                   PRG_DESC,
61                                                   PRG_VERSION,
62                                                   PRG_DATE,
63                                                   E_MAIL,
64                                                   WWW,
65                                                   ForesterUtil.getForesterLibraryInformation() );
66             CommandLineArguments cla = null;
67             try {
68                 cla = new CommandLineArguments( args );
69             }
70             catch ( final Exception e ) {
71                 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
72             }
73             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
74                 System.out.println();
75                 print_help();
76                 System.exit( 0 );
77             }
78             else if ( ( args.length != 2 && args.length != 3 ) ) {
79                 System.out.println();
80                 System.out.println( "Wrong number of arguments." );
81                 System.out.println();
82                 print_help();
83                 System.exit( -1 );
84             }
85             final List<String> allowed_options = new ArrayList<String>();
86             allowed_options.add( SEP_OPTION );
87             final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
88             if ( dissallowed_options.length() > 0 ) {
89                 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
90             }
91             final String separator;
92             if ( cla.isOptionSet( SEP_OPTION ) ) {
93                 separator = cla.getOptionValue( SEP_OPTION );
94             }
95             else {
96                 separator = null;
97             }
98             final File intreefile = cla.getFile( 0 );
99             final String query = cla.getName( 1 );
100             System.out.println( "Input tree: " + intreefile );
101             System.out.println( "Query     : " + query );
102             if ( !ForesterUtil.isEmpty( separator ) ) {
103                 System.out.println( "Separator : " + separator );
104             }
105             else {
106                 System.out.println( "Separator : none" );
107             }
108             Phylogeny p = null;
109             try {
110                 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
111                 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( intreefile, true );
112                 p = factory.create( intreefile, pp )[ 0 ];
113             }
114             catch ( final IOException e ) {
115                 System.out.println( "\nCould not read \"" + intreefile + "\" [" + e.getMessage() + "]\n" );
116                 System.exit( -1 );
117             }
118             final Result res = Analysis.execute( p, query, separator );
119             System.out.println();
120             System.out.println( "Result:" );
121             System.out.println( "Query                        : " + query );
122             System.out.print( "Greatest Common Prefix       : " + res.getGreatestCommonPrefix() );
123             if ( !ForesterUtil.isEmpty( res.getGreatestCommonPrefix() )
124                     && !ForesterUtil.isEmpty( res.getGreatestCommonCladeSubtreeConfidence() ) ) {
125                 System.out.println( "\t(" + res.getGreatestCommonCladeSubtreeConfidence() + ")" );
126             }
127             else {
128                 System.out.println();
129             }
130             System.out.print( "Greatest Common Prefix Up    : " + res.getGreatestCommonPrefixUp() );
131             if ( !ForesterUtil.isEmpty( res.getGreatestCommonPrefixUp() )
132                     && !ForesterUtil.isEmpty( res.getGreatestCommonCladeUpSubtreeConfidence() ) ) {
133                 System.out.println( "\t(" + res.getGreatestCommonCladeUpSubtreeConfidence() + ")" );
134             }
135             else {
136                 System.out.println();
137             }
138             System.out.print( "Greatest Common Prefix Down  : " + res.getGreatestCommonPrefixDown() );
139             if ( !ForesterUtil.isEmpty( res.getGreatestCommonPrefixDown() )
140                     && !ForesterUtil.isEmpty( res.getGreatestCommonCladeDownSubtreeConfidence() ) ) {
141                 System.out.println( "\t(" + res.getGreatestCommonCladeDownSubtreeConfidence() + ")" );
142             }
143             else {
144                 System.out.println();
145             }
146             System.out.println( "Least Encompassing Clade size: " + res.getLeastEncompassingCladeSize()
147                     + " external nodes" );
148             final double lec_ratio = ( 100.0 * res.getLeastEncompassingCladeSize() ) / res.getTreeSize();
149             System.out.println( "Least Encompassing Clade size: " + df2.format( lec_ratio ) + "%" );
150             System.out.println( "Total tree size              : " + res.getTreeSize() + " external nodes" );
151             if ( res.getWarnings().size() > 0 ) {
152                 System.out.println( "Warnings:" );
153                 for( final String s : res.getWarnings() ) {
154                     System.out.println( s );
155                 }
156             }
157             System.out.println();
158         }
159         catch ( final IllegalArgumentException e ) {
160             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
161         }
162         catch ( final Exception e ) {
163             e.printStackTrace();
164             ForesterUtil.fatalError( PRG_NAME, "Unexpected errror!" );
165         }
166     }
167
168     private final static void print_help() {
169         System.out.println( "Usage:" );
170         System.out.println();
171         System.out.println( PRG_NAME + " [options] <gene tree file> <query>" );
172         System.out.println();
173         System.out.println( " options:" );
174         System.out.println( "  -" + SEP_OPTION + "=<separator>: the separator to be used" );
175         System.out.println();
176         System.out.println( "Example:" );
177         System.out.println();
178         System.out.println( " " + PRG_NAME + " -s=. my_tree.xml A.1.1.1" );
179         System.out.println();
180     }
181 }