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) 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: 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.util.ArrayList;
31 import java.util.List;
32
33 import org.forester.io.parsers.PhylogenyParser;
34 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
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.Taxonomy;
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 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      = "170721";
51     final static private String PRG_DESC      = "clades within clades";
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
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 ) ) {
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             final File intreefile = cla.getFile( 0 );
87             final String query = cla.getName( 1 );
88             System.out.println( "Input tree: " + intreefile );
89             System.out.println( "Query:      " + query );
90             Phylogeny p = null;
91             try {
92                 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
93                 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( intreefile, true );
94                 p = factory.create( intreefile, pp )[ 0 ];
95             }
96             catch ( final Exception e ) {
97                 System.out.println( "\nCould not read \"" + intreefile + "\" [" + e.getMessage() + "]\n" );
98                 System.exit( -1 );
99             }
100             execute( p, query );
101         }
102         catch ( final Exception e ) {
103             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
104         }
105     }
106
107     private static void execute( final Phylogeny p, final String query ) {
108         final PhylogenyNode qnode = p.getNode( query );
109         if ( qnode.isRoot() ) {
110             throw new IllegalStateException( "Unexpected error: Query " + query
111                     + " is root. This should have never happened" );
112         }
113         if ( qnode.getParent().isRoot() ) {
114             throw new IllegalStateException( "Unexpected error: Parent of query " + query
115                     + " is root. This should have never happened" );
116         }
117         final PhylogenyNode qnode_pp = qnode.getParent().getParent();
118         final List<PhylogenyNode> qnode_ext_nodes = qnode_pp.getAllExternalDescendants();
119         final int lec_ext_nodes = qnode_ext_nodes.size() - 1;
120         final int p_ext_nodes = p.getNumberOfExternalNodes() - 1;
121         final double lec_ratio = ( 100.0 * lec_ext_nodes ) / p_ext_nodes;
122         final List<String> qnode_ext_nodes_names = new ArrayList<String>();
123         for( final PhylogenyNode qnode_ext_node : qnode_ext_nodes ) {
124             String name = qnode_ext_node.getName();
125             if ( ForesterUtil.isEmptyTrimmed( name ) ) {
126                 throw new IllegalArgumentException( "external node(s) with empty names found" );
127             }
128             name = name.trim();
129             if ( !name.equals( query ) ) {
130                 qnode_ext_nodes_names.add( name );
131             }
132         }
133         final String greatest_common_prefix = ForesterUtil.greatestCommonPrefix( qnode_ext_nodes_names );
134         System.out.println( );
135         System.out.println( "Results:");
136         if ( greatest_common_prefix.length() < 1 ) {
137             System.out.println( "WARNING: No greatest common prefix" );
138         }
139         else {
140             System.out.println( "Greatest common prefix: " + greatest_common_prefix );
141         }
142         if ( qnode_pp.isRoot() ) {
143             System.out.println( "WARNING: Least Encompassing Clade is entire tree" );
144         }
145         System.out.println( "Least Encompassing Clade has " + lec_ext_nodes + " external nodes (" +lec_ratio + "% of a total of "+ p_ext_nodes +")" );
146     }
147
148     private final static void print_help() {
149         System.out.println( "Usage: " + PRG_NAME
150                 + " <gene tree file> <query>" );
151         System.out.println();
152     }
153 }