File: bdb.rb

package info (click to toggle)
ruby-bio 2.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,108 kB
  • sloc: ruby: 68,331; perl: 13; makefile: 11; sh: 1
file content (253 lines) | stat: -rw-r--r-- 5,683 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# 
# bio/io/flatfile/bdb.rb - OBDA flatfile index by Berkley DB 
# 
# Copyright:: Copyright (C) 2002 GOTO Naohisa <ng@bioruby.org> 
# License::   The Ruby License
# 
#  $Id: bdb.rb,v 1.10 2007/04/05 23:35:41 trevor Exp $ 
# 
 
begin 
  require 'bdb' 
rescue LoadError,NotImplementedError
end 

require 'bio/io/flatfile/index'
require 'bio/io/flatfile/indexer'

module Bio
  class FlatFileIndex

    module BDBdefault
      def permission
        (0666 & (0777 ^ File.umask))
      end
      module_function :permission

      def flag_read
        BDB::RDONLY
      end
      module_function :flag_read

      def flag_write
        (BDB::CREATE | BDB::TRUNCATE)
      end
      module_function :flag_write

      def flag_append
        'r+'
      end
      module_function :flag_append
    end #module BDBdefault

    class BDBwrapper
      def initialize(name, filename, *arg)
        @dbname = name
        @file = nil
        @filename = filename
        #self.open(*arg)
      end

      def filename
        File.join(@dbname, @filename)
      end

      def open(flag = BDBdefault.flag_read,
               permission = BDBdefault.permission)
        unless @file then
          DEBUG.print "BDBwrapper: open #{filename}\n"
          @file = BDB::Btree.open(filename, nil, flag, permission)
        end
        true
      end

      def close
        if @file
          DEBUG.print "BDBwrapper: close #{filename}\n"
          @file.close
          @file = nil
        end
        nil
      end

      def [](arg)
        #self.open
        if @file then
          @file[arg]
        else
          nil
        end
      end

      def []=(key, val)
        #self.open
        @file[key.to_s] = val.to_s
      end

      def writeback_array(prefix, array, *arg)
        self.close
        self.open(*arg)
        array.each_with_index do |val, key|
          @file["#{prefix}#{key}"] = val.to_s
        end
      end

      def keys
        if @file then
          @file.keys
        else
          []
        end
      end
    end #class BDBwrapper

    module BDB_1
      class BDBMappingFile
        def self.open(*arg)
          self.new(*arg)
        end

        def initialize(filename, flag = BDBdefault.flag_read,
                       permission = BDBdefault.permission)
          @filename = filename
          @flag = flag
          @permission = permission
          #@bdb = BDB::Btree.open(@filename, nil, @flag, @permission)
        end
        attr_reader :filename
        attr_accessor :flag, :permission

        def open
          unless @bdb then
            DEBUG.print "BDBMappingFile: open #{@filename}\n"
            @bdb = BDB::Btree.open(@filename, nil, @flag, @permission)
            true
          else
            nil
          end
        end

        def close
          if @bdb then
            DEBUG.print "BDBMappingFile: close #{@filename}\n"
            @bdb.close
            @bdb = nil
          end
          nil
        end

        def records
          @bdb.size
        end
        alias size records

        # methods for writing
        def add(key, val)
          open
          val = val.to_a.join("\t")
          s = @bdb[key]
          if s then
            s << "\t"
            s << val
            val = s
          end
          @bdb[key] = val
          #DEBUG.print "add: key=#{key.inspect}, val=#{val.inspect}\n"
          val
        end

        def add_exclusive(key, val)
          open
          val = val.to_a.join("\t")
          s = @bdb[key]
          if s then
            raise RuntimeError, "keys must be unique, but key #{key.inspect} already exists"
          end
          @bdb[key] = val
          #DEBUG.print "add_exclusive: key=#{key.inspect}, val=#{val.inspect}\n"
          val
        end

        def add_overwrite(key, val)
          open
          val = val.to_a.join("\t")
          s = @bdb[key]
          if s then
            DEBUG.print "Warining: overwrote unique id #{key.inspect}\n"
          end
          @bdb[key] = val
          #DEBUG.print "add_overwrite: key=#{key.inspect}, val=#{val.inspect}\n"
          val
        end

        def add_nr(key, val)
          open
          s = @bdb[key]
          if s then
            a = s.split("\t")
          else
            a = []
          end
          a.concat val.to_a
          a.sort!
          a.uniq!
          str = a.join("\t")
          @bdb[key] = str
          #DEBUG.print "add_nr: key=#{key.inspect}, val=#{str.inspect}\n"
          str
        end
            
        # methods for searching
        def search(key)
          open
          s = @bdb[key]
          if s then
            a = s.split("\t")
            a
          else
            []
          end
        end
      end #class BDBMappingFile

      class PrimaryNameSpace < Template::NameSpace
        def mapping(filename)
          BDBMappingFile.new(filename)
        end
        def filename
          File.join(dbname, "key_#{name}")
        end
        def search(key)
          r = super(key)
          unless r.empty? then
            [ r ]
          else
            r
          end
        end
      end #class PrimaryNameSpace

      class SecondaryNameSpace < Template::NameSpace
        def mapping(filename)
          BDBMappingFile.new(filename)
        end
        def filename
          File.join(dbname, "id_#{name}")
        end #class SecondaryNameSpaces
      
        def search(key)
          r = super(key)
          file.close
          r
        end
      end #class SecondaryNameSpace
    end #module BDB_1

  end #class FlatFileIndex
end #module Bio

=begin

  * Classes/modules in this file are internal use only.

=end