Module: Mspire::SpectrumLike
- Includes:
- Enumerable
- Included in:
- Mzml::Spectrum, Spectrum
- Defined in:
- lib/mspire/spectrum_like.rb
Instance Attribute Summary (collapse)
-
- (Object) centroided
boolean for if the spectrum represents centroided data or not.
-
- (Object) data_arrays
The underlying data store.
-
- (Object) ms_level
Returns the value of attribute ms_level.
-
- (Object) precursors
Returns the value of attribute precursors.
-
- (Object) products
Returns the value of attribute products.
-
- (Object) scans
Returns the value of attribute scans.
Instance Method Summary (collapse)
-
- (Object) ==(other)
if the mzs and intensities are the same then the spectra are considered equal.
-
- (Object) [](array_index)
retrieve an m/z and intensity doublet at that index.
- - (Boolean) centroided?
- - (Object) find_all_nearest(val)
- - (Object) find_all_nearest_index(val)
-
- (Object) find_nearest(val)
returns the m/z that is closest to the value, favoring the lower m/z in the case of a tie.
-
- (Object) find_nearest_index(val)
same as find_nearest but returns the index of the point.
- - (Mspire::Spectrum) initialize(data_arrays, centroided = true)
-
- (Object) intensities
An array of the intensities data, corresponding to mzs.
- - (Object) intensities=(ar)
-
- (Object) mzs
An array of the mz data.
- - (Object) mzs=(ar)
- - (Object) mzs_and_intensities
-
- (Object) normalize(norm_by = :tic)
returns a new spectrum whose intensities have been normalized by the tic of another given value.
-
- (Object) peaks(&block)
(also: #each, #each_peak)
yields(mz, inten) across the spectrum, or array of doublets if no block.
-
- (Object) size
be 2 (m/z and intensities).
-
- (Object) sort!
ensures that the m/z values are monotonically ascending (some instruments are bad about this) returns self.
- - (Object) tic
-
- (Object) to_peaklist(peak_id = nil)
returns a bonafide Peaklist object (i.e., each peak is cast as a Mspire::Peak object).
Methods included from Enumerable
Instance Attribute Details
- (Object) centroided
boolean for if the spectrum represents centroided data or not
13 14 15 |
# File 'lib/mspire/spectrum_like.rb', line 13 def centroided @centroided end |
- (Object) data_arrays
The underlying data store. methods are implemented so that data_arrays is the m/z's and data_arrays is intensities
17 18 19 |
# File 'lib/mspire/spectrum_like.rb', line 17 def data_arrays @data_arrays end |
- (Object) ms_level
Returns the value of attribute ms_level
10 11 12 |
# File 'lib/mspire/spectrum_like.rb', line 10 def ms_level @ms_level end |
- (Object) precursors
Returns the value of attribute precursors
8 9 10 |
# File 'lib/mspire/spectrum_like.rb', line 8 def precursors @precursors end |
- (Object) products
Returns the value of attribute products
7 8 9 |
# File 'lib/mspire/spectrum_like.rb', line 7 def products @products end |
- (Object) scans
Returns the value of attribute scans
9 10 11 |
# File 'lib/mspire/spectrum_like.rb', line 9 def scans @scans end |
Instance Method Details
- (Object) ==(other)
if the mzs and intensities are the same then the spectra are considered equal
98 99 100 |
# File 'lib/mspire/spectrum_like.rb', line 98 def ==(other) mzs == other.mzs && intensities == other.intensities end |
- (Object) [](array_index)
retrieve an m/z and intensity doublet at that index
63 64 65 |
# File 'lib/mspire/spectrum_like.rb', line 63 def [](array_index) [@data_arrays[0][array_index], @data_arrays[1][array_index]] end |
- (Boolean) centroided?
20 |
# File 'lib/mspire/spectrum_like.rb', line 20 def centroided?() centroided end |
- (Object) find_all_nearest(val)
156 157 158 |
# File 'lib/mspire/spectrum_like.rb', line 156 def find_all_nearest(val) find_all_nearest_index(val).map {|i| mzs[i] } end |
- (Object) find_all_nearest_index(val)
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/mspire/spectrum_like.rb', line 134 def find_all_nearest_index(val) _mzs = mzs index = _mzs.bsearch_lower_boundary {|v| v <=> val } if index == _mzs.size [_mzs.size-1] else # if the previous m/z diff is smaller, use it if index == 0 [index] else case (val - _mzs[index-1]).abs <=> (_mzs[index] - val).abs when -1 [index-1] when 0 [index-1, index] when 1 [index] end end end end |
- (Object) find_nearest(val)
returns the m/z that is closest to the value, favoring the lower m/z in the case of a tie. Uses a binary search.
125 126 127 |
# File 'lib/mspire/spectrum_like.rb', line 125 def find_nearest(val) mzs[find_nearest_index(val)] end |
- (Object) find_nearest_index(val)
same as find_nearest but returns the index of the point
130 131 132 |
# File 'lib/mspire/spectrum_like.rb', line 130 def find_nearest_index(val) find_all_nearest_index(val).first end |
- (Mspire::Spectrum) initialize(data_arrays, centroided = true)
25 26 27 28 |
# File 'lib/mspire/spectrum_like.rb', line 25 def initialize(data_arrays, centroided=true) @data_arrays = data_arrays @centroided = centroided end |
- (Object) intensities
An array of the intensities data, corresponding to mzs.
50 51 52 |
# File 'lib/mspire/spectrum_like.rb', line 50 def intensities @data_arrays[1] end |
- (Object) intensities=(ar)
54 55 56 |
# File 'lib/mspire/spectrum_like.rb', line 54 def intensities=(ar) @data_arrays[1] = ar end |
- (Object) mzs
An array of the mz data.
41 42 43 |
# File 'lib/mspire/spectrum_like.rb', line 41 def mzs @data_arrays[0] end |
- (Object) mzs=(ar)
45 46 47 |
# File 'lib/mspire/spectrum_like.rb', line 45 def mzs=(ar) @data_arrays[0] = ar end |
- (Object) mzs_and_intensities
58 59 60 |
# File 'lib/mspire/spectrum_like.rb', line 58 def mzs_and_intensities [@data_arrays[0], @data_arrays[1]] end |
- (Object) normalize(norm_by = :tic)
returns a new spectrum whose intensities have been normalized by the tic of another given value
104 105 106 107 |
# File 'lib/mspire/spectrum_like.rb', line 104 def normalize(norm_by=:tic) norm_by = tic if norm_by == :tic Mspire::Spectrum.new([self.mzs, self.intensities.map {|v| v / norm_by }]) end |
- (Object) peaks(&block) Also known as: each, each_peak
yields(mz, inten) across the spectrum, or array of doublets if no block. Note that each peak is merely an array of m/z and intensity. For a genuine
70 71 72 |
# File 'lib/mspire/spectrum_like.rb', line 70 def peaks(&block) @data_arrays[0].zip(@data_arrays[1], &block) end |
- (Object) size
be 2 (m/z and intensities)
32 33 34 |
# File 'lib/mspire/spectrum_like.rb', line 32 def size @data_arrays.size end |
- (Object) sort!
ensures that the m/z values are monotonically ascending (some instruments are bad about this) returns self
116 117 118 119 120 121 |
# File 'lib/mspire/spectrum_like.rb', line 116 def sort! _peaks = peaks.to_a _peaks.sort! _peaks.each_with_index {|(mz,int), i| @data_arrays[0][i] = mz ; @data_arrays[1][i] = int } self end |
- (Object) tic
109 110 111 |
# File 'lib/mspire/spectrum_like.rb', line 109 def tic self.intensities.reduce(:+) end |
- (Object) to_peaklist(peak_id = nil)
returns a bonafide Peaklist object (i.e., each peak is cast as a Mspire::Peak object). If peak_id is defined, each peak will be cast as a TaggedPeak object with the given peak_id
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/mspire/spectrum_like.rb', line 80 def to_peaklist(peak_id=nil) # realize this isn't dry, but it is in such an inner loop it needs to be # as fast as possible. pl = Peaklist.new if peak_id peaks.each_with_index do |peak,i| pl[i] = Mspire::Peak.new( peak ) end else peaks.each_with_index do |peak,i| pl[i] = Mspire::TaggedPeak.new( peak, peak_id ) end end pl end |