3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
27 package org.forester.application;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.List;
35 import org.forester.evoinference.distance.NeighborJoining;
36 import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
37 import org.forester.evoinference.matrix.distance.DistanceMatrix;
38 import org.forester.io.parsers.SymmetricalDistanceMatrixParser;
39 import org.forester.io.writers.PhylogenyWriter;
40 import org.forester.phylogeny.Phylogeny;
41 import org.forester.util.CommandLineArguments;
42 import org.forester.util.ForesterUtil;
46 final static private String HELP_OPTION_1 = "help";
47 final static private String HELP_OPTION_2 = "h";
48 final static private String VERBOSE_OPTION = "v";
49 final static private String UPPER_TRIANGLE_OPTION = "u";
50 final static private String PRG_NAME = "nj";
51 final static private String PRG_VERSION = "0.0.1";
52 final static private String PRG_DATE = "2008.03.04";
53 final static private String E_MAIL = "czmasek@burnham.org";
54 final static private String WWW = "www.phylosoft.org/forester/";
56 public static void main( final String args[] ) {
57 ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW );
58 final List<String> allowed_options = new ArrayList<String>();
59 allowed_options.add( HELP_OPTION_1 );
60 allowed_options.add( HELP_OPTION_2 );
61 allowed_options.add( VERBOSE_OPTION );
62 allowed_options.add( UPPER_TRIANGLE_OPTION );
63 if ( ( args.length < 2 ) ) {
67 CommandLineArguments cla = null;
69 cla = new CommandLineArguments( args );
71 catch ( final Exception e ) {
72 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
74 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) ) {
78 if ( cla.getNumberOfNames() != 2 ) {
82 boolean verbose = false;
83 boolean upper_triangle = false;
84 if ( cla.isOptionSet( VERBOSE_OPTION ) ) {
87 if ( cla.isOptionSet( UPPER_TRIANGLE_OPTION ) ) {
88 upper_triangle = true;
90 final File infile = cla.getFile( 0 );
91 final File outfile = cla.getFile( 1 );
92 final String error1 = ForesterUtil.isReadableFile( infile );
93 if ( !ForesterUtil.isEmpty( error1 ) ) {
94 ForesterUtil.fatalError( PRG_NAME, "cannot read from infile [" + infile + "]: " + error1 );
96 if ( outfile.exists() ) {
97 ForesterUtil.fatalError( PRG_NAME, "outfile [" + outfile + "] already exists" );
99 final String error2 = ForesterUtil.isWritableFile( outfile );
100 if ( !ForesterUtil.isEmpty( error2 ) ) {
101 ForesterUtil.fatalError( PRG_NAME, "cannot write to outfile [" + outfile + "]: " + error2 );
103 final SymmetricalDistanceMatrixParser parser = SymmetricalDistanceMatrixParser.createInstance();
104 if ( upper_triangle ) {
105 parser.setInputMatrixType( SymmetricalDistanceMatrixParser.InputMatrixType.UPPER_TRIANGLE );
108 parser.setInputMatrixType( SymmetricalDistanceMatrixParser.InputMatrixType.LOWER_TRIANGLE );
110 DistanceMatrix[] matrices = null;
112 matrices = parser.parse( infile );
114 catch ( final IOException e ) {
115 ForesterUtil.fatalError( PRG_NAME, "failed to read from infile [" + infile + "]: " + e.getMessage() );
118 System.out.println( PRG_NAME + " > read " + matrices.length + " pairwise distance matrice(s) of size "
119 + matrices[ 0 ].getSize() );
121 final List<Phylogeny> ps = new ArrayList<Phylogeny>();
122 final NeighborJoining nj = NeighborJoining.createInstance( verbose );
123 final long start_time = new Date().getTime();
124 for( final DistanceMatrix matrix : matrices ) {
125 ps.add( nj.execute( ( BasicSymmetricalDistanceMatrix ) matrix ) );
127 final long end_time = new Date().getTime();
128 final PhylogenyWriter w = new PhylogenyWriter();
130 w.toPhyloXML( outfile, ps, 1, ForesterUtil.LINE_SEPARATOR );
132 catch ( final IOException e ) {
133 ForesterUtil.fatalError( PRG_NAME, "failed to write to outfile [" + outfile + "]: " + e.getMessage() );
135 System.out.println();
136 System.out.println( PRG_NAME + " > OK [" + ( end_time - start_time ) + "ms]" );
137 System.out.println();
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();