Module: Rubabel

Defined in:
lib/rubabel.rb,
lib/rubabel.rb,
lib/rubabel/smarts.rb,
lib/rubabel/molecule_data.rb,
lib/rubabel/molecule/fragmentable.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 strings

%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)
NUM_TO_ELEMENT =

atomic number to properly capitalized element abbreviation

NUM_TO_EL =

atomic number to lowercase symbol abbreviation

EL_TO_NUM =
NUM_TO_EL.invert
ELEMENT_TO_NUM =
NUM_TO_ELEMENT.invert

Class Method Summary collapse

Class Method Details

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



27
28
29
# File 'lib/rubabel.rb', line 27

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

.force_field(type = DEFAULT_FORCEFIELD) ⇒ Object



31
32
33
# File 'lib/rubabel.rb', line 31

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



64
65
66
67
68
69
70
71
72
73
# File 'lib/rubabel.rb', line 64

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.



49
50
51
52
# File 'lib/rubabel.rb', line 49

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



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

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



101
102
103
104
105
106
107
108
# File 'lib/rubabel.rb', line 101

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

.in_formatsObject

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



37
38
39
# File 'lib/rubabel.rb', line 37

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.



78
79
80
81
# File 'lib/rubabel.rb', line 78

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



84
85
86
# File 'lib/rubabel.rb', line 84

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



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

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.



92
93
94
95
96
97
98
99
# File 'lib/rubabel.rb', line 92

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