clean up
[jalview.git] / forester / java / src / org / forester / application / nj.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
26
27 package org.forester.application;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.List;
34
35 import org.forester.evoinference.distance.NeighborJoining;
36 import org.forester.evoinference.matrix.distance.DistanceMatrix;
37 import org.forester.io.parsers.SymmetricalDistanceMatrixParser;
38 import org.forester.io.writers.PhylogenyWriter;
39 import org.forester.phylogeny.Phylogeny;
40 import org.forester.util.CommandLineArguments;
41 import org.forester.util.ForesterUtil;
42
43 public class nj {
44
45     final static private String HELP_OPTION_1         = "help";
46     final static private String HELP_OPTION_2         = "h";
47     final static private String VERBOSE_OPTION        = "v";
48     final static private String UPPER_TRIANGLE_OPTION = "u";
49     final static private String PRG_NAME              = "nj";
50     final static private String PRG_VERSION           = "0.0.1";
51     final static private String PRG_DATE              = "2008.03.04";
52     final static private String E_MAIL                = "czmasek@burnham.org";
53     final static private String WWW                   = "www.phylosoft.org/forester/";
54
55     public static void main( final String args[] ) {
56         ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW );
57         final List<String> allowed_options = new ArrayList<String>();
58         allowed_options.add( HELP_OPTION_1 );
59         allowed_options.add( HELP_OPTION_2 );
60         allowed_options.add( VERBOSE_OPTION );
61         allowed_options.add( UPPER_TRIANGLE_OPTION );
62         if ( ( args.length < 2 ) ) {
63             printHelp();
64             System.exit( -1 );
65         }
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             printHelp();
75             System.exit( 0 );
76         }
77         if ( cla.getNumberOfNames() != 2 ) {
78             printHelp();
79             System.exit( -1 );
80         }
81         boolean verbose = false;
82         boolean upper_triangle = false;
83         if ( cla.isOptionSet( VERBOSE_OPTION ) ) {
84             verbose = true;
85         }
86         if ( cla.isOptionSet( UPPER_TRIANGLE_OPTION ) ) {
87             upper_triangle = true;
88         }
89         final File infile = cla.getFile( 0 );
90         final File outfile = cla.getFile( 1 );
91         final String error1 = ForesterUtil.isReadableFile( infile );
92         if ( !ForesterUtil.isEmpty( error1 ) ) {
93             ForesterUtil.fatalError( PRG_NAME, "cannot read from infile [" + infile + "]: " + error1 );
94         }
95         if ( outfile.exists() ) {
96             ForesterUtil.fatalError( PRG_NAME, "outfile [" + outfile + "] already exists" );
97         }
98         final String error2 = ForesterUtil.isWritableFile( outfile );
99         if ( !ForesterUtil.isEmpty( error2 ) ) {
100             ForesterUtil.fatalError( PRG_NAME, "cannot write to outfile [" + outfile + "]: " + error2 );
101         }
102         final SymmetricalDistanceMatrixParser parser = SymmetricalDistanceMatrixParser.createInstance();
103         if ( upper_triangle ) {
104             parser.setInputMatrixType( SymmetricalDistanceMatrixParser.InputMatrixType.UPPER_TRIANGLE );
105         }
106         else {
107             parser.setInputMatrixType( SymmetricalDistanceMatrixParser.InputMatrixType.LOWER_TRIANGLE );
108         }
109         DistanceMatrix[] matrices = null;
110         try {
111             matrices = parser.parse( infile );
112         }
113         catch ( final IOException e ) {
114             ForesterUtil.fatalError( PRG_NAME, "failed to read from infile [" + infile + "]: " + e.getMessage() );
115         }
116         if ( verbose ) {
117             System.out.println( PRG_NAME + " > read " + matrices.length + " pairwise distance matrice(s) of size "
118                     + matrices[ 0 ].getSize() );
119         }
120         final List<Phylogeny> ps = new ArrayList<Phylogeny>();
121         final NeighborJoining nj = NeighborJoining.createInstance();
122         nj.setVerbose( verbose );
123         final long start_time = new Date().getTime();
124         for( final DistanceMatrix matrix : matrices ) {
125             ps.add( nj.execute( matrix ) );
126         }
127         final long end_time = new Date().getTime();
128         final PhylogenyWriter w = new PhylogenyWriter();
129         try {
130             w.toPhyloXML( outfile, ps, 1, ForesterUtil.LINE_SEPARATOR );
131         }
132         catch ( final IOException e ) {
133             ForesterUtil.fatalError( PRG_NAME, "failed to write to outfile [" + outfile + "]: " + e.getMessage() );
134         }
135         System.out.println();
136         System.out.println( PRG_NAME + " > OK [" + ( end_time - start_time ) + "ms]" );
137         System.out.println();
138     }
139
140     private static void printHelp() {
141         System.out.println();
142         System.out.println( "Usage:" );
143         System.out.println();
144         System.out.println( "% java -cp forester.jar org.forester.applications." + PRG_NAME
145                 + " [options] <pairwise distances infile> <out file>" );
146         System.out.println();
147         System.out.println( " Options: " );
148         System.out.println( VERBOSE_OPTION + ": verbose on" );
149         System.out.println( UPPER_TRIANGLE_OPTION + ": upper triangle option on (lower triangle is default)" );
150         System.out.println();
151     }
152 }