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