module Mspire::SpectrumLike

Attributes

centroided[RW]

boolean for if the spectrum represents centroided data or not

data_arrays[RW]

The underlying data store. methods are implemented so that data_arrays is the m/z’s and data_arrays is intensities

ms_level[RW]
precursors[RW]
products[RW]
scans[RW]

Public Class Methods

new(data_arrays, centroided=true) click to toggle source

@return [Mspire::Spectrum] @param [Array] data two element array of mzs and intensities @param [Boolean] centroided is the spectrum centroided or not

# File lib/mspire/spectrum_like.rb, line 25
def initialize(data_arrays, centroided=true)
  @data_arrays = data_arrays
  @centroided = centroided
end

Public Instance Methods

==(other) click to toggle source
# File lib/mspire/spectrum_like.rb, line 36
def ==(other)
  mzs == other.mzs && intensities == other.intensities
end
[](array_index) click to toggle source

retrieve an m/z and intensity doublet at that index

# File lib/mspire/spectrum_like.rb, line 63
def [](array_index)
  [@data_arrays[0][array_index], @data_arrays[1][array_index]]
end
centroided?() click to toggle source
# File lib/mspire/spectrum_like.rb, line 20
def centroided?() centroided end
each(&block)
Alias for: peaks
each_peak(&block)
Alias for: peaks
find_all_nearest(val) click to toggle source
# File lib/mspire/spectrum_like.rb, line 175
def find_all_nearest(val)
  find_all_nearest_index(val).map {|i| mzs[i] }
end
find_all_nearest_index(val) click to toggle source
# File lib/mspire/spectrum_like.rb, line 153
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) click to toggle source

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.

# File lib/mspire/spectrum_like.rb, line 144
def find_nearest(val)
  mzs[find_nearest_index(val)]
end
find_nearest_index(val) click to toggle source

same as find_nearest but returns the index of the point

# File lib/mspire/spectrum_like.rb, line 149
def find_nearest_index(val)
  find_all_nearest_index(val).first
end
intensities() click to toggle source

An array of the intensities data, corresponding to mzs.

# File lib/mspire/spectrum_like.rb, line 50
def intensities
  @data_arrays[1]
end
intensities=(ar) click to toggle source
# File lib/mspire/spectrum_like.rb, line 54
def intensities=(ar)
  @data_arrays[1] = ar
end
mzs() click to toggle source

An array of the mz data.

# File lib/mspire/spectrum_like.rb, line 41
def mzs
  @data_arrays[0]
end
mzs=(ar) click to toggle source
# File lib/mspire/spectrum_like.rb, line 45
def mzs=(ar)
  @data_arrays[0] = ar
end
mzs_and_intensities() click to toggle source
# File lib/mspire/spectrum_like.rb, line 58
def mzs_and_intensities
  [@data_arrays[0], @data_arrays[1]]
end
normalize(norm_by=:tic) click to toggle source

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

# 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
peaks(&block) click to toggle source

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

# File lib/mspire/spectrum_like.rb, line 70
def peaks(&block)
  @data_arrays[0].zip(@data_arrays[1], &block)
end
Also aliased as: each, each_peak
select_indices(range, exclude_begin=false) click to toggle source

the range begin value will also be excluded on an exact match if exclude_begin is true. (respects the ranges exclude_end? value for the end).

# File lib/mspire/spectrum_like.rb, line 126
def select_indices(range, exclude_begin=false)
  indices = []
  _mzs = mzs
  lo_i = _mzs.bsearch_lower_boundary {|v| v <=> range.begin }
  return indices unless lo_i

  hi_i = nil
  (lo_i..._mzs.size).each do |i|
    break unless range === _mzs[i]
    indices << i
  end

  indices.shift if exclude_begin && range.begin == _mzs[indices.first]
  indices
end
size() click to toggle source

found by querying the size of the data store. This should almost always be 2 (m/z and intensities)

# File lib/mspire/spectrum_like.rb, line 32
def size
  @data_arrays.size
end
sort!() click to toggle source

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

# 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
tic() click to toggle source
# File lib/mspire/spectrum_like.rb, line 109
def tic
  self.intensities.reduce(:+)
end
to_peaklist(peak_id=nil) click to toggle source

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

# 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::TaggedPeak.new( peak, peak_id )
    end
  else
    peaks.each_with_index do |peak,i|
      pl[i] = Mspire::Peak.new( peak )
    end
  end
  pl
end