Skip to content

Commit

Permalink
1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
phuang26 committed Nov 18, 2024
1 parent 0f49d4e commit 67a1365
Show file tree
Hide file tree
Showing 22 changed files with 183 additions and 40 deletions.
2 changes: 2 additions & 0 deletions lib/labimotion.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# In your_gem_name.rb or main Ruby file
module Labimotion

autoload :CONF, 'labimotion/conf'
autoload :VERSION, 'labimotion/version'

def self.logger
Expand Down Expand Up @@ -62,6 +63,7 @@ def self.log_exception(exception, current_user = nil)
autoload :ExportDataset, 'labimotion/libs/export_dataset'
autoload :SampleAssociation, 'labimotion/libs/sample_association'
autoload :PropertiesHandler, 'labimotion/libs/properties_handler'
autoload :AttachmentHandler, 'labimotion/libs/attachment_handler'

######## Utils
autoload :Prop, 'labimotion/utils/prop'
Expand Down
21 changes: 18 additions & 3 deletions lib/labimotion/apis/generic_element_api.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'labimotion/version'
require 'labimotion/conf'
require 'labimotion/libs/export_element'

module Labimotion
Expand All @@ -10,12 +10,26 @@ class GenericElementAPI < Grape::API
helpers ContainerHelpers
helpers ParamsHelpers
helpers CollectionHelpers
helpers UserLabelHelpers
helpers Labimotion::SampleAssociationHelpers
helpers Labimotion::GenericHelpers
helpers Labimotion::ElementHelpers
helpers Labimotion::ParamHelpers

resource :generic_elements do
# might be removed because the file is moved to public folder
namespace :current do
desc 'Return serialized elements of current user'
get do
klasses_json_path = Labimotion::KLASSES_JSON # Rails.root.join('app/packs/klasses.json')
klasses = JSON.parse(File.read(klasses_json_path))
{ klasses: klasses }
rescue StandardError => e
Labimotion.log_exception(e, current_user)
{ klasses: [] }
end
end

namespace :klass do
desc 'get klass info'
params do
Expand Down Expand Up @@ -327,7 +341,7 @@ class GenericElementAPI < Grape::API
Labimotion.log_exception(e, current_user)
{ error: e.message }
end
end
end

desc 'Return serialized elements of current user'
params do
Expand All @@ -336,6 +350,7 @@ class GenericElementAPI < Grape::API
optional :el_type, type: String, desc: 'element klass name'
optional :from_date, type: Integer, desc: 'created_date from in ms'
optional :to_date, type: Integer, desc: 'created_date to in ms'
optional :user_label, type: Integer, desc: 'user label'
optional :filter_created_at, type: Boolean, desc: 'filter by created at or updated at'
optional :sort_column, type: String, desc: 'sort by updated_at or selected layers property'
end
Expand Down Expand Up @@ -426,7 +441,7 @@ class GenericElementAPI < Grape::API
raise e
end
end
end
end
end
end
end
10 changes: 7 additions & 3 deletions lib/labimotion/collection/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ def self.import_datasets(data, instances, gt, current_user_id, &update_instances
begin
data.fetch('Labimotion::Dataset', {}).each do |uuid, fields|
klass_id = fields['dataset_klass_id']
dk_obj = data.fetch('Labimotion::DatasetKlass', {})[klass_id]
dk_id = dk_obj['identifier']
next if data.fetch('Labimotion::DatasetKlass', {}).empty?

dk_obj = data.fetch('Labimotion::DatasetKlass', {}) && data.fetch('Labimotion::DatasetKlass', {})[klass_id]
next if dk_obj.nil?

dk_id = dk_obj && dk_obj['identifier']
element_uuid = fields.fetch('element_id')
element_type = fields.fetch('element_type')
element = instances.fetch(element_type).fetch(element_uuid)
Expand Down Expand Up @@ -78,7 +82,7 @@ def self.import_datasets(data, instances, gt, current_user_id, &update_instances
end
rescue StandardError => e
Labimotion.log_exception(e)
raise
# raise
end
end

Expand Down
6 changes: 6 additions & 0 deletions lib/labimotion/conf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

## Labimotion Configuration
module Labimotion
KLASSES_JSON = Rails.root.join('public', 'klasses.json').to_s.freeze # Rails.root.join('app/packs/klasses.json').to_s.freeze
end
4 changes: 2 additions & 2 deletions lib/labimotion/entities/element_revision_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def properties

object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_label'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el.get_svg_path : File.join('/images', 'molecules', el.molecule_svg_file)
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el.get_svg_path : File.join('/images', 'molecules', el&.molecule_svg_file || 'nosvg')
end

field_tables = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::TABLE }
Expand All @@ -41,7 +41,7 @@ def properties
find_mol = Molecule.find_by(id: sub_value[col_id]['value']['el_id'])
next unless find_mol.present?

sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_mol.molecule_svg_file)
sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_mol.molecule_svg_file) if find_mol&.molecule_svg_file&.present?
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
Expand Down
4 changes: 2 additions & 2 deletions lib/labimotion/entities/eln_element_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def properties
el.short_label
end
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] =
field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el.get_svg_path : File.join('/images', 'molecules', el.molecule_svg_file)
field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el.get_svg_path : File.join('/images', 'molecules', el&.molecule_svg_file || 'nosvg')
end

