labimotion 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/lib/labimotion/api.rb +19 -0
  3. data/lib/labimotion/apis/converter_api.rb +68 -0
  4. data/lib/labimotion/apis/generic_dataset_api.rb +50 -0
  5. data/lib/labimotion/apis/generic_element_api.rb +339 -0
  6. data/lib/labimotion/apis/labimotion_hub_api.rb +53 -0
  7. data/lib/labimotion/apis/segment_api.rb +144 -0
  8. data/lib/labimotion/entities/application_entity.rb +88 -0
  9. data/lib/labimotion/entities/dataset_entity.rb +16 -0
  10. data/lib/labimotion/entities/dataset_klass_entity.rb +9 -0
  11. data/lib/labimotion/entities/element_entity.rb +108 -0
  12. data/lib/labimotion/entities/element_klass_entity.rb +10 -0
  13. data/lib/labimotion/entities/element_revision_entity.rb +57 -0
  14. data/lib/labimotion/entities/eln_element_entity.rb +110 -0
  15. data/lib/labimotion/entities/generic_entity.rb +54 -0
  16. data/lib/labimotion/entities/generic_klass_entity.rb +14 -0
  17. data/lib/labimotion/entities/generic_public_entity.rb +25 -0
  18. data/lib/labimotion/entities/klass_revision_entity.rb +20 -0
  19. data/lib/labimotion/entities/segment_entity.rb +62 -0
  20. data/lib/labimotion/entities/segment_klass_entity.rb +8 -0
  21. data/lib/labimotion/entities/segment_revision_entity.rb +55 -0
  22. data/lib/labimotion/helpers/converter_helpers.rb +13 -0
  23. data/lib/labimotion/helpers/dataset_helpers.rb +38 -0
  24. data/lib/labimotion/helpers/element_helpers.rb +268 -0
  25. data/lib/labimotion/helpers/generic_helpers.rb +252 -0
  26. data/lib/labimotion/helpers/repository_helpers.rb +14 -0
  27. data/lib/labimotion/helpers/sample_association_helpers.rb +126 -0
  28. data/lib/labimotion/helpers/search_helpers.rb +62 -0
  29. data/lib/labimotion/helpers/segment_helpers.rb +97 -0
  30. data/lib/labimotion/libs/converter.rb +325 -0
  31. data/lib/labimotion/libs/export_dataset.rb +121 -0
  32. data/lib/labimotion/libs/nmr_mapper.rb +265 -0
  33. data/lib/labimotion/libs/nmr_mapper_repo.rb +263 -0
  34. data/lib/labimotion/libs/template_hub.rb +55 -0
  35. data/lib/labimotion/models/collections_element.rb +42 -0
  36. data/lib/labimotion/models/concerns/attachment_converter.rb +42 -0
  37. data/lib/labimotion/models/concerns/datasetable.rb +50 -0
  38. data/lib/labimotion/models/concerns/generic_klass_revisions.rb +39 -0
  39. data/lib/labimotion/models/concerns/generic_revisions.rb +43 -0
  40. data/lib/labimotion/models/concerns/segmentable.rb +74 -0
  41. data/lib/labimotion/models/dataset.rb +14 -0
  42. data/lib/labimotion/models/dataset_klass.rb +25 -0
  43. data/lib/labimotion/models/dataset_klasses_revision.rb +9 -0
  44. data/lib/labimotion/models/datasets_revision.rb +9 -0
  45. data/lib/labimotion/models/element.rb +121 -0
  46. data/lib/labimotion/models/element_klass.rb +25 -0
  47. data/lib/labimotion/models/element_klasses_revision.rb +9 -0
  48. data/lib/labimotion/models/elements_element.rb +11 -0
  49. data/lib/labimotion/models/elements_revision.rb +8 -0
  50. data/lib/labimotion/models/elements_sample.rb +11 -0
  51. data/lib/labimotion/models/segment.rb +14 -0
  52. data/lib/labimotion/models/segment_klass.rb +24 -0
  53. data/lib/labimotion/models/segment_klasses_revision.rb +9 -0
  54. data/lib/labimotion/models/segments_revision.rb +9 -0
  55. data/lib/labimotion/utils/con_state.rb +13 -0
  56. data/lib/labimotion/utils/export.rb +112 -0
  57. data/lib/labimotion/utils/import.rb +186 -0
  58. data/lib/labimotion/utils/search.rb +112 -0
  59. data/lib/labimotion/utils/serializer.rb +78 -0
  60. data/lib/labimotion/version.rb +6 -0
  61. data/lib/labimotion.rb +95 -0
  62. metadata +119 -0
