| Class | Bio::FlatFileIndex::DataBank |
| In: |
lib/bio/io/flatfile/index.rb
|
| Parent: | Object |
databank
Internal use only.
| always_check | [R] | |
| dbname | [R] | |
| index_type | [R] |
# File lib/bio/io/flatfile/index.rb, line 1131
1131: def self.filename(dbname)
1132: File.join(dbname, 'config.dat')
1133: end
# File lib/bio/io/flatfile/index.rb, line 1148
1148: def initialize(name, idx_type = nil, hash = {})
1149: @dbname = name.dup
1150: @dbname.freeze
1151: @bdb = nil
1152:
1153: @always_check = true
1154: self.index_type = (hash['index'] or idx_type)
1155:
1156: if @bdb then
1157: @config = BDBwrapper.new(@dbname, 'config')
1158: @bdb_fileids = BDBwrapper.new(@dbname, 'fileids')
1159: @nsclass_pri = BDB_1::PrimaryNameSpace
1160: @nsclass_sec = BDB_1::SecondaryNameSpace
1161: else
1162: @config = hash
1163: @nsclass_pri = Flat_1::PrimaryNameSpace
1164: @nsclass_sec = Flat_1::SecondaryNameSpace
1165: end
1166: true
1167: end
# File lib/bio/io/flatfile/index.rb, line 1144
1144: def self.open(*arg)
1145: self.read(*arg)
1146: end
# File lib/bio/io/flatfile/index.rb, line 1135
1135: def self.read(name, mode = 'rb', *bdbarg)
1136: f = File.open(filename(name), mode)
1137: hash = file2hash(f)
1138: f.close
1139: db = self.new(name, nil, hash)
1140: db.bdb_open(*bdbarg)
1141: db
1142: end
high level methods
# File lib/bio/io/flatfile/index.rb, line 1305
1305: def always_check=(bool)
1306: if bool then
1307: @always_check = true
1308: else
1309: @always_check = false
1310: end
1311: end
# File lib/bio/io/flatfile/index.rb, line 1204
1204: def bdb_open(*bdbarg)
1205: if @bdb then
1206: @config.close
1207: @config.open(*bdbarg)
1208: @bdb_fileids.close
1209: @bdb_fileids.open(*bdbarg)
1210: true
1211: else
1212: nil
1213: end
1214: end
# File lib/bio/io/flatfile/index.rb, line 1374
1374: def check_consistency
1375: fileids.check_all
1376: end
# File lib/bio/io/flatfile/index.rb, line 1234
1234: def close
1235: DEBUG.print "DataBank: close #{@dbname}\n"
1236: primary.close
1237: secondary.close
1238: fileids.close
1239: if @bdb then
1240: @config.close
1241: @bdb_fileids.close
1242: end
1243: nil
1244: end
# File lib/bio/io/flatfile/index.rb, line 1288
1288: def fileids
1289: unless @fileids then
1290: init_fileids
1291: end
1292: @fileids
1293: end
# File lib/bio/io/flatfile/index.rb, line 1281
1281: def format
1282: unless @format then
1283: self.format = @config['format']
1284: end
1285: @format
1286: end
# File lib/bio/io/flatfile/index.rb, line 1277
1277: def format=(str)
1278: @format = str.to_s.dup
1279: end
# File lib/bio/io/flatfile/index.rb, line 1314
1314: def get_flatfile_data(f, pos, length)
1315: fi = fileids[f.to_i]
1316: if @always_check then
1317: raise "flatfile #{fi.filename.inspect} may be modified" unless fi.check
1318: end
1319: fi.get(pos.to_i, length.to_i)
1320: end
# File lib/bio/io/flatfile/index.rb, line 1171
1171: def index_type=(str)
1172: case str
1173: when MAGIC_BDB
1174: @index_type = MAGIC_BDB
1175: @bdb = true
1176: unless defined?(BDB)
1177: raise RuntimeError, "Berkeley DB support not found"
1178: end
1179: when MAGIC_FLAT, '', nil, false
1180: @index_type = MAGIC_FLAT
1181: @bdb = false
1182: else
1183: raise 'unknown or unsupported index type'
1184: end
1185: end
# File lib/bio/io/flatfile/index.rb, line 1295
1295: def init_fileids
1296: if @bdb then
1297: @fileids = FileIDs.new('', @bdb_fileids)
1298: else
1299: @fileids = FileIDs.new('fileid_', @config)
1300: end
1301: @fileids
1302: end
parameters
# File lib/bio/io/flatfile/index.rb, line 1247
1247: def primary
1248: unless @primary then
1249: self.primary = @config['primary_namespace']
1250: end
1251: @primary
1252: end
# File lib/bio/io/flatfile/index.rb, line 1254
1254: def primary=(pri_name)
1255: if !pri_name or pri_name.empty? then
1256: pri_name = 'UNIQUE'
1257: end
1258: @primary = @nsclass_pri.new(@dbname, pri_name)
1259: @primary
1260: end
# File lib/bio/io/flatfile/index.rb, line 1341
1341: def search_all(key)
1342: s = search_all_get_unique_id(key)
1343: search_primary(*s)
1344: end
# File lib/bio/io/flatfile/index.rb, line 1322
1322: def search_all_get_unique_id(key)
1323: s = secondary.search(key)
1324: p = primary.include?(key)
1325: s.push p if p
1326: s.sort!
1327: s.uniq!
1328: s
1329: end
# File lib/bio/io/flatfile/index.rb, line 1369
1369: def search_namespaces(key, *names)
1370: s = search_namespaces_get_unique_id(key, *names)
1371: search_primary(*s)
1372: end
# File lib/bio/io/flatfile/index.rb, line 1353
1353: def search_namespaces_get_unique_id(key, *names)
1354: if names.include?(primary.name) then
1355: n2 = names.dup
1356: n2.delete(primary.name)
1357: p = primary.include?(key)
1358: else
1359: n2 = names
1360: p = nil
1361: end
1362: s = secondary.search_names(key, *n2)
1363: s.push p if p
1364: s.sort!
1365: s.uniq!
1366: s
1367: end
# File lib/bio/io/flatfile/index.rb, line 1331
1331: def search_primary(*arg)
1332: r = Results.new
1333: arg.each do |x|
1334: a = primary.search(x)
1335: # a is empty or a.size==1 because primary key must be unique
1336: r.store(x, get_flatfile_data(*a[0])) unless a.empty?
1337: end
1338: r
1339: end
# File lib/bio/io/flatfile/index.rb, line 1346
1346: def search_primary_get_unique_id(key)
1347: s = []
1348: p = primary.include?(key)
1349: s.push p if p
1350: s
1351: end
# File lib/bio/io/flatfile/index.rb, line 1262
1262: def secondary
1263: unless @secondary then
1264: self.secondary = @config['secondary_namespaces']
1265: end
1266: @secondary
1267: end
# File lib/bio/io/flatfile/index.rb, line 1269
1269: def secondary=(sec_names)
1270: if !sec_names then
1271: sec_names = []
1272: end
1273: @secondary = NameSpaces.new(@dbname, @nsclass_sec, sec_names)
1274: @secondary
1275: end
# File lib/bio/io/flatfile/index.rb, line 1187
1187: def to_s
1188: a = ""
1189: a << "index\t#{@index_type}\n"
1190:
1191: unless @bdb then
1192: a << "format\t#{@format}\n"
1193: @fileids.each_with_index do |x, i|
1194: a << "#{x.to_s(i)}\n"
1195: end
1196: a << "primary_namespace\t#{@primary.name}\n"
1197: a << "secondary_namespaces\t"
1198: a << @secondary.names.join("\t")
1199: a << "\n"
1200: end
1201: a
1202: end
# File lib/bio/io/flatfile/index.rb, line 1216
1216: def write(mode = 'wb', *bdbarg)
1217: unless FileTest.directory?(@dbname) then
1218: Dir.mkdir(@dbname)
1219: end
1220: f = File.open(self.class.filename(@dbname), mode)
1221: f.write self.to_s
1222: f.close
1223:
1224: if @bdb then
1225: bdb_open(*bdbarg)
1226: @config['format'] = format
1227: @config['primary_namespace'] = @primary.name
1228: @config['secondary_namespaces'] = @secondary.names.join("\t")
1229: @bdb_fileids.writeback_array('', fileids, *bdbarg)
1230: end
1231: true
1232: end