module Rubabel

Constants

AROMATIC_ELEMENTS

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

AVAILABLE_FORCEFIELDS

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

BUILDER
CMD

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

DEFAULT_FORCEFIELD
ELEMENTS

capitalized Symbols

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

MASS_E

the mass of an electron

NUM_TO_ELEMENT

atomic number to properly capitalized element abbreviation (as Symbol)

VERSION

Public Class Methods

[](string, type=Rubabel::Molecule::DEFAULT_IN_TYPE) click to toggle source

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

# 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
filetype(filename)
Alias for: format_from_ext
force_field(type=DEFAULT_FORCEFIELD) click to toggle source
# 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) click to toggle source

determines the extension from filename if type is nil

# 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) click to toggle source

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

# 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
Also aliased as: filetype
format_from_mime(mime_type) click to toggle source

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

# 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) click to toggle source
# 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_formats() click to toggle source

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

# File lib/rubabel.rb, line 57
def id_formats
  Rubabel::Molecule::ID_TYPES
end
in_formats() click to toggle source

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

# 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) click to toggle source

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.

# 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) click to toggle source

reads one molecule from the string

# 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_formats() click to toggle source

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

# 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) click to toggle source

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.

# 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