labimotion 2.0.0.rc3 → 2.0.0.rc5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d75eb36d68f9212354fef2d83838274d4a4b59a59852c80ce2f0c26f2852c95
4
- data.tar.gz: 1cc4625e88286bcd2febeb0b2ded75c3091f416c93db7d505e703ed04e8ce849
3
+ metadata.gz: a48b13c9036bfeb9b2a827cfa78179cf2244e9dc839aa2102d6811113f5274a7
4
+ data.tar.gz: 7425f00a429413c91f26bfd9c75dbb66dbfd30cacbbe58e361a985e1d17ae85b
5
5
  SHA512:
6
- metadata.gz: c630024f9111ad2c991cf8a442d8958fa4641f1b050a1c9894fab37ad1208537a650065139a13243ba560ad1c77cf77a633a3c74853a7c2857833331b3f03123
7
- data.tar.gz: 4cee0102f4d4e88ba2e840eccada4294751de262c4b7a232cda167cca23970550df901343a7b1e6a0f1e5c66b4c803bd4e6b0e126ea708ca94a24579e7285b50
6
+ metadata.gz: 59781f646145406ab451be32166cf03a32707456c3896754db074d853bbfe31f3a60cc0e69f59fbda2344863a98b2e8f41fe2bcdb79513924935a70bbda4ded0
7
+ data.tar.gz: c7efaf38bcb81bebf9c61111e63fca0c935723ac15064c58018ad26510608de3e9c4fdbc3cd3b52a645b153485e0c04fb9e080bfa2eaa50ffa4492505887124a
@@ -15,6 +15,7 @@ module Labimotion
15
15
 
16
16
  module Mapper
17
17
  NMR_CONFIG = ::File.join(__dir__, 'libs', 'data', 'mapper', 'Source.json').freeze
18
+ WIKI_CONFIG = ::File.join(__dir__, 'libs', 'data', 'mapper', 'Chemwiki.json').freeze
18
19
  end
19
20
  end
20
21
  end
@@ -12,7 +12,7 @@ module Labimotion
12
12
  private
13
13
 
14
14
  def process_layers
15
- (object&.properties.is_a?(Hash) && object.properties[Labimotion::Prop::LAYERS]&.keys || []).each do |key|
15
+ (object&.properties.is_a?(Hash) && (object.properties[Labimotion::Prop::LAYERS]&.keys || [])).each do |key|
16
16
  yield(key, object.properties[Labimotion::Prop::LAYERS][key])
17
17
  end
18
18
  end
@@ -25,7 +25,8 @@ module Labimotion
25
25
  end
26
26
 
27
27
  def process_sample_and_molecule_fields(key, layer)
28
- select_fields(layer, [Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_MOLECULE]).each do |field, idx|
28
+ select_fields(layer,
29
+ [Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_MOLECULE]).each do |field, idx|
29
30
  update_sample_or_molecule_field(key, field, idx)
30
31
  end
31
32
  end
@@ -60,13 +61,20 @@ module Labimotion
60
61
  return unless sid.present?
61
62
 
62
63
  el = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
63
- return unless el.present? && object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value').present?
64
+ return unless el.present? && object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx,
65
+ 'value').present?
64
66
 
67
+ label = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el&.short_label : el.iupac_name
65
68
  update_field_value(key, idx, {
66
- 'el_label' => el.short_label,
67
- 'el_tip' => el.short_label,
68
- 'el_svg' => field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el.get_svg_path : File.join('/images', 'molecules', el&.molecule_svg_file || 'nosvg')
69
- })
69
+ 'el_label' => label,
70
+ 'el_tip' => label,
71
+ 'el_svg' => if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
72
+ el&.get_svg_path
73
+ else
74
+ File.join('/images',
75
+ 'molecules', el&.molecule_svg_file || 'nosvg')
76
+ end
77
+ })
70
78
  end
71
79
 
72
80
  def update_reaction_field(key, field, idx)
@@ -79,23 +87,34 @@ module Labimotion
79
87
  return
80
88
  end
81
89
 
82
- return unless object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value').present?
90
+ return unless object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx,
91
+ 'value').present?
83
92
 
84
93
  update_field_value(key, idx, {
85
- 'el_label' => el.short_label,
86
- 'el_tip' => el.short_label,
87
- 'el_svg' => el.reaction_svg_file
88
- })
94
+ 'el_label' => el.short_label,
95
+ 'el_tip' => el.short_label,
96
+ 'el_svg' => el.reaction_svg_file
97
+ })
89
98
  end
90
99
 
91
100
  def update_table_field(key, field, idx)
