Skip to content

Commit

Permalink
drag sample element to segment
Browse files Browse the repository at this point in the history
  • Loading branch information
phuang26 committed Nov 14, 2023
1 parent 4bba8a4 commit f38b0ed
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 227 deletions.
3 changes: 3 additions & 0 deletions lib/labimotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def self.log_exception(exception, current_user = nil)
autoload :ConverterAPI, 'labimotion/apis/converter_api'

######## Entities
autoload :PropertiesEntity, 'labimotion/entities/properties_entity'

autoload :ElementEntity, 'labimotion/entities/element_entity'
autoload :ElnElementEntity, 'labimotion/entities/eln_element_entity'

Expand Down Expand Up @@ -56,6 +58,7 @@ def self.log_exception(exception, current_user = nil)
autoload :NmrMapperRepo, 'labimotion/libs/nmr_mapper_repo' ## for Chemotion Repository
autoload :TemplateHub, 'labimotion/libs/template_hub'
autoload :ExportDataset, 'labimotion/libs/export_dataset'
autoload :SampleAssociation, 'labimotion/libs/sample_association'

######## Utils
autoload :ConState, 'labimotion/utils/con_state'
Expand Down
7 changes: 6 additions & 1 deletion lib/labimotion/apis/generic_element_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,10 @@ class GenericElementAPI < Grape::API
end
route_param :id do
before do
error!('401 Unauthorized', 401) unless current_user.matrix_check_by_name('genericElement') && ElementPolicy.new(current_user, Element.find(params[:id])).read?
@element_policy = ElementPolicy.new(current_user, Element.find(params[:id]))
error!('401 Unauthorized', 401) unless current_user.matrix_check_by_name('genericElement') && @element_policy.read?
rescue ActiveRecord::RecordNotFound
error!('404 Not Found', 404)
end

get do
Expand All @@ -321,10 +324,12 @@ class GenericElementAPI < Grape::API
attachments: attach_thumbnail(element&.attachments)
}
else
#byebug
{
element: Labimotion::ElementEntity.represent(
element,
detail_levels: ElementDetailLevelCalculator.new(user: current_user, element: element).detail_levels,
policy: @element_policy
),
attachments: attach_thumbnail(element&.attachments)
}
Expand Down
78 changes: 13 additions & 65 deletions lib/labimotion/entities/element_entity.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

require 'labimotion/entities/application_entity'
require 'labimotion/entities/properties_entity'
## TODO: Refactor labimotion to use the same entities as chemotion
module Labimotion
class ElementEntity < ApplicationEntity
class ElementEntity < PropertiesEntity
with_options(anonymize_below: 0) do
expose! :can_copy
expose! :can_copy, unless: :displayed_in_list
expose! :can_publish, unless: :displayed_in_list
expose! :can_update, unless: :displayed_in_list
expose! :container, using: 'Entities::ContainerEntity'
expose! :created_by
expose! :id
Expand Down Expand Up @@ -35,75 +37,21 @@ def is_restricted
detail_levels[Labimotion::Element] < 10
end

# TODO: Refactor this method to something more readable/understandable
def properties
(object.properties['layers']&.keys || []).each do |key|
# layer = object.properties[key]
field_sample_molecules = object.properties['layers'][key]['fields'].select { |ss| ss['type'] == 'drag_sample' || ss['type'] == 'drag_molecule' }
field_sample_molecules.each do |field|
idx = object.properties['layers'][key]['fields'].index(field)
sid = field.dig('value', 'el_id')
next unless sid.present?

el = field['type'] == 'drag_sample' ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
next unless el.present?
next unless object.properties.dig('layers', key, 'fields', idx, 'value').present?

object.properties['layers'][key]['fields'][idx]['value']['el_label'] = el.short_label if field['type'] == 'drag_sample'
object.properties['layers'][key]['fields'][idx]['value']['el_tip'] = el.short_label if field['type'] == 'drag_sample'
object.properties['layers'][key]['fields'][idx]['value']['el_svg'] = field['type'] == 'drag_sample' ? el.get_svg_path : File.join('/images', 'molecules', el.molecule_svg_file)
end

field_tables = object.properties['layers'][key]['fields'].select { |ss| ss['type'] == 'table' }
field_tables.each do |field|
idx = object.properties['layers'][key]['fields'].index(field)
next unless field['sub_values'].present? && field['sub_fields'].present?

field_table_molecules = field['sub_fields'].select { |ss| ss['type'] == 'drag_molecule' }
object.properties['layers'][key]['fields'][idx] = set_table(field, field_table_molecules, 'Molecule') if field_table_molecules.present?

