2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.application;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
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;
46 public class cladinator {
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";
57 public static void main( final String args[] ) {
59 ForesterUtil.printProgramInformation( PRG_NAME,
65 ForesterUtil.getForesterLibraryInformation() );
66 CommandLineArguments cla = null;
68 cla = new CommandLineArguments( args );
70 catch ( final Exception e ) {
71 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
73 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
78 else if ( ( args.length != 2 ) ) {
80 System.out.println( "Wrong number of arguments." );
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 );
92 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
93 final PhylogenyParser pp = ParserUtils.createParserDependingOnFileType( intreefile, true );
94 p = factory.create( intreefile, pp )[ 0 ];
96 catch ( final Exception e ) {
97 System.out.println( "\nCould not read \"" + intreefile + "\" [" + e.getMessage() + "]\n" );
102 catch ( final Exception e ) {
103 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
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" );
113 if ( qnode.getParent().isRoot() ) {
114 throw new IllegalStateException( "Unexpected error: Parent of query " + query
115 + " is root. This should have never happened" );
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" );
129 if ( !name.equals( query ) ) {
130 qnode_ext_nodes_names.add( name );
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" );
140 System.out.println( "Greatest common prefix: " + greatest_common_prefix );
142 if ( qnode_pp.isRoot() ) {
143 System.out.println( "WARNING: Least Encompassing Clade is entire tree" );
145 System.out.println( "Least Encompassing Clade has " + lec_ext_nodes + " external nodes (" +lec_ratio + "% of a total of "+ p_ext_nodes +")" );
148 private final static void print_help() {
149 System.out.println( "Usage: " + PRG_NAME
150 + " <gene tree file> <query>" );
151 System.out.println();