Class: Labimotion::NmrMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/labimotion/libs/nmr_mapper.rb

Overview

NmrMapper

Class Method Summary collapse

Class Method Details

.build_ds(id, content) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/labimotion/libs/nmr_mapper.rb', line 271

def self.build_ds(id, content)
  ds = Container.find_by(id: id)
  return if ds.nil? || content.nil?

  ols = content[:ols]
   = content[:metadata]

  return if ols.nil? || .nil?

  klass = Labimotion::DatasetKlass.find_by(ols_term_id: ols)
  return if klass.nil?

  uuid = SecureRandom.uuid
  props = klass.properties_release
  props['uuid'] = uuid
  props['pkg'] = Labimotion::Utils.pkg(props['pkg'])
  props['klass'] = 'Dataset'
  dataset = Labimotion::Dataset.create!(
    uuid: uuid,
    dataset_klass_id: klass.id,
    element_type: 'Container',
    element_id: ds.id,
    properties: props,
    properties_release: klass.properties_release,
    klass_uuid: klass.uuid,
  )
  { dataset: dataset, metadata: , ols: ols }
end

.clean(id) ⇒ Object



267
268
269
# File 'lib/labimotion/libs/nmr_mapper.rb', line 267

def self.clean(id)
  Labimotion::NmrMapper.ts('delete', id)
end

.fetch_content(id) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/labimotion/libs/nmr_mapper.rb', line 68

def self.fetch_content(id)
  atts = Attachment.where(attachable_id: id)
  return if atts.nil?

  atts.each do |att|
    content = Labimotion::NmrMapper.ts('read', att.id)
    return content if content.present?
  end
end

