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



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

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 = 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['klass'] = 'Dataset'
  dataset = 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



222
223
224
# File 'lib/labimotion/libs/nmr_mapper_repo.rb', line 222

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

.fetch_content(id) ⇒ Object



32
33
34
# File 'lib/labimotion/libs/nmr_mapper_repo.rb', line 32

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

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



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

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

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

  Analyses::NmrMapper.update_ds_1h(id, obj, current_user) if obj[:ols] == 'CHMO:0000593'
  Analyses::NmrMapper.update_ds_1h(id, obj, current_user) if obj[:ols] == 'CHMO:0000595'
  Analyses::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
# 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'

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

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



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

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



219
220
# File 'lib/labimotion/libs/nmr_mapper_repo.rb', line 219

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

.update_ds_13c(id, obj) ⇒ Object



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

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



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

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|
    fi['id'] = element.id
    fi['label'] = element.short_label
    new_prop['layers']['general']['fields'][idx] = fi
  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