Class: BiologicalAssociation

Overview

A BiologicalAssociation defines a (biological) relationship between two entities. It is an edge in the graph of biological relationships. The relationship can be between two Otus, an Otu and a Collection Object, or between two Collection Objects. For example 'Species Aus bus is the host_of individual A.'

Defined Under Namespace

Modules: DwcExtensions, GlobiExtensions

Constant Summary collapse

GRAPH_ENTRY_POINTS =
[:asserted_distributions].freeze

Constants included from Shared::IsIndexedBiologicalAssociation

Shared::IsIndexedBiologicalAssociation::DWC_TYPES, Shared::IsIndexedBiologicalAssociation::SOURCE_TYPES

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared::QueryBatchUpdate

#query_update

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_in_use?, #similar

Methods included from Shared::IsIndexedBiologicalAssociation

#biological_association_citation_year, #biological_association_citations, #biological_association_established_date, #biological_association_index_attributes, #biological_association_object_label, #biological_association_object_otu_id, #biological_association_object_properties, #biological_association_object_taxonomy_field, #biological_association_object_uuid, #biological_association_remarks, #biological_association_subject_label, #biological_association_subject_otu_id, #biological_association_subject_properties, #biological_association_subject_taxonomy_field, #biological_association_subject_uuid, #biological_relationship_uri, #get_biological_association_index, #set_biological_association_index

Methods included from Shared::AutoUuid

#create_object_uuid, #generate_uuid_if_required

Methods included from Shared::Depictions

#has_depictions?, #image_array=, #reject_depictions, #reject_images

Methods included from Shared::Confidences

#reject_confidences

Methods included from Shared::Notes

#concatenated_notes_string, #reject_notes

Methods included from Shared::DataAttributes

#import_attributes, #internal_attributes, #keyword_value_hash, #reject_data_attributes

Methods included from Shared::Identifiers

#dwc_occurrence_id, #identified?, #next_by_identifier, #previous_by_identifier, #reject_identifiers, #uri, #uuid, #visible_identifiers_for

Methods included from Shared::Tags

#reject_tags, #tag_with, #tagged?, #tagged_with?

Methods included from Shared::Citations

#cited?, #mark_citations_for_destruction, #nomenclature_date, #origin_citation_source_id, #reject_citations, #requires_citation?, #sources_by_topic_id

Methods included from SoftValidation

#clear_soft_validations, #fix_for, #fix_soft_validations, #soft_fixed?, #soft_valid?, #soft_validate, #soft_validated?, #soft_validations, #soft_validators

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#biological_association_object_idInteger

Returns Rails polymorphic, id of the object.

Returns:

  • (Integer)

    Rails polymorphic, id of the object



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
108
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'app/models/biological_association.rb', line 29

class BiologicalAssociation < ApplicationRecord
  include Housekeeping
  include SoftValidation
  include Shared::Citations
  include Shared::Tags
  include Shared::Identifiers
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Depictions
  include Shared::AutoUuid
  include Shared::AssertedDistributions
  include Shared::IsIndexedBiologicalAssociation
  include Shared::IsData

  include BiologicalAssociation::GlobiExtensions
  include BiologicalAssociation::DwcExtensions

  include Shared::QueryBatchUpdate

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  belongs_to :biological_relationship, inverse_of: :biological_associations

  has_many :subject_biological_relationship_types, through: :biological_relationship
  has_many :object_biological_relationship_types, through: :biological_relationship

  has_many :subject_biological_properties, through: :subject_biological_relationship_types, source: :biological_property
  has_many :object_biological_properties, through: :object_biological_relationship_types, source: :biological_property

  belongs_to :biological_association_subject, polymorphic: true, inverse_of: :biological_associations
  belongs_to :biological_association_object, polymorphic: true, inverse_of: :related_biological_associations

  has_many :biological_associations_biological_associations_graphs, inverse_of: :biological_association, dependent: :destroy
  has_many :biological_associations_graphs, through: :biological_associations_biological_associations_graphs, inverse_of: :biological_associations

  validates_presence_of :biological_relationship
  validates_presence_of :biological_association_subject
  validates_presence_of :biological_association_object

  validate :association_is_unique

  validate :biological_association_subject_type_is_allowed
  validate :biological_association_object_type_is_allowed

  attr_accessor :subject_global_id
  attr_accessor :object_global_id # TODO: this is badly named

  attr_accessor :rotate

  def rotate=(value)
    s = self.biological_association_subject
    o = self.biological_association_object

    self.biological_association_subject = o
    self.biological_association_object = s
  end

  def subject_global_id=(value)
    o = GlobalID::Locator.locate(value)
    write_attribute(:biological_association_subject_id, o.id)
    write_attribute(:biological_association_subject_type, o.metamorphosize.class.name)
  end

  def object_global_id=(value)
    o = GlobalID::Locator.locate(value)
    write_attribute(:biological_association_object_id, o.id)
    write_attribute(:biological_association_object_type, o.metamorphosize.class.name)
  end

  # TODO: Why?! this is just biological_association.biological_association_subject_type
  def subject_class_name
    biological_association_subject.try(:class).base_class.name
  end

  # TODO: Why?! this is just biological_association.biological_association_object_type
  def object_class_name
    biological_association_object.try(:class).base_class.name
  end

  # !! You can not set with this method
  def subject
    biological_association_subject
  end

  # !! You can not set with this method
  def object
    biological_association_object
  end

  # @return [Array of Integer]
  #   OTU ids reachable through this BiologicalAssociation's subject and object.
  #   Handles direct Otu subject/object, CollectionObject and FieldOccurrence
  #   (via position=1 taxon determination), and AnatomicalPart (via cached_otu_id).
  def otu_ids
    ids = []
    [:subject, :object].each do |role|
      type = send("biological_association_#{role}_type")
      id   = send("biological_association_#{role}_id")
      case type
      when 'Otu'
        ids << id
      when 'CollectionObject', 'FieldOccurrence'
        otu_id = ::TaxonDetermination
          .where(taxon_determination_object_type: type, taxon_determination_object_id: id, position: 1)
          .pick(:otu_id)
        ids << otu_id if otu_id
      when 'AnatomicalPart'
        otu_id = ::AnatomicalPart.where(id:).pick(:cached_otu_id)
        ids << otu_id if otu_id
      end
    end
    ids.uniq
  end

  # @return [Array of Integer]
  #   OTU ids reachable across a collection of BiologicalAssociations,
  #   batching queries by type to avoid N+1.
  def self.collect_otu_ids(biological_associations)
    bas = biological_associations.to_a
    ids = []

    [:subject, :object].each do |role|
      type_attr = "biological_association_#{role}_type"
      id_attr   = "biological_association_#{role}_id"
      by_type   = bas.group_by { |ba| ba.send(type_attr) }

      (by_type['Otu'] || []).each { |ba| ids << ba.send(id_attr) }

      %w[CollectionObject FieldOccurrence].each do |type|
        next unless (typed_bas = by_type[type])
        typed_ids = typed_bas.map { |ba| ba.send(id_attr) }
        ids.concat(
          ::TaxonDetermination
            .where(taxon_determination_object_type: type, taxon_determination_object_id: typed_ids, position: 1)
            .pluck(:otu_id)
        )
      end

      if (ap_bas = by_type['AnatomicalPart'])
        ap_ids = ap_bas.map { |ba| ba.send(id_attr) }
        ids.concat(::AnatomicalPart.where(id: ap_ids).pluck(:cached_otu_id).compact)
      end
    end

    ids.uniq
  end

  class << self

    def set_batch_cap(request)
      a = request.filter
      total = a.all.pluck(:biological_relationship_id).uniq

      cap = 0

      case total.size
      when 1
        cap = 5000
        request.cap_reason = 'Maximum allowed.'
      when 2
        cap = 2000
        request.cap_reason = 'Maximum allowed when 2 biological relationships present.'
      else
        cap = 25
        request.cap_reason = 'Maximum allowed when 3 or more biological relationships present.'
      end

      request.cap = cap
      request
    end

    def batch_update(params)
      request = QueryBatchRequest.new(
        klass: 'BiologicalAssociation',
        object_filter_params: params[:biological_association_query],
        object_params: params[:biological_association],
        async_cutoff: (params[:async_cutoff] || 26),
        preview: params[:preview],
        user_id: params[:user_id],
        project_id: params[:project_id]
      )

      set_batch_cap(request)
      query_batch_update(request)
    end

  end

  def dwc_extension_select
    BiologicalAssociation
      .joins("LEFT JOIN identifiers id_s ON id_s.identifier_object_type = biological_associations.biological_associations_subject_type AND ids_s.type = 'Identifier::Global::Uuid'" )
      .joins("LEFT JOIN identifiers id_o ON id_o.identifier_object_type = biological_associations.biological_associations_object_type AND ids_o.type = 'Identifier::Global::Uuid'" )
      .joins("LEFT JOIN identifiers id_r ON id_o.identifier_object_type = 'BiologicalRelationship' AND idr_.identifier_object_id = biological_associations.biological_relationship_id AND ids_r.type = 'Identifier::Global::Uri'" )
  end

  # @return [ActiveRecord::Relation]
  def targeted_join(target: 'subject', target_class: ::Otu)
    a = arel_table
    b = target_class.arel_table

    j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
    joins(j.join_sources)
  end

  # @return [ActiveRecord::Relation]
  def targeted_join2(target: 'subject', target_class: ::Otu)
    a = arel_table
    b = target_class.arel_table

    j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
  end

  # Not used
  # @return [ActiveRecord::Relation]
  def targeted_left_join(target: 'subject', target_class: ::Otu )
    a = arel_table
    b = target_class.arel_table

    j = a.join(b, Arel::Nodes::OuterJoin).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
    joins(j.join_sources)
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return BiologicalAssociation.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('BiologicalAssociation')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = BiologicalAssociation.arel_table

    case used_on
    when 'AssertedDistribution'
      BiologicalAssociation.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: BiologicalAssociation.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  end

  private

  def association_is_unique
    if a = BiologicalAssociation.where.not(id:).where(
        biological_association_subject:,
        biological_association_object:,
        biological_relationship:
    ).first
      # For unify purposes, self has changed either subject or object, a is the
      # identical BA we will perhaps unify with.
      if will_save_change_to_biological_association_subject_id?
        errors.add(:biological_association_subject, 'has already been taken')
      elsif will_save_change_to_biological_association_object_id?
        errors.add(:biological_association_object, 'has already been taken')
      else
        errors.add(:biological_association, 'already exists')
      end
    end
  end


  def biological_association_subject_type_is_allowed
    errors.add(:biological_association_subject_type, 'is not permitted') unless biological_association_subject && biological_association_subject.class.is_biologically_relatable?
  end

  def biological_association_object_type_is_allowed
    errors.add(:biological_association_object_type, 'is not permitted') unless biological_association_object && biological_association_object.class.is_biologically_relatable?
  end
