Class: Mspire::Mzml::DataArray
- 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
-
#external ⇒ Object
set this if the data is written to an external file (such as the ibd file for imzML files).
Attributes included from CV::Paramable
#cv_params, #ref_param_groups, #user_params
Class Method Summary collapse
-
.from_binary(base64, *args) ⇒ Object
returns a new Mspire::Mzml::DataArray object (an array).
-
.list_xml(arrays, builder) ⇒ Object
takes an array of DataArray objects or other kinds of objects.
-
.to_binary(array_ish, *args) ⇒ Object
returns a base64 string that can be used for xml representations of the data.
Instance Method Summary collapse
- #array_initialize ⇒ Object
-
#initialize(*args) ⇒ DataArray
constructor
A new instance of DataArray.
- #params_initialize ⇒ Object
- #params_to_xml ⇒ Object
-
#to_binary(*args) ⇒ Object
calls the class to_binary method with self and the given args.
- #to_xml(builder, dtype = DEFAULT_DTYPE, compression = DEFAULT_COMPRESSION) ⇒ Object
- #type ⇒ Object
- #type=(arg) ⇒ Object
Methods included from CV::Paramable
#accessionable_params, #describe!, #describe_many!, #fetch, #fetch_by_accession, #param?, #param_by_accession, #params, #params?
Methods inherited from Array
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
#external ⇒ Object
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 95 96 97 98 99 100 101 |
# 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 # some implementations leave data blank if there aren't peaks # even if they say it is zlib compressed... unzipped = if data.size > 0 compressed ? Zlib::Inflate.inflate(data) : data else data end self.new( unzipped.unpack(precision_unpack) ) end |
.list_xml(arrays, builder) ⇒ Object
takes an array of DataArray objects or other kinds of objects
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/mspire/mzml/data_array.rb', line 163 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
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mspire/mzml/data_array.rb', line 109 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_initialize ⇒ Object
8 |
# File 'lib/mspire/mzml/data_array.rb', line 8 alias_method :array_initialize, :initialize |
#params_initialize ⇒ Object
10 |
# File 'lib/mspire/mzml/data_array.rb', line 10 alias_method :params_initialize, :initialize |
#params_to_xml ⇒ Object
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
139 140 141 |
# File 'lib/mspire/mzml/data_array.rb', line 139 def to_binary(*args) self.class.to_binary(self, *args) end |
#to_xml(builder, dtype = DEFAULT_DTYPE, compression = DEFAULT_COMPRESSION) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/mspire/mzml/data_array.rb', line 143 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 |
#type ⇒ Object
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 |