v 1.04
[jalview.git] / forester / ruby / scripts / pfam_to_scop.rb
1 #!/usr/local/bin/ruby -w
2 #
3 # = pfam_to_scop
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_to_scop.rb,v 1.2 2008/08/28 17:09:07 cmzmasek Exp $
9 #
10 # This extracts ID and SCOP fa (or fa and sf) from Pfam data files.
11 #
12 # Created 2008-06-25 in San Diego, CA, USA by CMZ
13 #
14 # Usage: pfam_to_scop.rb <infile: Pfam data file such as Pfam-A.full> <outfile>
15
16 require 'iconv'
17
18 module ForesterScripts
19
20     if RUBY_VERSION !~ /1.9/
21                       puts( "Your ruby version is #{RUBY_VERSION}, expected 1.9.x " )
22                       exit( -1 )
23                 end      
24     
25     SF = true
26
27     SEP = "\t"
28     LINE_DELIMITER  = "\n"
29
30     if ( ARGV == nil || ARGV.length != 2 )
31         puts( "usage: pfam_to_scop.rb <infile: Pfam data file such as Pfam-A.full> <outfile>" )
32         exit( -1 )
33     end
34
35     pfamfile = ARGV[ 0 ]
36     outfile  = ARGV[ 1 ]
37
38     if ( !File.exists?( pfamfile ) )
39         puts( "Pfam data file [" + pfamfile + "] does not exist" )
40         exit( -1 )
41     end
42     if ( File.exists?( outfile ) )
43         puts( "outfile [" + outfile + "] already exists" )
44         exit( -1 )
45     end
46
47     ic = Iconv.new( 'UTF-8//IGNORE', 'UTF-8' )
48
49     id = nil
50     scops = Array.new()
51     line_count = 0
52     count = 0
53     scop_count = 0
54
55     out = File.open( outfile, 'w' )
56
57     File.open( pfamfile ) do | file |
58         while line = file.gets
59             line_count += 1
60
61             line = ic.iconv( line )
62
63             if ( line =~ /#=GF ID\s+(.+)/ )
64                 if ( id != nil )
65                     puts( "Pfam data file [" + pfamfile + "] format error [line: " + line + "]" )
66                     exit( -1 )
67                 end
68                 id = $1
69             elsif ( line =~ /#=GF\s+DR\s+SCOP;\s+(\w+);\s+fa/ )
70                 scops.push( $1 )
71             elsif ( SF && line =~ /#=GF\s+DR\s+SCOP;\s+(\w+);\s+sf/ )
72                 scops.push( $1 )
73             elsif ( line =~ /^\/\// )
74                 if ( id == nil )
75                     puts( "Pfam data file [" + pfamfile + "] format error [line: " + line + "]" )
76                     exit( -1 )
77                 end
78                 scops.each { |s|
79                     out.write( id )
80                     out.write( SEP )
81                     out.write( s )
82                     out.write( LINE_DELIMITER )
83                     scop_count += 1
84                 }
85                 id = nil
86                 scops = Array.new()
87                 count += 1
88             end
89         end
90     end
91
92     out.close
93
94     puts()
95     if ( SF )
96         puts( "Extracted #{scop_count} scop fa and sf identifiers for #{count.to_s} individual Pfams to " + outfile )
97     else
98         puts( "Extracted #{scop_count} scop fa identifiers for #{count.to_s} individual Pfams to " + outfile )
99     end
100     puts( "OK" )
101     puts()
102
103 end # module ForesterScripts