.generate_ds(id, cid, data, current_user = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/labimotion/libs/nmr_mapper.rb', line 79

def self.generate_ds(id, cid, data, current_user = {})
  return if data.nil? || cid.nil?

  obj = Labimotion::NmrMapper.build_ds(cid, data[:content])
  return if obj.nil? || obj[:ols].nil?

  Labimotion::NmrMapper.update_ds_1h(cid, obj, current_user) if obj[:ols] == 'CHMO:0000593'
  Labimotion::NmrMapper.update_ds_1h(cid, obj, current_user) if obj[:ols] == 'CHMO:0000595'
end

.is_brucker_binary(id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/labimotion/libs/nmr_mapper.rb', line 30

def self.is_brucker_binary(id)
  att = Attachment.find_by(id: id, con_state: Labimotion::ConState::NMR)
  return if att.nil?

  if att&.attachment_attacher&.file&.url
    Zip::File.open(att.attachment_attacher.file.url) do |zip_file|
      zip_file.each do |entry|
        if entry.name.include?('/pdata/') && entry.name.include?('parm.txt')
           = entry.get_input_stream.read.force_encoding('UTF-8')
          return { is_bagit: false, metadata:  }
        elsif entry.name.include?('metadata/') && entry.name.include?('converter.json')
          return { is_bagit: true, metadata: nil }
        end
      end
    end
  end
  { is_bagit: false, metadata: nil }
end

.process(att, id, content) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/labimotion/libs/nmr_mapper.rb', line 49

def self.process(att, id, content)
  return if att.nil? || content.nil?

  lines = content.split("\n").reject(&:empty?)
   = {}
  lines.map do |ln|
    arr = ln.split(/\s+/)
    [arr[0]] = arr[1..-1].join(' ') if arr.length > 1
  end
  ols = 'CHMO:0000593' if ['NUC1'] == '1H'
  ols = 'CHMO:0000595' if ['NUC1'] == '13C'

  { content: { metadata: , ols: ols } }
  # if content.present? && att.present?
  #   Labimotion::NmrMapper.ts('write', att.attachable_id,
  #                         content: { metadata: metadata, ols: ols })
  # end
end

.process_ds(id, current_user = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/labimotion/libs/nmr_mapper.rb', line 9

def self.process_ds(id, current_user = {})
  att = Attachment.find_by(id: id, con_state: Labimotion::ConState::NMR)
  return if att.nil?

  result = is_brucker_binary(id)
  if result[:is_bagit] == true
    att.update_column(:con_state, Labimotion::ConState::CONVERTED)
    Labimotion::Converter.(id)
    Labimotion::ConState::COMPLETED
  elsif result[:metadata] == nil
    Labimotion::ConState::NONE
  else
    ds = Container.find_by(id: att.attachable_id)
    return if ds.nil? || ds.parent&.container_type != 'analysis'

    data = process(att, id, result[:metadata])
    generate_ds(id, att.attachable_id, data, current_user)
    Labimotion::ConState::COMPLETED
  end
end

.set_data(prop, field, idx, layer_name, field_name, value) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/labimotion/libs/nmr_mapper.rb', line 98

def self.set_data(prop, field, idx, layer_name, field_name, value)
  return if field['field'] != field_name || value&.empty?

  field['value'] = value
  prop[Labimotion::Prop::LAYERS][layer_name][Labimotion::Prop::FIELDS][idx] = field
  prop
end

.ts(method, identifier, params = nil) ⇒ Object



263
264
265
# File 'lib/labimotion/libs/nmr_mapper.rb', line 263

def self.ts(method, identifier, params = nil)
  Rails.cache.send(method, "#{Labimotion::NmrMapper.new.class.name}#{identifier}", params)
end

.update_ds_13c(id, obj) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/labimotion/libs/nmr_mapper.rb', line 89

def self.update_ds_13c(id, obj)
  # dataset = obj[:dataset]
  # metadata = obj[:metadata]
  # new_prop = dataset.properties

  # dataset.properties = new_prop
  # dataset.save!
end

.update_ds_1h(id, obj, current_user) ⇒ Object



106
107
108
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/labimotion/libs/nmr_mapper.rb', line 106

def self.update_ds_1h(id, obj, current_user)
  dataset = obj[:dataset]
   = obj[:metadata]
  new_prop = dataset.properties
  new_prop.dig(Labimotion::Prop::LAYERS, 'general', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
    #  new_prop = set_data(new_prop, fi, idx, 'general', 'title', metadata['NAME'])
    if fi['field'] == 'title' && ['NAME'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['NAME']
      fi['device'] = ['NAME']
      fi['dkey'] = 'NAME'
      new_prop[Labimotion::Prop::LAYERS]['general'][Labimotion::Prop::FIELDS][idx] = fi
    end

    if fi['field'] == 'date' && ['Date_'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['Date_']
      fi['device'] = ['Date_']
      fi['dkey'] = 'Date_'
      new_prop[Labimotion::Prop::LAYERS]['general'][Labimotion::Prop::FIELDS][idx] = fi
    end

    if fi['field'] == 'time' && ['Time'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['Time']
      fi['device'] = ['Time']
      fi['dkey'] = 'Time'
      new_prop[Labimotion::Prop::LAYERS]['general'][Labimotion::Prop::FIELDS][idx] = fi
    end

    if fi['field'] == 'creator' && current_user.present?
      ## fi['label'] = fi['label']
      fi['value'] = current_user.name
      new_prop[Labimotion::Prop::LAYERS]['general'][Labimotion::Prop::FIELDS][idx] = fi
    end
  end
  element = Container.find(id)&.root_element
  element.present? && element&.class&.name == 'Sample' && new_prop.dig(Labimotion::Prop::LAYERS, 'sample_details',
                                                                      Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
    if fi['field'] == 'label'
      fi['value'] = element.short_label
      new_prop[Labimotion::Prop::LAYERS]['sample_details'][Labimotion::Prop::FIELDS][idx] = fi
    end
    if fi['field'] == 'id'
      fi['value'] = element.id
      new_prop[Labimotion::Prop::LAYERS]['sample_details'][Labimotion::Prop::FIELDS][idx] = fi
    end
  end

  new_prop.dig(Labimotion::Prop::LAYERS, 'instrument', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
    if fi['field'] == 'instrument' && ['INSTRUM'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['INSTRUM']
      fi['device'] = ['INSTRUM']
      fi['dkey'] = 'INSTRUM'
      new_prop[Labimotion::Prop::LAYERS]['instrument'][Labimotion::Prop::FIELDS][idx] = fi
    end
  end

  new_prop.dig(Labimotion::Prop::LAYERS, 'equipment', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
    if fi['field'] == 'probehead' && ['PROBHD'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['PROBHD']
      fi['device'] = ['PROBHD']
      fi['dkey'] = 'PROBHD'
      new_prop[Labimotion::Prop::LAYERS]['equipment'][Labimotion::Prop::FIELDS][idx] = fi
    end
  end

  new_prop.dig(Labimotion::Prop::LAYERS, 'sample_preparation', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
    if fi['field'] == 'solvent' && ['SOLVENT'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['SOLVENT']
      fi['device'] = ['SOLVENT']
      fi['dkey'] = 'SOLVENT'
      fi['value'] = 'chloroform-D1 (CDCl3)' if ['SOLVENT'] == 'CDCl3'
      new_prop[Labimotion::Prop::LAYERS]['sample_preparation'][Labimotion::Prop::FIELDS][idx] = fi
    end
  end

  new_prop.dig(Labimotion::Prop::LAYERS, 'set', Labimotion::Prop::FIELDS)&.each_with_index do |fi, idx|
    if fi['field'] == 'temperature' && ['TE'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['TE'].split(/\s+/).first
      fi['device'] = ['TE']
      fi['dkey'] = 'TE'
      fi['value_system'] = ['TE'].split(/\s+/).last
      new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
    end
    if fi['field'] == 'ns' && ['NS'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['NS']
      fi['device'] = ['NS']
      fi['dkey'] = 'NS'
      new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
    end
    if fi['field'] == 'PULPROG' && ['PULPROG'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['PULPROG']
      fi['device'] = ['PULPROG']
      fi['dkey'] = 'PULPROG'
      new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
    end
    if fi['field'] == 'td' && ['TD'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['TD']
      fi['device'] = ['TD']
      fi['dkey'] = 'TD'
      new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
    end
    if fi['field'] == 'done' && ['D1'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['D1']
      fi['device'] = ['D1']
      fi['dkey'] = 'D1'
      new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
    end
    if fi['field'] == 'sf' && ['SF'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['SF']
      fi['device'] = ['SF']
      fi['dkey'] = 'SF'
      new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
    end
    if fi['field'] == 'sfoone' && ['SFO1'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['SFO1']
      fi['device'] = ['SFO1']
      fi['dkey'] = 'SFO1'
      new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
    end
    if fi['field'] == 'sfotwo' && ['SFO2'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['SFO2']
      fi['device'] = ['SFO2']
      fi['dkey'] = 'SFO2'
      new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
    end
    if fi['field'] == 'nucone' && ['NUC1'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['NUC1']
      fi['device'] = ['NUC1']
      fi['dkey'] = 'NUC1'
      new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
    end
    if fi['field'] == 'nuctwo' && ['NUC2'].present?
      ## fi['label'] = fi['label']
      fi['value'] = ['NUC2']
      fi['device'] = ['NUC2']
      fi['dkey'] = 'NUC2'
      new_prop[Labimotion::Prop::LAYERS]['set'][Labimotion::Prop::FIELDS][idx] = fi
    end
  end
  dataset.properties = new_prop
  dataset.save!
end