Applet is going more functions.
[jalview.git] / forester / java / src / org / forester / go / PfamToGoParser.java
1
2 package org.forester.go;
3
4 import java.io.BufferedReader;
5 import java.io.File;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12
13 import org.forester.util.ForesterUtil;
14
15 public class PfamToGoParser {
16
17     // Pfam:PF00001 7tm_1 > GO:rhodopsin-like receptor activity ; GO:0001584
18     private static final String  PFAM_TO_GO_FORMAT     = "Pfam:\\S+\\s+(\\S+)\\s*>\\s*GO:.+;\\s*(\\S+)";
19     private static final Pattern PFAM_TO_GO_PATTERN    = Pattern.compile( PFAM_TO_GO_FORMAT );
20     private static final String  PFAMACC_TO_GO_FORMAT  = "Pfam:(\\S+)\\s+\\S+\\s*>\\s*GO:.+;\\s*(\\S+)";
21     private static final Pattern PFAMACC_TO_GO_PATTERN = Pattern.compile( PFAMACC_TO_GO_FORMAT );
22     private final File           _input_file;
23     private int                  _mapping_count;
24     private boolean              _use_acc;
25
26     public PfamToGoParser( final File input_file ) {
27         _input_file = input_file;
28         init();
29     }
30
31     private File getInputFile() {
32         return _input_file;
33     }
34
35     public int getMappingCount() {
36         return _mapping_count;
37     }
38
39     private void init() {
40         setMappingCount( 0 );
41         setUseAccessors( false );
42     }
43
44     public boolean isUseAccessors() {
45         return _use_acc;
46     }
47
48     public List<PfamToGoMapping> parse() throws IOException {
49         final String error = ForesterUtil.isReadableFile( getInputFile() );
50         if ( !ForesterUtil.isEmpty( error ) ) {
51             throw new IOException( error );
52         }
53         final BufferedReader br = new BufferedReader( new FileReader( getInputFile() ) );
54         String line;
55         final List<PfamToGoMapping> mappings = new ArrayList<PfamToGoMapping>();
56         int line_number = 0;
57         try {
58             while ( ( line = br.readLine() ) != null ) {
59                 line_number++;
60                 line = line.trim();
61                 if ( ( line.length() > 0 ) && !line.startsWith( "!" ) ) {
62                     Matcher m = null;
63                     if ( isUseAccessors() ) {
64                         m = PFAMACC_TO_GO_PATTERN.matcher( line );
65                     }
66                     else {
67                         m = PFAM_TO_GO_PATTERN.matcher( line );
68                     }
69                     if ( !m.matches() ) {
70                         throw new IOException( "unexpected format [\"" + line + "\"]" );
71                     }
72                     if ( m.groupCount() != 2 ) {
73                         throw new IOException( "unexpected format [\"" + line + "\"]" );
74                     }
75                     final String pfam = m.group( 1 );
76                     final String go = m.group( 2 );
77                     if ( ForesterUtil.isEmpty( pfam ) || ForesterUtil.isEmpty( go ) ) {
78                         throw new IOException( "unexpected format [\"" + line + "\"]" );
79                     }
80                     final PfamToGoMapping map = new PfamToGoMapping( pfam, new GoId( go ) );
81                     ++_mapping_count;
82                     mappings.add( map );
83                 }
84             } // while ( ( line = br.readLine() ) != null )
85         }
86         catch ( final Exception e ) {
87             throw new IOException( "parsing problem: " + e.getMessage() + " [at line " + line_number + "]" );
88         }
89         return mappings;
90     }
91
92     private void setMappingCount( final int mapping_count ) {
93         _mapping_count = mapping_count;
94     }
95
96     public void setUseAccessors( final boolean use_ids ) {
97         _use_acc = use_ids;
98     }
99 }