92
101
  return unless field['sub_values'].present? && field[Labimotion::Prop::SUBFIELDS].present?
93
102
 
94
- field_table_molecules = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_MOLECULE }
95
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] = set_table(field, field_table_molecules, Labimotion::Prop::MOLECULE) if field_table_molecules.present?
103
+ field_table_molecules = field[Labimotion::Prop::SUBFIELDS].select do |ss|
104
+ ss['type'] == Labimotion::FieldType::DRAG_MOLECULE
105
+ end
106
+ if field_table_molecules.present?
107
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] =
108
+ set_table(field, field_table_molecules, Labimotion::Prop::MOLECULE)
109
+ end
110
+
111
+ field_table_samples = field[Labimotion::Prop::SUBFIELDS].select do |ss|
112
+ ss['type'] == Labimotion::FieldType::DRAG_SAMPLE
113
+ end
114
+ return unless field_table_samples.present?
96
115
 
97
- field_table_samples = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_SAMPLE }
98
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] = set_table(field, field_table_samples, Labimotion::Prop::SAMPLE) if field_table_samples.present?
116
+ object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] =
117
+ set_table(field, field_table_samples, Labimotion::Prop::SAMPLE)
99
118
  end
100
119
 
101
120
  def update_voc_field(key, field, idx)
@@ -122,9 +141,9 @@ module Labimotion
122
141
  end
123
142
 
124
143
  def update_element_voc_field(key, field, idx, root_element)
125
- if field['identifier'] == 'element.name'
126
- update_field_value(key, idx, root_element&.name)
127
- end
144
+ return unless field['identifier'] == 'element.name'
145
+
146
+ update_field_value(key, idx, root_element&.name)
128
147
  end
129
148
 
130
149
  def update_segment_voc_field(key, field, idx, root_element)
@@ -132,21 +151,28 @@ module Labimotion
132
151
  return if segs.empty? || field['layer_id'].blank? || field['field_id'].blank?
133
152
 
134
153
  seg = segs&.first
135
- seg_fields = seg.properties.dig(Labimotion::Prop::LAYERS, field['layer_id'], Labimotion::Prop::FIELDS).select { |ff| ff['field'] == field['field_id'] }
154
+ seg_fields = seg.properties.dig(Labimotion::Prop::LAYERS, field['layer_id'],
155
+ Labimotion::Prop::FIELDS).select do |ff|
156
+ ff['field'] == field['field_id']
157
+ end
136
158
  seg_field = seg_fields&.first
137
159
  update_field_value(key, idx, seg_field['value'])
138
160
  end
139
161
 
140
162
  def update_dataset_voc_field(key, field, idx, root_element)
141
- dk = DatasetKlass.find_by(identifier: field["source_id"])
142
- dk["ols_term_id"]
143
- anas = root_element.analyses.select { |ana| ana.extended_metadata["kind"].split("|")&.first&.strip == dk["ols_term_id"] }
163
+ dk = DatasetKlass.find_by(identifier: field['source_id'])
164
+ dk['ols_term_id']
165
+ anas = root_element.analyses.select do |ana|
166
+ ana.extended_metadata['kind'].split('|')&.first&.strip == dk['ols_term_id']
167
+ end
144
168
  anas.each do |ana|
145
169
  ana.children.each do |cds|
146
170
  next unless cds.dataset.present?
147
171
 
148
172
  ds_prop = cds.dataset.properties
149
- ds_fields = ds_prop.dig(Labimotion::Prop::LAYERS, field['layer_id'], Labimotion::Prop::FIELDS).select { |ff| ff['field'] == field['field_id'] }
173
+ ds_fields = ds_prop.dig(Labimotion::Prop::LAYERS, field['layer_id'], Labimotion::Prop::FIELDS).select do |ff|
174
+ ff['field'] == field['field_id']
175
+ end
150
176
  ds_field = ds_fields&.first
151
177
  if object.properties[Labimotion::Prop::LAYERS][key].present? && ds_field['value'].present?
152
178
  update_field_value(key, idx, ds_field['value'])
@@ -155,15 +181,38 @@ module Labimotion
155
181
  end
156
182
  end
157
183
 
158
- def update_field_value(key, idx, value)
159
- object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = value
184
+ def update_field_value(key, idx, value, act = 'merge')
185
+ field_path = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]
186
+ original_value = field_path['value']
187
+
188
+ field_path['value'] = merge_values(original_value, value, act)
189
+ end
190
+
191
+ def merge_by_type(original_value, new_value)
192
+ if original_value.is_a?(Hash)
193
+ merge_hash_values(original_value, new_value)
194
+ # elsif original_value.is_a?(Array)
195
+ # merge_array_values(original_value, new_value)
196
+ else
197
+ new_value
198
+ end
199
+ end
200
+
201
+ def merge_values(original_value, new_value, act)
202
+ return new_value if act == 'overwrite'
203
+ return new_value if original_value.blank?
204
+ return original_value if new_value.blank?
205
+
206
+ merge_by_type(original_value, new_value)
160
207
  end