end

#biological_association_object_typeString

Returns Rails polymorphic, type of the object (e.g. CollectionObject).

Returns:

  • (String)

    Rails polymorphic, type of the object (e.g. CollectionObject)



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
108
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'app/models/biological_association.rb', line 29

class BiologicalAssociation < ApplicationRecord
  include Housekeeping
  include SoftValidation
  include Shared::Citations
  include Shared::Tags
  include Shared::Identifiers
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Depictions
  include Shared::AutoUuid
  include Shared::AssertedDistributions
  include Shared::IsIndexedBiologicalAssociation
  include Shared::IsData

  include BiologicalAssociation::GlobiExtensions
  include BiologicalAssociation::DwcExtensions

  include Shared::QueryBatchUpdate

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  belongs_to :biological_relationship, inverse_of: :biological_associations

  has_many :subject_biological_relationship_types, through: :biological_relationship
  has_many :object_biological_relationship_types, through: :biological_relationship

  has_many :subject_biological_properties, through: :subject_biological_relationship_types, source: :biological_property
  has_many :object_biological_properties, through: :object_biological_relationship_types, source: :biological_property

  belongs_to :biological_association_subject, polymorphic: true, inverse_of: :biological_associations
  belongs_to :biological_association_object, polymorphic: true, inverse_of: :related_biological_associations

  has_many :biological_associations_biological_associations_graphs, inverse_of: :biological_association, dependent: :destroy
  has_many :biological_associations_graphs, through: :biological_associations_biological_associations_graphs, inverse_of: :biological_associations

  validates_presence_of :biological_relationship
  validates_presence_of :biological_association_subject
  validates_presence_of :biological_association_object

  validate :association_is_unique

  validate :biological_association_subject_type_is_allowed
  validate :biological_association_object_type_is_allowed

  attr_accessor :subject_global_id
  attr_accessor :object_global_id # TODO: this is badly named

  attr_accessor :rotate

  def rotate=(value)
    s = self.biological_association_subject
    o = self.biological_association_object

    self.biological_association_subject = o
    self.biological_association_object = s
  end

  def subject_global_id=(value)
    o = GlobalID::Locator.locate(value)
    write_attribute(:biological_association_subject_id, o.id)
    write_attribute(:biological_association_subject_type, o.metamorphosize.class.name)
  end

  def object_global_id=(value)
    o = GlobalID::Locator.locate(value)
    write_attribute(:biological_association_object_id, o.id)
    write_attribute(:biological_association_object_type, o.metamorphosize.class.name)
  end

  # TODO: Why?! this is just biological_association.biological_association_subject_type
  def subject_class_name
    biological_association_subject.try(:class).base_class.name
  end

  # TODO: Why?! this is just biological_association.biological_association_object_type
  def object_class_name
    biological_association_object.try(:class).base_class.name
  end

  # !! You can not set with this method
  def subject
    biological_association_subject
  end

  # !! You can not set with this method
  def object
    biological_association_object
  end

  # @return [Array of Integer]
  #   OTU ids reachable through this BiologicalAssociation's subject and object.
  #   Handles direct Otu subject/object, CollectionObject and FieldOccurrence
  #   (via position=1 taxon determination), and AnatomicalPart (via cached_otu_id).
  def otu_ids
    ids = []
    [:subject, :object].each do |role|
      type = send("biological_association_#{role}_type")
      id   = send("biological_association_#{role}_id")
      case type
      when 'Otu'
        ids << id
      when 'CollectionObject', 'FieldOccurrence'
        otu_id = ::TaxonDetermination
          .where(taxon_determination_object_type: type, taxon_determination_object_id: id, position: 1)
          .pick(:otu_id)
        ids << otu_id if otu_id
      when 'AnatomicalPart'
        otu_id = ::AnatomicalPart.where(id:).pick(:cached_otu_id)
        ids << otu_id if otu_id
      end
    end
    ids.uniq
  end

  # @return [Array of Integer]
  #   OTU ids reachable across a collection of BiologicalAssociations,
  #   batching queries by type to avoid N+1.
  def self.collect_otu_ids(biological_associations)
    bas = biological_associations.to_a
    ids = []

    [:subject, :object].each do |role|
      type_attr = "biological_association_#{role}_type"
      id_attr   = "biological_association_#{role}_id"
      by_type   = bas.group_by { |ba| ba.send(type_attr) }

      (by_type['Otu'] || []).each { |ba| ids << ba.send(id_attr) }

      %w[CollectionObject FieldOccurrence].each do |type|
        next unless (typed_bas = by_type[type])
        typed_ids = typed_bas.map { |ba| ba.send(id_attr) }
        ids.concat(
          ::TaxonDetermination
            .where(taxon_determination_object_type: type, taxon_determination_object_id: typed_ids, position: 1)
            .pluck(:otu_id)
        )
      end

      if (ap_bas = by_type['AnatomicalPart'])
        ap_ids = ap_bas.map { |ba| ba.send(id_attr) }
        ids.concat(::AnatomicalPart.where(id: ap_ids).pluck(:cached_otu_id).compact)
      end
    end

    ids.uniq
  end

  class << self

    def set_batch_cap(request)
      a = request.filter
      total = a.all.pluck(:biological_relationship_id).uniq

      cap = 0

      case total.size
      when 1
        cap = 5000
        request.cap_reason = 'Maximum allowed.'
      when 2
        cap = 2000
        request.cap_reason = 'Maximum allowed when 2 biological relationships present.'
      else
        cap = 25
        request.cap_reason = 'Maximum allowed when 3 or more biological relationships present.'
      end

      request.cap = cap
      request
    end

    def batch_update(params)
      request = QueryBatchRequest.new(
        klass: 'BiologicalAssociation',
        object_filter_params: params[:biological_association_query],
        object_params: params[:biological_association],
        async_cutoff: (params[:async_cutoff] || 26),
        preview: params[:preview],
        user_id: params[:user_id],
        project_id: params[:project_id]
      )

      set_batch_cap(request)
      query_batch_update(request)
    end

  end

  def dwc_extension_select
    BiologicalAssociation
      .joins("LEFT JOIN identifiers id_s ON id_s.identifier_object_type = biological_associations.biological_associations_subject_type AND ids_s.type = 'Identifier::Global::Uuid'" )
      .joins("LEFT JOIN identifiers id_o ON id_o.identifier_object_type = biological_associations.biological_associations_object_type AND ids_o.type = 'Identifier::Global::Uuid'" )
      .joins("LEFT JOIN identifiers id_r ON id_o.identifier_object_type = 'BiologicalRelationship' AND idr_.identifier_object_id = biological_associations.biological_relationship_id AND ids_r.type = 'Identifier::Global::Uri'" )
  end

  # @return [ActiveRecord::Relation]
  def targeted_join(target: 'subject', target_class: ::Otu)
    a = arel_table
    b = target_class.arel_table

    j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
    joins(j.join_sources)
  end

  # @return [ActiveRecord::Relation]
  def targeted_join2(target: 'subject', target_class: ::Otu)
    a = arel_table
    b = target_class.arel_table

    j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
  end

  # Not used
  # @return [ActiveRecord::Relation]
  def targeted_left_join(target: 'subject', target_class: ::Otu )
    a = arel_table
    b = target_class.arel_table

    j = a.join(b, Arel::Nodes::OuterJoin).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
    joins(j.join_sources)
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return BiologicalAssociation.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('BiologicalAssociation')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = BiologicalAssociation.arel_table

    case used_on
    when 'AssertedDistribution'
      BiologicalAssociation.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: BiologicalAssociation.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  end

  private

  def association_is_unique
    if a = BiologicalAssociation.where.not(id:).where(
        biological_association_subject:,
        biological_association_object:,
        biological_relationship:
    ).first
      # For unify purposes, self has changed either subject or object, a is the
      # identical BA we will perhaps unify with.
      if will_save_change_to_biological_association_subject_id?
        errors.add(:biological_association_subject, 'has already been taken')
      elsif will_save_change_to_biological_association_object_id?
        errors.add(:biological_association_object, 'has already been taken')
      else
        errors.add(:biological_association, 'already exists')
      end
    end
  end


  def biological_association_subject_type_is_allowed
    errors.add(:biological_association_subject_type, 'is not permitted') unless biological_association_subject && biological_association_subject.class.is_biologically_relatable?
  end

  def biological_association_object_type_is_allowed
    errors.add(:biological_association_object_type, 'is not permitted') unless biological_association_object && biological_association_object.class.is_biologically_relatable?
  end
