Class: Bio::FlatFileIndex::DataBank

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/io/flatfile/index.rb

Overview

databank

Internal use only.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (DataBank) initialize(name, idx_type = nil, hash = {})



1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
# File 'lib/bio/io/flatfile/index.rb', line 1148

def initialize(name, idx_type = nil, hash = {})
  @dbname = name.dup
  @dbname.freeze
  @bdb = nil

  @always_check = true
  self.index_type = (hash['index'] or idx_type)

  if @bdb then
    @config = BDBwrapper.new(@dbname, 'config')
    @bdb_fileids = BDBwrapper.new(@dbname, 'fileids')
    @nsclass_pri = BDB_1::PrimaryNameSpace
    @nsclass_sec = BDB_1::SecondaryNameSpace
  else
    @config = hash
    @nsclass_pri = Flat_1::PrimaryNameSpace
    @nsclass_sec = Flat_1::SecondaryNameSpace
  end
  true
end

Instance Attribute Details

- (Object) always_check

Returns the value of attribute always_check



1312
1313
1314
# File 'lib/bio/io/flatfile/index.rb', line 1312

def always_check
  @always_check
end

- (Object) dbname (readonly)

Returns the value of attribute dbname



1169
1170
1171
# File 'lib/bio/io/flatfile/index.rb', line 1169

def dbname
  @dbname
end

- (Object) index_type

Returns the value of attribute index_type



1169
1170
1171
# File 'lib/bio/io/flatfile/index.rb', line 1169

def index_type
  @index_type
end

Class Method Details

+ (Object) filename(dbname)



1131
1132
1133
# File 'lib/bio/io/flatfile/index.rb', line 1131

def self.filename(dbname)
  File.join(dbname, 'config.dat')
end

+ (Object) open(*arg)



1144
1145
1146
# File 'lib/bio/io/flatfile/index.rb', line 1144

def self.open(*arg)
  self.read(*arg)
end

+ (Object) read(name, mode = 'rb', *bdbarg)



1135
1136
1137
1138
1139
1140
1141
1142
# File 'lib/bio/io/flatfile/index.rb', line 1135

def self.read(name, mode = 'rb', *bdbarg)
  f = File.open(filename(name), mode)
  hash = file2hash(f)
  f.close
  db = self.new(name, nil, hash)
  db.bdb_open(*bdbarg)
  db
end

Instance Method Details

- (Object) bdb_open(*bdbarg)



1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
# File 'lib/bio/io/flatfile/index.rb', line 1204

def bdb_open(*bdbarg)
  if @bdb then
    @config.close
    @config.open(*bdbarg)
    @bdb_fileids.close
    @bdb_fileids.open(*bdbarg)
    true
  else
    nil
  end
end

- (Object) check_consistency



1374
1375
1376
# File 'lib/bio/io/flatfile/index.rb', line 1374

def check_consistency
  fileids.check_all
end

- (Object) close



1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
# File 'lib/bio/io/flatfile/index.rb', line 1234

def close
  DEBUG.print "DataBank: close #{@dbname}\n"
  primary.close
  secondary.close
  fileids.close
  if @bdb then
    @config.close
    @bdb_fileids.close
  end
  nil
end

- (Object) fileids



1288
1289
1290
1291
1292
1293
# File 'lib/bio/io/flatfile/index.rb', line 1288

def fileids
  unless @fileids then
    init_fileids
  end
  @fileids
end

- (Object) format



1281
1282
1283
1284
1285
1286
# File 'lib/bio/io/flatfile/index.rb', line 1281

def format
  unless @format then
    self.format = @config['format']
  end
  @format
end

- (Object) format=(str)



1277
1278
1279
# File 'lib/bio/io/flatfile/index.rb', line 1277

def format=(str)
  @format = str.to_s.dup
end

- (Object) get_flatfile_data(f, pos, length)



1314
1315
1316
1317
1318
1319
1320
# File 'lib/bio/io/flatfile/index.rb', line 1314

def get_flatfile_data(f, pos, length)
  fi = fileids[f.to_i]
  if @always_check then
    raise "flatfile #{fi.filename.inspect} may be modified" unless fi.check
  end
  fi.get(pos.to_i, length.to_i)
end

- (Object) init_fileids



1295
1296
1297
1298
1299
1300
1301
1302
# File 'lib/bio/io/flatfile/index.rb', line 1295

def init_fileids
  if @bdb then
    @fileids = FileIDs.new('', @bdb_fileids)
  else
    @fileids = FileIDs.new('fileid_', @config)
  end
  @fileids
