Class: Labimotion::PropertiesEntity

Inherits:
ApplicationEntity show all
Defined in:
lib/labimotion/entities/properties_entity.rb

Direct Known Subclasses

DatasetEntity, ElementEntity, SegmentEntity

Constant Summary

Constants inherited from ApplicationEntity

ApplicationEntity::CUSTOM_ENTITY_OPTIONS

Instance Method Summary collapse

Methods inherited from ApplicationEntity

expose!, expose_timestamps

Instance Method Details

#propertiesObject

TODO: Refactor this method to something more readable/understandable



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/labimotion/entities/properties_entity.rb', line 8

def properties
  (object&.properties.is_a?(Hash) && object.properties[Labimotion::Prop::LAYERS]&.keys || []).each do |key|


    field_sample_molecules = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_SAMPLE || ss['type'] == Labimotion::FieldType::DRAG_MOLECULE }
    field_sample_molecules.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 = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? Sample.find_by(id: sid) : Molecule.find_by(id: sid)
      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 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 || '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 }
    field_tables.each do |field|
      idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
      next unless field['sub_values'].present? && field[Labimotion::Prop::SUBFIELDS].present?

      field_table_molecules = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_MOLECULE }
      object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] = set_table(field, field_table_molecules, Labimotion::Prop::MOLECULE) if field_table_molecules.present?

      field_table_samples = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_SAMPLE }
      object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] = set_table(field, field_table_samples, Labimotion::Prop::SAMPLE) if field_table_samples.present?
    end

    field_vocs = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['is_voc'] == true }
    field_vocs.each do |field|
      idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
      next if idx.nil?

      case object.class.name
      when Labimotion::Prop::L_ELEMENT
        root_element = object
      when Labimotion::Prop::L_SEGMENT
        root_element = object&.element
      when Labimotion::Prop::L_DATASET
        root_element = object&.element&.root_element
      end

      case field['source']
      when Labimotion::Prop::ELEMENT
        if field['identifier'] == 'element.name'
          object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = root_element&.name
        end
      when Labimotion::Prop::SEGMENT
        segs = root_element&.segments&.select { |ss| field['source_id'] == ss.segment_klass&.identifier }
        next if segs.empty? || field['layer_id'].blank? || field['field_id'].blank?

        seg = segs&.first
        seg_fields = seg.properties.dig(Labimotion::Prop::LAYERS, field['layer_id'], Labimotion::Prop::FIELDS).select { |ff| ff['field'] == field['field_id'] }
        seg_field = seg_fields&.first
        object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = seg_field['value']
      when Labimotion::Prop::DATASET
        dk = DatasetKlass.find_by(identifier: field["source_id"])
        dk["ols_term_id"]
        anas = root_element.analyses.select { |ana| ana.["kind"].split("|")&.first&.strip == dk["ols_term_id"] }
        anas.each do |ana|
          cdss = ana.children
          cdss.each do |cds|
            next unless cds.dataset.present?

            ds_prop = cds.dataset.properties
            ds_fields = ds_prop.dig(Labimotion::Prop::LAYERS, field['layer_id'], Labimotion::Prop::FIELDS).select { |ff| ff['field'] == field['field_id'] }
            ds_field = ds_fields&.first
            if object.properties[Labimotion::Prop::LAYERS][key].present? && ds_field['value'].present?
              object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = ds_field['value']
            end
          end
        end
      end
    end
  end
  object.properties
end

#set_table(field, field_table_objs, obj) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/labimotion/entities/properties_entity.rb', line 109

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 if find_obj.blank?

      case obj
      when Labimotion::Prop::MOLECULE
        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
        sub_value[col_id]['value']['el_molecular_weight'] = find_obj.molecular_weight
      when Labimotion::Prop::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