Sha256: 38064dd6fc3ac40a81910256d3b8a01ea1a7b7ba57535d7d309df52310c418cc

Contents?: true

Size: 1.61 KB

Versions: 12

Compression:

Stored size: 1.61 KB

Contents

require 'mspire/isotope'
require 'mspire/molecular_formula'

module Mspire
  module Mass

    # takes a molecular formula as a string, hash or MolecularFormula object
    # and returns the exact mass.
    def self.formula_to_exact_mass(formula)
      Mspire::MolecularFormula.from_any(formula).mass
    end

    MONO_STR = {
      'h+' => 1.00727646677,
      'e' => 0.0005486,   # www.mikeblaber.org/oldwine/chm1045/notes/Atoms/.../Atoms03.htm
      'neutron' => 1.0086649156,
    }

    Mspire::Isotope::BY_ELEMENT.each do |el, isotopes|
      MONO_STR[el.to_s] = isotopes.find {|iso| iso.mono }.atomic_mass
    end
    MONO_STR['h2o'] = %w(h h o).map {|el| MONO_STR[el] }.reduce(:+)
    MONO_STR['oh'] = %w(o h).map {|el| MONO_STR[el] }.reduce(:+)
    # add on deuterium
    MONO_STR['d'] = Mspire::Isotope::BY_ELEMENT[:h].find {|iso| iso.element == :h && iso.mass_number == 2 }.atomic_mass

    AVG_STR = {
      'h+' => 1.007276, # using Mascot_H_plus mass (is this right for AVG??)
      'e' => 0.0005486,
      'neutron' => 1.0086649156,
    }
    Mspire::Isotope::BY_ELEMENT.each do |el, isotopes|
      AVG_STR[el.to_s] = isotopes.first.average_mass
    end
    AVG_STR['h2o'] = %w(h h o).map {|el| AVG_STR[el] }.reduce(:+)
    AVG_STR['oh'] = %w(o h).map {|el| AVG_STR[el] }.reduce(:+)

    # sets MONO_SYM, MONO, AVG_SYM, and AVG
    %w(MONO AVG).each do |type|
      const_set "#{type}_SYM", Hash[ const_get("#{type}_STR").map {|k,v| [k.to_sym, v] } ]
      const_set type, const_get("#{type}_STR").merge( const_get("#{type}_SYM") )
    end

    ELECTRON = MONO[:e]
    NEUTRON = MONO[:neutron]
    H_PLUS = MONO['h+']

  end
end


Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
mspire-0.8.7 lib/mspire/mass.rb
mspire-0.8.6.2 lib/mspire/mass.rb
mspire-0.8.6.1 lib/mspire/mass.rb
mspire-0.8.6 lib/mspire/mass.rb
mspire-0.8.5 lib/mspire/mass.rb
mspire-0.8.4 lib/mspire/mass.rb
mspire-0.8.3 lib/mspire/mass.rb
mspire-0.8.2 lib/mspire/mass.rb
mspire-0.8.1 lib/mspire/mass.rb
mspire-0.8.0 lib/mspire/mass.rb
mspire-0.7.18 lib/mspire/mass.rb
mspire-0.7.17 lib/mspire/mass.rb