@@ -0,0 +1,265 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labimotion
4
+ ## NmrMapper
5
+ class NmrMapper
6
+ def self.is_brucker_binary(id)
7
+ att = Attachment.find(id)
8
+ if att&.attachment_attacher&.file&.url
9
+ Zip::File.open(att.attachment_attacher.file.url) do |zip_file|
10
+ zip_file.each do |entry|
11
+ if entry.name.include?('/pdata/') && entry.name.include?('parm.txt')
12
+ metadata = entry.get_input_stream.read.force_encoding('UTF-8')
13
+ return metadata
14
+ end
15
+ end
16
+ end
17
+ end
18
+ nil
19
+ end
20
+
21
+ def self.process(id, content)
22
+ att = Attachment.find(id)
23
+ lines = content.split("\n").reject(&:empty?)
24
+ metadata = {}
25
+ lines.map do |ln|
26
+ arr = ln.split(/\s+/)
27
+ metadata[arr[0]] = arr[1..-1].join(' ') if arr.length > 1
28
+ end
29
+ ols = 'CHMO:0000593' if metadata['NUC1'] == '1H'
30
+ ols = 'CHMO:0000595' if metadata['NUC1'] == '13C'
31
+ if content.present? && att.present?
32
+ Labimotion::NmrMapper.ts('write', att.attachable_id,
33
+ content: { metadata: metadata, ols: ols })
34
+ end
35
+ end
36
+
37
+ def self.fetch_content(id)
38
+ Labimotion::NmrMapper.ts('read', id)
39
+ end
40
+
41
+ def self.generate_ds(id, current_user = {})
42
+ data = Labimotion::NmrMapper.fetch_content(id)
43
+ return if data.nil?
44
+
45
+ obj = Labimotion::NmrMapper.build_ds(id, data[:content])
46
+ return if obj.nil? || obj[:ols].nil?
47
+
48
+ Labimotion::NmrMapper.update_ds_1h(id, obj, current_user) if obj[:ols] == 'CHMO:0000593'
49
+ Labimotion::NmrMapper.update_ds_1h(id, obj, current_user) if obj[:ols] == 'CHMO:0000595'
50
+ Labimotion::NmrMapper.clean(id)
51
+ end
52
+
53
+ def self.update_ds_13c(id, obj)
54
+ # dataset = obj[:dataset]
55
+ # metadata = obj[:metadata]
56
+ # new_prop = dataset.properties
57
+
58
+ # dataset.properties = new_prop
59
+ # dataset.save!
60
+ end
61
+
62
+ def self.set_data(prop, field, idx, layer_name, field_name, value)
63
+ return if field['field'] != field_name || value&.empty?
64
+
65
+ field['value'] = value
66
+ prop['layers'][layer_name]['fields'][idx] = field
67
+ prop
68
+ end
69
+
70
+ def self.update_ds_1h(id, obj, current_user)
71
+ dataset = obj[:dataset]
72
+ metadata = obj[:metadata]
73
+ new_prop = dataset.properties
74
+ new_prop.dig('layers', 'general', 'fields')&.each_with_index do |fi, idx|
75
+ # new_prop = set_data(new_prop, fi, idx, 'general', 'title', metadata['NAME'])
76
+ if fi['field'] == 'title' && metadata['NAME'].present?
77
+ ## fi['label'] = fi['label']
78
+ fi['value'] = metadata['NAME']
79
+ fi['device'] = metadata['NAME']
80
+ fi['dkey'] = 'NAME'
81
+ new_prop['layers']['general']['fields'][idx] = fi
82
+ end
83
+
84
+ if fi['field'] == 'date' && metadata['Date_'].present?
85
+ ## fi['label'] = fi['label']
86
+ fi['value'] = metadata['Date_']
87
+ fi['device'] = metadata['Date_']
88
+ fi['dkey'] = 'Date_'
89
+ new_prop['layers']['general']['fields'][idx] = fi
90
+ end
91
+
92
+ if fi['field'] == 'time' && metadata['Time'].present?
93
+ ## fi['label'] = fi['label']
94
+ fi['value'] = metadata['Time']
95
+ fi['device'] = metadata['Time']
96
+ fi['dkey'] = 'Time'
97
+ new_prop['layers']['general']['fields'][idx] = fi
98
+ end
99
+
100
+ if fi['field'] == 'creator' && current_user.present?
101
+ ## fi['label'] = fi['label']
102
+ fi['value'] = current_user.name
103
+ new_prop['layers']['general']['fields'][idx] = fi
104
+ end
105
+ end
106
+ element = Container.find(id)&.root_element
107
+ element.present? && element&.class&.name == 'Sample' && new_prop.dig('layers', 'sample_details',
108
+ 'fields')&.each_with_index do |fi, idx|
109
+ if fi['field'] == 'label'
110
+ fi['value'] = element.short_label
111
+ new_prop['layers']['sample_details']['fields'][idx] = fi
112
+ end
113
+ if fi['field'] == 'id'
114
+ fi['value'] = element.id
115
+ new_prop['layers']['sample_details']['fields'][idx] = fi
116
+ end
117
+ end
118
+
119
+ new_prop.dig('layers', 'instrument', 'fields')&.each_with_index do |fi, idx|
120
+ if fi['field'] == 'instrument' && metadata['INSTRUM'].present?
121
+ ## fi['label'] = fi['label']
122
+ fi['value'] = metadata['INSTRUM']
123
+ fi['device'] = metadata['INSTRUM']
124
+ fi['dkey'] = 'INSTRUM'
125
+ new_prop['layers']['instrument']['fields'][idx] = fi
126
+ end
127
+ end
128
+
129
+ new_prop.dig('layers', 'equipment', 'fields')&.each_with_index do |fi, idx|
130
+ if fi['field'] == 'probehead' && metadata['PROBHD'].present?
131
+ ## fi['label'] = fi['label']
132
+ fi['value'] = metadata['PROBHD']
133
+ fi['device'] = metadata['PROBHD']
134
+ fi['dkey'] = 'PROBHD'
135
+ new_prop['layers']['equipment']['fields'][idx] = fi
136
+ end
137
+ end
138
+
139
+ new_prop.dig('layers', 'sample_preparation', 'fields')&.each_with_index do |fi, idx|
140
+ if fi['field'] == 'solvent' && metadata['SOLVENT'].present?
141
+ ## fi['label'] = fi['label']
142
+ fi['value'] = metadata['SOLVENT']
143
+ fi['device'] = metadata['SOLVENT']
144
+ fi['dkey'] = 'SOLVENT'
145
+ fi['value'] = 'chloroform-D1 (CDCl3)' if metadata['SOLVENT'] == 'CDCl3'
146
+ new_prop['layers']['sample_preparation']['fields'][idx] = fi
147
+ end
148
+ end
149
+
150
+ new_prop.dig('layers', 'set', 'fields')&.each_with_index do |fi, idx|
151
+ if fi['field'] == 'temperature' && metadata['TE'].present?
152
+ ## fi['label'] = fi['label']
153
+ fi['value'] = metadata['TE'].split(/\s+/).first
154
+ fi['device'] = metadata['TE']
155
+ fi['dkey'] = 'TE'
156
+ fi['value_system'] = metadata['TE'].split(/\s+/).last
157
+ new_prop['layers']['set']['fields'][idx] = fi
158
+ end
159
+ if fi['field'] == 'ns' && metadata['NS'].present?
160
+ ## fi['label'] = fi['label']
161
+ fi['value'] = metadata['NS']
162
+ fi['device'] = metadata['NS']
163
+ fi['dkey'] = 'NS'
164
+ new_prop['layers']['set']['fields'][idx] = fi
165
+ end
166
+ if fi['field'] == 'PULPROG' && metadata['PULPROG'].present?
167
+ ## fi['label'] = fi['label']
168
+ fi['value'] = metadata['PULPROG']
169
+ fi['device'] = metadata['PULPROG']
170
+ fi['dkey'] = 'PULPROG'
171
+ new_prop['layers']['set']['fields'][idx] = fi
172
+ end
173
+ if fi['field'] == 'td' && metadata['TD'].present?
174
+ ## fi['label'] = fi['label']
175
+ fi['value'] = metadata['TD']
176
+ fi['device'] = metadata['TD']
177
+ fi['dkey'] = 'TD'
178
+ new_prop['layers']['set']['fields'][idx] = fi
179
+ end
180
+ if fi['field'] == 'done' && metadata['D1'].present?
181
+ ## fi['label'] = fi['label']
182
+ fi['value'] = metadata['D1']
183
+ fi['device'] = metadata['D1']
184
+ fi['dkey'] = 'D1'
185
+ new_prop['layers']['set']['fields'][idx] = fi
186
+ end
187
+ if fi['field'] == 'sf' && metadata['SF'].present?
188
+ ## fi['label'] = fi['label']
189
+ fi['value'] = metadata['SF']
190
+ fi['device'] = metadata['SF']
191
+ fi['dkey'] = 'SF'
192
+ new_prop['layers']['set']['fields'][idx] = fi
193
+ end
194
+ if fi['field'] == 'sfoone' && metadata['SFO1'].present?
195
+ ## fi['label'] = fi['label']
196
+ fi['value'] = metadata['SFO1']
197
+ fi['device'] = metadata['SFO1']
198
+ fi['dkey'] = 'SFO1'
199
+ new_prop['layers']['set']['fields'][idx] = fi
200
+ end
201
+ if fi['field'] == 'sfotwo' && metadata['SFO2'].present?
202
+ ## fi['label'] = fi['label']
203
+ fi['value'] = metadata['SFO2']
204
+ fi['device'] = metadata['SFO2']
205
+ fi['dkey'] = 'SFO2'
206
+ new_prop['layers']['set']['fields'][idx] = fi
207
+ end
208
+ if fi['field'] == 'nucone' && metadata['NUC1'].present?
209
+ ## fi['label'] = fi['label']
210
+ fi['value'] = metadata['NUC1']
211
+ fi['device'] = metadata['NUC1']
212
+ fi['dkey'] = 'NUC1'
213
+ new_prop['layers']['set']['fields'][idx] = fi
214
+ end
215
+ if fi['field'] == 'nuctwo' && metadata['NUC2'].present?
216
+ ## fi['label'] = fi['label']
217
+ fi['value'] = metadata['NUC2']
218
+ fi['device'] = metadata['NUC2']
219
+ fi['dkey'] = 'NUC2'
220
+ new_prop['layers']['set']['fields'][idx] = fi
221
+ end
222
+ end
223
+ dataset.properties = new_prop
224
+ dataset.save!
225
+ end
226
+
227
+ def self.ts(method, identifier, params = nil)
228
+ Rails.cache.send(method, "#{Labimotion::NmrMapper.new.class.name}#{identifier}", params)
229
+ end
230
+
231
+ def self.clean(id)
232
+ Labimotion::NmrMapper.ts('delete', id)
233
+ end
234
+
235
+ def self.build_ds(id, content)
236
+ ds = Container.find_by(id: id)
237
+ return if ds.nil? || content.nil?
238
+
239
+ ols = content[:ols]
240
+ metadata = content[:metadata]
241
+
242
+ return if ols.nil? || metadata.nil?
243
+
244
+ klass = Labimotion::DatasetKlass.find_by(ols_term_id: ols)
245
+ return if klass.nil?
246
+
247
+ uuid = SecureRandom.uuid
248
+ props = klass.properties_release
249
+ props['uuid'] = uuid
250
+ props['eln'] = Chemotion::Application.config.version
251
+ props['labimotion'] = Labimotion::VERSION
252
+ props['klass'] = 'Dataset'
253
+ dataset = Labimotion::Dataset.create!(
254
+ uuid: uuid,
255
+ dataset_klass_id: klass.id,
256
+ element_type: 'Container',
257
+ element_id: ds.id,
258
+ properties: props,
259
+ properties_release: klass.properties_release,
260
+ klass_uuid: klass.uuid,
261
+ )
262
+ { dataset: dataset, metadata: metadata, ols: ols }
263
+ end
264
+ end
265
+ end
@@ -0,0 +1,263 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labimotion
4
+ class NmrMapperRepo
5
+ def self.is_brucker_binary(id)
6
+ att = Attachment.find(id)
7
+ Zip::File.open(att.store.path) do |zip_file|
8
+ zip_file.each do |entry|
9
+ if entry.name.include?('/pdata/') && entry.name.include?('parm.txt')
10
+ metadata = entry.get_input_stream.read.force_encoding('UTF-8')
11
+ return metadata
12
+ end
13
+ end
14
+ end
15
+ nil
16
+ end
17
+
18
+ def self.process(id, content)
19
+ att = Attachment.find(id)
20
+ lines = content.split("\n").reject(&:empty?)
21
+ metadata = {}
22
+ lines.map do |ln|
23
+ arr = ln.split(/\s+/)
24
+ metadata[arr[0]] = arr[1..-1].join(' ') if arr.length > 1
25
+ end
26
+ ols = 'CHMO:0000593' if metadata['NUC1'] == '1H'
27
+ ols = 'CHMO:0000595' if metadata['NUC1'] == '13C'
28
+ if content.present? && att.present?
29
+ Labimotion::NmrMapper.ts('write', att.attachable_id,
30
+ content: { metadata: metadata, ols: ols })
31
+ end
32
+ end
33
+
34
+ def self.fetch_content(id)
35
+ Labimotion::NmrMapper.ts('read', id)
36
+ end
37
+
38
+ def self.generate_ds(id, current_user = {})
39
+ data = Labimotion::NmrMapper.fetch_content(id)
40
+ return if data.nil?
41
+
42
+ obj = Labimotion::NmrMapper.build_ds(id, data[:content])
43
+ return if obj.nil? || obj[:ols].nil?
44
+
45
+ Labimotion::NmrMapper.update_ds_1h(id, obj, current_user) if obj[:ols] == 'CHMO:0000593'
46
+ Labimotion::NmrMapper.update_ds_1h(id, obj, current_user) if obj[:ols] == 'CHMO:0000595'
47
+ Labimotion::NmrMapper.clean(id)
48
+ end
49
+
50
+ def self.update_ds_13c(id, obj)
51
+ # dataset = obj[:dataset]
52
+ # metadata = obj[:metadata]
53
+ # new_prop = dataset.properties
54
+
55
+ # dataset.properties = new_prop
56
+ # dataset.save!
57
+ end
58
+
59
+ def self.set_data(prop, field, idx, layer_name, field_name, value)
60
+ return if field['field'] != field_name || value&.empty?
61
+
62
+ field['value'] = value
63
+ prop['layers'][layer_name]['fields'][idx] = field
64
+ prop
65
+ end
66
+
67
+ def self.update_ds_1h(id, obj, current_user)
68
+ dataset = obj[:dataset]
69
+ metadata = obj[:metadata]
70
+ new_prop = dataset.properties
71
+ new_prop.dig('layers', 'general', 'fields')&.each_with_index do |fi, idx|
72
+ # new_prop = set_data(new_prop, fi, idx, 'general', 'title', metadata['NAME'])
73
+ if fi['field'] == 'title' && metadata['NAME'].present?
74
+ ## fi['label'] = fi['label']
75
+ fi['value'] = metadata['NAME']
76
+ fi['device'] = metadata['NAME']
77
+ fi['dkey'] = 'NAME'
78
+ new_prop['layers']['general']['fields'][idx] = fi
79
+ end
80
+
81
+ if fi['field'] == 'date' && metadata['Date_'].present?
82
+ ## fi['label'] = fi['label']
83
+ fi['value'] = metadata['Date_']
84
+ fi['device'] = metadata['Date_']
85
+ fi['dkey'] = 'Date_'
86
+ new_prop['layers']['general']['fields'][idx] = fi
87
+ end
88
+
89
+ if fi['field'] == 'time' && metadata['Time'].present?
90
+ ## fi['label'] = fi['label']
91
+ fi['value'] = metadata['Time']
92
+ fi['device'] = metadata['Time']
93
+ fi['dkey'] = 'Time'
94
+ new_prop['layers']['general']['fields'][idx] = fi
95
+ end
96
+
97
+ if fi['field'] == 'creator' && current_user.present?
98
+ ## fi['label'] = fi['label']
99
+ fi['value'] = current_user.name
100
+ new_prop['layers']['general']['fields'][idx] = fi
101
+ end
102
+ end
103
+ element = Container.find(id)&.root_element
104
+ element.present? && element&.class&.name == 'Sample' && new_prop.dig('layers', 'sample_details', 'fields')&.each_with_index do |fi, idx|
105
+ if fi['field'] == 'label'
106
+ fi['value'] = element.short_label
107
+ new_prop['layers']['sample_details']['fields'][idx] = fi
108
+ end
109
+ if fi['field'] == 'id'
110
+ fi['value'] = element.id
111
+ new_prop['layers']['sample_details']['fields'][idx] = fi
112
+ end
113
+ end
114
+
115
+ new_prop.dig('layers', 'instrument', 'fields')&.each_with_index do |fi, idx|
116
+ if fi['field'] == 'instrument' && metadata['INSTRUM'].present?
117
+ # fi['label'] = fi['label']
118
+ fi['value'] = metadata['INSTRUM']
119
+ fi['device'] = metadata['INSTRUM']
120
+ fi['dkey'] = 'INSTRUM'
121
+ new_prop['layers']['instrument']['fields'][idx] = fi
122
+ end
123
+ end
124
+
125
+
126
+ new_prop.dig('layers', 'equipment', 'fields')&.each_with_index do |fi, idx|
127
+ if fi['field'] == 'probehead' && metadata['PROBHD'].present?
128
+ # fi['label'] = fi['label']
129
+ fi['value'] = metadata['PROBHD']
130
+ fi['device'] = metadata['PROBHD']
131
+ fi['dkey'] = 'PROBHD'
132
+ new_prop['layers']['equipment']['fields'][idx] = fi
133
+ end
134
+ end
135
+
136
+ new_prop.dig('layers', 'sample_preparation', 'fields')&.each_with_index do |fi, idx|
137
+ if fi['field'] == 'solvent' && metadata['SOLVENT'].present?
138
+ # fi['label'] = fi['label']
139
+ fi['value'] = metadata['SOLVENT']
140
+ fi['device'] = metadata['SOLVENT']
141
+ fi['dkey'] = 'SOLVENT'
142
+ fi['value'] = 'chloroform-D1 (CDCl3)' if metadata['SOLVENT'] == 'CDCl3'
143
+ new_prop['layers']['sample_preparation']['fields'][idx] = fi
144
+ end
145
+ end
146
+
147
+
148
+ new_prop.dig('layers', 'set', 'fields')&.each_with_index do |fi, idx|
149
+ if fi['field'] == 'temperature' && metadata['TE'].present?
150
+ # fi['label'] = fi['label']
151
+ fi['value'] = metadata['TE'].split(/\s+/).first
152
+ fi['device'] = metadata['TE']
153
+ fi['dkey'] = 'TE'
154
+ fi['value_system'] = metadata['TE'].split(/\s+/).last
155
+ new_prop['layers']['set']['fields'][idx] = fi
156
+ end
157
+ if fi['field'] == 'ns' && metadata['NS'].present?
158
+ # fi['label'] = fi['label']
159
+ fi['value'] = metadata['NS']
160
+ fi['device'] = metadata['NS']
161
+ fi['dkey'] = 'NS'
162
+ new_prop['layers']['set']['fields'][idx] = fi
163
+ end
164
+ if fi['field'] == 'PULPROG' && metadata['PULPROG'].present?
165
+ # fi['label'] = fi['label']
166
+ fi['value'] = metadata['PULPROG']
167
+ fi['device'] = metadata['PULPROG']
168
+ fi['dkey'] = 'PULPROG'
169
+ new_prop['layers']['set']['fields'][idx] = fi
170
+ end
171
+ if fi['field'] == 'td' && metadata['TD'].present?
172
+ # fi['label'] = fi['label']
173
+ fi['value'] = metadata['TD']
174
+ fi['device'] = metadata['TD']
175
+ fi['dkey'] = 'TD'
176
+ new_prop['layers']['set']['fields'][idx] = fi
177
+ end
178
+ if fi['field'] == 'done' && metadata['D1'].present?
179
+ # fi['label'] = fi['label']
180
+ fi['value'] = metadata['D1']
181
+ fi['device'] = metadata['D1']
182
+ fi['dkey'] = 'D1'
183
+ new_prop['layers']['set']['fields'][idx] = fi
184
+ end
185
+ if fi['field'] == 'sf' && metadata['SF'].present?
186
+ ## fi['label'] = fi['label']
187
+ fi['value'] = metadata['SF']
188
+ fi['device'] = metadata['SF']
189
+ fi['dkey'] = 'SF'
190
+ new_prop['layers']['set']['fields'][idx] = fi
191
+ end
192
+ if fi['field'] == 'sfoone' && metadata['SFO1'].present?
193
+ ## fi['label'] = fi['label']
194
+ fi['value'] = metadata['SFO1']
195
+ fi['device'] = metadata['SFO1']
196
+ fi['dkey'] = 'SFO1'
197
+ new_prop['layers']['set']['fields'][idx] = fi
198
+ end
199
+ if fi['field'] == 'sfotwo' && metadata['SFO2'].present?
200
+ ## fi['label'] = fi['label']
201
+ fi['value'] = metadata['SFO2']
202
+ fi['device'] = metadata['SFO2']
203
+ fi['dkey'] = 'SFO2'
204
+ new_prop['layers']['set']['fields'][idx] = fi
205
+ end
206
+ if fi['field'] == 'nucone' && metadata['NUC1'].present?
207
+ ## fi['label'] = fi['label']
208
+ fi['value'] = metadata['NUC1']
209
+ fi['device'] = metadata['NUC1']
210
+ fi['dkey'] = 'NUC1'
211
+ new_prop['layers']['set']['fields'][idx] = fi
212
+ end
213
+ if fi['field'] == 'nuctwo' && metadata['NUC2'].present?
214
+ ## fi['label'] = fi['label']
215
+ fi['value'] = metadata['NUC2']
216
+ fi['device'] = metadata['NUC2']
217
+ fi['dkey'] = 'NUC2'
218
+ new_prop['layers']['set']['fields'][idx] = fi
219
+ end
220
+ end
221
+ dataset.properties = new_prop
222
+ dataset.save!
223
+ end
224
+
225
+
226
+ def self.ts(method, identifier, params = nil)
227
+ Rails.cache.send(method, "#{Labimotion::NmrMapper.new.class.name}#{identifier}", params)end
228
+
229
+ def self.clean(id)
230
+ Labimotion::NmrMapper.ts('delete', id)
231
+ end
232
+
233
+ def self.build_ds(id, content)
234
+ ds = Container.find_by(id: id)
235
+ return if ds.nil? || content.nil?
236
+
237
+ ols = content[:ols]
238
+ metadata = content[:metadata]
239
+
240
+ return if ols.nil? || metadata.nil?
241
+
242
+ klass = Labimotion::DatasetKlass.find_by(ols_term_id: ols)
243
+ return if klass.nil?
244
+
245
+ uuid = SecureRandom.uuid
246
+ props = klass.properties_release
247
+ props['uuid'] = uuid
248
+ props['eln'] = Chemotion::Application.config.version
249
+ props['labimotion'] = Labimotion::VERSION
250
+ props['klass'] = 'Dataset'
251
+ dataset = Labimotion::Dataset.create!(
252
+ uuid: uuid,
253
+ dataset_klass_id: klass.id,
254
+ element_type: 'Container',
255
+ element_id: ds.id,
256
+ properties: props,
257
+ properties_release: klass.properties_release,
258
+ klass_uuid: klass.uuid
259
+ )
260
+ { dataset: dataset, metadata: metadata, ols: ols }
261
+ end
262
+ end
263
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'json'
6
+ require 'date'
7
+
8
+ # rubocop: disable Metrics/AbcSize
9
+ # rubocop: disable Metrics/MethodLength
10
+
11
+ module Labimotion
12
+ ## TemplateHub
13
+ class TemplateHub
14
+ TARGET = Rails.env.production? ? 'https://www.chemotion-repository.net/' : 'http://localhost:3000/'
15
+
16
+ def self.uri(api_name)
17
+ url = TARGET
18
+ "#{url}api/v1/labimotion_hub/#{api_name}"
19
+ end
20
+
21
+
22
+ def self.header(opt = {})
23
+ opt || {}
24
+ end
25
+
26
+ def self.handle_response(oat, response) # rubocop: disable Metrics/PerceivedComplexity
27
+ begin
28
+ response&.success? ? 'OK' : 'ERROR'
29
+ rescue StandardError => e
30
+ raise e
31
+ ensure
32
+ ## oat.update(status: response&.success? ? 'done' : 'failure')
33
+ end
34
+ end
35
+
36
+ def self.list(klass)
37
+ body = { klass: klass }
38
+ response = HTTParty.get("#{uri('list')}?klass=#{klass}")
39
+ # response.parsed_response if response.code == 200
40
+ JSON.parse(response.body) if response.code == 200
41
+ end
42
+
43
+ def self.fetch_identifier(klass, identifier)
44
+ # body = { klass: klass, identifier: identifier }
45
+ response = HTTParty.get("#{uri('fetch')}?klass=#{klass}&identifier=#{identifier}")
46
+ # response.parsed_response if response.code == 200
47
+ JSON.parse(response.body) if response.code == 200
48
+ end
49
+ end
50
+ end
51
+
52
+ # rubocop: enable Metrics/AbcSize
53
+ # rubocop: enable Metrics/MethodLength
54
+ # rubocop: enable Metrics/ClassLength
55
+ # rubocop: enable Metrics/CyclomaticComplexity
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labimotion
4
+ class CollectionsElement < ApplicationRecord
5
+ acts_as_paranoid
6
+ self.table_name = :collections_elements
7
+ belongs_to :collection
8
+ belongs_to :element, class_name: 'Labimotion::Element'
9
+
10
+ include Tagging
11
+ include Collecting
12
+
13
+ def self.get_elements_by_collection_type(collection_ids, type)
14
+ self.where(collection_id: collection_ids, element_type: type).pluck(:element_id).compact.uniq
15
+ end
16
+
17
+ def self.remove_in_collection(eids, from_col_ids)
18
+ element_ids = Labimotion::Element.get_associated_elements(eids)
19
+ sample_ids = Labimotion::Element.get_associated_samples(element_ids)
20
+ delete_in_collection(element_ids, from_col_ids)
21
+ update_tag_by_element_ids(element_ids)
22
+ CollectionsSample.remove_in_collection(sample_ids, from_col_ids)
23
+ end
24
+
25
+ def self.move_to_collection(eids, from_col_ids, to_col_ids, element_type='')
26
+ element_ids = Labimotion::Element.get_associated_elements(eids)
27
+ sample_ids = Labimotion::Element.get_associated_samples(element_ids)
28
+ delete_in_collection(element_ids, from_col_ids)
29
+ static_create_in_collection(element_ids, to_col_ids)
30
+ CollectionsSample.move_to_collection(sample_ids, from_col_ids, to_col_ids)
31
+ update_tag_by_element_ids(element_ids)
32
+ end
33
+
34
+ def self.create_in_collection(eids, to_col_ids, element_type='')
35
+ element_ids = Labimotion::Element.get_associated_elements(eids)
36
+ sample_ids = Labimotion::Element.get_associated_samples(element_ids)
37
+ static_create_in_collection(element_ids, to_col_ids)
38
+ CollectionsSample.create_in_collection(sample_ids, to_col_ids)
39
+ update_tag_by_element_ids(element_ids)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Labimotion
4
+ module AttachmentConverter
5
+ ACCEPTED_FORMATS = (Rails.configuration.try(:converter).try(:ext) || []).freeze
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ before_create :init_converter
10
+ after_update :exec_converter
11
+ def init_converter
12
+ return if con_state.present?
13
+ if Rails.configuration.try(:converter).try(:url) && ACCEPTED_FORMATS.include?(File.extname(filename&.downcase))
14
+ self.con_state = Labimotion::ConState::WAIT
15
+ end
16
+
17
+ if File.extname(filename&.downcase) == '.zip' && attachable&.dataset.nil?
18
+ self.con_state = Labimotion::ConState::NMR
19
+ end
20
+
21
+ self.con_state = Labimotion::ConState::NONE if con_state.nil?
22
+ end
23
+
24
+ def exec_converter
25
+ #return if attachable_id.nil?
26
+ return if con_state.nil? || con_state == Labimotion::ConState::NONE
27
+ case con_state
28
+ when Labimotion::ConState::NMR
29
+ content = Labimotion::NmrMapperRepo.is_brucker_binary(id)
30
+ if content.nil?
31
+ self.con_state = Labimotion::ConState::NONE
32
+ else
33
+ Labimotion::NmrMapperRepo.process(id, content)
34
+ self.con_state = Labimotion::ConState::PROCESSED
35
+ end
36
+ when Labimotion::ConState::WAIT
37
+ self.con_state = Labimotion::Converter.jcamp_converter(id)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end