161
208
 
162
209
  def set_table(field, field_table_objs, obj)
163
210
  col_ids = field_table_objs.map { |x| x.values[0] }
164
211
  col_ids.each do |col_id|
165
212
  field['sub_values'].each do |sub_value|
166
- next unless sub_value[col_id].present? && sub_value[col_id]['value'].present? && sub_value[col_id]['value']['el_id'].present?
213
+ unless sub_value[col_id].present? && sub_value[col_id]['value'].present? && sub_value[col_id]['value']['el_id'].present?
214
+ next
215
+ end
167
216
 
168
217
  find_obj = obj.constantize.find_by(id: sub_value[col_id]['value']['el_id'])
169
218
  next if find_obj.blank?
@@ -181,23 +230,35 @@ module Labimotion
181
230
 
182
231
  def update_molecule_sub_value(sub_value, col_id, find_obj)
183
232
  sub_value[col_id]['value'].merge!({
184
- 'el_svg' => File.join('/images', 'molecules', find_obj.molecule_svg_file),
185
- 'el_inchikey' => find_obj.inchikey,
186
- 'el_smiles' => find_obj.cano_smiles,
187
- 'el_iupac' => find_obj.iupac_name,
188
- 'el_molecular_weight' => find_obj.molecular_weight
189
- })
233
+ 'el_svg' => File.join('/images', 'molecules', find_obj.molecule_svg_file),
234
+ 'el_inchikey' => find_obj.inchikey,
235
+ 'el_smiles' => find_obj.cano_smiles,
236
+ 'el_iupac' => find_obj.iupac_name,
237
+ 'el_molecular_weight' => find_obj.molecular_weight
238
+ })
190
239
  end
191
240
 
192
241
  def update_sample_sub_value(sub_value, col_id, find_obj)
193
242
  sub_value[col_id]['value'].merge!({
194
- 'el_svg' => find_obj.get_svg_path,
195
- 'el_label' => find_obj.short_label,
196
- 'el_short_label' => find_obj.short_label,
197
- 'el_name' => find_obj.name,
198
- 'el_external_label' => find_obj.external_label,
199
- 'el_molecular_weight' => find_obj.decoupled ? find_obj.molecular_mass : find_obj.molecule.molecular_weight
200
- })
243
+ 'el_svg' => find_obj.get_svg_path,
244
+ 'el_label' => find_obj.short_label,
245
+ 'el_short_label' => find_obj.short_label,
246
+ 'el_name' => find_obj.name,
247
+ 'el_external_label' => find_obj.external_label,
248
+ 'el_molecular_weight' => find_obj.decoupled ? find_obj.molecular_mass : find_obj.molecule.molecular_weight
249
+ })
250
+ end
251
+
252
+ def merge_hash_values(original, new_value)
253
+ return original unless new_value.is_a?(Hash)
254
+
255
+ original.merge(new_value)
256
+ end
257
+
258
+ def merge_array_values(original, new_value)
259
+ return original unless new_value.respond_to?(:to_a)
260
+
261
+ (original + new_value.to_a).uniq
201
262
  end
202
263
  end