end

#biological_association_subject_idInteger

Returns Rails polymorphic, id of the subject of the relationship.

Returns:

  • (Integer)

    Rails polymorphic, id of the subject of the relationship



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
108
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'app/models/biological_association.rb', line 29

class BiologicalAssociation < ApplicationRecord
  include Housekeeping
  include SoftValidation
  include Shared::Citations
  include Shared::Tags
  include Shared::Identifiers
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Depictions
  include Shared::AutoUuid
  include Shared::AssertedDistributions
  include Shared::IsIndexedBiologicalAssociation
  include Shared::IsData

  include BiologicalAssociation::GlobiExtensions
  include BiologicalAssociation::DwcExtensions

  include Shared::QueryBatchUpdate

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  belongs_to :biological_relationship, inverse_of: :biological_associations

  has_many :subject_biological_relationship_types, through: :biological_relationship
  has_many :object_biological_relationship_types, through: :biological_relationship

  has_many :subject_biological_properties, through: :subject_biological_relationship_types, source: :biological_property
  has_many :object_biological_properties, through: :object_biological_relationship_types, source: :biological_property

  belongs_to :biological_association_subject, polymorphic: true, inverse_of: :biological_associations
  belongs_to :biological_association_object, polymorphic: true, inverse_of: :related_biological_associations

  has_many :biological_associations_biological_associations_graphs, inverse_of: :biological_association, dependent: :destroy
  has_many :biological_associations_graphs, through: :biological_associations_biological_associations_graphs, inverse_of: :biological_associations

  validates_presence_of :biological_relationship
  validates_presence_of :biological_association_subject
  validates_presence_of :biological_association_object

  validate :association_is_unique

  validate :biological_association_subject_type_is_allowed
  validate :biological_association_object_type_is_allowed

  attr_accessor :subject_global_id
  attr_accessor :object_global_id # TODO: this is badly named

  attr_accessor :rotate

  def rotate=(value)
    s = self.biological_association_subject
    o = self.biological_association_object

    self.biological_association_subject = o
    self.biological_association_object = s
  end

  def subject_global_id=(value)
    o = GlobalID::Locator.locate(value)
    write_attribute(:biological_association_subject_id, o.id)
    write_attribute(:biological_association_subject_type, o.metamorphosize.class.name)
  end

  def object_global_id=(value)
    o = GlobalID::Locator.locate(value)
    write_attribute(:biological_association_object_id, o.id)
    write_attribute(:biological_association_object_type, o.metamorphosize.class.name)
  end

  # TODO: Why?! this is just biological_association.biological_association_subject_type
  def subject_class_name
    biological_association_subject.try(:class).base_class.name
  end

  # TODO: Why?! this is just biological_association.biological_association_object_type
  def object_class_name
    biological_association_object.try(:class).base_class.name
  end

  # !! You can not set with this method
  def subject
    biological_association_subject
  end

  # !! You can not set with this method
  def object
    biological_association_object
  end

  # @return [Array of Integer]
  #   OTU ids reachable through this BiologicalAssociation's subject and object.
  #   Handles direct Otu subject/object, CollectionObject and FieldOccurrence
  #   (via position=1 taxon determination), and AnatomicalPart (via cached_otu_id).
  def otu_ids
    ids = []
    [:subject, :object].each do |role|
      type = send("biological_association_#{role}_type")
      id   = send("biological_association_#{role}_id")
      case type
      when 'Otu'
        ids << id
      when 'CollectionObject', 'FieldOccurrence'
        otu_id = ::TaxonDetermination
          .where(taxon_determination_object_type: type, taxon_determination_object_id: id, position: 1)
          .pick(:otu_id)
        ids << otu_id if otu_id
      when 'AnatomicalPart'
        otu_id = ::AnatomicalPart.where(id:).pick(:cached_otu_id)
        ids << otu_id if otu_id
      end
    end
    ids.uniq
  end

  # @return [Array of Integer]
  #   OTU ids reachable across a collection of BiologicalAssociations,
  #   batching queries by type to avoid N+1.
  def self.collect_otu_ids(biological_associations)
    bas = biological_associations.to_a
    ids = []

    [:subject, :object].each do |role|
      type_attr = "biological_association_#{role}_type"
      id_attr   = "biological_association_#{role}_id"
      by_type   = bas.group_by { |ba| ba.send(type_attr) }

      (by_type['Otu'] || []).each { |ba| ids << ba.send(id_attr) }

      %w[CollectionObject FieldOccurrence].each do |type|
        next unless (typed_bas = by_type[type])
        typed_ids = typed_bas.map { |ba| ba.send(id_attr) }
        ids.concat(
          ::TaxonDetermination
            .where(taxon_determination_object_type: type, taxon_determination_object_id: typed_ids, position: 1)
            .pluck(:otu_id)
        )
      end

      if (ap_bas = by_type['AnatomicalPart'])
        ap_ids = ap_bas.map { |ba| ba.send(id_attr) }
        ids.concat(::AnatomicalPart.where(id: ap_ids).pluck(:cached_otu_id).compact)
      end
    end

    ids.uniq
  end

  class << self

    def set_batch_cap(request)
      a = request.filter
      total = a.all.pluck(:biological_relationship_id).uniq

      cap = 0

      case total.size
      when 1
        cap = 5000
        request.cap_reason = 'Maximum allowed.'
      when 2
        cap = 2000
        request.cap_reason = 'Maximum allowed when 2 biological relationships present.'
      else
        cap = 25
        request.cap_reason = 'Maximum allowed when 3 or more biological relationships present.'
      end

      request.cap = cap
      request
    end

    def batch_update(params)
      request = QueryBatchRequest.new(
        klass: 'BiologicalAssociation',
        object_filter_params: params[:biological_association_query],
        object_params: params[:biological_association],
        async_cutoff: (params[:async_cutoff] || 26),
        preview: params[:preview],
        user_id: params[:user_id],
        project_id: params[:project_id]
      )

      set_batch_cap(request)
      query_batch_update(request)
    end

  end

  def dwc_extension_select
    BiologicalAssociation
      .joins("LEFT JOIN identifiers id_s ON id_s.identifier_object_type = biological_associations.biological_associations_subject_type AND ids_s.type = 'Identifier::Global::Uuid'" )
      .joins("LEFT JOIN identifiers id_o ON id_o.identifier_object_type = biological_associations.biological_associations_object_type AND ids_o.type = 'Identifier::Global::Uuid'" )
      .joins("LEFT JOIN identifiers id_r ON id_o.identifier_object_type = 'BiologicalRelationship' AND idr_.identifier_object_id = biological_associations.biological_relationship_id AND ids_r.type = 'Identifier::Global::Uri'" )
  end

  # @return [ActiveRecord::Relation]
  def targeted_join(target: 'subject', target_class: ::Otu)
    a = arel_table
    b = target_class.arel_table

    j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
    joins(j.join_sources)
  end

  # @return [ActiveRecord::Relation]
  def targeted_join2(target: 'subject', target_class: ::Otu)
    a = arel_table
    b = target_class.arel_table

    j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
  end

  # Not used
  # @return [ActiveRecord::Relation]
  def targeted_left_join(target: 'subject', target_class: ::Otu )
    a = arel_table
    b = target_class.arel_table

    j = a.join(b, Arel::Nodes::OuterJoin).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
    joins(j.join_sources)
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return BiologicalAssociation.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('BiologicalAssociation')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = BiologicalAssociation.arel_table

    case used_on
    when 'AssertedDistribution'
      BiologicalAssociation.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: BiologicalAssociation.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  end

  private

  def association_is_unique
    if a = BiologicalAssociation.where.not(id:).where(
        biological_association_subject:,
        biological_association_object:,
        biological_relationship:
    ).first
      # For unify purposes, self has changed either subject or object, a is the
      # identical BA we will perhaps unify with.
      if will_save_change_to_biological_association_subject_id?
        errors.add(:biological_association_subject, 'has already been taken')
      elsif will_save_change_to_biological_association_object_id?
        errors.add(:biological_association_object, 'has already been taken')
      else
        errors.add(:biological_association, 'already exists')
      end
    end
  end


  def biological_association_subject_type_is_allowed
    errors.add(:biological_association_subject_type, 'is not permitted') unless biological_association_subject && biological_association_subject.class.is_biologically_relatable?
  end

  def biological_association_object_type_is_allowed
    errors.add(:biological_association_object_type, 'is not permitted') unless biological_association_object && biological_association_object.class.is_biologically_relatable?
  end
