Module: Rubabel

Defined in:
lib/rubabel.rb,
lib/rubabel.rb,
lib/rubabel/smarts.rb,
lib/rubabel/version.rb,
lib/rubabel/molecule_data.rb,
lib/rubabel/molecule.rb,
lib/rubabel/bond.rb,
lib/rubabel/atom.rb

Defined Under Namespace

Classes: Atom, Bond, Molecule, MoleculeData, Smarts

Constant Summary collapse

MASS_E =

the mass of an electron

0.0005486
AVAILABLE_FORCEFIELDS =

available force-fields (would like to generate this with introspection)

[:mmff94, :ghemical, :mm2, :uff]
DEFAULT_FORCEFIELD =
AVAILABLE_FORCEFIELDS.first
BUILDER =
OpenBabel::OBBuilder.new
CMD =

the command to execute the utility. They are initialized to be eponymous.

{
  babel: 'babel',
  obabel: 'obabel',
}
ELEMENTS =

capitalized Symbols

%w(H He Li Be B C N O F Ne Na Mg Al Si P S Cl Ar K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe Cs Ba La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn Fr Ra Ac Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No Lr Rf Db Sg Bh Hs Mt Ds Rg Cn Uut Fl Uup Lv Uus Uuo).map(&:to_sym)
NUM_TO_ELEMENT =

atomic number to properly capitalized element abbreviation (as Symbol)

AROMATIC_ELEMENTS =

the SMILES aromatic elements, listed in proper capitalized notation (e.g., :Se)

[:C, :O, :S, :Se, :N]
ELEMENT_TO_NUM =

Along with properly capitalized element symbols (e.g., :Se) ELEMENT_TO_NUM will include keys to lowercase versions of the AROMATIC_ELEMENTS

NUM_TO_ELEMENT.invert
VERSION =
"0.4.3"

Class Method Summary collapse

Class Method Details

.[](string, type = Rubabel::Molecule::DEFAULT_IN_TYPE) ⇒ Object

accepts a string specifying the molecule (calling Rubabel::Molecule.from_string) or an id (calls Rubabel::Molecule.from_id)



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

def [](string, type=Rubabel::Molecule::DEFAULT_IN_TYPE)
  methd = 
    if type && Rubabel::Molecule::ID_TYPE_KEYS.include?(type)
      :from_id
    else
      :from_string
    end
  Rubabel::Molecule.send(methd, string, type)
end

.force_field(type = DEFAULT_FORCEFIELD) ⇒ Object



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

def force_field(type=DEFAULT_FORCEFIELD)
  OpenBabel::OBForceField.find_force_field(type.to_s)
end

.foreach(filename, type = nil, &block) ⇒ Object

determines the extension from filename if type is nil



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

def foreach(filename, type=nil, &block)
  block or return enum_for(__method__, filename, type)
  (obmol, obconv, not_at_end) = read_first_obmol(filename, type)
  # the obmol is not valid if we are already at the end!
  while not_at_end
    block.call Rubabel::Molecule.new(obmol)
    obmol = OpenBabel::OBMol.new
    not_at_end = obconv.read(obmol)
  end
end

.format_from_ext(filename) ⇒ Object Also known as: filetype

returns the format Symbol that can be used for conversion, or nil if the extension is not recognized.



63
64
65
66
# File 'lib/rubabel.rb', line 63

def format_from_ext(filename)
  obformat = OpenBabel::OBConversion.format_from_ext(filename)
  obformat.get_id.to_sym if obformat
end

.format_from_mime(mime_type) ⇒ Object

returns a format Symbol that can be used for conversion, or nil if the mime-type is not recognized



72
73
74
75
# File 'lib/rubabel.rb', line 72

def format_from_mime(mime_type)
  obformat = OpenBabel::OBConversion.format_from_mime(mime_type)
  obformat.get_id.to_sym if obformat
end

.formats_to_hash(format_strings) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/rubabel.rb', line 115

def formats_to_hash(format_strings)
  Hash[ 
    format_strings.map do |str| 
      pair = str.split(/\s+--\s+/)
      [pair[0].to_sym, pair[1]]
    end 
  ]
end

.id_formatsObject

returns the formats retrievable by url lookup of the id or key



57
58
59
# File 'lib/rubabel.rb', line 57

def id_formats
  Rubabel::Molecule::ID_TYPES
end

.in_formatsObject

returns a hash keyed by type (Symbol) pointing to a description of the format



46
47
48
# File 'lib/rubabel.rb', line 46

def in_formats
  @in_formats ||= formats_to_hash(OpenBabel::OBConversion.new.get_supported_input_format)
end

.molecule_from_file(filename, type = nil) ⇒ Object

returns a Rubabel::Molecule (the first in the file if there are multiples). See ::foreach for accessing all molecules in a file determines the type from the extension if type is nil.



92
93
94
95
# File 'lib/rubabel.rb', line 92

def molecule_from_file(filename, type=nil)
  (obmol, obconv, not_at_end) = read_first_obmol(filename, type).first
  Rubabel::Molecule.new(obmol)
end

.molecule_from_string(string, type = Rubabel::Molecule::DEFAULT_IN_TYPE) ⇒ Object

reads one molecule from the string



98
99
100
# File 'lib/rubabel.rb', line 98

def molecule_from_string(string, type=Rubabel::Molecule::DEFAULT_IN_TYPE)
  Rubabel::Molecule.from_string(string, type)
end

.out_formatsObject

returns a hash keyed by type (Symbol) pointing to a description of the format



52
53
54
# File 'lib/rubabel.rb', line 52

def out_formats
  @out_formats ||= formats_to_hash(OpenBabel::OBConversion.new.get_supported_output_format)
end

.read_first_obmol(filename, type = nil) ⇒ Object

reads the first entry and returns the OBMol object, the OBConversion object, and the boolean not_at_end. This method is not intended for public usage but is necessary based on discrepancies between accessing the first molecule and subsequent molecules.



106
107
108
109
110
111
112
113
# File 'lib/rubabel.rb', line 106

def read_first_obmol(filename, type=nil)
  type ||= format_from_ext(filename)
  obconv = OpenBabel::OBConversion.new
  obconv.set_in_format(type.to_s) || raise(ArgumentError, "invalid format #{type}")
  obmol = OpenBabel::OBMol.new
  not_at_end = obconv.read_file(obmol, filename)
  [obmol, obconv, not_at_end]
end