203
- end
264
+ end
@@ -0,0 +1,236 @@
1
+ {
2
+ "CHMO:0000025": {
3
+ "index": ["A", "B", "C", "Q1", "Q2", "Q3", "G", "H", "Q4", "Q5", "K", "L", "Q6", "N", "O", "P", "Q7", "R", "S", "T", "U", "V", "W", "X", "Q8", "Q9", "Q10"],
4
+ "mapper": {
5
+ "A": {
6
+ "sources": ["inchikey"]
7
+ },
8
+ "B": {
9
+ "sources": ["molfile"]
10
+ },
11
+ "C": {
12
+ "sources": ["concentration"]
13
+ },
14
+ "G": {
15
+ "sources": ["solvent_volume"]
16
+ },
17
+ "H": {
18
+ "sources": ["elect_inchikey"]
19
+ },
20
+ "K": {
21
+ "sources": ["elect_concentration"]
22
+ },
23
+ "L": {
24
+ "sources": ["reference_internal"]
25
+ },
26
+ "N": {
27
+ "sources": ["scan_rate"]
28
+ },
29
+ "O": {
30
+ "sources": ["scan_number"]
31
+ },
32
+ "P": {
33
+ "sources": ["voltage_limit_one", "voltage_limit_two"],
34
+ "separator": ","
35
+ },
36
+ "R": {
37
+ "sources": ["atmosphere"]
38
+ },
39
+ "S": {
40
+ "sources": ["temperature"]
41
+ },
42
+ "T": {
43
+ "sources": ["condition"]
44
+ },
45
+ "U": {
46
+ "sources": ["we"]
47
+ },
48
+ "V": {
49
+ "sources": ["we_area"]
50
+ },
51
+ "W": {
52
+ "sources": ["ce"]
53
+ },
54
+ "X": {
55
+ "sources": ["re"]
56
+ },
57
+ "Q1": {
58
+ "sources": ["redox"],
59
+ "separator": ","
60
+ },
61
+ "Q2": {
62
+ "sources": ["solvent"]
63
+ },
64
+ "Q3": {
65
+ "sources": ["solvent_molfile"]
66
+ },
67
+ "Q4": {
68
+ "sources": ["elect_molfile"]
69
+ },
70
+ "Q5": {
71
+ "sources": ["elect_purity"]
72
+ },
73
+ "Q6": {
74
+ "sources": ["reference_internal_compound_molfile"]
75
+ },
76
+ "Q7": {
77
+ "sources": ["scan_direction"],
78
+ "separator": ","
79
+ },
80
+ "Q8": {
81
+ "sources": ["details"],
82
+ "separator": ","
83
+ },
84
+ "Q9": {
85
+ "sources": ["includes"],
86
+ "separator": ","
87
+ },
88
+ "Q10": {
89
+ "sources": ["base_page"],
90
+ "separator": ","
91
+ }
92
+ },
93
+ "source": {
94
+ "concentration": {
95
+ "title": "Analyte concentration [mM]",
96
+ "param": "sample_preparation.concentration",
97
+ "type": "dataset"
98
+ },
99
+ "temperature": {
100
+ "title": "Temperature [°C]",
101
+ "param": "set.temperature",
102
+ "type": "dataset"
103
+ },
104
+ "condition": {
105
+ "title": "Condition",
106
+ "param": "set.conditions",
107
+ "type": "dataset"
108
+ },
109
+ "scan_rate": {
110
+ "title": "Scan rate [mV/s]",
111
+ "param": "meas.scan_rate",
112
+ "type": "dataset"
113
+ },
114
+ "atmosphere": {
115
+ "title": "Gas",
116
+ "param": "set.atmosphere",
117
+ "type": "dataset"
118
+ },
119
+ "scan_number": {
120
+ "title": "Scan number",
121
+ "param": "meas.cycles",
122
+ "type": "dataset"
123
+ },
124
+ "we": {
125
+ "title": "Working electrode",
126
+ "param": "electrodes.working",
127
+ "type": "dataset"
128
+ },
129
+ "we_area": {
130
+ "title": "Working electrode surface area",
131
+ "param": "electrodes.working_area",
132
+ "type": "dataset"
133
+ },
134
+ "ce": {
135
+ "title": "Counter electrode",
136
+ "param": "electrodes.counter",
137
+ "type": "dataset"
138
+ },
139
+ "re": {
140
+ "title": "Reference electrode",
141
+ "param": "electrodes.reference",
142
+ "type": "dataset"
143
+ },
144
+ "voltage_limit_one": {
145
+ "title": "Potential window",
146
+ "param": "meas.voltage_limit_one",
147
+ "type": "dataset"
148
+ },
149
+ "voltage_limit_two": {
150
+ "title": "Potential window",
151
+ "param": "meas.voltage_limit_two",
152
+ "type": "dataset"
153
+ },
154
+ "reference_internal": {
155
+ "title": "Internal reference compound_inchikey",
156
+ "param": "meas.reference_internal",
157
+ "type": "dataset"
158
+ },
159
+ "elect_inchikey": {
160
+ "title": "Electrolyte_inchikey",
161
+ "param": "sample_preparation.salt",
162
+ "type": "dataset"
163
+ },
164
+ "elect_concentration": {
165
+ "title": "Electrolyte concentration [M]",
166
+ "param": "sample_preparation.concentration_salt",
167
+ "type": "dataset"
168
+ },
169
+ "solvent": {
170
+ "title": "Solvent A_inchikey",
171
+ "param": "sample_preparation.solvent",
172
+ "type": "dataset"
173
+ },
174
+ "solvent_volume": {
175
+ "title": "Solvent volume [ml]",
176
+ "param": "sample_preparation.amount_sol",
177
+ "type": "dataset"
178
+ },
179
+ "solvent_molfile": {
180
+ "title": "Solvent A_molfile",
181
+ "param": "n/a",
182
+ "type": "string"
183
+ },
184
+ "scan_direction": {
185
+ "title": "Scan direction",
186
+ "param": "n/a",
187
+ "type": "string"
188
+ },
189
+ "reference_internal_compound_molfile": {
190
+ "title": "Internal reference compound_molfile",
191
+ "param": "n/a",
192
+ "type": "string"
193
+ },
194
+ "elect_purity": {
195
+ "title": "Electrolyte purity [%]",
196
+ "param": "n/a",
197
+ "type": "string"
198
+ },
199
+ "redox": {
200
+ "title": "Redox-Potential",
201
+ "param": "n/a",
202
+ "type": "string"
203
+ },
204
+ "elect_molfile": {
205
+ "title": "Electrolyte_inchikey",
206
+ "param": "n/a",
207
+ "type": "string"
208
+ },
209
+ "inchikey": {
210
+ "title": "Analyte_inchikey",
211
+ "param": "inchikey",
212
+ "type": "molecule"
213
+ },
214
+ "molfile": {
215
+ "title": "Analyte_molfile",
216
+ "param": "molfile",
217
+ "type": "sample"
218
+ },
219
+ "details": {
220
+ "title": "Details",
221
+ "param": "n/a",
222
+ "type": "string"
223
+ },
224
+ "includes": {
225
+ "title": "Included",
226
+ "param": "n/a",
227
+ "type": "string"
228
+ },
229
+ "base_page": {
230
+ "title": "BasePageName",
231
+ "param": "n/a",
232
+ "type": "string"
233
+ }
234
+ }
235
+ }
236
+ }
@@ -8,47 +8,190 @@ module Labimotion
8
8
  DEFAULT_ROW_WIDTH = 100