end

#biological_association_subject_typeString

Returns Rails polymorphic, type fo the subject (e.g. Otu).

Returns:

  • (String)

    Rails polymorphic, type fo the subject (e.g. Otu)



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
108
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'app/models/biological_association.rb', line 29

class BiologicalAssociation < ApplicationRecord
  include Housekeeping
  include SoftValidation
  include Shared::Citations
  include Shared::Tags
  include Shared::Identifiers
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Depictions
  include Shared::AutoUuid
  include Shared::AssertedDistributions
  include Shared::IsIndexedBiologicalAssociation
  include Shared::IsData

  include BiologicalAssociation::GlobiExtensions
  include BiologicalAssociation::DwcExtensions

  include Shared::QueryBatchUpdate

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  belongs_to :biological_relationship, inverse_of: :biological_associations

  has_many :subject_biological_relationship_types, through: :biological_relationship
  has_many :object_biological_relationship_types, through: :biological_relationship

  has_many :subject_biological_properties, through: :subject_biological_relationship_types, source: :biological_property
  has_many :object_biological_properties, through: :object_biological_relationship_types, source: :biological_property

  belongs_to :biological_association_subject, polymorphic: true, inverse_of: :biological_associations
  belongs_to :biological_association_object, polymorphic: true, inverse_of: :related_biological_associations

  has_many :biological_associations_biological_associations_graphs, inverse_of: :biological_association, dependent: :destroy
  has_many :biological_associations_graphs, through: :biological_associations_biological_associations_graphs, inverse_of: :biological_associations

  validates_presence_of :biological_relationship
  validates_presence_of :biological_association_subject
  validates_presence_of :biological_association_object

  validate :association_is_unique

  validate :biological_association_subject_type_is_allowed
  validate :biological_association_object_type_is_allowed

  attr_accessor :subject_global_id
  attr_accessor :object_global_id # TODO: this is badly named

  attr_accessor :rotate

  def rotate=(value)
    s = self.biological_association_subject
    o = self.biological_association_object

    self.biological_association_subject = o
    self.biological_association_object = s
  end

  def subject_global_id=(value)
    o = GlobalID::Locator.locate(value)
    write_attribute(:biological_association_subject_id, o.id)
    write_attribute(:biological_association_subject_type, o.metamorphosize.class.name)
  end

  def object_global_id=(value)
    o = GlobalID::Locator.locate(value)
    write_attribute(:biological_association_object_id, o.id)
    write_attribute(:biological_association_object_type, o.metamorphosize.class.name)
  end

  # TODO: Why?! this is just biological_association.biological_association_subject_type
  def subject_class_name
    biological_association_subject.try(:class).base_class.name
  end

  # TODO: Why?! this is just biological_association.biological_association_object_type
  def object_class_name
    biological_association_object.try(:class).base_class.name
  end

  # !! You can not set with this method
  def subject
    biological_association_subject
  end

  # !! You can not set with this method
  def object
    biological_association_object
  end

  # @return [Array of Integer]
  #   OTU ids reachable through this BiologicalAssociation's subject and object.
  #   Handles direct Otu subject/object, CollectionObject and FieldOccurrence
  #   (via position=1 taxon determination), and AnatomicalPart (via cached_otu_id).
  def otu_ids
    ids = []
    [:subject, :object].each do |role|
      type = send("biological_association_#{role}_type")
      id   = send("biological_association_#{role}_id")
      case type
      when 'Otu'
        ids << id
      when 'CollectionObject', 'FieldOccurrence'
        otu_id = ::TaxonDetermination
          .where(taxon_determination_object_type: type, taxon_determination_object_id: id, position: 1)
          .pick(:otu_id)
        ids << otu_id if otu_id
      when 'AnatomicalPart'
        otu_id = ::AnatomicalPart.where(id:).pick(:cached_otu_id)
        ids << otu_id if otu_id
      end
    end
    ids.uniq
  end

  # @return [Array of Integer]
  #   OTU ids reachable across a collection of BiologicalAssociations,
  #   batching queries by type to avoid N+1.
  def self.collect_otu_ids(biological_associations)
    bas = biological_associations.to_a
    ids = []

    [:subject, :object].each do |role|
      type_attr = "biological_association_#{role}_type"
      id_attr   = "biological_association_#{role}_id"
      by_type   = bas.group_by { |ba| ba.send(type_attr) }

      (by_type['Otu'] || []).each { |ba| ids << ba.send(id_attr) }

      %w[CollectionObject FieldOccurrence].each do |type|
        next unless (typed_bas = by_type[type])
        typed_ids = typed_bas.map { |ba| ba.send(id_attr) }
        ids.concat(
          ::TaxonDetermination
            .where(taxon_determination_object_type: type, taxon_determination_object_id: typed_ids, position: 1)
            .pluck(:otu_id)
        )
      end

      if (ap_bas = by_type['AnatomicalPart'])
        ap_ids = ap_bas.map { |ba| ba.send(id_attr) }
        ids.concat(::AnatomicalPart.where(id: ap_ids).pluck(:cached_otu_id).compact)
      end
    end

    ids.uniq
  end

  class << self

    def set_batch_cap(request)
      a = request.filter
      total = a.all.pluck(:biological_relationship_id).uniq

      cap = 0

      case total.size
      when 1
        cap = 5000
        request.cap_reason = 'Maximum allowed.'
      when 2
        cap = 2000
        request.cap_reason = 'Maximum allowed when 2 biological relationships present.'
      else
        cap = 25
        request.cap_reason = 'Maximum allowed when 3 or more biological relationships present.'
      end

      request.cap = cap
      request
    end

    def batch_update(params)
      request = QueryBatchRequest.new(
        klass: 'BiologicalAssociation',
        object_filter_params: params[:biological_association_query],
        object_params: params[:biological_association],
        async_cutoff: (params[:async_cutoff] || 26),
        preview: params[:preview],
        user_id: params[:user_id],
        project_id: params[:project_id]
      )

      set_batch_cap(request)
      query_batch_update(request)
    end

  end

  def dwc_extension_select
    BiologicalAssociation
      .joins("LEFT JOIN identifiers id_s ON id_s.identifier_object_type = biological_associations.biological_associations_subject_type AND ids_s.type = 'Identifier::Global::Uuid'" )
      .joins("LEFT JOIN identifiers id_o ON id_o.identifier_object_type = biological_associations.biological_associations_object_type AND ids_o.type = 'Identifier::Global::Uuid'" )
      .joins("LEFT JOIN identifiers id_r ON id_o.identifier_object_type = 'BiologicalRelationship' AND idr_.identifier_object_id = biological_associations.biological_relationship_id AND ids_r.type = 'Identifier::Global::Uri'" )
  end

  # @return [ActiveRecord::Relation]
  def targeted_join(target: 'subject', target_class: ::Otu)
    a = arel_table
    b = target_class.arel_table

    j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
    joins(j.join_sources)
  end

  # @return [ActiveRecord::Relation]
  def targeted_join2(target: 'subject', target_class: ::Otu)
    a = arel_table
    b = target_class.arel_table

    j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
  end

  # Not used
  # @return [ActiveRecord::Relation]
  def targeted_left_join(target: 'subject', target_class: ::Otu )
    a = arel_table
    b = target_class.arel_table

    j = a.join(b, Arel::Nodes::OuterJoin).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
    joins(j.join_sources)
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return BiologicalAssociation.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('BiologicalAssociation')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = BiologicalAssociation.arel_table

    case used_on
    when 'AssertedDistribution'
      BiologicalAssociation.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: BiologicalAssociation.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  end

  private

  def association_is_unique
    if a = BiologicalAssociation.where.not(id:).where(
        biological_association_subject:,
        biological_association_object:,
        biological_relationship:
    ).first
      # For unify purposes, self has changed either subject or object, a is the
      # identical BA we will perhaps unify with.
      if will_save_change_to_biological_association_subject_id?
        errors.add(:biological_association_subject, 'has already been taken')
      elsif will_save_change_to_biological_association_object_id?
        errors.add(:biological_association_object, 'has already been taken')
      else
        errors.add(:biological_association, 'already exists')
      end
    end
  end


  def biological_association_subject_type_is_allowed
    errors.add(:biological_association_subject_type, 'is not permitted') unless biological_association_subject && biological_association_subject.class.is_biologically_relatable?
  end

  def biological_association_object_type_is_allowed
    errors.add(:biological_association_object_type, 'is not permitted') unless biological_association_object && biological_association_object.class.is_biologically_relatable?
  end
