clean up
[jalview.git] / forester / java / src / org / forester / application / nhx_too.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: www.phylosoft.org/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.writers.PhylogenyWriter;
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 class nhx_too {
42
43     final static private String PRG_NAME                 = "nhx_too";
44     final static private String PRG_VERSION              = "0.1";
45     final static private String PRG_DATE                 = "2008.03.04";
46     final static private String INT_NODE_NAME_IS_SUPPORT = "is";
47
48     public static void main( final String args[] ) {
49         ForesterUtil.printProgramInformation( nhx_too.PRG_NAME, nhx_too.PRG_VERSION, nhx_too.PRG_DATE );
50         if ( ( args.length < 3 ) || ( args.length > 3 ) ) {
51             System.out.println();
52             System.out.println( nhx_too.PRG_NAME + ": wrong number of arguments" );
53             System.out.println();
54             System.out.println( "Usage: \"" + nhx_too.PRG_NAME + " [options] <infile> <outfile>\n" );
55             System.out.println( " Options: -" + nhx_too.INT_NODE_NAME_IS_SUPPORT
56                     + ": internal node names are support values (i.e. MrBayes output)" );
57             System.out.println();
58             System.exit( -1 );
59         }
60         CommandLineArguments cla = null;
61         try {
62             cla = new CommandLineArguments( args );
63         }
64         catch ( final Exception e ) {
65             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
66         }
67         final List<String> allowed_options = new ArrayList<String>();
68         allowed_options.add( nhx_too.INT_NODE_NAME_IS_SUPPORT );
69         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
70         if ( dissallowed_options.length() > 0 ) {
71             ForesterUtil.fatalError( nhx_too.PRG_NAME, "Unknown option(s): " + dissallowed_options );
72         }
73         final File phylogeny_infile = cla.getFile( 0 );
74         final File phylogeny_outfile = cla.getFile( 1 );
75         boolean int_node_name_is_support = false;
76         if ( cla.isOptionSet( nhx_too.INT_NODE_NAME_IS_SUPPORT ) ) {
77             int_node_name_is_support = true;
78         }
79         Phylogeny p = null;
80         try {
81             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
82             final PhylogenyParser pp = ForesterUtil.createParserDependingOnFileType( phylogeny_infile, true );
83             p = factory.create( phylogeny_infile, pp )[ 0 ];
84         }
85         catch ( final Exception e ) {
86             ForesterUtil.fatalError( nhx_too.PRG_NAME, "Could not read \"" + phylogeny_infile + "\" [" + e.getMessage()
87                     + "]" );
88         }
89         if ( int_node_name_is_support ) {
90             try {
91                 ForesterUtil.transferInternalNodeNamesToConfidence( p );
92             }
93             catch ( final Exception e ) {
94                 ForesterUtil.unexpectedFatalError( nhx_too.PRG_NAME,
95                                                    "Failure during moving of internal names to support values ["
96                                                            + e.getMessage() + "]" );
97             }
98         }
99         try {
100             final PhylogenyWriter w = new PhylogenyWriter();
101             w.toNewHampshireX( p, phylogeny_outfile );
102         }
103         catch ( final IOException e ) {
104             ForesterUtil.fatalError( nhx_too.PRG_NAME, "Failure to write output [" + e.getMessage() + "]" );
105         }
106         System.out.println();
107         System.out.println( "Done [wrote \"" + phylogeny_outfile + "\"]." );
108         System.out.println();
109     }
110 }