field_table_samples = field['sub_fields'].select { |ss| ss['type'] == 'drag_sample' }
object.properties['layers'][key]['fields'][idx] = set_table(field, field_table_samples, 'Sample') if field_table_samples.present?
end
end
object.properties
end

def type
object.element_klass.name # 'genericEl' #object.type
end

def set_table(field, field_table_objs, obj)
col_ids = field_table_objs.map { |x| x.values[0] }
col_ids.each do |col_id|
field['sub_values'].each do |sub_value|
next unless sub_value[col_id].present? && sub_value[col_id]['value'].present? && sub_value[col_id]['value']['el_id'].present?

find_obj = obj.constantize.find_by(id: sub_value[col_id]['value']['el_id'])
next unless find_obj.present?

case obj
when 'Molecule'
sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_obj.molecule_svg_file)
sub_value[col_id]['value']['el_inchikey'] = find_obj.inchikey
sub_value[col_id]['value']['el_smiles'] = find_obj.cano_smiles
sub_value[col_id]['value']['el_iupac'] = find_obj.iupac_name
sub_value[col_id]['value']['el_molecular_weight'] = find_obj.molecular_weight
when 'Sample'
sub_value[col_id]['value']['el_svg'] = find_obj.get_svg_path
sub_value[col_id]['value']['el_label'] = find_obj.short_label
sub_value[col_id]['value']['el_short_label'] = find_obj.short_label
sub_value[col_id]['value']['el_name'] = find_obj.name
sub_value[col_id]['value']['el_external_label'] = find_obj.external_label
sub_value[col_id]['value']['el_molecular_weight'] = find_obj.decoupled ? find_obj.molecular_mass : find_obj.molecule.molecular_weight
end
end
end
field
def can_update
self.options[:policy].try(:update?) || false
end

def can_copy
true
self.options[:policy].try(:copy?) || false
end

def can_publish
self.options[:policy].try(:destroy?) || false
end

end
end
72 changes: 72 additions & 0 deletions lib/labimotion/entities/properties_entity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'labimotion/entities/application_entity'

# Entity module
module Labimotion
class PropertiesEntity < Labimotion::ApplicationEntity

# TODO: Refactor this method to something more readable/understandable
def properties
(object&.properties.is_a?(Hash) && object.properties['layers']&.keys || []).each do |key|
# layer = object.properties[key]
field_sample_molecules = object.properties['layers'][key]['fields'].select { |ss| ss['type'] == 'drag_sample' || ss['type'] == 'drag_molecule' }
field_sample_molecules.each do |field|
idx = object.properties['layers'][key]['fields'].index(field)
sid = field.dig('value', 'el_id')
next unless sid.present?

el = field['type'] == 'drag_sample' ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
next unless el.present?
next unless object.properties.dig('layers', key, 'fields', idx, 'value').present?

object.properties['layers'][key]['fields'][idx]['value']['el_label'] = el.short_label if field['type'] == 'drag_sample'
object.properties['layers'][key]['fields'][idx]['value']['el_tip'] = el.short_label if field['type'] == 'drag_sample'
object.properties['layers'][key]['fields'][idx]['value']['el_svg'] = field['type'] == 'drag_sample' ? el.get_svg_path : File.join('/images', 'molecules', el.molecule_svg_file)
end

field_tables = object.properties['layers'][key]['fields'].select { |ss| ss['type'] == 'table' }
field_tables.each do |field|
idx = object.properties['layers'][key]['fields'].index(field)
next unless field['sub_values'].present? && field['sub_fields'].present?

field_table_molecules = field['sub_fields'].select { |ss| ss['type'] == 'drag_molecule' }
object.properties['layers'][key]['fields'][idx] = set_table(field, field_table_molecules, 'Molecule') if field_table_molecules.present?

field_table_samples = field['sub_fields'].select { |ss| ss['type'] == 'drag_sample' }
object.properties['layers'][key]['fields'][idx] = set_table(field, field_table_samples, 'Sample') if field_table_samples.present?
end
end
object.properties
end


def set_table(field, field_table_objs, obj)
col_ids = field_table_objs.map { |x| x.values[0] }
col_ids.each do |col_id|
field['sub_values'].each do |sub_value|
next unless sub_value[col_id].present? && sub_value[col_id]['value'].present? && sub_value[col_id]['value']['el_id'].present?

