Class: Labimotion::NmrMapperRepo

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

Class Method Summary collapse

Class Method Details

.build_ds(id, content) ⇒ Object



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_repo.rb', line 233

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['eln'] = Chemotion::Application.config.version
  props['labimotion'] = Labimotion::VERSION
  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



229
230
231
# File 'lib/labimotion/libs/nmr_mapper_repo.rb', line 229

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

.fetch_content(id) ⇒ Object



34
35
36
# File 'lib/labimotion/libs/nmr_mapper_repo.rb', line 34

def self.fetch_content(id)
  Labimotion::NmrMapper.ts('read', id)
end

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



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/labimotion/libs/nmr_mapper_repo.rb', line 38

def self.generate_ds(id, current_user = {})
  data = Labimotion::NmrMapper.fetch_content(id)
  return if data.nil?

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

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

.is_brucker_binary(id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/labimotion/libs/nmr_mapper_repo.rb', line 5

def self.is_brucker_binary(id)
  att = Attachment.find(id)
  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
  nil
end

.process(id, content) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/labimotion/libs/nmr_mapper_repo.rb', line 18

def self.process(id, content)
  att = Attachment.find(id)
  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'
  if content.present? && att.present?
    Labimotion::NmrMapper.ts('write', att.attachable_id,
                          content: { metadata: , ols: ols })
  end
end

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



59
60
61
62
63
64
65
# File 'lib/labimotion/libs/nmr_mapper_repo.rb', line 59

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



226
227
# File 'lib/labimotion/libs/nmr_mapper_repo.rb', line 226

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



50
51
52
53
54
55
56
57
# File 'lib/labimotion/libs/nmr_mapper_repo.rb', line 50

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



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
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
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
# File 'lib/labimotion/libs/nmr_mapper_repo.rb', line 67

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