field_tables = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::TABLE }
Expand Down Expand Up @@ -88,7 +88,7 @@ def set_table(field, field_table_objs, obj)

case obj
when Labimotion::Prop::MOLECULE
sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_obj.molecule_svg_file)
sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_obj.molecule_svg_file) if find_obj&.molecule_svg_file&.present?
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
Expand Down
24 changes: 22 additions & 2 deletions lib/labimotion/entities/properties_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,27 @@ def properties

object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_label'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el.get_svg_path : File.join('/images', 'molecules', el.molecule_svg_file)
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el.get_svg_path : File.join('/images', 'molecules', el&.molecule_svg_file || 'nosvg')
end

field_sys_reactions = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::SYS_REACTION }
field_sys_reactions.each do |field|
idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
sid = field.dig('value', 'el_id')
next unless sid.present?

el = Reaction.find_by(id: sid)
if el.blank?
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = 'ERROR'
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] = ''
end

next unless el.present?
next unless object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value').present?

object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_label'] = el.short_label
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = el.short_label
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] = el.reaction_svg_file
end

field_tables = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::TABLE }
Expand Down Expand Up @@ -49,7 +69,7 @@ def set_table(field, field_table_objs, obj)

case obj
when Labimotion::Prop::MOLECULE
sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_obj.molecule_svg_file)
sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_obj.molecule_svg_file) if find_obj&.molecule_svg_file&.present?
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
Expand Down
4 changes: 2 additions & 2 deletions lib/labimotion/entities/segment_revision_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def properties

object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_label'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = el.short_label if field['type'] == Labimotion::FieldType::DRAG_SAMPLE
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el.get_svg_path : File.join('/images', 'molecules', el.molecule_svg_file)
object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el.get_svg_path : File.join('/images', 'molecules', el&.molecule_svg_file || 'nosvg')
end

field_tables = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::TABLE }
Expand All @@ -40,7 +40,7 @@ def properties
find_mol = Molecule.find_by(id: sub_value[col_id]['value']['el_id'])
next unless find_mol.present?

sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_mol.molecule_svg_file)
sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_mol.molecule_svg_file) if find_mol&.molecule_svg_file&.present?
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
Expand Down
10 changes: 8 additions & 2 deletions lib/labimotion/helpers/element_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'grape'
require 'labimotion/conf'
require 'labimotion/utils/utils'
# require 'labimotion/models/element_klass'
module Labimotion
Expand Down Expand Up @@ -33,7 +34,7 @@ def create_element_klass(current_user, params)
new_klass = Labimotion::ElementKlass.create!(attributes)
new_klass.reload
new_klass.create_klasses_revision(current_user)
klass_names_file = Rails.root.join('app/packs/klasses.json')
klass_names_file = Labimotion::KLASSES_JSON # Rails.root.join('app/packs/klasses.json')
klasses = Labimotion::ElementKlass.where(is_active: true)&.pluck(:name) || []
File.write(klass_names_file, klasses)
klasses
Expand Down Expand Up @@ -95,6 +96,7 @@ def create_element(current_user, params)
element.properties = update_sample_association(params[:properties], current_user, element)
element.container = update_datamodel(params[:container], current_user)
element.save!
update_element_labels(element, params[:user_labels], current_user.id)
element.save_segments(segments: params[:segments], current_user_id: current_user.id)
element
rescue StandardError => e
Expand All @@ -108,6 +110,9 @@ def update_element_by_id(current_user, params)
properties = update_sample_association(params[:properties], current_user, element)
params.delete(:container)
params.delete(:properties)
update_element_labels(element, params[:user_labels], current_user.id)
params.delete(:user_labels)

attributes = declared(params.except(:segments), include_missing: false)
properties['pkg'] = Labimotion::Utils.pkg(properties['pkg'])
if element.klass_uuid != properties['klass_uuid'] || element.properties != properties || element.name != params[:name]
Expand Down Expand Up @@ -288,6 +293,7 @@ def list_serialized_elements(params, current_user)
scope = scope.elements_created_time_to(Time.at(to) + 1.day) if to && by_created_at
scope = scope.elements_updated_time_from(Time.at(from)) if from && !by_created_at
scope = scope.elements_updated_time_to(Time.at(to) + 1.day) if to && !by_created_at
scope = scope.by_user_label(params[:user_label]) if params[:user_label]
scope
rescue StandardError => e
Labimotion.log_exception(e, current_user)
Expand Down Expand Up @@ -315,7 +321,7 @@ def validate_klass(attributes)
return { status: 'success', message: "The element: #{attributes['name']} has been created using version: #{attributes['version']}!" }
end
end

rescue StandardError => e
Labimotion.log_exception(e, current_user)
return { status: 'error', message: e.message }
Expand Down
7 changes: 4 additions & 3 deletions lib/labimotion/helpers/generic_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true
require 'grape'
require 'labimotion/conf'
require 'labimotion/utils/utils'
# Helper for associated sample
module Labimotion
Expand All @@ -23,7 +24,7 @@ def fetch_klass(name, id)
def deactivate_klass(params)
klz = fetch_klass(params[:klass], params[:id])
klz&.update!(is_active: params[:is_active])
generate_klass_file unless klz.class.name != 'ElementKlass'
generate_klass_file unless klz.class.name != 'Labimotion::ElementKlass'
klz
rescue StandardError => e
Labimotion.log_exception(e, current_user)
Expand All @@ -34,7 +35,7 @@ def delete_klass(params)
authenticate_admin!(params[:klass].gsub(/(Klass)/, 's').downcase)
klz = fetch_klass(params[:klass], params[:id])
klz&.destroy!
generate_klass_file unless klz.class.name != 'ElementKlass'
generate_klass_file unless klz.class.name != 'Labimotion::ElementKlass'
status 201
rescue StandardError => e
Labimotion.log_exception(e, current_user)
Expand Down Expand Up @@ -97,7 +98,7 @@ def list_klass_revisions(params)

###############
def generate_klass_file
klass_names_file = Rails.root.join('app/packs/klasses.json')
klass_names_file = Labimotion::KLASSES_JSON # Rails.root.join('app/packs/klasses.json')
klasses = Labimotion::ElementKlass.where(is_active: true)&.pluck(:name) || []
File.write(klass_names_file, klasses)
rescue StandardError => e
Expand Down
10 changes: 6 additions & 4 deletions lib/labimotion/helpers/param_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module ParamHelpers
optional :sync_time, type: DateTime, desc: 'Klass sync_time'
optional :version, type: String, desc: 'Klass version'
end