9
9
  DEFAULT_ROW_HEIGHT = 20
10
10
 
11
- def initialize(**args)
11
+ def initialize(id)
12
12
  @xfile = Axlsx::Package.new
13
- @file_extension = 'xlsx'
14
13
  @xfile.workbook.styles.fonts.first.name = 'Calibri'
14
+ @file_extension = 'xlsx'
15
+
16
+ @id = id
17
+ @dataset = Labimotion::Dataset.find_by(element_id: @id, element_type: 'Container')
18
+ return if @dataset.nil?
19
+
20
+ @klass = @dataset.dataset_klass
21
+ @ols_term_id = @klass.ols_term_id
22
+ @label = @klass.label
23
+ @analysis = @dataset&.element&.parent
24
+ @element = @analysis&.root&.containable if @analysis.present?
25
+ @element_type = @element.class.name if @element.present?
26
+ end
27
+
28
+ def read
29
+ @xfile.to_stream.read
15
30
  end
16
31
 
17
- def res_name(id)
18
- element_name = Container.find(id)&.root_element&.short_label
19
- ols = ols_name(id)
32
+ def res_name
33
+ element_name = Container.find(@id)&.root_element&.short_label
34
+ ols = ols_name
20
35
  "#{element_name}_#{ols.gsub(' ', '_')}.xlsx"
21
36
  rescue StandardError => e
22
37
  Labimotion.log_exception(e)
23
38
  end
24
39
 
25
- def ols_name(id)
26
- ds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container')
27
- return nil if ds.nil?
40
+ def spectra
41
+ name_mapping = []
42
+ ## gds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container')
43
+ cds = Container.find(@id)
44
+ cds_csv = cds.attachments.where(aasm_state: 'csv').order(:filename)
45
+ csv_length = cds_csv.length
46
+ return if csv_length.zero?
47
+
48
+ cds_csv.each_with_index do |att, idx|
49
+ sheet_name = "Sheet#{idx+1}"
50
+ sheet = @xfile.workbook.add_worksheet(name: sheet_name)
51
+ name_mapping.push([sheet_name, att.filename])
52
+ File.open(att.attachment_url) do |fi|
53
+ fi.each_line do |line|
54
+ sheet.add_row(line.split(','))
55
+ end
56
+ end
57
+ end
58
+ return unless name_mapping.length > 1
59
+
60
+ first_sheet = @xfile.workbook.worksheets&.first
61
+ header_style = first_sheet&.styles.add_style(sz: 12, fg_color: 'FFFFFF', bg_color: '00008B', border: { style: :thick, color: 'FF777777', edges: [:bottom] })
62
+ first_sheet&.add_row(['Sheet name', 'File name'], style: header_style)
63
+ name_mapping&.each do |mapping|
64
+ next if mapping.length < 2
65
+
66
+ @xfile.workbook.worksheets&.first&.add_row([mapping[0].to_s, mapping[1].to_s])
67
+ end
68
+ rescue StandardError => e
69
+ Labimotion.log_exception(e)
70
+ end
71
+
72
+ def export
73
+ return if @dataset.nil? || @analysis.nil? || @element.nil?
74
+
75
+ description
76
+ dataset_info
77
+ # element_info
78
+ chemwiki_info
79
+ rescue StandardError => e
80
+ Labimotion.log_exception(e)
81
+ end
82
+
83
+ private
84
+
85
+ def chemwiki_info
86
+ config = Labimotion::MapperUtils.load_config(File.read(Constants::Mapper::WIKI_CONFIG))
87
+ return if config.nil? || config[@ols_term_id].nil?
28
88
 
