(no commit message)
[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: https://sites.google.com/site/cmzmasek/home/software/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,
53                                  final List<PfamToGoMapping> mappings,
54                                  final boolean allow_duplicates ) throws IOException {
55         final BufferedReader reader = ForesterUtil.obtainReader( pfams_file );
56         String line = "";
57         int found_count = 0;
58         int not_found_count = 0;
59         Collection<String> encountered_domains = null;
60         if ( allow_duplicates ) {
61             encountered_domains = new ArrayList<String>();
62         }
63         else {
64             encountered_domains = new HashSet<String>();
65         }
66         while ( ( line = reader.readLine() ) != null ) {
67             line = line.trim();
68             if ( ForesterUtil.isEmpty( line ) || line.startsWith( "##" ) ) {
69                 continue;
70             }
71             else if ( line.startsWith( "#" ) ) {
72                 encountered_domains.clear();
73                 line = line.replace( '#', '>' );
74                 System.out.println( line );
75             }
76             else {
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().equals( line ) ) {
82                             System.out.println( mapping.getValue() );
83                             found = true;
84                         }
85                     }
86                     if ( found ) {
87                         found_count++;
88                     }
89                     else {
90                         not_found_count++;
91                     }
92                 }
93                 else {
94                     System.err.println( "# duplicate domain: " + line );
95                 }
96             }
97         }
98         System.out.println( "# pfams with mapping to GO   : " + found_count );
99         System.out.println( "# pfams without mapping to GO: " + not_found_count );
100         reader.close();
101     }
102
103     public static void main( final String args[] ) {
104         CommandLineArguments cla = null;
105         try {
106             cla = new CommandLineArguments( args );
107         }
108         catch ( final Exception e ) {
109             ForesterUtil.fatalError( PRG_NAME, e.getMessage() );
110         }
111         if ( cla.isOptionSet( HELP_OPTION_1 ) || cla.isOptionSet( HELP_OPTION_2 ) || ( args.length == 0 ) ) {
112             printHelp();
113             System.exit( 0 );
114         }
115         final List<String> allowed_options = new ArrayList<String>();
116         allowed_options.add( ALLOW_DUPLICATES_OPTION );
117         if ( ( cla.getNumberOfNames() != 2 ) && ( cla.getNumberOfNames() != 3 ) ) {
118             printHelp();
119             System.exit( -1 );
120         }
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 );
124         }
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;
130         }
131         final PfamToGoParser pfam2go_parser = new PfamToGoParser( pfam2go_file );
132         List<PfamToGoMapping> mappings = null;
133         try {
134             mappings = pfam2go_parser.parse();
135         }
136         catch ( final IOException e ) {
137             e.printStackTrace();
138         }
139         try {
140             process( pfams_file, mappings, allow_duplicates );
141         }
142         catch ( final IOException e ) {
143             e.printStackTrace();
144         }
145         System.out.println();
146     }
147
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();
156     }
157 }