in progress...
[jalview.git] / forester / java / src / org / forester / application / meta_ontologizer.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
25
26 package org.forester.application;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.forester.go.PfamToGoMapping;
34 import org.forester.go.PfamToGoParser;
35 import org.forester.go.etc.MetaOntologizer;
36 import org.forester.util.CommandLineArguments;
37 import org.forester.util.ForesterUtil;
38
39 public class meta_ontologizer {
40
41     final static private String HELP_OPTION_1      = "help";
42     final static private String HELP_OPTION_2      = "h";
43     final static private String P_OPTION           = "p";
44     final static private String PRG_NAME           = "meta_ontologizer";
45     final static private String PRG_VERSION        = "1.10";
46     final static private String PRG_DATE           = "2009.04.29";
47     final static private String E_MAIL             = "czmasek@burnham.org";
48     final static private String WWW                = "www.phylosoft.org/forester/";
49     private static final String RESULT_FILE_PREFIX = "table";
50
51     public static void main( final String args[] ) {
52         ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW );
53         CommandLineArguments cla = null;
54         try {
55             cla = new CommandLineArguments( args );
56         }
57         catch ( final Exception e ) {
58             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
59         }
60         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
61             printHelp();
62             System.exit( 0 );
63         }
64         if ( args.length < 4 ) {
65             System.out.println();
66             System.out.println( "[" + PRG_NAME + "] incorrect number of arguments" );
67             System.out.println();
68             printHelp();
69             System.exit( -1 );
70         }
71         final List<String> allowed_options = new ArrayList<String>();
72         allowed_options.add( P_OPTION );
73         final List<String> mandatory_options = new ArrayList<String>();
74         mandatory_options.add( P_OPTION );
75         if ( ( cla.getNumberOfNames() != 5 ) && ( cla.getNumberOfNames() != 6 ) ) {
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 missing = cla.validateMandatoryOptionsAsString( mandatory_options );
83         if ( missing.length() > 0 ) {
84             ForesterUtil.fatalError( PRG_NAME, "missing option(s): " + missing );
85         }
86         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
87         if ( dissallowed_options.length() > 0 ) {
88             ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
89         }
90         final File obo_file = cla.getFile( 0 );
91         final File pfam2go_file = cla.getFile( 1 );
92         final File ontologizer_outdir = cla.getFile( 2 );
93         File domain_gain_loss_file = null;
94         String outfile_base = null;
95         String comment = null;
96         if ( cla.getNumberOfNames() == 6 ) {
97             domain_gain_loss_file = cla.getFile( 3 );
98             outfile_base = cla.getName( 4 );
99             comment = cla.getName( 5 );
100         }
101         else {
102             outfile_base = cla.getName( 3 );
103             comment = cla.getName( 4 );
104         }
105         double p_adjusted_upper_limit = -1;
106         try {
107             p_adjusted_upper_limit = cla.getOptionValueAsDouble( P_OPTION );
108         }
109         catch ( final IOException e ) {
110             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
111         }
112         try {
113             final PfamToGoParser parser = new PfamToGoParser( pfam2go_file );
114             final List<PfamToGoMapping> pfam_to_go_mappings = parser.parse();
115             ForesterUtil.programMessage( PRG_NAME, "parsed " + pfam_to_go_mappings.size() + " Pfam to GO mappings" );
116             MetaOntologizer.reformat( ontologizer_outdir,
117                                       RESULT_FILE_PREFIX,
118                                       domain_gain_loss_file,
119                                       outfile_base,
120                                       obo_file,
121                                       p_adjusted_upper_limit,
122                                       comment,
123                                       pfam_to_go_mappings );
124         }
125         catch ( final IOException e ) {
126             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
127             e.printStackTrace();
128         }
129         ForesterUtil.programMessage( PRG_NAME, "OK" );
130         System.out.println();
131     }
132
133     private static void printHelp() {
134         System.out.println( "Usage:" );
135         System.out.println();
136         System.out
137         .println( PRG_NAME
138                   + " -p=<adj P value limit> <obo file> <pfam to go file> <ontologizer outdir> [domain gain loss file] <base for meta ontologizer outfile> <comment>" );
139         System.out.println();
140     }
141 }