clean up
[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.HashSet;
33 import java.util.List;
34 import java.util.Set;
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     final static private String HELP_OPTION_1 = "help";
44     final static private String HELP_OPTION_2 = "h";
45     final static private String PRG_NAME      = "pfam2go";
46     final static private String PRG_VERSION   = "1.00";
47     final static private String PRG_DATE      = "2010.02.02";
48     final static private String E_MAIL        = "czmasek@burnham.org";
49     final static private String WWW           = "www.phylosoft.org";
50
51     private static void doit( final File pfams_file, final List<PfamToGoMapping> mappings ) throws IOException {
52         final BufferedReader reader = ForesterUtil.obtainReader( pfams_file );
53         String line = "";
54         int found_count = 0;
55         int not_found_count = 0;
56         final Set<String> encountered_domains = new HashSet<String>();
57         while ( ( line = reader.readLine() ) != null ) {
58             line = line.trim();
59             if ( ForesterUtil.isEmpty( line ) || line.startsWith( "##" ) ) {
60                 continue;
61             }
62             else if ( line.startsWith( "#" ) ) {
63                 encountered_domains.clear();
64                 line = line.replace( '#', '>' );
65                 System.out.println( line );
66             }
67             else {
68                 if ( !encountered_domains.contains( line ) ) {
69                     encountered_domains.add( line );
70                     boolean found = false;
71                     for( final PfamToGoMapping mapping : mappings ) {
72                         if ( mapping.getKey().getId().equals( line ) ) {
73                             System.out.println( mapping.getValue() );
74                             found = true;
75                         }
76                     }
77                     if ( found ) {
78                         found_count++;
79                     }
80                     else {
81                         not_found_count++;
82                     }
83                 }
84                 else {
85                     System.err.println( "# duplicate domain: " + line );
86                 }
87             }
88         }
89         System.out.println( "# pfams with mapping to GO   : " + found_count );
90         System.out.println( "# pfams without mapping to GO: " + not_found_count );
91         reader.close();
92     }
93
94     public static void main( final String args[] ) {
95         CommandLineArguments cla = null;
96         try {
97             cla = new CommandLineArguments( args );
98         }
99         catch ( final Exception e ) {
100             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
101         }
102         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
103             printHelp();
104             System.exit( 0 );
105         }
106         final List<String> allowed_options = new ArrayList<String>();
107         if ( cla.getNumberOfNames() != 2 ) {
108             printHelp();
109             System.exit( -1 );
110         }
111         final String dissallowed_options = cla.validateAllowedOptionsAsString( allowed_options );
112         if ( dissallowed_options.length() > 0 ) {
113             ForesterUtil.fatalError( PRG_NAME, "unknown option(s): " + dissallowed_options );
114         }
115         final File pfam2go_file = cla.getFile( 0 );
116         final File pfams_file = cla.getFile( 1 );
117         final PfamToGoParser pfam2go_parser = new PfamToGoParser( pfam2go_file );
118         List<PfamToGoMapping> mappings = null;
119         try {
120             mappings = pfam2go_parser.parse();
121         }
122         catch ( final IOException e ) {
123             e.printStackTrace();
124         }
125         try {
126             doit( pfams_file, mappings );
127         }
128         catch ( final IOException e ) {
129             e.printStackTrace();
130         }
131         System.out.println();
132     }
133
134     private static void printHelp() {
135         ForesterUtil.printProgramInformation( PRG_NAME, PRG_VERSION, PRG_DATE, E_MAIL, WWW );
136         System.out.println( "Usage:" );
137         System.out.println();
138         System.out.println( PRG_NAME + " <pfam2go file> <file with pfams>" );
139         System.out.println();
140         System.out.println();
141     }
142 }