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.application;
28 import java.io.BufferedWriter;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.io.Writer;
33 import java.util.ArrayList;
34 import java.util.List;
36 import org.forester.go.GoTerm;
37 import org.forester.go.OBOparser;
38 import org.forester.util.CommandLineArguments;
39 import org.forester.util.ForesterUtil;
41 public class obo_tool {
43 private static final String IDS_TO_NAMES_SUFFIX = "_ids_to_names";
44 final static private String HELP_OPTION_1 = "help";
45 final static private String HELP_OPTION_2 = "h";
46 final static private String GO_ID_TO_NAME_OPTION = "i";
47 final static private String PRG_NAME = "obo_tool";
48 final static private String PRG_VERSION = "1.00";
49 final static private String PRG_DATE = "2008.11.26";
50 final static private String E_MAIL = "czmasek@burnham.org";
51 final static private String WWW = "www.phylosoft.org/forester/";
53 public static void main( final String args[] ) {
54 ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW );
55 CommandLineArguments cla = null;
57 cla = new CommandLineArguments( args );
59 catch ( final Exception e ) {
60 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
62 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
66 if ( args.length < 3 ) {
68 System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
73 final List<String> allowed_options = new ArrayList<String>();
74 allowed_options.add( GO_ID_TO_NAME_OPTION );
75 if ( cla.getNumberOfNames() != 2 ) {
77 System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
82 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
83 if ( dissallowed_options.length() > 0 ) {
84 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
86 boolean output_ids_to_names = false;
87 if ( cla.isOptionSet( GO_ID_TO_NAME_OPTION ) ) {
88 output_ids_to_names = true;
90 final File infile = cla.getFile( 0 );
91 final File outfile = cla.getFile( 1 );
92 final OBOparser parser = new OBOparser( infile, OBOparser.ReturnType.BASIC_GO_TERM );
93 List<GoTerm> go_terms = null;
95 go_terms = parser.parse();
97 catch ( final IOException e ) {
98 ForesterUtil.fatalError( PRG_NAME, e.toString() );
100 ForesterUtil.programMessage( PRG_NAME, "successfully read in " + go_terms.size() + " GO terms from [" + infile
102 if ( output_ids_to_names ) {
103 final File outfile_ids_to_names = new File( outfile + IDS_TO_NAMES_SUFFIX );
104 final String error = ForesterUtil.isWritableFile( outfile_ids_to_names );
105 if ( !ForesterUtil.isEmpty( error ) ) {
106 ForesterUtil.fatalError( PRG_NAME, error );
109 final Writer out = new BufferedWriter( new FileWriter( outfile_ids_to_names ) );
110 for( final GoTerm go_term : go_terms ) {
111 out.write( go_term.getGoId().getId() );
113 out.write( go_term.getDefinition() );
114 out.write( ForesterUtil.LINE_SEPARATOR );
118 catch ( final IOException e ) {
119 ForesterUtil.fatalError( PRG_NAME, e.toString() );
121 ForesterUtil.programMessage( PRG_NAME, "wrote: [" + outfile_ids_to_names + "]" );
123 ForesterUtil.programMessage( PRG_NAME, "OK" );
124 System.out.println();
127 private static void printHelp() {
128 System.out.println( "Usage:" );
129 System.out.println();
130 System.out.println( PRG_NAME + " <options> <obo infile> <outfile>" );
131 System.out.println();
132 System.out.println( " options: " );
133 System.out.println();
134 System.out.println( " -" + GO_ID_TO_NAME_OPTION + ": output GO id to name map file" );
135 System.out.println();