end

#biological_relationship_idInteger

Returns the BiologicalRelationship id.

Returns:

  • (Integer)

    the BiologicalRelationship id



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
108
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'app/models/biological_association.rb', line 29

class BiologicalAssociation < ApplicationRecord
  include Housekeeping
  include SoftValidation
  include Shared::Citations
  include Shared::Tags
  include Shared::Identifiers
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Depictions
  include Shared::AutoUuid
  include Shared::AssertedDistributions
  include Shared::IsIndexedBiologicalAssociation
  include Shared::IsData

  include BiologicalAssociation::GlobiExtensions
  include BiologicalAssociation::DwcExtensions

  include Shared::QueryBatchUpdate

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  belongs_to :biological_relationship, inverse_of: :biological_associations

  has_many :subject_biological_relationship_types, through: :biological_relationship
  has_many :object_biological_relationship_types, through: :biological_relationship

  has_many :subject_biological_properties, through: :subject_biological_relationship_types, source: :biological_property
  has_many :object_biological_properties, through: :object_biological_relationship_types, source: :biological_property

  belongs_to :biological_association_subject, polymorphic: true, inverse_of: :biological_associations
  belongs_to :biological_association_object, polymorphic: true, inverse_of: :related_biological_associations

  has_many :biological_associations_biological_associations_graphs, inverse_of: :biological_association, dependent: :destroy
  has_many :biological_associations_graphs, through: :biological_associations_biological_associations_graphs, inverse_of: :biological_associations

  validates_presence_of :biological_relationship
  validates_presence_of :biological_association_subject
  validates_presence_of :biological_association_object

  validate :association_is_unique

  validate :biological_association_subject_type_is_allowed
  validate :biological_association_object_type_is_allowed

  attr_accessor :subject_global_id
  attr_accessor :object_global_id # TODO: this is badly named

  attr_accessor :rotate

  def rotate=(value)
    s = self.biological_association_subject
    o = self.biological_association_object

    self.biological_association_subject = o
    self.biological_association_object = s
  end

  def subject_global_id=(value)
    o = GlobalID::Locator.locate(value)
    write_attribute(:biological_association_subject_id, o.id)
    write_attribute(:biological_association_subject_type, o.metamorphosize.class.name)
  end

  def object_global_id=(value)
    o = GlobalID::Locator.locate(value)
    write_attribute(:biological_association_object_id, o.id)
    write_attribute(:biological_association_object_type, o.metamorphosize.class.name)
  end

  # TODO: Why?! this is just biological_association.biological_association_subject_type
  def subject_class_name
    biological_association_subject.try(:class).base_class.name
  end

  # TODO: Why?! this is just biological_association.biological_association_object_type
  def object_class_name
    biological_association_object.try(:class).base_class.name
  end

  # !! You can not set with this method
  def subject
    biological_association_subject
  end

  # !! You can not set with this method
  def object
    biological_association_object
  end

  # @return [Array of Integer]
  #   OTU ids reachable through this BiologicalAssociation's subject and object.
  #   Handles direct Otu subject/object, CollectionObject and FieldOccurrence
  #   (via position=1 taxon determination), and AnatomicalPart (via cached_otu_id).
  def otu_ids
    ids = []
    [:subject, :object].each do |role|
      type = send("biological_association_#{role}_type")
      id   = send("biological_association_#{role}_id")
      case type
      when 'Otu'
        ids << id
      when 'CollectionObject', 'FieldOccurrence'
        otu_id = ::TaxonDetermination
          .where(taxon_determination_object_type: type, taxon_determination_object_id: id, position: 1)
          .pick(:otu_id)
        ids << otu_id if otu_id
      when 'AnatomicalPart'
        otu_id = ::AnatomicalPart.where(id:).pick(:cached_otu_id)
        ids << otu_id if otu_id
      end
    end
    ids.uniq
  end

  # @return [Array of Integer]
  #   OTU ids reachable across a collection of BiologicalAssociations,
  #   batching queries by type to avoid N+1.
  def self.collect_otu_ids(biological_associations)
    bas = biological_associations.to_a
    ids = []

    [:subject, :object].each do |role|
      type_attr = "biological_association_#{role}_type"
      id_attr   = "biological_association_#{role}_id"
      by_type   = bas.group_by { |ba| ba.send(type_attr) }

      (by_type['Otu'] || []).each { |ba| ids << ba.send(id_attr) }

      %w[CollectionObject FieldOccurrence].each do |type|
        next unless (typed_bas = by_type[type])
        typed_ids = typed_bas.map { |ba| ba.send(id_attr) }
        ids.concat(
          ::TaxonDetermination
            .where(taxon_determination_object_type: type, taxon_determination_object_id: typed_ids, position: 1)
            .pluck(:otu_id)
        )
      end

      if (ap_bas = by_type['AnatomicalPart'])
        ap_ids = ap_bas.map { |ba| ba.send(id_attr) }
        ids.concat(::AnatomicalPart.where(id: ap_ids).pluck(:cached_otu_id).compact)
      end
    end

    ids.uniq
  end

  class << self

    def set_batch_cap(request)
      a = request.filter
      total = a.all.pluck(:biological_relationship_id).uniq

      cap = 0

      case total.size
      when 1
        cap = 5000
        request.cap_reason = 'Maximum allowed.'
      when 2
        cap = 2000
        request.cap_reason = 'Maximum allowed when 2 biological relationships present.'
      else
        cap = 25
        request.cap_reason = 'Maximum allowed when 3 or more biological relationships present.'
      end

      request.cap = cap
      request
    end

    def batch_update(params)
      request = QueryBatchRequest.new(
        klass: 'BiologicalAssociation',
        object_filter_params: params[:biological_association_query],
        object_params: params[:biological_association],
        async_cutoff: (params[:async_cutoff] || 26),
        preview: params[:preview],
        user_id: params[:user_id],
        project_id: params[:project_id]
      )

      set_batch_cap(request)
      query_batch_update(request)
    end

  end

  def dwc_extension_select
    BiologicalAssociation
      .joins("LEFT JOIN identifiers id_s ON id_s.identifier_object_type = biological_associations.biological_associations_subject_type AND ids_s.type = 'Identifier::Global::Uuid'" )
      .joins("LEFT JOIN identifiers id_o ON id_o.identifier_object_type = biological_associations.biological_associations_object_type AND ids_o.type = 'Identifier::Global::Uuid'" )
      .joins("LEFT JOIN identifiers id_r ON id_o.identifier_object_type = 'BiologicalRelationship' AND idr_.identifier_object_id = biological_associations.biological_relationship_id AND ids_r.type = 'Identifier::Global::Uri'" )
  end

  # @return [ActiveRecord::Relation]
  def targeted_join(target: 'subject', target_class: ::Otu)
    a = arel_table
    b = target_class.arel_table

    j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
    joins(j.join_sources)
  end

  # @return [ActiveRecord::Relation]
  def targeted_join2(target: 'subject', target_class: ::Otu)
    a = arel_table
    b = target_class.arel_table

    j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
  end

  # Not used
  # @return [ActiveRecord::Relation]
  def targeted_left_join(target: 'subject', target_class: ::Otu )
    a = arel_table
    b = target_class.arel_table

    j = a.join(b, Arel::Nodes::OuterJoin).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
    joins(j.join_sources)
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return BiologicalAssociation.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('BiologicalAssociation')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = BiologicalAssociation.arel_table

    case used_on
    when 'AssertedDistribution'
      BiologicalAssociation.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: BiologicalAssociation.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  end

  private

  def association_is_unique
    if a = BiologicalAssociation.where.not(id:).where(
        biological_association_subject:,
        biological_association_object:,
        biological_relationship:
    ).first
      # For unify purposes, self has changed either subject or object, a is the
      # identical BA we will perhaps unify with.
      if will_save_change_to_biological_association_subject_id?
        errors.add(:biological_association_subject, 'has already been taken')
      elsif will_save_change_to_biological_association_object_id?
        errors.add(:biological_association_object, 'has already been taken')
      else
        errors.add(:biological_association, 'already exists')
      end
    end
  end


  def biological_association_subject_type_is_allowed
    errors.add(:biological_association_subject_type, 'is not permitted') unless biological_association_subject && biological_association_subject.class.is_biologically_relatable?
  end

  def biological_association_object_type_is_allowed
    errors.add(:biological_association_object_type, 'is not permitted') unless biological_association_object && biological_association_object.class.is_biologically_relatable?
  end
