2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2009 Christian M. Zmasek
6 // Copyright (C) 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.BufferedReader;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashSet;
34 import java.util.List;
36 import org.forester.go.PfamToGoMapping;
37 import org.forester.go.PfamToGoParser;
38 import org.forester.util.CommandLineArguments;
39 import org.forester.util.ForesterUtil;
41 public class pfam_go {
43 private static final String ALLOW_DUPLICATES_OPTION = "d";
44 final static private String HELP_OPTION_1 = "help";
45 final static private String HELP_OPTION_2 = "h";
46 final static private String PRG_NAME = "pfam_go";
47 final static private String PRG_VERSION = "1.10";
48 final static private String PRG_DATE = "2011.06.26";
49 final static private String E_MAIL = "czmasek@burnham.org";
50 final static private String WWW = "www.phylosoft.org";
52 private static void process( final File pfams_file,
53 final List<PfamToGoMapping> mappings,
54 final boolean allow_duplicates ) throws IOException {
55 final BufferedReader reader = ForesterUtil.obtainReader( pfams_file );
58 int not_found_count = 0;
59 Collection<String> encountered_domains = null;
60 if ( allow_duplicates ) {
61 encountered_domains = new ArrayList<String>();
64 encountered_domains = new HashSet<String>();
66 while ( ( line = reader.readLine() ) != null ) {
68 if ( ForesterUtil.isEmpty( line ) || line.startsWith( "##" ) ) {
71 else if ( line.startsWith( "#" ) ) {
72 encountered_domains.clear();
73 line = line.replace( '#', '>' );
74 System.out.println( line );
77 if ( allow_duplicates || !encountered_domains.contains( line ) ) {
78 encountered_domains.add( line );
79 boolean found = false;
80 for( final PfamToGoMapping mapping : mappings ) {
81 if ( mapping.getKey().getId().equals( line ) ) {
82 System.out.println( mapping.getValue() );
94 System.err.println( "# duplicate domain: " + line );
98 System.out.println( "# pfams with mapping to GO : " + found_count );
99 System.out.println( "# pfams without mapping to GO: " + not_found_count );
103 public static void main( final String args[] ) {
104 CommandLineArguments cla = null;
106 cla = new CommandLineArguments( args );
108 catch ( final Exception e ) {
109 ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
111 if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
115 final List<String> allowed_options = new ArrayList<String>();
116 allowed_options.add( ALLOW_DUPLICATES_OPTION );
117 if ( ( cla.getNumberOfNames() != 2 ) && ( cla.getNumberOfNames() != 3 ) ) {
121 final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
122 if ( dissallowed_options.length() > 0 ) {
123 ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
125 final File pfam2go_file = cla.getFile( 0 );
126 final File pfams_file = cla.getFile( 1 );
127 boolean allow_duplicates = false;
128 if ( cla.isOptionSet( ALLOW_DUPLICATES_OPTION ) ) {
129 allow_duplicates = true;
131 final PfamToGoParser pfam2go_parser = new PfamToGoParser( pfam2go_file );
132 List<PfamToGoMapping> mappings = null;
134 mappings = pfam2go_parser.parse();
136 catch ( final IOException e ) {
140 process( pfams_file, mappings, allow_duplicates );
142 catch ( final IOException e ) {
145 System.out.println();
148 private static void printHelp() {
149 ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW );
150 System.out.println( "Usage:" );
151 System.out.println();
152 System.out.println( PRG_NAME + " [-" + ALLOW_DUPLICATES_OPTION
153 + " to allow duplicates] <pfam2go file> <file with pfams>" );
154 System.out.println();
155 System.out.println();