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



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
299
300
# File 'lib/labimotion/libs/nmr_mapper.rb', line 273

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



269
270
271
# File 'lib/labimotion/libs/nmr_mapper.rb', line 269

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

.fetch_content(id) ⇒ Object



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

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



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

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/labimotion/libs/nmr_mapper.rb', line 23

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

  if Labimotion::IS_RAILS5 == true
    Zip::File.open(att.store.path) 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 
        end
      end
    end
  else
    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 
          end
        end
      end
    end
  end
  nil
end

.process(att, id, content) ⇒ Object



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

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
# 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?

  content = is_brucker_binary(id)
  if content.nil?
    Labimotion::ConState::NONE
  else
    data = process(att, id, content)
    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



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

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

  field['value'] = value
  prop['layers'][layer_name]['fields'][idx] = field
  prop
end

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



265
266
267
# File 'lib/labimotion/libs/nmr_mapper.rb', line 265

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



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

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



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
262
263
# File 'lib/labimotion/libs/nmr_mapper.rb', line 108

def self.update_ds_1h(id, obj, current_user)
  dataset = obj[:dataset]
   = obj[:metadata]
  new_prop = dataset.properties
  new_prop.dig('layers', 'general', '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['layers']['general']['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['layers']['general']['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['layers']['general']['fields'][idx] = fi
    end

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

  new_prop.dig('layers', 'instrument', '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['layers']['instrument']['fields'][idx] = fi
    end
  end

  new_prop.dig('layers', 'equipment', '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['layers']['equipment']['fields'][idx] = fi
    end
  end

  new_prop.dig('layers', 'sample_preparation', '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['layers']['sample_preparation']['fields'][idx] = fi
    end
  end

  new_prop.dig('layers', 'set', '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['layers']['set']['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['layers']['set']['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['layers']['set']['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['layers']['set']['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['layers']['set']['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['layers']['set']['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['layers']['set']['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['layers']['set']['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['layers']['set']['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['layers']['set']['fields'][idx] = fi
    end
  end
  dataset.properties = new_prop
  dataset.save!
end