Module: Mspire::SpectrumLike

Includes:
Enumerable
Included in:
Mzml::Spectrum, Spectrum
Defined in:
lib/mspire/spectrum_like.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#centroidedObject

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

#data_arraysObject

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

#ms_levelObject

Returns the value of attribute ms_level.



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

def ms_level
  @ms_level
end

#precursorsObject

Returns the value of attribute precursors.



8
9
10
# File 'lib/mspire/spectrum_like.rb', line 8

def precursors
  @precursors
end

#productsObject

Returns the value of attribute products.



7
8
9
# File 'lib/mspire/spectrum_like.rb', line 7

def products
  @products
end

#scansObject

Returns the value of attribute scans.



9
10
11
# File 'lib/mspire/spectrum_like.rb', line 9

def scans
  @scans
end

Instance Method Details

#==(other) ⇒ Object

if the mzs and intensities are the same then the spectra are considered equal



77
78
79
# File 'lib/mspire/spectrum_like.rb', line 77

def ==(other)
  mzs == other.mzs && intensities == other.intensities
end

#[](array_index) ⇒ Object

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

#centroided?Boolean

Returns:

  • (Boolean)


20
# File 'lib/mspire/spectrum_like.rb', line 20

def centroided?() centroided end

#find_all_nearest(val) ⇒ Object



135
136
137
# File 'lib/mspire/spectrum_like.rb', line 135

def find_all_nearest(val)
  find_all_nearest_index(val).map {|i| mzs[i] }
end

#find_all_nearest_index(val) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mspire/spectrum_like.rb', line 113

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

#find_nearest(val) ⇒ Object

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.



104
105
106
# File 'lib/mspire/spectrum_like.rb', line 104

def find_nearest(val)
  mzs[find_nearest_index(val)]
end

#find_nearest_index(val) ⇒ Object

same as find_nearest but returns the index of the point



109
110
111
# File 'lib/mspire/spectrum_like.rb', line 109

def find_nearest_index(val)
  find_all_nearest_index(val).first
end

#initialize(data_arrays, centroided = true) ⇒ Mspire::Spectrum

Parameters:

  • data (Array)

    two element array of mzs and intensities

  • centroided (Boolean) (defaults to: true)

    is the spectrum centroided or not

Returns:



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

#intensitiesObject

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

#intensities=(ar) ⇒ Object



54
55
56
# File 'lib/mspire/spectrum_like.rb', line 54

def intensities=(ar)
  @data_arrays[1] = ar
end

#mzsObject

An array of the mz data.



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

def mzs
  @data_arrays[0]
end

#mzs=(ar) ⇒ Object



45
46
47
# File 'lib/mspire/spectrum_like.rb', line 45

def mzs=(ar)
  @data_arrays[0] = ar
end

#mzs_and_intensitiesObject



58
59
60
# File 'lib/mspire/spectrum_like.rb', line 58

def mzs_and_intensities
  [@data_arrays[0], @data_arrays[1]]
end

#normalize(norm_by = :tic) ⇒ Object

returns a new spectrum whose intensities have been normalized by the tic of another given value



83
84
85
86
# File 'lib/mspire/spectrum_like.rb', line 83

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

#peaks(&block) ⇒ Object Also known as: each, each_peak

yields(mz, inten) across the spectrum, or array of doublets if no block



68
69
70
# File 'lib/mspire/spectrum_like.rb', line 68

def peaks(&block)
  @data_arrays[0].zip(@data_arrays[1], &block)
end

#sizeObject

be 2 (m/z and intensities)



32
33
34
# File 'lib/mspire/spectrum_like.rb', line 32

def size
  @data_arrays.size
end

#sort!Object

ensures that the m/z values are monotonically ascending (some instruments are bad about this) returns self



95
96
97
98
99
100
# File 'lib/mspire/spectrum_like.rb', line 95

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

#ticObject



88
89
90
# File 'lib/mspire/spectrum_like.rb', line 88

def tic
  self.intensities.reduce(:+)
end