find_obj = obj.constantize.find_by(id: sub_value[col_id]['value']['el_id'])
next unless find_obj.present?

case obj
when 'Molecule'
sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_obj.molecule_svg_file)
sub_value[col_id]['value']['el_inchikey'] = find_obj.inchikey
sub_value[col_id]['value']['el_smiles'] = find_obj.cano_smiles
sub_value[col_id]['value']['el_iupac'] = find_obj.iupac_name
sub_value[col_id]['value']['el_molecular_weight'] = find_obj.molecular_weight
when 'Sample'
sub_value[col_id]['value']['el_svg'] = find_obj.get_svg_path
sub_value[col_id]['value']['el_label'] = find_obj.short_label
sub_value[col_id]['value']['el_short_label'] = find_obj.short_label
sub_value[col_id]['value']['el_name'] = find_obj.name
sub_value[col_id]['value']['el_external_label'] = find_obj.external_label
sub_value[col_id]['value']['el_molecular_weight'] = find_obj.decoupled ? find_obj.molecular_mass : find_obj.molecule.molecular_weight
end
end
end
field
end

end
end
50 changes: 1 addition & 49 deletions lib/labimotion/entities/segment_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,12 @@
require 'labimotion/entities/application_entity'
module Labimotion
## Segment entity
class SegmentEntity < ApplicationEntity
class SegmentEntity < PropertiesEntity
expose :id, :segment_klass_id, :element_type, :element_id, :properties, :properties_release, :uuid, :klass_uuid, :klass_label

def klass_label
object.segment_klass.label
end

def properties # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength
return unless object.respond_to?(:properties)

return if object&.properties.dig('layers').blank?

object&.properties['layers'].each_key do |key| # rubocop:disable Metrics/BlockLength
next if object&.properties.dig('layers', key, 'fields').blank?

field_sample_molecules = object&.properties['layers'][key]['fields'].select { |ss| ss['type'] == 'drag_molecule' }
field_sample_molecules.each do |field|
idx = object&.properties['layers'][key]['fields'].index(field)
sid = field.dig('value', 'el_id')
next if sid.blank?

el = Molecule.find_by(id: sid)
next if el.blank?

next if object&.properties.dig('layers', key, 'fields', idx, 'value').blank?

object&.properties['layers'][key]['fields'][idx]['value']['el_svg'] = File.join('/images', 'molecules', el.molecule_svg_file)
end

field_tables = object.properties['layers'][key]['fields'].select { |ss| ss['type'] == 'table' }
field_tables.each do |field|
next unless field['sub_values'].present? && field['sub_fields'].present?

field_table_molecules = field['sub_fields'].select { |ss| ss['type'] == 'drag_molecule' }
next if field_table_molecules.blank?

col_ids = field_table_molecules.map { |x| x.values[0] }
col_ids.each do |col_id|
field['sub_values'].each do |sub_value|
next unless sub_value[col_id].present? && sub_value[col_id]['value'].present? && sub_value[col_id]['value']['el_id'].present?

find_mol = Molecule.find_by(id: sub_value[col_id]['value']['el_id'])
next if find_mol.blank?

sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_mol.molecule_svg_file)
sub_value[col_id]['value']['el_inchikey'] = find_mol.inchikey
sub_value[col_id]['value']['el_smiles'] = find_mol.cano_smiles
sub_value[col_id]['value']['el_iupac'] = find_mol.iupac_name
sub_value[col_id]['value']['el_molecular_weight'] = find_mol.molecular_weight
end
end
end
end
object&.properties
end
end
end
4 changes: 2 additions & 2 deletions lib/labimotion/helpers/element_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def create_element(current_user, params)
all_coll = Collection.get_all_collection_for_user(current_user.id)
element.collections << all_coll
element.save!
element.properties = update_sample_association(element, params[:properties], current_user)
element.properties = update_sample_association(params[:properties], current_user, element)
element.container = update_datamodel(params[:container], current_user)
element.save!
element.save_segments(segments: params[:segments], current_user_id: current_user.id)
Expand All @@ -105,7 +105,7 @@ def create_element(current_user, params)
def update_element_by_id(current_user, params)
element = Labimotion::Element.find(params[:id])
update_datamodel(params[:container], current_user)
properties = update_sample_association(element, params[:properties], current_user)
properties = update_sample_association(params[:properties], current_user, element)
params.delete(:container)
params.delete(:properties)
attributes = declared(params.except(:segments), include_missing: false)
Expand Down
Loading

0 comments on commit f38b0ed

Please sign in to comment.