end

#object_global_idObject

TODO: this is badly named



75
76
77
# File 'app/models/biological_association.rb', line 75

def object_global_id
  @object_global_id
end

#project_idInteger

the project ID

Returns:

  • (Integer)


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
108
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'app/models/biological_association.rb', line 29

class BiologicalAssociation < ApplicationRecord
  include Housekeeping
  include SoftValidation
  include Shared::Citations
  include Shared::Tags
  include Shared::Identifiers
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Depictions
  include Shared::AutoUuid
  include Shared::AssertedDistributions
  include Shared::IsIndexedBiologicalAssociation
  include Shared::IsData

  include BiologicalAssociation::GlobiExtensions
  include BiologicalAssociation::DwcExtensions

  include Shared::QueryBatchUpdate

  GRAPH_ENTRY_POINTS = [:asserted_distributions].freeze

  belongs_to :biological_relationship, inverse_of: :biological_associations

  has_many :subject_biological_relationship_types, through: :biological_relationship
  has_many :object_biological_relationship_types, through: :biological_relationship

  has_many :subject_biological_properties, through: :subject_biological_relationship_types, source: :biological_property
  has_many :object_biological_properties, through: :object_biological_relationship_types, source: :biological_property

  belongs_to :biological_association_subject, polymorphic: true, inverse_of: :biological_associations
  belongs_to :biological_association_object, polymorphic: true, inverse_of: :related_biological_associations

  has_many :biological_associations_biological_associations_graphs, inverse_of: :biological_association, dependent: :destroy
  has_many :biological_associations_graphs, through: :biological_associations_biological_associations_graphs, inverse_of: :biological_associations

  validates_presence_of :biological_relationship
  validates_presence_of :biological_association_subject
  validates_presence_of :biological_association_object

  validate :association_is_unique

  validate :biological_association_subject_type_is_allowed
  validate :biological_association_object_type_is_allowed

  attr_accessor :subject_global_id
  attr_accessor :object_global_id # TODO: this is badly named

  attr_accessor :rotate

  def rotate=(value)
    s = self.biological_association_subject
    o = self.biological_association_object

    self.biological_association_subject = o
    self.biological_association_object = s
  end

  def subject_global_id=(value)
    o = GlobalID::Locator.locate(value)
    write_attribute(:biological_association_subject_id, o.id)
    write_attribute(:biological_association_subject_type, o.metamorphosize.class.name)
  end

  def object_global_id=(value)
    o = GlobalID::Locator.locate(value)
    write_attribute(:biological_association_object_id, o.id)
    write_attribute(:biological_association_object_type, o.metamorphosize.class.name)
  end

  # TODO: Why?! this is just biological_association.biological_association_subject_type
  def subject_class_name
    biological_association_subject.try(:class).base_class.name
  end

  # TODO: Why?! this is just biological_association.biological_association_object_type
  def object_class_name
    biological_association_object.try(:class).base_class.name
  end

  # !! You can not set with this method
  def subject
    biological_association_subject
  end

  # !! You can not set with this method
  def object
    biological_association_object
  end

  # @return [Array of Integer]
  #   OTU ids reachable through this BiologicalAssociation's subject and object.
  #   Handles direct Otu subject/object, CollectionObject and FieldOccurrence
  #   (via position=1 taxon determination), and AnatomicalPart (via cached_otu_id).
  def otu_ids
    ids = []
    [:subject, :object].each do |role|
      type = send("biological_association_#{role}_type")
      id   = send("biological_association_#{role}_id")
      case type
      when 'Otu'
        ids << id
      when 'CollectionObject', 'FieldOccurrence'
        otu_id = ::TaxonDetermination
          .where(taxon_determination_object_type: type, taxon_determination_object_id: id, position: 1)
          .pick(:otu_id)
        ids << otu_id if otu_id
      when 'AnatomicalPart'
        otu_id = ::AnatomicalPart.where(id:).pick(:cached_otu_id)
        ids << otu_id if otu_id
      end
    end
    ids.uniq
  end

  # @return [Array of Integer]
  #   OTU ids reachable across a collection of BiologicalAssociations,
  #   batching queries by type to avoid N+1.
  def self.collect_otu_ids(biological_associations)
    bas = biological_associations.to_a
    ids = []

    [:subject, :object].each do |role|
      type_attr = "biological_association_#{role}_type"
      id_attr   = "biological_association_#{role}_id"
      by_type   = bas.group_by { |ba| ba.send(type_attr) }

      (by_type['Otu'] || []).each { |ba| ids << ba.send(id_attr) }

      %w[CollectionObject FieldOccurrence].each do |type|
        next unless (typed_bas = by_type[type])
        typed_ids = typed_bas.map { |ba| ba.send(id_attr) }
        ids.concat(
          ::TaxonDetermination
            .where(taxon_determination_object_type: type, taxon_determination_object_id: typed_ids, position: 1)
            .pluck(:otu_id)
        )
      end

      if (ap_bas = by_type['AnatomicalPart'])
        ap_ids = ap_bas.map { |ba| ba.send(id_attr) }
        ids.concat(::AnatomicalPart.where(id: ap_ids).pluck(:cached_otu_id).compact)
      end
    end

    ids.uniq
  end

  class << self

    def set_batch_cap(request)
      a = request.filter
      total = a.all.pluck(:biological_relationship_id).uniq

      cap = 0

      case total.size
      when 1
        cap = 5000
        request.cap_reason = 'Maximum allowed.'
      when 2
        cap = 2000
        request.cap_reason = 'Maximum allowed when 2 biological relationships present.'
      else
        cap = 25
        request.cap_reason = 'Maximum allowed when 3 or more biological relationships present.'
      end

      request.cap = cap
      request
    end

    def batch_update(params)
      request = QueryBatchRequest.new(
        klass: 'BiologicalAssociation',
        object_filter_params: params[:biological_association_query],
        object_params: params[:biological_association],
        async_cutoff: (params[:async_cutoff] || 26),
        preview: params[:preview],
        user_id: params[:user_id],
        project_id: params[:project_id]
      )

      set_batch_cap(request)
      query_batch_update(request)
    end

  end

  def dwc_extension_select
    BiologicalAssociation
      .joins("LEFT JOIN identifiers id_s ON id_s.identifier_object_type = biological_associations.biological_associations_subject_type AND ids_s.type = 'Identifier::Global::Uuid'" )
      .joins("LEFT JOIN identifiers id_o ON id_o.identifier_object_type = biological_associations.biological_associations_object_type AND ids_o.type = 'Identifier::Global::Uuid'" )
      .joins("LEFT JOIN identifiers id_r ON id_o.identifier_object_type = 'BiologicalRelationship' AND idr_.identifier_object_id = biological_associations.biological_relationship_id AND ids_r.type = 'Identifier::Global::Uri'" )
  end

  # @return [ActiveRecord::Relation]
  def targeted_join(target: 'subject', target_class: ::Otu)
    a = arel_table
    b = target_class.arel_table

    j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
    joins(j.join_sources)
  end

  # @return [ActiveRecord::Relation]
  def targeted_join2(target: 'subject', target_class: ::Otu)
    a = arel_table
    b = target_class.arel_table

    j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
  end

  # Not used
  # @return [ActiveRecord::Relation]
  def targeted_left_join(target: 'subject', target_class: ::Otu )
    a = arel_table
    b = target_class.arel_table

    j = a.join(b, Arel::Nodes::OuterJoin).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
    joins(j.join_sources)
  end

  # @return [Scope]
  #    the max 10 most recently used
  def self.used_recently(user_id, project_id, used_on)
    t = case used_on
        when 'AssertedDistribution'
          AssertedDistribution.arel_table
        else
          return BiologicalAssociation.none
        end

    # i is a select manager
    i = case used_on
        when 'AssertedDistribution'
          t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
            .where(
              t['updated_at'].gt(1.week.ago).and(
                t['asserted_distribution_object_type'].eq('BiologicalAssociation')
              )
            )
            .where(t['updated_by_id'].eq(user_id))
            .where(t['project_id'].eq(project_id))
            .order(t['updated_at'].desc)
        end

    z = i.as('recent_t')
    p = BiologicalAssociation.arel_table

    case used_on
    when 'AssertedDistribution'
      BiologicalAssociation.joins(
        Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
      ).pluck(:id).uniq
    end
  end

  def self.select_optimized(user_id, project_id, klass)
    r = used_recently(user_id, project_id, klass)
    h = {
      quick: [],
      pinboard: BiologicalAssociation.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
    }

    if r.empty?
      h[:quick] = BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
      h[:quick] = (BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                   BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
    end

    h
  end

  private

  def association_is_unique
    if a = BiologicalAssociation.where.not(id:).where(
        biological_association_subject:,
        biological_association_object:,
        biological_relationship:
    ).first
      # For unify purposes, self has changed either subject or object, a is the
      # identical BA we will perhaps unify with.
      if will_save_change_to_biological_association_subject_id?
        errors.add(:biological_association_subject, 'has already been taken')
      elsif will_save_change_to_biological_association_object_id?
        errors.add(:biological_association_object, 'has already been taken')
      else
        errors.add(:biological_association, 'already exists')
      end
    end
  end


  def biological_association_subject_type_is_allowed
    errors.add(:biological_association_subject_type, 'is not permitted') unless biological_association_subject && biological_association_subject.class.is_biologically_relatable?
  end

  def biological_association_object_type_is_allowed
    errors.add(:biological_association_object_type, 'is not permitted') unless biological_association_object && biological_association_object.class.is_biologically_relatable?
  end
end

#rotateObject

Returns the value of attribute rotate.



77
78
79
# File 'app/models/biological_association.rb', line 77

def rotate
  @rotate
end

#subject_global_idObject

Returns the value of attribute subject_global_id.



74
75
76
# File 'app/models/biological_association.rb', line 74

def subject_global_id
  @subject_global_id
end

Class Method Details

.batch_update(params) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/biological_association.rb', line 201

def batch_update(params)
  request = QueryBatchRequest.new(
    klass: 'BiologicalAssociation',
    object_filter_params: params[:biological_association_query],
    object_params: params[:biological_association],
    async_cutoff: (params[:async_cutoff] || 26),
    preview: params[:preview],
    user_id: params[:user_id],
    project_id: params[:project_id]
  )

  set_batch_cap(request)
  query_batch_update(request)
end

.collect_otu_ids(biological_associations) ⇒ Array of Integer

Returns OTU ids reachable across a collection of BiologicalAssociations, batching queries by type to avoid N+1.

Returns:

  • (Array of Integer)

    OTU ids reachable across a collection of BiologicalAssociations, batching queries by type to avoid N+1.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/models/biological_association.rb', line 147

def self.collect_otu_ids(biological_associations)
  bas = biological_associations.to_a
  ids = []

  [:subject, :object].each do |role|
    type_attr = "biological_association_#{role}_type"
    id_attr   = "biological_association_#{role}_id"
    by_type   = bas.group_by { |ba| ba.send(type_attr) }

    (by_type['Otu'] || []).each { |ba| ids << ba.send(id_attr) }

    %w[CollectionObject FieldOccurrence].each do |type|
      next unless (typed_bas = by_type[type])
      typed_ids = typed_bas.map { |ba| ba.send(id_attr) }
      ids.concat(
        ::TaxonDetermination
          .where(taxon_determination_object_type: type, taxon_determination_object_id: typed_ids, position: 1)
          .pluck(:otu_id)
      )
    end

    if (ap_bas = by_type['AnatomicalPart'])
      ap_ids = ap_bas.map { |ba| ba.send(id_attr) }
      ids.concat(::AnatomicalPart.where(id: ap_ids).pluck(:cached_otu_id).compact)
    end
  end

  ids.uniq
end

.select_optimized(user_id, project_id, klass) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'app/models/biological_association.rb', line 287

def self.select_optimized(user_id, project_id, klass)
  r = used_recently(user_id, project_id, klass)
  h = {
    quick: [],
    pinboard: BiologicalAssociation.pinned_by(user_id).where(project_id: project_id).to_a,
    recent: []
  }

  if r.empty?
    h[:quick] = BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
  else
    h[:recent] = BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(10) ).order(updated_at: :desc).to_a
    h[:quick] = (BiologicalAssociation.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
                 BiologicalAssociation.where('"biological_associations"."id" IN (?)', r.first(4) ).order(updated_at: :desc).to_a).uniq
  end

  h
