inprogress
[jalview.git] / forester / ruby / scripts / hmm_split.rb
1 #!/usr/local/bin/ruby -w
2 #
3 # = hmm_split 
4 #
5 # Copyright::  Copyright (C) 2006-2008 Christian M. Zmasek
6 # License::    GNU Lesser General Public License (LGPL)
7 #
8 # $Id: hmm_split.rb,v 1.5 2008/11/17 22:32:43 cmzmasek Exp $
9 #
10 # To split a Pfam HMM file into one file for each HMM.
11 #
12
13
14 module ForesterScripts
15     
16     if RUBY_VERSION !~ /1.9/
17                       puts( "Your ruby version is #{RUBY_VERSION}, expected 1.9.x " )
18                       exit( -1 )
19                 end     
20     
21     
22     if ( ARGV == nil || ARGV.length != 3 )
23         puts( "usage: hmm_split.rb <Pfam HMM file> <outfile suffix> <outdir>" )         
24         exit( -1 )
25     end    
26        
27        hmmfile = ARGV[ 0 ]
28        suffix  = ARGV[ 1 ]
29        outdir  = ARGV[ 2 ]
30      
31        if ( !File.exists?( outdir ) )
32            puts( "outdir [" + outdir + "] does not exist" )
33            exit( -1 ) 
34        end 
35        if ( !File.exists?( hmmfile ) ) 
36            puts( "Pfam HMM file [" + hmmfile + "] does not exist" )
37            exit( -1 ) 
38        end                
39        
40        data = String.new
41        name = String.new
42        line_count = 0
43        count = 0
44        
45        File.open( hmmfile ) do | file |
46            while line = file.gets
47                data = data + line
48                line_count += 1
49                if ( line =~ /NAME\s+(.+)/ )
50                    if name.length > 0
51                        puts( "Pfam HMM file [" + hmmfile + "] format error [line: " + line + "]" )
52                        exit( -1 )                        
53                    end    
54                    name = $1    
55                elsif ( line =~ /\/\// )
56                    if name.length < 1
57                        puts( "Pfam HMM file [" + hmmfile + "] format error [line: " + line + "]" )
58                        exit( -1 )                        
59                    end
60                                        
61                    outfile = outdir + '/' + name + suffix
62                    if ( File.exists?( outfile ) ) 
63                        puts( "file [" + outfile + "] already exists" )
64                        exit( -1 )  
65                    end                   
66                    open( outfile, 'w' ) do | out |
67                        out.write( data )
68                    end
69                    count += 1
70                    puts( count.to_s + ": " + name )
71                    data = String.new
72                    name = String.new                                      
73                end
74            end
75        end 
76          
77        puts()
78        puts( "wrote " + count.to_s + " individual HMM files to " + outdir )    
79     
80 end