Class: Mspire::Mzml::DataArray

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

Constant Summary collapse

DEFAULT_DTYPE =
:float64
DEFAULT_COMPRESSION =
true
DTYPE_TO_ACC =
{
  float64: 'MS:1000523',
  float32: 'MS:1000521',
  # float16: 'MS:1000520',  # <- not supported w/o other gems
  int64: 'MS:1000522', # signed
  int32: 'MS:1000519', # signed
}

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_many!, #fetch, #fetch_by_accession, #param?, #param_by_accession, #params, #params?

Methods inherited from Array

#in_groups

Constructor Details

#initialize(*args) ⇒ DataArray

Returns a new instance of DataArray.



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

def initialize(*args)
  array_initialize(*args)
  params_initialize
end

Instance Attribute Details

#externalObject

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



47
48
49
# File 'lib/mspire/mzml/data_array.rb', line 47

def external
  @external
end

Class Method Details

.from_binary(base64, *args) ⇒ Object

returns a new Mspire::Mzml::DataArray object (an array)

args:
  base64, set-like               # where set-like responds to include?
  base64, type=:float64, compression=true

examples:
Mspire::Mzml::Spectrum.unpack_binary('eJxjYACBD/YMEOAAoTgcABe3Abg=', ['MS:1000574', MS:1000523']).
Mspire::Mzml::Spectrum.unpack_binary("ADBA/=", :float32, true)
Mspire::Mzml::Spectrum.unpack_binary("ADBA/=") # uses float64 and compression


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mspire/mzml/data_array.rb', line 64

def self.from_binary(base64, *args)
  if args.first.respond_to?(:include?)
    accessions = args.first
    compressed =
      if accessions.include?('MS:1000574') then true # zlib compression
      elsif accessions.include?('MS:1000576') then false # no compression
      else raise 'no compression info: check your MS accession numbers'
      end
    precision_unpack = 
      if accessions.include?('MS:1000523') then 'E*'
      elsif accessions.include?('MS:1000521') then 'e*'
      else raise 'unrecognized precision: check your MS accession numbers'
      end
  else
    compressed = args.last || true
    precision_unpack =
      case args.first
      when :float64
        'E*'
      when :float32
        'e*'
      when nil
        'E*'
      else
        raise ArgumentError, "#{args.first} must be one of :float64, :float32 or other acceptable type"
      end
  end
  data = base64.unpack("m*").first
  unzipped = compressed ? Zlib::Inflate.inflate(data) : data
  self.new( unzipped.unpack(precision_unpack) )
end

.list_xml(arrays, builder) ⇒ Object

takes an array of DataArray objects or other kinds of objects



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/mspire/mzml/data_array.rb', line 156

def self.list_xml(arrays, builder)
  builder.binaryDataArrayList(count: arrays.size) do |bdal_n|
    arrays.zip([:mz, :intensity]) do |data_ar, typ|
      ar = 
        if data_ar.is_a?(Mspire::Mzml::DataArray)
          data_ar
        else
          Mspire::Mzml::DataArray.new(data_ar)
        end
      ar.type = typ unless ar.type
      ar.to_xml(bdal_n)
    end
  end
end

.to_binary(array_ish, *args) ⇒ Object

returns a base64 string that can be used for xml representations of the data

args:
  array-like  set-like               # where set-like responds to include?
  array-like  dtype=:float64, compression=true


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/mspire/mzml/data_array.rb', line 102

def self.to_binary(array_ish, *args)
  if args.first.respond_to?(:include?)
    accessions = args.first
    dtype = 
      if accessions.include?('MS:1000521') 
        :float32
      else
        :float64
      end
    compression = accessions.include?('MS:1000576') ? false : true 
  else
    dtype = args[0] || DEFAULT_DTYPE
    compression = args[1] || DEFAULT_COMPRESSION
  end

  pack_code = 
    case dtype
    when :float64 ; 'E*'
    when :float32 ; 'e*'
    when :int64   ; 'q<*'
    when :int32   ; 'l<*'
    else ; raise "unsupported dtype: #{dtype}"
    end
  # TODO: support faster pack method for NArray's in future
  string = array_ish.to_a.pack(pack_code) 
  string = Zlib::Deflate.deflate(string) if compression
  Base64.strict_encode64(string)
end

Instance Method Details

#array_initializeObject



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

alias_method :array_initialize, :initialize

#params_initializeObject



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

alias_method :params_initialize, :initialize

#params_to_xmlObject



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

alias_method :params_to_xml, :to_xml

#to_binary(*args) ⇒ Object

calls the class to_binary method with self and the given args



132
133
134
# File 'lib/mspire/mzml/data_array.rb', line 132

def to_binary(*args)
  self.class.to_binary(self, *args)
end

#to_xml(builder, dtype = DEFAULT_DTYPE, compression = DEFAULT_COMPRESSION) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/mspire/mzml/data_array.rb', line 136

def to_xml(builder, dtype=DEFAULT_DTYPE, compression=DEFAULT_COMPRESSION)
  encoded_length = 
    if @external
      0
    else
      base64 = self.class.to_binary(self, dtype, compression)
      base64.bytesize
    end

  builder.binaryDataArray(encodedLength: encoded_length) do |bda_n|
    params_to_xml(bda_n)
    unless self.external
      Mspire::CV::Param[ DTYPE_TO_ACC[dtype] ].to_xml(bda_n)
      Mspire::CV::Param[ compression ? 'MS:1000574' : 'MS:1000576' ].to_xml(bda_n)
      bda_n.binary(base64)
    end
  end
end

#typeObject



35
36
37
38
39
40
41
42
43
# File 'lib/mspire/mzml/data_array.rb', line 35

def type
  if params
    if params.any? {|param| param.accession == 'MS:1000514' }
      :mz
    elsif params.any? {|param| param.accession == 'MS:1000515' }
      :intensity
    end
  end
end

#type=(arg) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mspire/mzml/data_array.rb', line 23

def type=(arg)
  all_accs = %w(MS:1000514 MS:1000515)
  params.delete_if {|param| all_accs.include?(param.accession) } if params
  case arg
  when :mz
    describe! all_accs[0] # , nil, "MS:1000040"
  when :intensity
    describe! all_accs[1] # , nil, "MS:1000131"
  end
  arg
end