29
- name = ds.dataset_klass.label
89
+ sheet = @xfile.workbook.add_worksheet(name: 'ChemWiki')
90
+ map_index = config.dig(@ols_term_id, 'index')
91
+ map_mapper = config.dig(@ols_term_id, 'mapper')
92
+ map_source = config.dig(@ols_term_id, 'source')
30
93
 
31
- match = name.match(/\((.*?)\)/)
32
- name = match && match.length > 1 ? match[1] : name
94
+ return if map_index.nil? || map_mapper.nil? || map_source.nil?
33
95
 
34
- name = '1H NMR' if ds.dataset_klass.ols_term_id == 'CHMO:0000593'
35
- name = '13C NMR' if ds.dataset_klass.ols_term_id == 'CHMO:0000595'
96
+ return unless map_index.is_a?(Array) && map_mapper.is_a?(Hash) && map_source.is_a?(Hash)
97
+
98
+ # sheet.column_info.each { |col| col.width = map_width }
99
+
100
+ array_header = []
101
+ array_data = []
102
+ map_index.each do |key|
103
+ mapper = map_mapper.dig(key)
104
+ next if mapper.nil? || mapper['sources'].nil? || !mapper.is_a?(Hash)
105
+
106
+ col_header = ''
107
+ col_value = ''
108
+ mapper['sources'].each_with_index do |source_key, idx|
109
+ source = map_source&.dig(source_key)
110
+ next if source.nil? || source['title'].nil?
111
+
112
+ col_header = source['title']
113
+
114
+ if col_value.present?
115
+ col_value += (mapper['separator'] || '') + source_data(source)
116
+ else
117
+ col_value = source_data(source)
118
+ end
119
+ end
120
+ array_header.push(col_header || '')
121
+ array_data.push(col_value || '')
122
+ end
123
+ sheet.add_row(array_header)
124
+ sheet.add_row(array_data)
125
+ rescue StandardError => e
126
+ Labimotion.log_exception(e)
127
+ end
128
+
129
+
130
+ def conv_value(field, properties)
131
+ return '' if field.nil?
132
+
133
+ case field['type']
134
+ when 'select'
135
+ Labimotion::Utils.find_options_val(field, properties) || field['value']
136
+ else
137
+ field['value']
138
+ end
139
+ end
140
+
141
+ def source_data(source)
142
+ param = source['param']
143
+ return '' if param.nil? || source['type'].nil?
144
+
145
+ val = case source['type']
146
+ when 'string'
147
+ param
148
+ when 'sample'
149
+ @element.send(param) if @element.respond_to?(param)
150
+ when 'molecule'
151
+ @element.molecule.send(param) if @element_type == 'Sample' && @element&.molecule&.respond_to?(param)
152
+ when 'dataset'
153
+ field = Labimotion::Utils.find_field(@dataset.properties, param)
154
+ # (field && field['value']) || ''
155
+ conv_value(field, @dataset.properties)
156
+ end
157
+ val || ''
158
+ rescue StandardError => e
159
+ Labimotion.log_exception(e)
160
+ end
161
+
162
+ def element_info
163
+ if @element.class.name == 'Sample'
164
+ sheet = @xfile.workbook.add_worksheet(name: 'Element')
165
+ sheet.add_row(['inchikey', @element.molecule_inchikey])
166
+ sheet.add_row(['molfile', @element.molfile])
167
+ end
168
+ rescue StandardError => e
169
+ Labimotion.log_exception(e)
170
+ end
171
+
172
+ def ols_name
173
+ #name = @dataset.dataset_klass.label
174
+
175
+ match = @label.match(/\((.*?)\)/)
176
+ name = match && match.length > 1 ? match[1] : @label
177
+
178
+ name = '1H NMR' if @ols_term_id == 'CHMO:0000593'
179
+ name = '13C NMR' if @ols_term_id == 'CHMO:0000595'
36
180
  name.slice(0, 26)