end

- (Object) primary

parameters



1247
1248
1249
1250
1251
1252
# File 'lib/bio/io/flatfile/index.rb', line 1247

def primary
  unless @primary then
    self.primary = @config['primary_namespace']
  end
  @primary
end

- (Object) primary=(pri_name)



1254
1255
1256
1257
1258
1259
1260
# File 'lib/bio/io/flatfile/index.rb', line 1254

def primary=(pri_name)
  if !pri_name or pri_name.empty? then
    pri_name = 'UNIQUE'
  end
  @primary = @nsclass_pri.new(@dbname, pri_name)
  @primary
end

- (Object) search_all(key)



1341
1342
1343
1344
# File 'lib/bio/io/flatfile/index.rb', line 1341

def search_all(key)
  s = search_all_get_unique_id(key)
  search_primary(*s)
end

- (Object) search_all_get_unique_id(key)



1322
1323
1324
1325
1326
1327
1328
1329
# File 'lib/bio/io/flatfile/index.rb', line 1322

def search_all_get_unique_id(key)
  s = secondary.search(key)
  p = primary.include?(key)
  s.push p if p
  s.sort!
  s.uniq!
  s
end

- (Object) search_namespaces(key, *names)



1369
1370
1371
1372
# File 'lib/bio/io/flatfile/index.rb', line 1369

def search_namespaces(key, *names)
  s = search_namespaces_get_unique_id(key, *names)
  search_primary(*s)
end

- (Object) search_namespaces_get_unique_id(key, *names)



1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
# File 'lib/bio/io/flatfile/index.rb', line 1353

def search_namespaces_get_unique_id(key, *names)
  if names.include?(primary.name) then
    n2 = names.dup
    n2.delete(primary.name)
    p = primary.include?(key)
  else
    n2 = names
    p = nil
  end
  s = secondary.search_names(key, *n2)
  s.push p if p
  s.sort!
  s.uniq!
  s
end

- (Object) search_primary(*arg)



1331
1332
1333
1334
1335
1336
1337
1338
1339
# File 'lib/bio/io/flatfile/index.rb', line 1331

def search_primary(*arg)
  r = Results.new
  arg.each do |x|
    a = primary.search(x)
    # a is empty or a.size==1 because primary key must be unique
    r.store(x, get_flatfile_data(*a[0])) unless a.empty?
  end
  r
end

- (Object) search_primary_get_unique_id(key)



1346
1347
1348
1349
1350
1351
# File 'lib/bio/io/flatfile/index.rb', line 1346

def search_primary_get_unique_id(key)
  s = []
  p = primary.include?(key)
  s.push p if p
  s
end

- (Object) secondary



1262
1263
1264
1265
1266
1267
# File 'lib/bio/io/flatfile/index.rb', line 1262

def secondary
  unless @secondary then
    self.secondary = @config['secondary_namespaces']
  end
  @secondary
end

- (Object) secondary=(sec_names)



1269
1270
1271
1272
1273
1274
1275
# File 'lib/bio/io/flatfile/index.rb', line 1269

def secondary=(sec_names)
  if !sec_names then
    sec_names = []
  end
  @secondary = NameSpaces.new(@dbname, @nsclass_sec, sec_names)
  @secondary
end

- (Object) to_s



1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
# File 'lib/bio/io/flatfile/index.rb', line 1187

def to_s
  a = ""
  a << "index\t#{@index_type}\n"

  unless @bdb then
    a << "format\t#{@format}\n"
    @fileids.each_with_index do |x, i|
      a << "#{x.to_s(i)}\n"
    end
    a << "primary_namespace\t#{@primary.name}\n"
    a << "secondary_namespaces\t"
    a << @secondary.names.join("\t")
    a << "\n"
  end
  a
end

- (Object) write(mode = 'wb', *bdbarg)



1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
# File 'lib/bio/io/flatfile/index.rb', line 1216

def write(mode = 'wb', *bdbarg)
  unless FileTest.directory?(@dbname) then
    Dir.mkdir(@dbname)
  end
  f = File.open(self.class.filename(@dbname), mode)
  f.write self.to_s
  f.close

  if @bdb then
    bdb_open(*bdbarg)
    @config['format'] = format
    @config['primary_namespace'] = @primary.name
    @config['secondary_namespaces'] = @secondary.names.join("\t")
    @bdb_fileids.writeback_array('', fileids, *bdbarg)
  end
  true
end