Sha256: 4ecf75a091e787db805d3cd8fdf709fa08e5b6f0c5c8ae5762ccfca29bfb9ba9

Contents?: true

Size: 1.6 KB

Versions: 8

Compression:

Stored size: 1.6 KB

Contents

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

module Mspire
  module Mass

    # takes a molecular formula in this format: C2BrH12O
    def self.formula_to_exact_mass(formula)
      Mspire::MolecularFormula.new(formula).map do |el,cnt|
        MONO[el] * cnt
      end.reduce(:+)
    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

8 entries across 8 versions & 1 rubygems

Version Path
mspire-0.7.9 lib/mspire/mass.rb
mspire-0.7.8 lib/mspire/mass.rb
mspire-0.7.7 lib/mspire/mass.rb
mspire-0.7.6 lib/mspire/mass.rb
mspire-0.7.5 lib/mspire/mass.rb
mspire-0.7.4 lib/mspire/mass.rb
mspire-0.7.3 lib/mspire/mass.rb
mspire-0.7.2 lib/mspire/mass.rb