params :create_element_klass_params do
requires :name, type: String, desc: 'Element Klass Name'
requires :label, type: String, desc: 'Element Klass Label'
Expand All @@ -40,7 +40,7 @@ module ParamHelpers
optional :desc, type: String, desc: 'Element Klass Desc'
optional :place, type: String, desc: 'Element Klass Place'
end

## Element Params
params :create_element_params do
requires :element_klass, type: Hash
Expand All @@ -49,18 +49,20 @@ module ParamHelpers
optional :properties_release, type: Hash
optional :collection_id, type: Integer
requires :container, type: Hash
optional :user_labels, type: Array
optional :segments, type: Array, desc: 'Segments'
end

params :update_element_params do
requires :id, type: Integer, desc: 'element id'
optional :name, type: String
requires :properties, type: Hash
optional :properties_release, type: Hash
requires :container, type: Hash
optional :user_labels, type: Array
optional :segments, type: Array, desc: 'Segments'
end

## Segment Klass Params
params :upload_segment_klass_params do
requires :label, type: String, desc: 'Klass label'
Expand Down
4 changes: 2 additions & 2 deletions lib/labimotion/helpers/segment_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ def create_repo_klass(params, current_user, origin)
attributes['updated_by'] = current_user.id
attributes['sync_by'] = current_user.id
attributes['sync_time'] = DateTime.now
attr_klass = response.dig('element_klass', {}) # response['element_klass']
attr_klass = response['element_klass'] ## response.dig('element_klass', {}) # response['element_klass']
validate_klass(attributes, attr_klass)

rescue StandardError => e
Labimotion.log_exception(e, current_user)
raise e
Expand Down
29 changes: 29 additions & 0 deletions lib/labimotion/libs/attachment_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true
require 'labimotion/version'

module Labimotion
## ExportDataset
class AttachmentHandler
def self.copy(original_attach, element_id, element_type, current_user_id)
copy_attach = Attachment.new(
attachable_id: element_id,
attachable_type: element_type,
aasm_state: original_attach.aasm_state,
created_by: current_user_id,
created_for: current_user_id,
filename: original_attach.filename,
)
copy_attach.save

copy_io = original_attach.attachment_attacher.get.to_io
attacher = copy_attach.attachment_attacher
attacher.attach copy_io
copy_attach.file_path = copy_io.path
copy_attach.save

Usecases::Attachments::Copy.update_annotation(original_attach.id, copy_attach.id) if (original_attach.attachment_data && original_attach.attachment_data['derivatives'])
copy_attach
end

end
end
7 changes: 5 additions & 2 deletions lib/labimotion/libs/export_dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ def export(id)
type = "#{field['type']}-#{field['option_layers']}" if field['type'] == Labimotion::FieldType::SELECT || field['type'] == Labimotion::FieldType::SYSTEM_DEFINED

show_value = field['value'] =~ /\A\d+,\d+\z/ ? field['value']&.gsub(',', '.') : field['value']
sheet.add_row([' ', field['label'], nil, field['value_system'], field['field'], type, from_device, field['dkey'], nil].freeze)
ols_short_form = (field['ontology'] && field['ontology']['short_form']) || ''
ols_label = (field['ontology'] && field['ontology']['label']) || ''
ols_iri = (field['ontology'] && field['ontology']['iri']) || ''
sheet.add_row([' ', field['label'], nil, field['value_system'], field['field'], type, from_device, field['dkey'], nil, ols_short_form, ols_label, ols_iri].freeze)

case field['type']
when Labimotion::FieldType::SELECT
Expand Down Expand Up @@ -166,7 +169,7 @@ def spectra(id)
end

def header
['Layer Label', 'Field Label', 'Value', 'Unit', 'Name', 'Type', 'Source?', 'Source identifier', 'Source data'].freeze
['Layer Label', 'Field Label', 'Value', 'Unit', 'Name', 'Type', 'Source?', 'Source identifier', 'Source data', 'Ontology', 'Ontology Label', 'iri'].freeze
end

def read
Expand Down
Loading

0 comments on commit 67a1365

Please sign in to comment.