Class: Rubabel::MoleculeData

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rubabel/molecule_data.rb

Overview

A hash-like interface for dealing with tag data in molecules inspired by the MoleculeData implementation in pybel. This does not actually hash the data, it just reads it from the OBMol object each time and acts like a hash.

Constant Summary collapse

OB_PAIR_DATA_TYPE =
OpenBabel::OBPairData.new.get_data_type
OB_COMMENT_DATA_TYPE =
OpenBabel::OBCommentData.new.get_data_type
DATA_TYPES =
[OB_PAIR_DATA_TYPE, OB_COMMENT_DATA_TYPE]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#index_by, #uniq_by

Constructor Details

#initialize(obmol) ⇒ MoleculeData

Returns a new instance of MoleculeData.



18
19
20
# File 'lib/rubabel/molecule_data.rb', line 18

def initialize(obmol)
  @obmol = obmol
end

Instance Attribute Details

#obmolObject

Returns the value of attribute obmol.



16
17
18
# File 'lib/rubabel/molecule_data.rb', line 16

def obmol
  @obmol
end

Instance Method Details

#[](key) ⇒ Object



48
49
50
# File 'lib/rubabel/molecule_data.rb', line 48

def [](key)
  pair_data.find {|pd| pd.get_attribute == key }.get_value
end

#[]=(key, val) ⇒ Object

returns the val



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubabel/molecule_data.rb', line 53

def []=(key,val)
  if key?(key)
    OpenBabel.to_pair_data(@obmol.get_data(key)).set_value(val)
    val
  else
    pd = OpenBabel::OBPairData.new
    pd.set_attribute(key)
    pd.set_value(val)
    @obmol.clone_data(pd)
    val
  end
end

#delete(key, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/rubabel/molecule_data.rb', line 78

def delete(key, &block)
  if key?(key)
    val = self[key]
    @obmol.delete_data( @obmol.get_data(key) )
    val
  else
    block ? block.call : nil
  end
end

#each(&block) ⇒ Object



32
33
34
35
36
37
# File 'lib/rubabel/molecule_data.rb', line 32

def each(&block)
  block or return enum_for(__method__)
  pair_data.each do |pd|
    block.call [pd.get_attribute, pd.get_value]
  end
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/rubabel/molecule_data.rb', line 66

def key?(key)
  pair_data.any? {|pd| pd.get_attribute == key }
end

#keysObject



70
71
72
# File 'lib/rubabel/molecule_data.rb', line 70

def keys
  pair_data.map(&:get_attribute)
end

#pair_data(&block) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/rubabel/molecule_data.rb', line 22

def pair_data(&block)
  block or return enum_for(__method__)
  # TODO: should do this with an ob iterator
  @obmol.get_data.each do |ob_gd|
    if DATA_TYPES.include?( ob_gd.get_data_type )
      block.call( OpenBabel.to_pair_data( ob_gd ) )
    end
  end
end

#sizeObject Also known as: length



43
44
45
# File 'lib/rubabel/molecule_data.rb', line 43

def size
  pair_data.to_a.size
end

#to_aObject



39
40
41
# File 'lib/rubabel/molecule_data.rb', line 39

def to_a
  each.to_a
end

#valuesObject



74
75
76
# File 'lib/rubabel/molecule_data.rb', line 74

def values
  pair_data.map(&:get_value)
end