2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
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.
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.
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
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.applications;
28 import java.io.BufferedReader;
29 import java.io.BufferedWriter;
31 import java.io.FileReader;
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.util.StringTokenizer;
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;
42 public class get_distances {
44 public static void main( final String args[] ) {
45 if ( args.length != 3 ) {
46 System.out.println( "\nget_distances: Wrong number of arguments.\n" );
47 System.out.println( "Usage: \"get_distances <phylogeny file> <file with node names> <outfile>\"\n" );
50 final File phylogeny_infile = new File( args[ 0 ] );
51 final File names_infile = new File( args[ 1 ] );
52 final File outfile = new File( args[ 2 ] );
55 final PhylogenyParser pp = org.forester.io.parsers.util.ParserUtils
56 .createParserDependingOnFileType( phylogeny_infile, true );
57 final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
58 p = factory.create( phylogeny_infile, pp )[ 0 ];
60 catch ( final Exception e ) {
61 System.out.println( "\nCould not read \"" + phylogeny_infile + "\" [" + e.getMessage() + "]\n" );
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 ) {
72 final StringTokenizer st = new StringTokenizer( line );
73 if ( st.countTokens() < 2 ) {
76 final double d = PhylogenyMethods.calculateDistance( p.getNode( st.nextToken() ),
77 p.getNode( st.nextToken() ) );
78 out.write( line + " " + d );
85 catch ( final IOException e ) {
86 System.out.println( "\nError during processing of \"" + names_infile + "\" [" + e.getMessage()
87 + "] at line \"" + line + "\"\n" );
90 System.out.println( "\nDone.\n" );