37
181
  rescue StandardError => e
38
182
  Labimotion.log_exception(e)
39
183
  'ols_name'
40
184
  end
41
185
 
42
- def description(ds, id)
43
- wb = @xfile.workbook
186
+ def description
44
187
  sheet = @xfile.workbook.add_worksheet(name: 'Description')
45
188
  header_style = sheet.styles.add_style(sz: 12, fg_color: 'FFFFFF', bg_color: '00008B', border: { style: :thick, color: 'FF777777', edges: [:bottom] })
46
- sheet.add_row(['File name', res_name(id)])
189
+ sheet.add_row(['File name', res_name])
47
190
  sheet.add_row(['Time', Time.now.strftime("%Y-%m-%d %H:%M:%S %Z")] )
48
191
  sheet.add_row(['(This file is automatically generated by the system.)'])
49
192
  sheet.add_row([''])
50
193
  sheet.add_row([''])
51
- sheet.add_row(['Fields description of sheet:' + ds.dataset_klass.label])
194
+ sheet.add_row(['Fields description of sheet:' + @dataset.dataset_klass.label])
52
195
  sheet.add_row(['Fields', 'Field description'], style: header_style)
53
196
  sheet.add_row(['Layer Label', 'The label of the layer'])
54
197
  sheet.add_row(['Field Label', 'The label of the field'])
@@ -65,24 +208,18 @@ module Labimotion
65
208
  Labimotion.log_exception(e)
66
209
  end
67
210
 
68
- def export(id)
69
- ds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container')
70
- return if ds.nil?
71
-
72
- description(ds, id)
73
-
74
- wb = @xfile.workbook
75
- name = ols_name(id)
211
+ def dataset_info
212
+ name = ols_name
76
213
  return if name.nil?
77
214
 
78
215
  sheet = @xfile.workbook.add_worksheet(name: name)
79
- sheet.add_row([ds.dataset_klass.label])
216
+ sheet.add_row([@dataset.dataset_klass.label])
80
217
  header_style = sheet.styles.add_style(sz: 12, fg_color: 'FFFFFF', bg_color: '00008B', border: { style: :thick, color: 'FF777777', edges: [:bottom] })
81
218
  layer_style = sheet.styles.add_style(b: true, bg_color: 'CEECF5')
82
219
  sheet.add_row(header, style: header_style)
83
220
 
84
- layers = ds.properties[Labimotion::Prop::LAYERS] || {}
85
- options = ds.properties[Labimotion::Prop::SEL_OPTIONS]
221
+ layers = @dataset.properties[Labimotion::Prop::LAYERS] || {}
222
+ options = @dataset.properties[Labimotion::Prop::SEL_OPTIONS]
86
223
  layer_keys = layers.keys.sort_by { |key| layers[key]['position'] }
87
224
  layer_keys.each do |key|
88
225
  layer = layers[key]
@@ -116,8 +253,6 @@ module Labimotion
116
253
  end
117
254
  sheet.rows.last.cells[2].value = show_value
118
255
  sheet.rows.last.cells[8].value = field['system'] || field['device']
119
-
120
-
121
256
  end
122
257
  # sheet.column_widths nil, nil, nil, nil, 0, 0, 0, 0, 0
123
258
  end
@@ -136,45 +271,9 @@ module Labimotion
136
271
  field['value']
137
272
  end
138
273
 
139
- def spectra(id)
140
- name_mapping = []
141
- wb = @xfile.workbook
142
- gds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container')
143
- cds = Container.find(id)
144
- cds_csv = cds.attachments.where(aasm_state: 'csv').order(:filename)
145
- csv_length = cds_csv.length
146
- return if csv_length.zero?
147
- cds_csv.each_with_index do |att, idx|
148
- sheet_name = "Sheet#{idx+1}"
149
- sheet = @xfile.workbook.add_worksheet(name: sheet_name)
150
- name_mapping.push([sheet_name, att.filename])
151
- File.open(att.attachment_url) do |fi|
152
- fi.each_line do |line|
153
- sheet.add_row(line.split(','))
154
- end
155
- end
156
- end
157
-
158
- if name_mapping.length > 1
159
- first_sheet = @xfile.workbook.worksheets&.first
160
- header_style = first_sheet&.styles.add_style(sz: 12, fg_color: 'FFFFFF', bg_color: '00008B', border: { style: :thick, color: 'FF777777', edges: [:bottom] })
161
- first_sheet&.add_row(['Sheet name', 'File name'], style: header_style)
162
- name_mapping&.each do |mapping|
163
- next if mapping.length < 2
164
-
165
- @xfile.workbook.worksheets&.first&.add_row([mapping[0].to_s, mapping[1].to_s])
166
- end
167
- end
168
-
169
- end
170
-
171
274
  def header