end

.set_batch_cap(request) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/models/biological_association.rb', line 179

def set_batch_cap(request)
  a = request.filter
  total = a.all.pluck(:biological_relationship_id).uniq

  cap = 0

  case total.size
  when 1
    cap = 5000
    request.cap_reason = 'Maximum allowed.'
  when 2
    cap = 2000
    request.cap_reason = 'Maximum allowed when 2 biological relationships present.'
  else
    cap = 25
    request.cap_reason = 'Maximum allowed when 3 or more biological relationships present.'
  end

  request.cap = cap
  request
end

.used_recently(user_id, project_id, used_on) ⇒ Scope

Returns the max 10 most recently used.

Returns:

  • (Scope)

    the max 10 most recently used



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'app/models/biological_association.rb', line 254

def self.used_recently(user_id, project_id, used_on)
  t = case used_on
      when 'AssertedDistribution'
        AssertedDistribution.arel_table
      else
        return BiologicalAssociation.none
      end

  # i is a select manager
  i = case used_on
      when 'AssertedDistribution'
        t.project(t['asserted_distribution_object_id'], t['updated_at']).from(t)
          .where(
            t['updated_at'].gt(1.week.ago).and(
              t['asserted_distribution_object_type'].eq('BiologicalAssociation')
            )
          )
          .where(t['updated_by_id'].eq(user_id))
          .where(t['project_id'].eq(project_id))
          .order(t['updated_at'].desc)
      end

  z = i.as('recent_t')
  p = BiologicalAssociation.arel_table

  case used_on
  when 'AssertedDistribution'
    BiologicalAssociation.joins(
      Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['asserted_distribution_object_id'].eq(p['id'])))
    ).pluck(:id).uniq
  end
