Class: Chem::GSpan
- Inherits:
-
Object
- Object
- Chem::GSpan
- Defined in:
- lib/chem/db/gspan.rb
Constant Summary collapse
- FIRST_NODE =
/\((\d+)\)/
- REG_NODE =
/\s*(\d+)\s*\((\d*)(f|b)(\d+)\)/
Class Method Summary collapse
-
.parse(str, name = "") ⇒ Object
Parse one-lined gSpan formatted string and return molecule object.
-
.save(mols, filename, params = {}) ⇒ Object
Save molecule as gSpan formatted file.
Class Method Details
.parse(str, name = "") ⇒ Object
Parse one-lined gSpan formatted string and return molecule object.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/chem/db/gspan.rb', line 22 def self.parse str, name = "" mol = GSpanMolecule.new mol.name = name first_atom = GSpanAtom.new(FIRST_NODE.match(str)[1].to_i) mol.nodes.push(first_atom) str.scan(REG_NODE) do |s| bond = GSpanBond.new( s[0].to_i ) if s[2] == 'f' from_atom = mol.nodes[ s[1].to_i ] to_atom = GSpanAtom.new( s[3].to_i ) mol.nodes.push(to_atom) else # s[2] == 'b' from_atom = mol.nodes[ mol.nodes.size-1 ] to_atom = mol.nodes[ s[3].to_i ] end mol.edges.push([bond, from_atom, to_atom ]) end mol end |
.save(mols, filename, params = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/chem/db/gspan.rb', line 48 def self.save mols, filename, params = {} open(filename, "w") do |out| mols.each_with_index do |mol, mol_idx| out.puts "t # #{mol_idx} -1 #{mol.name}" mol.nodes.each_with_index do |node, idx| out.puts "v %d %d" % [idx, node.atomic_number] end mol.edges.each_with_index do |(bond, node1, node2), idx| out.puts "e %d %d %d" % [mol.nodes.index(node1), mol.nodes.index(node2), bond.v] end out.puts end end end |