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