Module: Mspire::MolecularFormula::Reader
- Included in:
- Mspire::MolecularFormula
- Defined in:
- lib/mspire/molecular_formula/aa.rb,
lib/mspire/molecular_formula/reader.rb
Instance Method Summary collapse
-
#formula_and_charge(string) ⇒ Object
returns the formula portion and the charge portion (signed Int) of a string returns nil for charge if no charge specified.
-
#from_aaseq(aaseq, charge = 0, aa_formula_hash = Mspire::MolecularFormula::AA::FORMULAS_STRING) ⇒ Object
a linear peptide (so includes all the residue masses plus water).
-
#from_any(arg, charge = nil) ⇒ Object
(also: #[])
arg may be a String, Hash, or MolecularFormula object.
-
#from_string(arg, charge = nil) ⇒ Object
takes a string, with properly capitalized elements making up the formula.
Instance Method Details
#formula_and_charge(string) ⇒ Object
returns the formula portion and the charge portion (signed Int) of a string returns nil for charge if no charge specified. e.g. C2H4+3 => [‘C2H4’, 3] e.g. C2H4+++ => [‘C2H4’, 3] e.g. C2H4- => [‘C2H4’, -1]
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mspire/molecular_formula/reader.rb', line 11 def formula_and_charge(string) md = string.match(/([^+-]*)([\+-]+)(\d*)\Z/) if md charges_string = md[2] chrg = if md[3] != '' md[2] == '-' ? -md[3].to_i : md[3].to_i else sign = charges_string[0] cnt = charges_string.count(sign) sign == '-' ? -cnt : cnt end [md[1], chrg] else [string, nil] end end |
#from_aaseq(aaseq, charge = 0, aa_formula_hash = Mspire::MolecularFormula::AA::FORMULAS_STRING) ⇒ Object
a linear peptide (so includes all the residue masses plus water)
68 69 70 71 72 73 74 75 |
# File 'lib/mspire/molecular_formula/aa.rb', line 68 def from_aaseq(aaseq, charge=0, aa_formula_hash=Mspire::MolecularFormula::AA::FORMULAS_STRING) hash = aaseq.each_char.inject({}) do |hash,aa| hash.merge(aa_formula_hash[aa]) {|hash,old,new| (old ? old : 0) + new } end hash[:H] += 2 hash[:O] += 1 self.new(hash, charge) end |
#from_any(arg, charge = nil) ⇒ Object Also known as: []
arg may be a String, Hash, or MolecularFormula object.
43 44 45 46 47 48 49 |
# File 'lib/mspire/molecular_formula/reader.rb', line 43 def from_any(arg, charge=nil) if arg.is_a?(String) from_string(arg, charge) else self.new(arg, arg.respond_to?(:charge) ? arg.charge : 0) end end |
#from_string(arg, charge = nil) ⇒ Object
takes a string, with properly capitalized elements making up the formula. The elements may be in any order. A charge (e.g., 2, , -, -3 may be affixed to the end )
33 34 35 36 37 38 39 40 |
# File 'lib/mspire/molecular_formula/reader.rb', line 33 def from_string(arg, charge=nil) (mol_form_str, chrg_from_str) = formula_and_charge(arg) mf = self.new({}, charge || chrg_from_str || 0) mol_form_str.scan(/([A-Z][a-z]?)(\d*)/).each do |k,v| mf[k.to_sym] = (v == '' ? 1 : v.to_i) end mf end |