172
275
  ['Layer Label', 'Field Label', 'Value', 'Unit', 'Name', 'Type', 'Source?', 'Source identifier', 'Source data', 'Ontology', 'Ontology Label', 'iri'].freeze
173
276
  end
174
277
 
175
- def read
176
- @xfile.to_stream.read
177
- end
178
-
179
278
  end
180
279
  end
@@ -76,7 +76,7 @@ module Labimotion
76
76
 
77
77
  def finalize_ds(new_prop)
78
78
  Constants::FG_FINALIZE.each do |field_path|
79
- field = find_field(new_prop, field_path)
79
+ field = Labimotion::Utils.find_field(new_prop, field_path)
80
80
  next unless field
81
81
 
82
82
  update_finalized_field(field)
@@ -86,7 +86,7 @@ module Labimotion
86
86
 
87
87
  def sys_to_ds(new_prop, element, current_user)
88
88
  Constants::FG_SYSTEM.each do |field_path|
89
- field = find_field(new_prop, field_path)
89
+ field = Labimotion::Utils.find_field(new_prop, field_path)
90
90
  next unless field
91
91
 
92
92
  update_system_field(field, current_user, element)
@@ -185,16 +185,8 @@ module Labimotion
185
185
  Constants::DUP_FIELDS.exclude?(param_key.to_s.downcase)
186
186
  end
187
187
 
188
- def find_field(properties, field_path)
189
- layer_name, field_name = field_path.split('.')
190
- fields = properties.dig(Labimotion::Prop::LAYERS, layer_name, Labimotion::Prop::FIELDS)
191
- return unless fields
192
-
193
- fields.find { |f| f['field'] == field_name }
194
- end
195
-
196
188
  def update_param_field(properties, field_path, param_key, metadata)
197
- field = find_field(properties, field_path)
189
+ field = Labimotion::Utils.find_field(properties, field_path)
198
190
  return unless field
199
191
 
200
192
  update_field_value(field, param_key, metadata)
@@ -80,6 +80,28 @@ module Labimotion
80
80
  current_version
81
81
  end
82
82
 
83
+ def self.find_field(properties, field_path, separator = '.')
84
+ return if properties.nil? || field_path.nil?
85
+
86
+ layer_name, field_name = field_path.split(separator)
87
+ fields = properties.dig(Labimotion::Prop::LAYERS, layer_name, Labimotion::Prop::FIELDS)
88
+ return unless fields
89
+
90
+ fields.find { |f| f['field'] == field_name }
91
+ end
92
+
93
+ def self.find_options_val(field, properties)
94
+ return if field.nil? || properties.nil? || field['option_layers'].nil? || field['value'].nil?
95
+
96
+ option_layers = properties.dig(Labimotion::Prop::SEL_OPTIONS, field['option_layers'])
97
+ options = option_layers && option_layers['options']
98
+ return if options.nil?
99
+
100
+ sel_option = options.find { |o| o['key'] = field['value'] }
101
+ sel_option && sel_option['label']
102
+
103
+ end
104
+
83
105
  def self.pkg(pkg)
84
106
  pkg = {} if pkg.nil?
85
107
  pkg['eln'] = Chemotion::Application.config.version
@@ -2,5 +2,5 @@
2
2
 
3
3
  ## Labimotion Version
4
4
  module Labimotion
5
- VERSION = '2.0.0.rc3'
5
+ VERSION = '2.0.0.rc5'
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: labimotion
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.rc3
4
+ version: 2.0.0.rc5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chia-Lin Lin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2025-02-14 00:00:00.000000000 Z
12
+ date: 2025-02-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -76,6 +76,7 @@ files:
76
76
  - lib/labimotion/helpers/vocabulary_helpers.rb
77
77
  - lib/labimotion/libs/attachment_handler.rb
78
78
  - lib/labimotion/libs/converter.rb
79
+ - lib/labimotion/libs/data/mapper/Chemwiki.json
79
80
  - lib/labimotion/libs/data/mapper/Source.json
80
81
  - lib/labimotion/libs/data/vocab/Standard.json
81
82
  - lib/labimotion/libs/data/vocab/System.json