in progress
[jalview.git] / forester / java / src / org / forester / application / pfam_go.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2009 Christian M. Zmasek
6 // Copyright (C) 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.BufferedReader;
29 import java.io.File;
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;
35
36 import org.forester.go.PfamToGoMapping;
37 import org.forester.go.PfamToGoParser;
38 import org.forester.util.CommandLineArguments;
39 import org.forester.util.ForesterUtil;
40
41 public class pfam_go {
42
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";
51
52     private static void process( final File pfams_file, final List<PfamToGoMapping> mappings, boolean allow_duplicates )
53             throws IOException {
54         final BufferedReader reader = ForesterUtil.obtainReader( pfams_file );
55         String line = "";
56         int found_count = 0;
57         int not_found_count = 0;
58         Collection<String> encountered_domains = null;
59         if ( allow_duplicates ) {
60             encountered_domains = new ArrayList<String>();
61         }
62         else {
63             encountered_domains = new HashSet<String>();
64         }
65         while ( ( line = reader.readLine() ) != null ) {
66             line = line.trim();
67             if ( ForesterUtil.isEmpty( line ) || line.startsWith( "##" ) ) {
68                 continue;
69             }
70             else if ( line.startsWith( "#" ) ) {
71                 encountered_domains.clear();
72                 line = line.replace( '#', '>' );
73                 System.out.println( line );
74             }
75             else {
76                 if ( allow_duplicates || !encountered_domains.contains( line ) ) {
77                     encountered_domains.add( line );
78                     boolean found = false;
79                     for( final PfamToGoMapping mapping : mappings ) {
80                         if ( mapping.getKey().getId().equals( line ) ) {
81                             System.out.println( mapping.getValue() );
82                             found = true;
83                         }
84                     }
85                     if ( found ) {
86                         found_count++;
87                     }
88                     else {
89                         not_found_count++;
90                     }
91                 }
92                 else {
93                     System.err.println( "# duplicate domain: " + line );
94                 }
95             }
96         }
97         System.out.println( "# pfams with mapping to GO   : " + found_count );
98         System.out.println( "# pfams without mapping to GO: " + not_found_count );
99         reader.close();
100     }
101
102     public static void main( final String args[] ) {
103         CommandLineArguments cla = null;
104         try {
105             cla = new CommandLineArguments( args );
106         }
107         catch ( final Exception e ) {
108             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
109         }
110         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
111             printHelp();
112             System.exit( 0 );
113         }
114         final List<String> allowed_options = new ArrayList<String>();
115         allowed_options.add( ALLOW_DUPLICATES_OPTION );
116         if ( cla.getNumberOfNames() != 2 && cla.getNumberOfNames() != 3 ) {
117             printHelp();
118             System.exit( -1 );
119         }
120         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
121         if ( dissallowed_options.length() > 0 ) {
122             ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
123         }
124         final File pfam2go_file = cla.getFile( 0 );
125         final File pfams_file = cla.getFile( 1 );
126         boolean allow_duplicates = false;
127         if ( cla.isOptionSet( ALLOW_DUPLICATES_OPTION ) ) {
128             allow_duplicates = true;
129         }
130         final PfamToGoParser pfam2go_parser = new PfamToGoParser( pfam2go_file );
131         List<PfamToGoMapping> mappings = null;
132         try {
133             mappings = pfam2go_parser.parse();
134         }
135         catch ( final IOException e ) {
136             e.printStackTrace();
137         }
138         try {
139             process( pfams_file, mappings, allow_duplicates );
140         }
141         catch ( final IOException e ) {
142             e.printStackTrace();
143         }
144         System.out.println();
145     }
146
147     private static void printHelp() {
148         ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW );
149         System.out.println( "Usage:" );
150         System.out.println();
151         System.out.println( PRG_NAME + " [-" + ALLOW_DUPLICATES_OPTION
152                 + " to allow duplicates] <pfam2go file> <file with pfams>" );
153         System.out.println();
154         System.out.println();
155     }
156 }