first prototype
[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.text.DecimalFormat;
30
31 import org.forester.clade_analysis.Analysis;
32 import org.forester.clade_analysis.Result;
33 import org.forester.io.parsers.PhylogenyParser;
34 import org.forester.io.parsers.util.ParserUtils;
35 import org.forester.phylogeny.Phylogeny;
36 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
37 import org.forester.phylogeny.factories.PhylogenyFactory;
38 import org.forester.util.CommandLineArguments;
39 import org.forester.util.ForesterUtil;
40
41 public final class cladinator {
42
43     final static private String        PRG_NAME      = "cladinator";
44     final static private String        PRG_VERSION   = "0.100";
45     final static private String        PRG_DATE      = "170721";
46     final static private String        PRG_DESC      = "clades within clades -- analysis of pplacer type outputs";
47     final static private String        E_MAIL        = "phyloxml@gmail.com";
48     final static private String        WWW           = "https://sites.google.com/site/cmzmasek/home/software/forester";
49     final static private String        HELP_OPTION_1 = "help";
50     final static private String        HELP_OPTION_2 = "h";
51     private final static DecimalFormat df2           = new DecimalFormat( ".##" );
52
53     public static void main( final String args[] ) {
54         try {
55             ForesterUtil.printProgramInformation( PRG_NAME,
56                                                   PRG_DESC,
57                                                   PRG_VERSION,
58                                                   PRG_DATE,
59                                                   E_MAIL,
60                                                   WWW,
61                                                   ForesterUtil.getForesterLibraryInformation() );
62             CommandLineArguments cla = null;
63             try {
64                 cla = new CommandLineArguments( args );
65             }
66             catch ( final Exception e ) {
67                 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
68             }
69             if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
70                 System.out.println();
71                 print_help();
72                 System.exit( 0 );
73             }
74             else if ( ( args.length != 2 ) ) {
75                 System.out.println();
76                 System.out.println( "Wrong number of arguments." );
77                 System.out.println();
78                 print_help();
79                 System.exit( -1 );
80             }
81             //final List<String> allowed_options = new ArrayList<>();
82             final File intreefile = cla.getFile( 0 );
83             final String query = cla.getName( 1 );
84             System.out.println( "Input tree: " + intreefile );
85             System.out.println( "Query:      " + query );
86             Phylogeny p = null;
87             try {
88                 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
89                 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( intreefile, true );
90                 p = factory.create( intreefile, pp )[ 0 ];
91             }
92             catch ( final Exception e ) {
93                 System.out.println( "\nCould not read \"" + intreefile + "\" [" + e.getMessage() + "]\n" );
94                 System.exit( -1 );
95             }
96             final Result res = Analysis.execute( p, query );
97             System.out.println();
98             System.out.println( "Result:" );
99             System.out.println( "Greatest common prefix     : " + res.getGreatestCommonPrefix() );
100             System.out.println( "Greatest common prefix up  : " + res.getGreatestCommonPrefixUp() );
101             System.out.println( "Greatest common prefix down: " + res.getGreatestCommonPrefixDown() );
102             final double lec_ratio = ( 100.0 * res.getLeastEncompassingCladeSize() ) / res.getTreeSize();
103             System.out.println( "Least Encompassing Clade has " + res.getLeastEncompassingCladeSize()
104                     + " external nodes (" + df2.format( lec_ratio ) + "% of a total of " + res.getTreeSize() + ")" );
105             if ( res.getWarnings().size() > 0 ) {
106                 System.out.println( "Warnings:" );
107                 for( final String s : res.getWarnings() ) {
108                     System.out.println( s );
109                 }
110             }
111         }
112         catch ( final Exception e ) {
113             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
114         }
115     }
116
117     private final static void print_help() {
118         System.out.println( "Usage: " + PRG_NAME + " <gene tree file> <query>" );
119         System.out.println();
120     }
121 }