Class: Mspire::Mzml::DataArray

Inherits:
Array
  • Object
show all
Includes:
CV::Paramable
Defined in:
lib/mspire/mzml/data_array.rb

Overview

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.

Constant Summary collapse

DEFAULT_DTYPE_ACC =

float64

'MS:1000523'
DEFAULT_COMPRESSION_ACC =
'MS:1000574'
COMPRESSION_ACC =
'MS:1000574'
NO_COMPRESSION_ACC =
'MS:1000576'
ACC_TO_DTYPE =
{
  'MS:1000523' => :float64,
  'MS:1000521' => :float32,
  #'MS:1000520' => :float16,  # <- not supported w/o other gems
  'MS:1000522' => :int64, # signed
  'MS:1000519' => :int32, # signed
}
ACC_TO_UNPACK_CODE =

signed

{
  'MS:1000523' => 'E*',
  'MS:1000521' => 'e*',
  #'MS:1000520' => :float16,  # <- not supported w/o other gems
  'MS:1000522' => 'q<*',
  'MS:1000519' => 'l<*',
}
DTYPE_ACCS =
ACC_TO_DTYPE.keys

Instance Attribute Summary collapse

Attributes included from CV::Paramable

#cv_params, #ref_param_groups, #user_params

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CV::Paramable

#accessionable_params, #describe!, #describe_from_xml!, #describe_many!, #describe_self_from_xml!, #each_accessionable_param, #each_param, #fetch, #fetch_by_accession, #param?, #param_by_accession, #params, #params?, #reject!, #replace!, #replace_many!

Methods inherited from Array

#in_groups

Constructor Details

#initialize(*args) ⇒ DataArray

unless data type (see DTYPE_TO_ACC) or TYPE



43
44
45
46
# File 'lib/mspire/mzml/data_array.rb', line 43

def initialize(*args)
  params_init # paramable
  array_init(*args)
end

Instance Attribute Details

#data_processingObject

(optional) the DataProcessing object associated with this DataArray



49
50
51
# File 'lib/mspire/mzml/data_array.rb', line 49

def data_processing
  @data_processing
end

#externalObject

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



53
54
55
# File 'lib/mspire/mzml/data_array.rb', line 53

def external
  @external
end

Class Method Details

.[](*data) ⇒ Object

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



56
57
58
# File 'lib/mspire/mzml/data_array.rb', line 56

def self.[](*data)
  self.new(data)
end

.data_arrays_from_xml(xml, link) ⇒ Object



70
71
72
73
74
75
# 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) ⇒ Object

returns an array of DataArray objects (2)



66
67
68
# 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) ⇒ Object

returns an array of DataArray objects based on the given arrays



61
62
63
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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) ⇒ Object

takes an array of DataArray objects or other kinds of objects



173
174
175
176
177
178
179
# 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

Instance Method Details

#array_initObject



18
# File 'lib/mspire/mzml/data_array.rb', line 18

alias_method :array_init, :initialize

#to_binaryObject

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).



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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) ⇒ Object

will set the data type to DEFAULT_DTYPE and compression if n



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# 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