v 1.04
[jalview.git] / forester / ruby / scripts / pfam_summarize.rb
1 #!/usr/local/bin/ruby -w
2 #
3 # = pfam_summarize
4 #
5 # Copyright::  Copyright (C) 2008-2009 Christian M. Zmasek. All rights reserved.
6 # License::    GNU Lesser General Public License (LGPL)
7 #
8 # $Id: pfam_summarize.rb,v 1.2 2008/08/28 17:09:07 cmzmasek Exp $
9 #
10 # This extracts ID, AC, DE, TP, and DR values from Pfam data files.
11 #
12 # Created 2008-06-25 in San Diego, CA, USA by CMZ
13 #
14 # Usage: pfam_summarize.rb <infile: Pfam data file such as Pfam-A.full> <outfile>
15
16 require 'iconv'
17
18 module ForesterScripts
19     if RUBY_VERSION !~ /1.9/
20                       puts( "Your ruby version is #{RUBY_VERSION}, expected 1.9.x " )
21                       exit( -1 )
22                 end     
23     SEP = "\t"
24     LINE_DELIMITER  = "\n"
25
26     if ( ARGV == nil || ARGV.length != 2 )
27         puts( "usage: pfam_summarize.rb <infile: Pfam data file such as Pfam-A.full> <outfile>" )
28         exit( -1 )
29     end
30
31     pfamfile = ARGV[ 0 ]
32     outfile  = ARGV[ 1 ]
33
34     if ( !File.exists?( pfamfile ) )
35         puts( "Pfam data file [" + pfamfile + "] does not exist" )
36         exit( -1 )
37     end
38     if ( File.exists?( outfile ) )
39         puts( "outfile [" + outfile + "] already exists" )
40         exit( -1 )
41     end
42
43     ic = Iconv.new( 'UTF-8//IGNORE', 'UTF-8' )
44
45     id = nil
46     ac = nil
47     de = nil
48     tp = nil
49     dr = Array.new()
50     line_count = 0
51     count = 0
52
53     out = File.open( outfile, 'w' )
54
55     File.open( pfamfile ) do | file |
56         while line = file.gets
57             line_count += 1
58
59             line = ic.iconv( line )
60
61             if ( line =~ /#=GF ID\s+(.+)/ )
62                 if ( id != nil )
63                     puts( "Pfam data file [" + pfamfile + "] format error [line: " + line + "]" )
64                     exit( -1 )
65                 end
66                 id = $1
67             elsif ( line =~ /#=GF AC\s+(.+)/ )
68                 ac = $1
69             elsif ( line =~ /#=GF DE\s+(.+)/ )
70                 de = $1
71             elsif ( line =~ /#=GF TP\s+(.+)/ )
72                 tp = $1
73             elsif ( line =~ /#=GF DR\s+(.+)/ )
74                 dr.push( $1 )
75             elsif ( line =~ /^\/\// )
76                 if ( id == nil || ac == nil )
77                     puts( "Pfam data file [" + pfamfile + "] format error [line: " + line + "]" )
78                     exit( -1 )
79                 end
80                 out.write( id )
81                 out.write( SEP )
82                 out.write( ac )
83                 out.write( SEP )
84                 out.write( tp )
85                 out.write( SEP )
86                 out.write( '[' )
87                 out.write( de )
88                 out.write( ']' )
89                 out.write( SEP )
90                 out.write( '[' )
91                 dr.each { |d|
92                     out.write( d )
93                     out.write( ' ' )
94                 }
95                 out.write( ']' )
96                 out.write( LINE_DELIMITER )
97
98                 id = nil
99                 ac = nil
100                 de = nil
101                 tp = nil
102                 dr = Array.new()
103                 count += 1
104             end
105         end
106     end
107
108     out.close
109
110     puts()
111     puts( "Summarized data for " + count.to_s + " individual Pfams to " + outfile )
112     puts( "OK" )
113     puts()
114
115 end # module ForesterScripts
116