Class: Graph::SubGraphDB

Inherits:
Object
  • Object
show all
Defined in:
lib/chem/utils/graph_db.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dbname, mode = "w") ⇒ SubGraphDB

Create Database object



15
16
17
18
19
20
# File 'lib/chem/utils/graph_db.rb', line 15

def initialize dbname, mode = "w"
  @dbm = DBM.open("#{dbname}.dbm")
  @idx = open("#{dbname}.idx", mode)
  @mat = open("#{dbname}.mat", mode)
  @typ = open("#{dbname}.typ", mode)
end

Class Method Details

.open(dbname, mode = "r") ⇒ Object



22
23
24
25
26
27
28
# File 'lib/chem/utils/graph_db.rb', line 22

def self.open dbname, mode = "r"
  if mode == "r"
    SubGraphDB.new(dbname, mode)
  else
    self.new(dbname, mode)
  end
end

Instance Method Details

#[]=(key, mol) ⇒ Object



30
31
32
33
34
35
# File 'lib/chem/utils/graph_db.rb', line 30

def []= key, mol
  adj = mol.adj_matrix
  @idx.print [mol.nodes.length, @mat.tell, adj.length].pack("i*")
  @mat.print adj
  @typ.print mol.nodes.inject([]){ |ret, node| ret.push(node.atomic_number)}.pack("i*")
end

#closeObject

Closes database



38
39
40
41
42
43
# File 'lib/chem/utils/graph_db.rb', line 38

def close
  @dbm.close
  @idx.close
  @mat.close
  @typ.close
end

#search(mol) ⇒ Object

Searches molecule from database Example: db = SubGraphDB.open(“somewhere/dbname”) db.search(SMILES(“CCCC”))



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chem/utils/graph_db.rb', line 49

def search mol
  @idx.rewind
  i = 1
  until @idx.eof?
    n_nodes, mat, len_matrix = @idx.read(4 * 3).unpack("i*")
    m = [0xff].pack("c") * 100
    open("test.bin", "w").puts m

    matrix = read_mat(mat, len_matrix)
    #SubGraphDB.show(m, mol.nodes.length, n_nodes)
    if SubGraphDB.match(matrix, n_nodes, mol.adjacency_list, mol.nodes.length, m)
      puts "C%05d" % i
    end
    i += 1
  end
end