lib/rubabel/atom.rb in rubabel-0.1.0 vs lib/rubabel/atom.rb in rubabel-0.1.1

- old
+ new

@@ -1,6 +1,7 @@ require 'matrix' +require 'andand' require 'rubabel/bond' class OpenBabel::OBAtom def upcast @@ -10,10 +11,25 @@ module Rubabel class Atom include Enumerable + class << self + # takes an element symbol and creates that atom + def [](el_sym=:h, id=0) + ob_atom = OpenBabel::OBAtom.new + ob_atom.set_id(id) + ob_atom.set_atomic_num(Rubabel::EL_TO_NUM[el_sym]) + self.new(ob_atom) + end + end + + # returns the molecule that is parent of this atom + def mol + @ob.get_parent.andand.upcast + end + # the OpenBabel::OBAtom object attr_accessor :ob def initialize(obatom) @ob = obatom @@ -40,10 +56,20 @@ # abbreviated name, properly capitalized and as a String def element NUM_TO_ELEMENT[atomic_num] end + # creates a bond and adds it to both atoms + def add_atom!(other) + obbond = OpenBabel::OBBond.new + obbond.set_begin(self.ob) + obbond.set_end(other.ob) + @ob.add_bond(obbond) + other.ob.add_bond(obbond) + self + end + def each_bond(&block) block or return enum_for(__method__) iter = @ob.begin_bonds _bond = @ob.begin_bond(iter) while _bond @@ -52,10 +78,15 @@ end end alias_method :each, :each_bond + # retrieves the bond + def get_bond(atom) + @ob.get_bond(atom.ob).andand.upcast + end + # returns the bonds. Consider using each_bond. def bonds each_bond.map.to_a end @@ -90,10 +121,15 @@ def formal_charge @ob.get_formal_charge end alias_method :charge, :formal_charge + def formal_charge=(val) + @ob.set_formal_charge(val) + end + alias_method :charge=, :formal_charge= + def heavy_valence @ob.get_heavy_valence end def hetero_valence @@ -129,9 +165,51 @@ end def vector @ob.get_vector end + + def hydrogen?() @ob.is_hydrogen end + def carbon?() @ob.is_carbon end + def nitrogen?() @ob.is_nitrogen end + def oxygen?() @ob.is_oxygen end + def sulfur?() @ob.is_sulfur end + def phosphorus?() @ob.is_phosphorus end + def aromatic?() @ob.is_aromatic end + def in_ring?() @ob.is_in_ring end + def in_ring_size?() @ob.is_in_ring_size end + def heteroatom?() @ob.is_heteroatom end + def not_c_or_h?() @ob.is_not_cor_h end + def connected?() @ob.is_connected end + def one_three?() @ob.is_one_three end + def one_four?() @ob.is_one_four end + def carboxyl_oxygen?() @ob.is_carboxyl_oxygen end + def phosphate_oxygen?() @ob.is_phosphate_oxygen end + def sulfate_oxygen?() @ob.is_sulfate_oxygen end + def nitro_oxygen?() @ob.is_nitro_oxygen end + def amide_nitrogen?() @ob.is_amide_nitrogen end + def polar_hydrogen?() @ob.is_polar_hydrogen end + def non_polar_hydrogen?() @ob.is_non_polar_hydrogen end + def aromatic_noxide?() @ob.is_aromatic_noxide end + def chiral?() @ob.is_chiral end + def axial?() @ob.is_axial end + def clockwise?() @ob.is_clockwise end + def anti_clockwise?() @ob.is_anti_clockwise end + def positive_stereo?() @ob.is_positive_stereo end + def negative_stereo?() @ob.is_negative_stereo end + def chirality_specified?() @ob.has_chirality_specified end + def chiral_volume?() @ob.has_chiral_volume end + def hbond_acceptor?() @ob.is_hbond_acceptor end + def hbond_donor?() @ob.is_hbond_donor end + def hbond_donor_h?() @ob.is_hbond_donor_h end + + def carboxyl_carbon? + atoms.any?(&:carboxyl_oxygen?) + end + +# # does this carbon hold a primary alcohol +# def primary_alcohol_carbon? +# end def coords Vector[@ob.x, @ob.y, @ob.z] end