class Mspire::Mzml::DataArray

Mspire::Mzml::DataArray‘s are currently implemented as a standard Ruby array. Data may be input or output with less precision, but a standard data array will be accessible from ruby as Float (float64). Thus, the params merely alter what will be output to xml, so, to alter what is written with to_xml, change the params. If no params are changed in the data array it will be written with the same precision as it was read in with.

Constants

ACC_TO_DTYPE
ACC_TO_UNPACK_CODE
COMPRESSION_ACC
DEFAULT_COMPRESSION_ACC
DEFAULT_DTYPE_ACC
DTYPE_ACCS
NO_COMPRESSION_ACC

Attributes

data_processing[RW]

(optional) the DataProcessing object associated with this DataArray

external[RW]

set this if the data is written to an external file (such as the ibd file for imzML files)

Public Class Methods

[](*data) click to toggle source

returns a DataArray object. Analogous to [] for creating an array.

# File lib/mspire/mzml/data_array.rb, line 56
def self.[](*data)
  self.new(data)
end
data_arrays_from_xml(xml, link) click to toggle source
# File lib/mspire/mzml/data_array.rb, line 70
def self.data_arrays_from_xml(xml, link)
  data_arrays = xml.children.map do |binary_data_array_n|
    Mspire::Mzml::DataArray.from_xml(binary_data_array_n, link)
  end
  (data_arrays.size > 0) ? data_arrays : empty_data_arrays
end
empty_data_arrays(num=2) click to toggle source

returns an array of DataArray objects (2)

# File lib/mspire/mzml/data_array.rb, line 66
def self.empty_data_arrays(num=2)
  Array.new(num) { self.new }
end
from_arrays(arrays) click to toggle source

returns an array of DataArray objects based on the given arrays

# File lib/mspire/mzml/data_array.rb, line 61
def self.from_arrays(arrays)
  arrays.map {|ar| self.new(ar) }
end
from_xml(xml, link) click to toggle source
# File lib/mspire/mzml/data_array.rb, line 77
def self.from_xml(xml, link)
  da = self.new 
  binary_n = da.describe_from_xml!(xml, link[:ref_hash])

  if (dp_id = xml[:dataProcessingRef])
    da.data_processing = link[:data_processing_hash][dp_id]
  end

  zlib_compression = nil
  precision_unpack = nil
  # could also implement with set or hash lookup (need to test for
  # speed)
  da.each_accessionable_param do |param|
    acc = param.accession
    unless zlib_compression || zlib_compression == false
      case acc
      when 'MS:1000574' then zlib_compression = true
      when 'MS:1000576' then zlib_compression = false
      end
    end
    unless precision_unpack
      case acc
      when 'MS:1000523' then precision_unpack = 'E*'
      when 'MS:1000521' then precision_unpack = 'e*'
      end
    end
  end

  data = binary_n.text.unpack("m*").first

  # some implementations leave data blank if there aren't peaks
  # even if they say it is zlib compressed...
  unzipped = 
    if data.size > 0 then ( zlib_compression ? Zlib::Inflate.inflate(data) : data )
    else data end
  da.replace( unzipped.unpack(precision_unpack) )
  da
end
list_xml(arrays, builder) click to toggle source

takes an array of DataArray objects or other kinds of objects

# File lib/mspire/mzml/data_array.rb, line 173
def self.list_xml(arrays, builder)
  builder.binaryDataArrayList(count: arrays.size) do |bdal_n|
    arrays.each do |ar|
      ar.to_xml(bdal_n)
    end
  end
end
new(*args) click to toggle source

unless data type (see DTYPE_TO_ACC) or TYPE

# File lib/mspire/mzml/data_array.rb, line 43
def initialize(*args)
  params_init # paramable
  array_init(*args)
end
Also aliased as: array_init

Public Instance Methods

array_init(*args)
Alias for: new
to_binary() click to toggle source

creates a base64 binary string based on the objects params. If no dtype or compression are specified, then it will be set (i.e., params added to the object).

# File lib/mspire/mzml/data_array.rb, line 119
def to_binary
  # single pass over params for speed
  pack_code = nil
  compression = nil
  each_accessionable_param do |param|
    acc = param.accession
    if !pack_code && (code=ACC_TO_UNPACK_CODE[acc])
      pack_code = code
    end
    if compression.nil?
      compression = 
        case acc
        when COMPRESSION_ACC then true
        when NO_COMPRESSION_ACC then false
        end
    end
  end
  # can speed these up:
  unless pack_code
    describe! DEFAULT_DTYPE_ACC
    pack_code = ACC_TO_UNPACK_CODE[DEFAULT_DTYPE_ACC]
  end
  if compression.nil?
    describe! DEFAULT_COMPRESSION_ACC
    compression = 
      case DEFAULT_COMPRESSION_ACC
      when COMPRESSION_ACC then true
      when NO_COMPRESSION_ACC then false
      end
  end

  # TODO: support faster pack method with nmatrix or narray
  string = self.pack(pack_code) 
  string = Zlib::Deflate.deflate(string) if compression
  Base64.strict_encode64(string)
end
to_xml(builder) click to toggle source

will set the data type to DEFAULT_DTYPE and compression if n

Calls superclass method Mspire::Paramable#to_xml
# File lib/mspire/mzml/data_array.rb, line 157
def to_xml(builder)
  encoded_length = 
    if @external
      0
    else
      base64 = to_binary
      base64.bytesize
    end

  builder.binaryDataArray(encodedLength: encoded_length) do |bda_n|
    super(bda_n)
    bda_n.binary(base64) unless self.external
  end
end