added -rs option
[jalview.git] / forester / java / src / org / forester / application / obo_tool.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: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.application;
27
28 import java.io.BufferedWriter;
29 import java.io.File;
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;
35
36 import org.forester.go.GoTerm;
37 import org.forester.go.OBOparser;
38 import org.forester.util.CommandLineArguments;
39 import org.forester.util.ForesterUtil;
40
41 public class obo_tool {
42
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/";
52
53     public static void main( final String args[] ) {
54         ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW );
55         CommandLineArguments cla = null;
56         try {
57             cla = new CommandLineArguments( args );
58         }
59         catch ( final Exception e ) {
60             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
61         }
62         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
63             printHelp();
64             System.exit( 0 );
65         }
66         if ( args.length < 3 ) {
67             System.out.println();
68             System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
69             System.out.println();
70             printHelp();
71             System.exit( -1 );
72         }
73         final List<String> allowed_options = new ArrayList<String>();
74         allowed_options.add( GO_ID_TO_NAME_OPTION );
75         if ( cla.getNumberOfNames() != 2 ) {
76             System.out.println();
77             System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
78             System.out.println();
79             printHelp();
80             System.exit( -1 );
81         }
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 );
85         }
86         boolean output_ids_to_names = false;
87         if ( cla.isOptionSet( GO_ID_TO_NAME_OPTION ) ) {
88             output_ids_to_names = true;
89         }
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;
94         try {
95             go_terms = parser.parse();
96         }
97         catch ( final IOException e ) {
98             ForesterUtil.fatalError( PRG_NAME, e.toString() );
99         }
100         ForesterUtil.programMessage( PRG_NAME, "successfully read in " + go_terms.size() + " GO terms from [" + infile
101                                      + "]" );
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 );
107             }
108             try {
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() );
112                     out.write( "\t" );
113                     out.write( go_term.getDefinition() );
114                     out.write( ForesterUtil.LINE_SEPARATOR );
115                 }
116                 out.close();
117             }
118             catch ( final IOException e ) {
119                 ForesterUtil.fatalError( PRG_NAME, e.toString() );
120             }
121             ForesterUtil.programMessage( PRG_NAME, "wrote: [" + outfile_ids_to_names + "]" );
122         }
123         ForesterUtil.programMessage( PRG_NAME, "OK" );
124         System.out.println();
125     }
126
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();
136     }
137 }