work in airplane
[jalview.git] / forester / java / src / org / forester / application / get_distances.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.BufferedReader;
29 import java.io.BufferedWriter;
30 import java.io.File;
31 import java.io.FileReader;
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.util.StringTokenizer;
35
36 import org.forester.io.parsers.PhylogenyParser;
37 import org.forester.phylogeny.Phylogeny;
38 import org.forester.phylogeny.PhylogenyMethods;
39 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
40 import org.forester.phylogeny.factories.PhylogenyFactory;
41 import org.forester.util.ForesterUtil;
42
43 public class get_distances {
44
45     public static void main( final String args[] ) {
46         if ( args.length != 3 ) {
47             System.out.println( "\nget_distances: Wrong number of arguments.\n" );
48             System.out.println( "Usage: \"get_distances <phylogeny file> <file with node names> <outfile>\"\n" );
49             System.exit( -1 );
50         }
51         final File phylogeny_infile = new File( args[ 0 ] );
52         final File names_infile = new File( args[ 1 ] );
53         final File outfile = new File( args[ 2 ] );
54         Phylogeny p = null;
55         try {
56             final PhylogenyParser pp = ForesterUtil.createParserDependingOnFileType( phylogeny_infile, true );
57             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
58             p = factory.create( phylogeny_infile, pp )[ 0 ];
59         }
60         catch ( final Exception e ) {
61             System.out.println( "\nCould not read \"" + phylogeny_infile + "\" [" + e.getMessage() + "]\n" );
62             System.exit( -1 );
63         }
64         String line = "";
65         try {
66             final BufferedReader in = new BufferedReader( new FileReader( names_infile ) );
67             final BufferedWriter out = new BufferedWriter( new FileWriter( outfile ) );
68             while ( ( line = in.readLine() ) != null ) {
69                 if ( line.length() < 3 ) {
70                     continue;
71                 }
72                 final StringTokenizer st = new StringTokenizer( line );
73                 if ( st.countTokens() < 2 ) {
74                     continue;
75                 }
76                 final double d = PhylogenyMethods.getInstance().calculateDistance( p.getNode( st.nextToken() ),
77                                                                                    p.getNode( st.nextToken() ) );
78                 out.write( line + " " + d );
79                 out.newLine();
80             }
81             out.flush();
82             out.close();
83             in.close();
84         }
85         catch ( final IOException e ) {
86             System.out.println( "\nError during processing of \"" + names_infile + "\" [" + e.getMessage()
87                     + "] at line \"" + line + "\"\n" );
88             System.exit( -1 );
89         }
90         System.out.println( "\nDone.\n" );
91     }
92 }