clean up
[jalview.git] / forester / java / src / org / forester / application / pfamacc2pfamid.java
1 // $Id:
2 //
3 // forester -- software libraries and applications
4 // for genomics and evolutionary biology research.
5 //
6 // Copyright (C) 2010 Christian M Zmasek
7 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
26
27 package org.forester.application;
28
29 import java.io.BufferedReader;
30 import java.io.FileNotFoundException;
31 import java.io.FileReader;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 public class pfamacc2pfamid {
36
37     final static private String PRG_NAME = "pfamacc2pfamid";
38
39     public static void main( final String args[] ) {
40         if ( args.length != 2 ) {
41             printHelp();
42             System.exit( -1 );
43         }
44         BufferedReader br = null;
45         try {
46             br = new BufferedReader( new FileReader( args[ 0 ] ) );
47         }
48         catch ( final FileNotFoundException e ) {
49             printHelp();
50             e.printStackTrace();
51         }
52         String line;
53         final Map<String, String> acc_id = new HashMap<String, String>();
54         String id = null;
55         try {
56             while ( ( line = br.readLine() ) != null ) {
57                 if ( line.startsWith( "#=GF ID" ) ) {
58                     if ( id != null ) {
59                         System.err.println( "illegal format" );
60                         System.exit( -1 );
61                     }
62                     id = line.substring( 7 ).trim();
63                 }
64                 else if ( line.startsWith( "#=GF AC" ) ) {
65                     if ( id == null ) {
66                         System.err.println( "illegal format" );
67                         System.exit( -1 );
68                     }
69                     String acc = line.substring( 7 ).trim();
70                     if ( acc.indexOf( '.' ) > 0 ) {
71                         acc = acc.substring( 0, acc.indexOf( '.' ) );
72                     }
73                     acc_id.put( acc, id );
74                     id = null;
75                 }
76                 else if ( line.startsWith( "//" ) ) {
77                     if ( id != null ) {
78                         System.err.println( "illegal format" );
79                         System.exit( -1 );
80                     }
81                 }
82             }
83         }
84         catch ( final Exception e ) {
85             printHelp();
86             e.printStackTrace();
87         }
88         try {
89             br = new BufferedReader( new FileReader( args[ 1 ] ) );
90         }
91         catch ( final FileNotFoundException e ) {
92             printHelp();
93             e.printStackTrace();
94         }
95         int not_found = 0;
96         try {
97             while ( ( line = br.readLine() ) != null ) {
98                 line = line.trim();
99                 if ( ( line.length() > 0 ) && !line.startsWith( "#" ) ) {
100                     String[] pfam_accs = null;
101                     if ( line.contains( "," ) ) {
102                         pfam_accs = line.split( "," );
103                     }
104                     else {
105                         pfam_accs = new String[ 1 ];
106                         pfam_accs[ 0 ] = line;
107                     }
108                     for( final String pfam_acc : pfam_accs ) {
109                         if ( acc_id.containsKey( pfam_acc ) ) {
110                             System.out.println( acc_id.get( pfam_acc ) );
111                         }
112                         else {
113                             not_found++;
114                         }
115                     }
116                 }
117             }
118         }
119         catch ( final Exception e ) {
120             printHelp();
121             e.printStackTrace();
122         }
123         System.err.println( "# not found: " + not_found );
124     }
125
126     private static void printHelp() {
127         System.out.println();
128         System.out.println( PRG_NAME + " <Pfam full> <file with pfam accessors, newline and/or comma separated>" );
129         System.out.println();
130     }
131 }