small clean-up.
[jalview.git] / forester / java / src / org / forester / application / printAllSpecies.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.File;
29 import java.io.FileWriter;
30 import java.io.PrintWriter;
31
32 import org.forester.io.parsers.PhylogenyParser;
33 import org.forester.phylogeny.Phylogeny;
34 import org.forester.phylogeny.PhylogenyMethods;
35 import org.forester.phylogeny.PhylogenyNode;
36 import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
37 import org.forester.phylogeny.factories.PhylogenyFactory;
38 import org.forester.util.ForesterUtil;
39
40 public class printAllSpecies {
41
42     public static void main( final String args[] ) {
43         Phylogeny tree = null;
44         PhylogenyNode node = null;
45         PrintWriter out = null;
46         File infile = null, outfile = null;
47         if ( args.length != 2 ) {
48             System.err.println( "\nprintAllSpecies: Wrong number of arguments." );
49             System.err.println( "Usage: \"java printAllSpecies <infile> <outfile>\"\n" );
50             System.exit( -1 );
51         }
52         infile = new File( args[ 0 ] );
53         outfile = new File( args[ 1 ] );
54         try {
55             final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
56             final PhylogenyParser pp = ForesterUtil.createParserDependingOnFileType( infile, true );
57             tree = factory.create( infile, pp )[ 0 ];
58         }
59         catch ( final Exception e ) {
60             System.err.println( e + "\nCould not read " + infile + "\n" );
61             System.exit( -1 );
62         }
63         node = tree.getFirstExternalNode();
64         try {
65             out = new PrintWriter( new FileWriter( outfile ), true );
66             while ( node != null ) {
67                 out.println( PhylogenyMethods.getSpecies( node ) );
68                 node = node.getNextExternalNode();
69             }
70         }
71         catch ( final Exception e ) {
72             System.err.println( e + "\nException during writing.\n" );
73             System.exit( -1 );
74         }
75         finally {
76             out.close();
77         }
78     }
79 }