Class: Mspire::MolecularFormula

Inherits:
Hash
  • Object
show all
Extended by:
Reader
Includes:
Arithmetic, IsotopeDistribution, Mass
Defined in:
lib/mspire/molecular_formula.rb,
lib/mspire/molecular_formula.rb,
lib/mspire/molecular_formula/aa.rb,
lib/mspire/molecular_formula/mass.rb,
lib/mspire/molecular_formula/reader.rb,
lib/mspire/molecular_formula/version.rb,
lib/mspire/molecular_formula/arithmetic.rb,
lib/mspire/molecular_formula/isotope_distribution.rb

Defined Under Namespace

Modules: AA, Arithmetic, IsotopeDistribution, Mass, Reader

Constant Summary collapse

VERSION =
"0.1.0"

Constants included from IsotopeDistribution

IsotopeDistribution::NORMALIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Reader

formula_and_charge, from_aaseq, from_any, from_string

Methods included from IsotopeDistribution

#isotope_distribution, #isotope_intensity_distribution, #raw_isotope_distribution

Methods included from Mass

#avg_mass, #mass, #mz

Methods included from Arithmetic

#*, #+, #-, #/, #add!, #div!, #mul!, #sub!

Constructor Details

#initialize(hash = {}, charge = 0) ⇒ MolecularFormula

Takes a hash and an optional Integer expressing the charge

{H: 22, C: 12, N: 1, O: 3, S: 2}  # case and string/sym doesn't matter


10
11
12
13
# File 'lib/mspire/molecular_formula.rb', line 10

def initialize(hash={}, charge=0)
  @charge = charge
  self.merge!(hash)
end

Instance Attribute Details

#chargeObject

integer desribing the charge state mass calculations will add/remove electron mass from this



6
7
8
# File 'lib/mspire/molecular_formula.rb', line 6

def charge
  @charge
end

Instance Method Details

#==(other) ⇒ Object



41
42
43
# File 'lib/mspire/molecular_formula.rb', line 41

def ==(other)
  old_equal(other) && self.charge == other.charge
end

#inspectObject



30
31
32
# File 'lib/mspire/molecular_formula.rb', line 30

def inspect
  "{MolecularFormula #{super[1...-1]}, @charge=#{self.charge}}"
end

#to_hObject

returns a hash (note: does not pass along charge info!)



35
36
37
# File 'lib/mspire/molecular_formula.rb', line 35

def to_h
  Hash[ self ]
end

#to_s(include_charge_if_nonzero = true, alphabetize = true) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mspire/molecular_formula.rb', line 15

def to_s(include_charge_if_nonzero=true, alphabetize=true)
  h = alphabetize ? self.sort : self
  st = ''
  h.each do |k,v|
    if v > 0
      st << k.to_s.capitalize
      st << v.to_s if v > 1
    end
  end
  if include_charge_if_nonzero
    st << "#{charge > 0 ? '+' : '-'}#{charge.abs if charge.abs > 1}" unless charge.zero?
  end
  st
end