Class: Mspire::MolecularFormula
- Inherits:
-
Hash
- Object
- Hash
- Mspire::MolecularFormula
- Defined in:
- lib/mspire/molecular_formula.rb,
lib/mspire/isotope/distribution.rb
Instance Attribute Summary collapse
-
#charge ⇒ Object
integer desribing the charge state mass calculations will add/remove electron mass from this.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(arg, charge = 0) ⇒ MolecularFormula
constructor
takes a string or a hash:.
-
#isotope_distribution(normalize = Mspire::Isotope::Distribution::NORMALIZE, percent_cutoff = nil) ⇒ Object
takes any element composition (see any_to_num_elements).
-
#isotope_distribution_spectrum(*args) ⇒ Object
returns a spectrum object with mass values and intensity values.
-
#mass ⇒ Object
gives the monoisotopic mass adjusted by the current charge.
-
#raw_isotope_distribution ⇒ Object
returns relative ratios from low nominal mass to high nominal mass.
Constructor Details
#initialize(arg, charge = 0) ⇒ MolecularFormula
takes a string or a hash:
"H22C12N1O3S2BeLi2" # <= order doesn't matter
{h: 22, c: 12, n: 1, o: 3, s: 2} # case and string/sym doesn't matter
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/mspire/molecular_formula.rb', line 15 def initialize(arg, charge=0) @charge = charge if arg.is_a?(String) arg.scan(/([A-Z][a-z]?)(\d*)/).each do |k,v| self[k.downcase.to_sym] = (v == '' ? 1 : v.to_i) end else self.merge!(arg) end end |
Instance Attribute Details
#charge ⇒ Object
integer desribing the charge state mass calculations will add/remove electron mass from this
10 11 12 |
# File 'lib/mspire/molecular_formula.rb', line 10 def charge @charge end |
Class Method Details
.from_aaseq(aaseq) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/mspire/molecular_formula.rb', line 26 def self.from_aaseq(aaseq) hash = aaseq.each_char.inject({}) do |hash,aa| hash.merge(Mspire::Isotope::AA::ATOM_COUNTS[aa]) {|h,o,n| (o ? o : 0) +n } end hash[:h] += 2 hash[:o] += 1 self.new(hash) end |
Instance Method Details
#isotope_distribution(normalize = Mspire::Isotope::Distribution::NORMALIZE, percent_cutoff = nil) ⇒ Object
takes any element composition (see any_to_num_elements).
returns isotopic distribution beginning with monoisotopic peak. It cuts off when no more peaks contribute more than percent_cutoff to the total distribution. After that, normalization is performed.
all values will be fractional. normalize may be one of:
:total normalize to the total intensity
:max normalize to the highest peak intensity
:first normalize to the intensity of the first peak
(this is typically the monoisotopic peak)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mspire/isotope/distribution.rb', line 32 def isotope_distribution(normalize=Mspire::Isotope::Distribution::NORMALIZE, percent_cutoff=nil) mono_dist = raw_isotope_distribution if percent_cutoff total_signal = mono_dist.reduce(:+) cutoff_index = (mono_dist.size-1).downto(0).find do |i| (mono_dist[i] / total_signal) >= (percent_cutoff/100.0) end # deletes these elements if cutoff_index mono_dist.slice!((cutoff_index+1)..-1) else # no peaks pass that percent cutoff threshold! mono_dist = [] end end # normalization norm_by = case normalize when :total total_signal || mono_dist.reduce(:+) when :max mono_dist.max when :first mono_dist.first end mono_dist.map do |i| v = i / norm_by (v > 0) ? v : 0 end end |
#isotope_distribution_spectrum(*args) ⇒ Object
returns a spectrum object with mass values and intensity values. Arguments are passed directly to isotope_distribution. the molecule has a charge, this will be used to adjust the m/z values (by removing or adding electrons to the m/z and as the z)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/mspire/isotope/distribution.rb', line 69 def isotope_distribution_spectrum(*args) intensities = isotope_distribution(*args) mono = self.map {|el,cnt| Mspire::Mass::MONO[el]*cnt }.reduce(:+) masses = Array.new(intensities.size) neutron = Mspire::Mass::NEUTRON masses[0] = mono (1...masses.size).each {|i| masses[i] = masses[i-1] + neutron } if self.charge && self.charge != 0 masses.map! do |mass| (mass - (self.charge * Mspire::Mass::ELECTRON)) / self.charge end end Mspire::Spectrum.new [masses, intensities] end |
#mass ⇒ Object
gives the monoisotopic mass adjusted by the current charge
36 37 38 39 |
# File 'lib/mspire/molecular_formula.rb', line 36 def mass mss = inject(0.0) {|sum,(el,cnt)| sum + (Mspire::Mass::MONO[el]*cnt) } mss - (Mspire::Mass::ELECTRON * charge) end |
#raw_isotope_distribution ⇒ Object
returns relative ratios from low nominal mass to high nominal mass. These are not normalized at all.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mspire/isotope/distribution.rb', line 86 def raw_isotope_distribution low_nominal = 0 high_nominal = 0 self.each do |el,cnt| isotopes = Mspire::Isotope::BY_ELEMENT[el] low_nominal += (isotopes.first.mass_number * cnt) high_nominal += (isotopes.last.mass_number * cnt) end ffts = self.map do |el, cnt| isotope_el_ar = NArray.float(high_nominal+1) Mspire::Isotope::BY_ELEMENT[el].each do |isotope| isotope_el_ar[isotope.mass_number] = isotope.relative_abundance end FFTW3.fft(isotope_el_ar)**cnt end FFTW3.ifft(ffts.reduce(:*)).real.to_a[low_nominal..high_nominal] end |