From 7785dbee07a7326a7ea398ce5f6d643180e04e1a Mon Sep 17 00:00:00 2001 From: nh9378 Date: Fri, 1 Sep 2023 16:55:40 +0000 Subject: [PATCH] improve extract solvent and other functions for sdf export --- app/packs/src/fetchers/SamplesFetcher.js | 12 ++++++------ lib/export/export_sdf.rb | 10 +++++++--- lib/export/export_table.rb | 3 ++- lib/import/import_samples.rb | 21 +++++++++++++++------ 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/app/packs/src/fetchers/SamplesFetcher.js b/app/packs/src/fetchers/SamplesFetcher.js index d225b76bcc..ff45999681 100644 --- a/app/packs/src/fetchers/SamplesFetcher.js +++ b/app/packs/src/fetchers/SamplesFetcher.js @@ -169,11 +169,11 @@ export default class SamplesFetcher { } static importSamplesFromFileConfirm(params) { - let promise = fetch('/api/v1/samples/confirm_import/', { + const promise = fetch('/api/v1/samples/confirm_import/', { credentials: 'same-origin', method: 'post', headers: { - 'Accept': 'application/json', + Accept: 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ @@ -182,16 +182,16 @@ export default class SamplesFetcher { mapped_keys: params.mapped_keys, }) }).then((response) => { - return response.json(); + response.json(); }).then((json) => { if (Array.isArray(json.error_messages)) { - for (let i = 0; i < json.error_messages.length; i++) { + json.error_messages.forEach((message) => { NotificationActions.add({ - message: json.error_messages[i], + message, level: 'error', autoDismiss: 10 }); - } + }); } else { NotificationActions.add({ message: json.error_messages || json.message, diff --git a/lib/export/export_sdf.rb b/lib/export/export_sdf.rb index 519ce03e67..dd759c8307 100644 --- a/lib/export/export_sdf.rb +++ b/lib/export/export_sdf.rb @@ -72,14 +72,18 @@ def filter_with_permission_and_detail_level(sample) data.concat("\$\$\$\$\n") end + def extract_reference_values(raw_value) + regex = /[\[\]()]/ + string = raw_value.gsub(regex, '') + string.split(',').join(' - ') + end + def format_field(column, raw_value) field = column.gsub(/\s+/, '_').upcase reference_values = ['melting pt', 'boiling pt'] sample_column = if reference_values.include?(column) - regex = /[\[\]()]/ - string = raw_value.gsub(regex, '') - string.split(',').join(' - ') + extract_reference_values(raw_value) elsif column == 'solvent' extract_label_from_solvent_column(raw_value) || '' else diff --git a/lib/export/export_table.rb b/lib/export/export_table.rb index 0e65187fef..d657f1ecf1 100644 --- a/lib/export/export_table.rb +++ b/lib/export/export_table.rb @@ -61,7 +61,8 @@ def extract_label_from_solvent_column(sample_column) return nil if solvent_hash.nil? - solvent_hash[0]&.fetch('label', nil) + solvent_values = solvent_hash.map { |solvent| solvent&.fetch('label', nil) } + solvent_values.compact.join('-') end def generate_headers(table, excluded_columns = [], selected_columns = []) diff --git a/lib/import/import_samples.rb b/lib/import/import_samples.rb index a5c9e4e918..0a20709562 100644 --- a/lib/import/import_samples.rb +++ b/lib/import/import_samples.rb @@ -176,17 +176,26 @@ def included_fields Sample.attribute_names - excluded_fields end + def construct_solvents_array(solvents) + solvents_array = solvents.split('-') + solvents_array.map(&:capitalize) + end + def handle_sample_solvent_column(sample, row) return unless row['solvent'].is_a? String - solvent = Chemotion::SampleConst.solvents_smiles_options.find { |s| s[:label].include?(row['solvent']) } - if solvent.present? - solvent_column = [{ label: solvent[:value][:external_label], - smiles: solvent[:value][:smiles], - ratio: '100' }] + solvent_array = construct_solvents_array(row['solvent']) + solvent_column = [] + solvent_array.each do |element| + solvent = Chemotion::SampleConst.solvents_smiles_options.find { |s| s[:label].include?(element) } + next if solvent.blank? + + solvent_column.push({ label: solvent[:value][:external_label], + smiles: solvent[:value][:smiles], + ratio: '1' }) end sample['solvent'] = '' if sample['solvent'].is_a? String - sample['solvent'] = solvent_column if solvent.present? + sample['solvent'] = solvent_column unless solvent_column.empty? end # format row[field] for melting and boiling point