end

Instance Method Details

#association_is_uniqueObject (private)



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'app/models/biological_association.rb', line 308

def association_is_unique
  if a = BiologicalAssociation.where.not(id:).where(
      biological_association_subject:,
      biological_association_object:,
      biological_relationship:
  ).first
    # For unify purposes, self has changed either subject or object, a is the
    # identical BA we will perhaps unify with.
    if will_save_change_to_biological_association_subject_id?
      errors.add(:biological_association_subject, 'has already been taken')
    elsif will_save_change_to_biological_association_object_id?
      errors.add(:biological_association_object, 'has already been taken')
    else
      errors.add(:biological_association, 'already exists')
    end
  end
end

#biological_association_object_type_is_allowedObject (private)



331
332
333
# File 'app/models/biological_association.rb', line 331

def biological_association_object_type_is_allowed
  errors.add(:biological_association_object_type, 'is not permitted') unless biological_association_object && biological_association_object.class.is_biologically_relatable?
end

#biological_association_subject_type_is_allowedObject (private)



327
328
329
# File 'app/models/biological_association.rb', line 327

def biological_association_subject_type_is_allowed
  errors.add(:biological_association_subject_type, 'is not permitted') unless biological_association_subject && biological_association_subject.class.is_biologically_relatable?
end

#dwc_extension_selectObject



218
219
220
221
222
223
# File 'app/models/biological_association.rb', line 218

def dwc_extension_select
  BiologicalAssociation
    .joins("LEFT JOIN identifiers id_s ON id_s.identifier_object_type = biological_associations.biological_associations_subject_type AND ids_s.type = 'Identifier::Global::Uuid'" )
    .joins("LEFT JOIN identifiers id_o ON id_o.identifier_object_type = biological_associations.biological_associations_object_type AND ids_o.type = 'Identifier::Global::Uuid'" )
    .joins("LEFT JOIN identifiers id_r ON id_o.identifier_object_type = 'BiologicalRelationship' AND idr_.identifier_object_id = biological_associations.biological_relationship_id AND ids_r.type = 'Identifier::Global::Uri'" )
end

#objectObject

!! You can not set with this method



115
116
117
# File 'app/models/biological_association.rb', line 115

def object
  biological_association_object
end

#object_class_nameObject

TODO: Why?! this is just biological_association.biological_association_object_type



105
106
107
# File 'app/models/biological_association.rb', line 105

def object_class_name
  biological_association_object.try(:class).base_class.name
end

#otu_idsArray of Integer

Returns OTU ids reachable through this BiologicalAssociation's subject and object. Handles direct Otu subject/object, CollectionObject and FieldOccurrence (via position=1 taxon determination), and AnatomicalPart (via cached_otu_id).

Returns:

  • (Array of Integer)

    OTU ids reachable through this BiologicalAssociation's subject and object. Handles direct Otu subject/object, CollectionObject and FieldOccurrence (via position=1 taxon determination), and AnatomicalPart (via cached_otu_id).



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/biological_association.rb', line 123

def otu_ids
  ids = []
  [:subject, :object].each do |role|
    type = send("biological_association_#{role}_type")
    id   = send("biological_association_#{role}_id")
    case type
    when 'Otu'
      ids << id
    when 'CollectionObject', 'FieldOccurrence'
      otu_id = ::TaxonDetermination
        .where(taxon_determination_object_type: type, taxon_determination_object_id: id, position: 1)
        .pick(:otu_id)
      ids << otu_id if otu_id
    when 'AnatomicalPart'
      otu_id = ::AnatomicalPart.where(id:).pick(:cached_otu_id)
      ids << otu_id if otu_id
    end
  end
  ids.uniq
end

#subjectObject

!! You can not set with this method



110
111
112
# File 'app/models/biological_association.rb', line 110

def subject
  biological_association_subject
end

#subject_class_nameObject

TODO: Why?! this is just biological_association.biological_association_subject_type



100
101
102
# File 'app/models/biological_association.rb', line 100

def subject_class_name
  biological_association_subject.try(:class).base_class.name
end

#targeted_join(target: 'subject', target_class: ::Otu) ⇒ ActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


226
227
228
229
230
231
232
# File 'app/models/biological_association.rb', line 226

def targeted_join(target: 'subject', target_class: ::Otu)
  a = arel_table
  b = target_class.arel_table

  j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
  joins(j.join_sources)
end

#targeted_join2(target: 'subject', target_class: ::Otu) ⇒ ActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


235
236
237
238
239
240
# File 'app/models/biological_association.rb', line 235

def targeted_join2(target: 'subject', target_class: ::Otu)
  a = arel_table
  b = target_class.arel_table

  j = a.join(b).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
end

#targeted_left_join(target: 'subject', target_class: ::Otu) ⇒ ActiveRecord::Relation

Not used

Returns:

  • (ActiveRecord::Relation)


244
245
246
247
248
249
250
# File 'app/models/biological_association.rb', line 244

def targeted_left_join(target: 'subject', target_class: ::Otu )
  a = arel_table
  b = target_class.arel_table

  j = a.join(b, Arel::Nodes::OuterJoin).on(a["biological_association_#{target}_type".to_sym].eq(target_class.name).and(a["biological_assoication_#{target}_id".to_sym].eq(b[:id])))
  joins(j.join_sources)
end