Skip to content

Commit

Permalink
improve extract solvent and other functions for sdf export
Browse files Browse the repository at this point in the history
  • Loading branch information
adambasha0 committed Sep 8, 2023
1 parent fa695bf commit 7785dbe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
12 changes: 6 additions & 6 deletions app/packs/src/fetchers/SamplesFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions lib/export/export_sdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/export/export_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand Down
21 changes: 15 additions & 6 deletions lib/import/import_samples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7785dbe

Please sign in to comment.