Class: Queries::BiologicalAssociation::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Citations, Concerns::Confidences, Concerns::Depictions, Concerns::Notes, Concerns::Tags
Defined in:
lib/queries/biological_association/filter.rb

Constant Summary collapse

PARAMS =
[
  :biological_association_id,
  :biological_associations_graph_id,
  :biological_relationship_id,
  :collecting_event_id,
  :collection_object_id,
  :descendants,
  :exclude_taxon_name_relationship,
  :geo_json,
  :geographic_area_id,
  :geographic_area_mode,
  :object_biological_property_id,
  :object_object_global_id,
  :object_taxon_name_id,
  :object_type,
  :otu_id,
  :subject_biological_property_id,
  :subject_object_global_id,
  :subject_taxon_name_id,
  :subject_type,
  :taxon_name_id,
  :taxon_name_id_mode,
  :wkt,
  :biological_association_object_id,
  :biological_association_object_type,
  :biological_association_subject_id,
  :biological_association_subject_type,

  any_global_id: [],
  biological_association_id: [],
  biological_associations_graph_id: [],
  biological_relationship_id: [],
  collecting_event_id: [],
  collection_object_id: [],
  geographic_area_id: [],
  object_biological_property_id: [],
  object_object_global_id: [],
  object_taxon_name_id: [],
  otu_id: [],
  subject_biological_property_id: [],
  subject_object_global_id: [],
  subject_taxon_name_id: [],
  taxon_name_id: [],
  biological_association_object_id: [],
  biological_association_object_type: [],
  biological_association_subject_id: [],
  biological_association_subject_type: []
].freeze
API_PARAM_EXCLUSIONS =
[
  :any_global_id
]

Constants inherited from Query::Filter

Query::Filter::FILTER_QUERIES, Query::Filter::SUBQUERIES

Instance Attribute Summary collapse

Attributes inherited from Query::Filter

#api, #asserted_distribution_query, #biological_association_query, #biological_associations_graph_query, #collecting_event_query, #collection_object_query, #content_query, #controlled_vocabulary_term_query, #data_attribute_query, #descriptor_query, #document_query, #dwc_occurrence_query, #extract_query, #field_occurrence_query, #image_query, #loan_query, #object_global_id, #observation_query, #order_by, #otu_query, #page, #paginate, #params, #per, #person_query, #project_id, #recent, #recent_target, #taxon_name_query, #venn, #venn_mode

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods inherited from Query::Filter

#all, #all_and_clauses, #all_merge_clauses, #annotator_and_clauses, #annotator_merge_clauses, annotator_params, api_except_params, api_excluded_params, #apply_venn, #attribute_exact_facet, base_filter, base_query_name, base_query_to_h, #deep_permit, included_annotator_facets, instantiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, #only_project?, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #query_name, #set_nested_queries, #shared_and_clauses, #subquery_vector, #target_and_clauses, #venn_query

Methods inherited from Query

#alphabetic_strings, #alphanumeric_strings, base_name, #base_name, #base_query, #build_terms, #cached_facet, #end_wildcard, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #no_terms?, referenced_klass, #referenced_klass, #referenced_klass_except, #referenced_klass_intersection, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces

Constructor Details

#initialize(query_params) ⇒ Filter

Returns a new instance of Filter.



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
# File 'lib/queries/biological_association/filter.rb', line 170

def initialize(query_params)
  super

  @any_global_id = params[:any_global_id]
  @biological_association_id = params[:biological_association_id]
  @biological_associations_graph_id = params[:biological_associations_graph_id]
  @biological_relationship_id = params[:biological_relationship_id]
  @collecting_event_id = params[:collecting_event_id]
  @collection_object_id = params[:collection_object_id]
  @descendants = boolean_param(params, :descendants)
  @exclude_taxon_name_relationship = boolean_param(params, :exclude_taxon_name_relationship)
  @geo_json = params[:geo_json]
  @geographic_area_id = params[:geographic_area_id]
  @geographic_area_mode = boolean_param(params, :geographic_area_mode)
  @object_biological_property_id = params[:object_biological_property_id]
  @object_object_global_id = params[:object_object_global_id]
  @object_taxon_name_id = params[:object_taxon_name_id]
  @object_type = params[:object_type]
  @otu_id = params[:otu_id]
  @subject_biological_property_id = params[:subject_biological_property_id]
  @subject_object_global_id = params[:subject_object_global_id]
  @subject_taxon_name_id = params[:subject_taxon_name_id]
  @subject_type = params[:subject_type]
  @taxon_name_id = params[:taxon_name_id]
  @taxon_name_id_mode = boolean_param(params, :taxon_name_id_mode)
  @wkt = params[:wkt]
  @biological_association_object_id = params[:biological_association_object_id]
  @biological_association_object_type = params[:biological_association_object_type]
  @biological_association_subject_id = params[:biological_association_subject_id]
  @biological_association_subject_type = params[:biological_association_subject_type]

  set_confidences_params(params)
  set_notes_params(params)
  set_tags_params(params)
  set_citations_params(params)
  set_depiction_params(params)
end

Instance Attribute Details

#any_global_idArray

Returns:

  • (Array)


145
146
147
# File 'lib/queries/biological_association/filter.rb', line 145

def any_global_id
  @any_global_id
end

#biological_association_idArray

Returns one or more BiologicalAssociation#id.

Parameters:

  • biological_association_id (Array, Integer)

Returns:

  • (Array)

    one or more BiologicalAssociation#id



125
126
127
# File 'lib/queries/biological_association/filter.rb', line 125

def biological_association_id
  @biological_association_id
end

#biological_association_object_idObject

Returns the value of attribute biological_association_object_id.



162
163
164
# File 'lib/queries/biological_association/filter.rb', line 162

def biological_association_object_id
  @biological_association_object_id
end

#biological_association_object_typeObject

Returns the value of attribute biological_association_object_type.



164
165
166
# File 'lib/queries/biological_association/filter.rb', line 164

def biological_association_object_type
  @biological_association_object_type
end

#biological_association_subject_idObject

Returns the value of attribute biological_association_subject_id.



166
167
168
# File 'lib/queries/biological_association/filter.rb', line 166

def biological_association_subject_id
  @biological_association_subject_id
end

#biological_association_subject_typeObject

Returns the value of attribute biological_association_subject_type.



168
169
170
# File 'lib/queries/biological_association/filter.rb', line 168

def biological_association_subject_type
  @biological_association_subject_type
end

#biological_associations_graph_idArray

Returns a biological relationship graph ID.

Parameters:

  • biological_associations_graph_id (Array, Integer)

Returns:

  • (Array)

    a biological relationship graph ID



112
113
114
# File 'lib/queries/biological_association/filter.rb', line 112

def biological_associations_graph_id
  @biological_associations_graph_id
end

#biological_relationship_idArray

See also exclude_taxon_name_relationship

Parameters:

  • biological_relationship_id (Array, Integer)

Returns:

  • (Array)

    one or more biological relationship ID



131
132
133
# File 'lib/queries/biological_association/filter.rb', line 131

def biological_relationship_id
  @biological_relationship_id
end

#collecting_event_idObject

linked to one or more CollectingEvent

Parameters:

  • collecting_event_id

Returns:

  • Integer All BiologicalAssociations with CollectionObjects



73
74
75
# File 'lib/queries/biological_association/filter.rb', line 73

def collecting_event_id
  @collecting_event_id
end

#collection_object_idArray

matching subject OR object

Parameters:

  • collection_object_id

Returns:

  • (Array)

    All biological relationships with CollectionObject (only)



85
86
87
# File 'lib/queries/biological_association/filter.rb', line 85

def collection_object_id
  @collection_object_id
end

#descendantsObject

Returns the value of attribute descendants.



108
109
110
# File 'lib/queries/biological_association/filter.rb', line 108

def descendants
  @descendants
end

#exclude_taxon_name_relationshipObject

those listed in biological_relationship_id

Returns:

  • Boolean, nil if true then return relationships excluding



67
68
69
# File 'lib/queries/biological_association/filter.rb', line 67

def exclude_taxon_name_relationship
  @exclude_taxon_name_relationship
end

#geo_jsonObject

Returns the value of attribute geo_json.



149
150
151
# File 'lib/queries/biological_association/filter.rb', line 149

def geo_json
  @geo_json
end

#geographic_area_idObject

Returns the value of attribute geographic_area_id.



150
151
152
# File 'lib/queries/biological_association/filter.rb', line 150

def geographic_area_id
  @geographic_area_id
end

#geographic_area_modeObject

Returns the value of attribute geographic_area_mode.



152
153
154
# File 'lib/queries/biological_association/filter.rb', line 152

def geographic_area_mode
  @geographic_area_mode
end

#object_biological_property_idArray

Returns All BiologicalAssociations with object having this property.

Returns:

  • (Array)

    All BiologicalAssociations with object having this property



120
121
122
# File 'lib/queries/biological_association/filter.rb', line 120

def object_biological_property_id
  @object_biological_property_id
end

#object_object_global_idArray

Returns:

  • (Array)


142
143
144
# File 'lib/queries/biological_association/filter.rb', line 142

def object_object_global_id
  @object_object_global_id
end

#object_taxon_name_idObject

its children !! TaxonName must be valid!

Parameters:

  • object_taxon_name_id

    All BiologicalAssociations matching this name or



97
98
99
# File 'lib/queries/biological_association/filter.rb', line 97

def object_taxon_name_id
  @object_taxon_name_id
end

#object_typenil, ...

Returns limit object to a type.

Returns:



160
161
162
# File 'lib/queries/biological_association/filter.rb', line 160

def object_type
  @object_type
end

#otu_idArray

matching subject OR object

Parameters:

  • otu_id

Returns:

  • (Array)

    All BiologicalAssociations with OTU (only)



79
80
81
# File 'lib/queries/biological_association/filter.rb', line 79

def otu_id
  @otu_id
end

#subject_biological_property_idArray

Returns All BiologicalAssociations with subject having this property.

Returns:

  • (Array)

    All BiologicalAssociations with subject having this property



116
117
118
# File 'lib/queries/biological_association/filter.rb', line 116

def subject_biological_property_id
  @subject_biological_property_id
end

#subject_object_global_idArray

Returns:

  • (Array)


139
140
141
# File 'lib/queries/biological_association/filter.rb', line 139

def subject_object_global_id
  @subject_object_global_id
end

#subject_taxon_name_idObject

its children. !! TaxonName must be valid!

Parameters:

  • subject_taxon_name_id

    All BiologicalAssociations matching this name or



91
92
93
# File 'lib/queries/biological_association/filter.rb', line 91

def subject_taxon_name_id
  @subject_taxon_name_id
end

#subject_typenil, ...

Returns limit subject to a type.

Returns:



156
157
158
# File 'lib/queries/biological_association/filter.rb', line 156

def subject_type
  @subject_type
end

#taxon_name_idArray

their children

Parameters:

  • taxon_name_id

Returns:

  • (Array)

    All BiologicalAssociations matching these names or



103
104
105
# File 'lib/queries/biological_association/filter.rb', line 103

def taxon_name_id
  @taxon_name_id
end

#taxon_name_id_modeBoolean?

Returns:

  • (Boolean, nil)


106
107
108
# File 'lib/queries/biological_association/filter.rb', line 106

def taxon_name_id_mode
  @taxon_name_id_mode
end

#wktObject

See lib/queries/otu/filter.rb



148
149
150
# File 'lib/queries/biological_association/filter.rb', line 148

def wkt
  @wkt
end

Instance Method Details

#and_clausesObject



720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/queries/biological_association/filter.rb', line 720

def and_clauses
  [
    any_global_id_facet,
    biological_association_id_facet,
    biological_relationship_id_facet,
    object_object_global_id_facet,
    object_type_facet,
    subject_object_global_id_facet,
    subject_type_facet,
    biological_association_object_id_facet,
    biological_association_object_type_facet,
    biological_association_subject_id_facet,
    biological_association_subject_id_facet
  ]
end

#any_global_id_facetObject



591
592
593
594
595
596
597
598
599
600
# File 'lib/queries/biological_association/filter.rb', line 591

def any_global_id_facet
  return nil if any_global_id.empty?
  a = matching_global_id(:subject, any_global_id)
  b = matching_global_id(:object, any_global_id)
  if a && b
    return a.or(b)
  else
    return a ? a : b
  end
end

#asserted_distribution_query_facetObject



658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/queries/biological_association/filter.rb', line 658

def asserted_distribution_query_facet
  return nil if asserted_distribution_query.nil?
  s = 'WITH query_ad_ba AS (' + asserted_distribution_query.all.to_sql + ') '

  a = ::BiologicalAssociation
    .joins("JOIN query_ad_ba as query_ad_ba1 on biological_associations.biological_association_subject_id = query_ad_ba1.otu_id AND biological_associations.biological_association_subject_type = 'Otu'")

  b = ::BiologicalAssociation
    .joins("JOIN query_ad_ba as query_ad_ba2 on biological_associations.biological_association_object_id = query_ad_ba2.otu_id AND biological_associations.biological_association_object_type = 'Otu'")

  s << referenced_klass_union([a,b]).to_sql
  ::BiologicalAssociation.from('(' + s + ') as biological_associations')
end

#base_collection_object_query(opts) ⇒ Object



383
384
385
386
387
# File 'lib/queries/biological_association/filter.rb', line 383

def base_collection_object_query(opts)
  q = ::Queries::CollectionObject::Filter.new(opts)
  q.project_id = nil # reset at use
  q
end

#base_otu_query(opts) ⇒ Object



377
378
379
380
381
# File 'lib/queries/biological_association/filter.rb', line 377

def base_otu_query(opts)
  q = ::Queries::Otu::Filter.new(opts)
  q.project_id = nil # reset at use
  q
end

#biological_association_id_facetObject



611
612
613
614
# File 'lib/queries/biological_association/filter.rb', line 611

def biological_association_id_facet
  return nil if biological_association_id.empty?
  table[:id].in(biological_association_id)
end

#biological_association_object_id_facetObject



301
302
303
304
# File 'lib/queries/biological_association/filter.rb', line 301

def biological_association_object_id_facet
  return nil if biological_association_object_id.empty?
  table[:biological_association_object_id].in(biological_association_object_id)
end

#biological_association_object_type_facetObject



296
297
298
299
# File 'lib/queries/biological_association/filter.rb', line 296

def biological_association_object_type_facet
  return nil if biological_association_object_type.empty?
  table[:biological_association_object_type].in(biological_association_object_type)
end

#biological_association_subject_id_facetObject



311
312
313
314
# File 'lib/queries/biological_association/filter.rb', line 311

def biological_association_subject_id_facet
  return nil if biological_association_subject_id.empty?
  table[:biological_association_subject_id].in(biological_association_subject_id)
end

#biological_association_subject_type_facetObject



306
307
308
309
# File 'lib/queries/biological_association/filter.rb', line 306

def biological_association_subject_type_facet
  return nil if biological_association_subject_type.empty?
  table[:biological_association_subject_type].in(biological_association_subject_type)
end

#biological_associations_graph_id_facetObject



328
329
330
331
# File 'lib/queries/biological_association/filter.rb', line 328

def biological_associations_graph_id_facet
  return nil if biological_associations_graph_id.empty?
  ::BiologicalAssociation.joins(:biological_associations_graphs).where(biological_associations_graphs: {id: biological_associations_graph_id})
end

#biological_relationship_id_facetObject



602
603
604
605
606
607
608
609
# File 'lib/queries/biological_association/filter.rb', line 602

def biological_relationship_id_facet
  return nil if biological_relationship_id.empty?
  if exclude_taxon_name_relationship
    table[:biological_relationship_id].not_in(biological_relationship_id)
  else
    table[:biological_relationship_id].in(biological_relationship_id)
  end
end

#collecting_event_query_facetObject



626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/queries/biological_association/filter.rb', line 626

def collecting_event_query_facet
  return nil if collecting_event_query.nil?

  s = 'WITH query_ce_ba AS (' + collecting_event_query.all.to_sql + ') '

  a = ::BiologicalAssociation
    .joins("JOIN collection_objects co1 on co1.id = biological_associations.biological_association_subject_id AND biological_associations.biological_association_subject_type = 'CollectionObject'")
    .joins('JOIN query_ce_ba as query_ce_ba1 on co1.collecting_event_id = query_ce_ba1.id')

  b = ::BiologicalAssociation
    .joins("JOIN collection_objects co2 on co2.id = biological_associations.biological_association_object_id AND biological_associations.biological_association_object_type = 'CollectionObject'")
    .joins('JOIN query_ce_ba as query_ce_ba2 on co2.collecting_event_id = query_ce_ba2.id')

  s << referenced_klass_union([a,b]).to_sql

  ::BiologicalAssociation.from('(' + s + ') as biological_associations')
end

#collection_object_facetObject



449
450
451
# File 'lib/queries/biological_association/filter.rb', line 449

def collection_object_facet
  subject_object_scope(subject_collection_object_query, object_collection_object_query, 'CollectionObject' )
end

#collection_object_paramsObject



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/queries/biological_association/filter.rb', line 333

def collection_object_params
  h = {}
  [
    :collecting_event_id,
    :collection_object_id,
    :geo_json,
    :geographic_area_id,
    :geographic_area_mode,
    :wkt,
  ].each
    .each do |p|
      v = send(p)
      h[p] = v if v.present?
    end
  h
end

#collection_object_query_facetObject



672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/queries/biological_association/filter.rb', line 672

def collection_object_query_facet
  return nil if collection_object_query.nil?
  s = 'WITH query_co_ba AS (' + collection_object_query.all.to_sql + ') '

  a = ::BiologicalAssociation
    .joins("JOIN query_co_ba as query_co_ba1 on biological_associations.biological_association_subject_id = query_co_ba1.id AND biological_associations.biological_association_subject_type = 'CollectionObject'")

  b = ::BiologicalAssociation
    .joins("JOIN query_co_ba as query_co_ba2 on biological_associations.biological_association_object_id = query_co_ba2.id AND biological_associations.biological_association_object_type = 'CollectionObject'")

  s << referenced_klass_union([a,b]).to_sql

  ::BiologicalAssociation.from('(' + s + ') as biological_associations')
end

#matching_global_id(target = :subject, global_id = []) ⇒ Object (private)



761
762
763
764
765
766
767
768
769
770
# File 'lib/queries/biological_association/filter.rb', line 761

def matching_global_id(target = :subject, global_id = [])
  a = global_id
  b = "#{target}_matches".to_sym

  q = send(b, object_for(a[0]))
  a[1..a.length].each do |i|
    q = q.or( send(b, object_for(i) ))
  end
  q
end

#merge_clausesObject



736
737
738
739
740
741
742
743
744
745
746
747
748
749
# File 'lib/queries/biological_association/filter.rb', line 736

def merge_clauses
  [
    asserted_distribution_query_facet,
    collecting_event_query_facet,
    collection_object_query_facet,
    otu_query_facet,
    taxon_name_query_facet,

    biological_associations_graph_id_facet,
    object_biological_property_id_facet,
    subject_biological_property_id_facet,
    subject_object_facet, # This handles all Otu/CollectionObject attributes
  ]
end

#object_biological_property_id_facetObject



316
317
318
319
320
# File 'lib/queries/biological_association/filter.rb', line 316

def object_biological_property_id_facet
  return nil if object_biological_property_id.empty?
  ::BiologicalAssociation.joins(:object_biological_relationship_types)
    .where(biological_relationship_types: {biological_property_id: object_biological_property_id })
end

#object_collection_object_queryObject



403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/queries/biological_association/filter.rb', line 403

def object_collection_object_query
  p = collection_object_params
  s = object_taxon_name_ids

  if p.blank? && s.empty? # .nil?
    return nil
  elsif s.present?
    p[:taxon_name_id] = s
    p[:descendants] = descendants
  end

  q = base_collection_object_query(p)
end

#object_for(global_id) ⇒ Object (private)



753
754
755
756
757
758
759
# File 'lib/queries/biological_association/filter.rb', line 753

def object_for(global_id)
  if o = GlobalID::Locator.locate(global_id)
    o
  else
    nil
  end
end

#object_matches(object) ⇒ Object



290
291
292
293
294
# File 'lib/queries/biological_association/filter.rb', line 290

def object_matches(object)
  table['biological_association_object_id'].eq(object.id).and(
    table['biological_association_object_type'].eq(object.class.base_class.name)
  )
end

#object_object_global_id_facetObject



586
587
588
589
# File 'lib/queries/biological_association/filter.rb', line 586

def object_object_global_id_facet
  return nil if object_object_global_id.empty?
  matching_global_id(:object, object_object_global_id)
end

#object_otu_queryObject



431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/queries/biological_association/filter.rb', line 431

def object_otu_query
  p = otu_params
  s = object_taxon_name_ids

  if p.blank? && s.nil?
    return nil
  elsif s.present?
    p[:taxon_name_id] = s
    p[:descendants] = descendants
  end

  base_otu_query(p)
end

#object_scopeObject



457
458
459
# File 'lib/queries/biological_association/filter.rb', line 457

def object_scope
  target_scope('object')
end

#object_taxon_name_idsObject (private)

!! Plural is correct



773
774
775
776
777
# File 'lib/queries/biological_association/filter.rb', line 773

def object_taxon_name_ids
  return taxon_name_id if taxon_name_id.present?
  return object_taxon_name_id if object_taxon_name_id.present?
  return nil
end

#object_type_facetObject



616
617
618
619
# File 'lib/queries/biological_association/filter.rb', line 616

def object_type_facet
  return nil if object_type.nil?
  table[:biological_association_object_type].eq(object_type)
end

#otu_facetObject



445
446
447
# File 'lib/queries/biological_association/filter.rb', line 445

def otu_facet
  subject_object_scope(subject_otu_query, object_otu_query, 'Otu' )
end

#otu_paramsObject



350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/queries/biological_association/filter.rb', line 350

def otu_params
  h = {}
  [
    :geo_json,
    :geographic_area_id,
    :geographic_area_mode,
    :otu_id,
    :wkt,
  ].each do |p|
    v = send(p)
    h[p] = v if v.present?
  end
  h
end

#otu_query_facetObject



644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/queries/biological_association/filter.rb', line 644

def otu_query_facet
  return nil if otu_query.nil?
  s = 'WITH query_otu_ba AS (' + otu_query.all.to_sql + ') '

  a = ::BiologicalAssociation
    .joins("JOIN query_otu_ba as query_otu_ba1 on biological_associations.biological_association_subject_id = query_otu_ba1.id AND biological_associations.biological_association_subject_type = 'Otu'")

  b = ::BiologicalAssociation
    .joins("JOIN query_otu_ba as query_otu_ba2 on biological_associations.biological_association_object_id = query_otu_ba2.id AND biological_associations.biological_association_object_type = 'Otu'")

  s << referenced_klass_union([a,b]).to_sql
  ::BiologicalAssociation.from('(' + s + ') as biological_associations')
end

#subject_biological_property_id_facetObject



322
323
324
325
326
# File 'lib/queries/biological_association/filter.rb', line 322

def subject_biological_property_id_facet
  return nil if subject_biological_property_id.empty?
  ::BiologicalAssociation.joins(:subject_biological_relationship_types)
    .where(biological_relationship_types: {biological_property_id: subject_biological_property_id })
end

#subject_collection_object_queryObject



389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/queries/biological_association/filter.rb', line 389

def subject_collection_object_query
  p = collection_object_params
  s = subject_taxon_name_ids

  if p.blank? && s.empty? #.nil?
    return nil
  elsif s.present?
    p[:taxon_name_id] = s
    p[:descendants] = descendants
  end

  q = base_collection_object_query(p)
end

#subject_matches(object) ⇒ Object



284
285
286
287
288
# File 'lib/queries/biological_association/filter.rb', line 284

def subject_matches(object)
  table['biological_association_subject_id'].eq(object.id).and(
    table['biological_association_subject_type'].eq(object.class.base_class.name)
  )
end

#subject_object_facetObject

Merges results from Otu and CollectionObject filters as the basis for the query



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/queries/biological_association/filter.rb', line 502

def subject_object_facet
  a = subject_scope
  b = object_scope

  return nil if a.nil? && b.nil?

  case taxon_name_id_mode
  when true # and
    ::BiologicalAssociation.from(
      '(' + [a,b].compact.collect{|q| '(' + q.to_sql + ')'}.join(' INTERSECT ') + ') as biological_associations'
    )
  when false, nil # or
    ::BiologicalAssociation.from(
      '(' + [a,b].compact.collect{|q| '(' + q.to_sql + ')'}.join(' UNION ') + ') as biological_associations'
    )
  end
end

#subject_object_global_id_facetObject



581
582
583
584
# File 'lib/queries/biological_association/filter.rb', line 581

def subject_object_global_id_facet
  return nil if subject_object_global_id.empty?
  matching_global_id(:subject, subject_object_global_id)
end

#subject_object_scope(subject_query, object_query, target = 'Otu') ⇒ Object

rubocop:disable Metrics/MethodLength This is “or”



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/queries/biological_association/filter.rb', line 522

def subject_object_scope(subject_query, object_query, target = 'Otu')
  a = subject_query
  b = object_query

  a_sql, b_sql = nil, nil

  if !a&.all(true).nil?
    a.project_id = project_id
    a_sql = a.all.to_sql
  end

  if !b&.all(true).nil?
    b.project_id = project_id
    b_sql = b.all.to_sql
  end



  return nil if a_sql.nil? and b_sql.nil?

  # Setup for "WITH" use
  t = []
  t.push 'a_objects AS (' + a_sql + ')' if a_sql
  t.push 'b_objects AS (' + b_sql + ')' if b_sql && (b_sql != a_sql)

  s = 'WITH ' + t.join(', ')

  # subject/object queries reference different params
  if a_sql && b_sql && (a_sql != b_sql)
    s << ' ' + ::BiologicalAssociation
      .joins("LEFT JOIN a_objects as a_objects1 on a_objects1.id = biological_associations.biological_association_subject_id AND biological_associations.biological_association_subject_type = '" + target + "'")
      .joins("LEFT JOIN b_objects as b_objects1 on b_objects1.id = biological_associations.biological_association_object_id AND biological_associations.biological_association_object_type = '" + target + "'")
      .where('a_objects1.id is not null').or('b_objects1.id is not null')
      .to_sql

    # subject/object queries reference same params
  elsif a_sql && b_sql
    s << ' ' + ::BiologicalAssociation
      .joins("LEFT JOIN a_objects as a_objects1 on a_objects1.id = biological_associations.biological_association_subject_id AND biological_associations.biological_association_subject_type = '" + target + "'")
      .joins("LEFT JOIN a_objects as a_objects2 on a_objects2.id = biological_associations.biological_association_object_id AND biological_associations.biological_association_object_type = '" + target + "'")
      .where('a_objects1.id is not null').or('a_objects2.id is not null')
      .to_sql

    # subject only
  elsif a_sql
    s << ' ' + ::BiologicalAssociation
      .joins("JOIN a_objects as a_objects1 on a_objects1.id = biological_associations.biological_association_subject_id AND biological_associations.biological_association_subject_type = '" + target + "'")
      .to_sql

    # object_only
  else
    s << ' ' + ::BiologicalAssociation
      .joins("JOIN b_objects as b_objects1 on b_objects1.id = biological_associations.biological_association_object_id AND biological_associations.biological_association_object_type = '" + target + "'")
      .to_sql
  end

  return ::BiologicalAssociation.from('(' + s + ') as biological_associations')
end

#subject_otu_queryObject



417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/queries/biological_association/filter.rb', line 417

def subject_otu_query
  p = otu_params
  s = subject_taxon_name_ids

  if p.blank? && s.nil?
    return nil
  elsif s.present?
    p[:taxon_name_id] = s
    p[:descendants] = descendants
  end

  base_otu_query(p)
end

#subject_scopeObject



453
454
455
# File 'lib/queries/biological_association/filter.rb', line 453

def subject_scope
  target_scope('subject')
end

#subject_taxon_name_idsObject (private)

!! Plural is correct



780
781
782
783
784
# File 'lib/queries/biological_association/filter.rb', line 780

def subject_taxon_name_ids
  return taxon_name_id if taxon_name_id.present?
  return subject_taxon_name_id if subject_taxon_name_id.present?
  return nil
end

#subject_type_facetObject



621
622
623
624
# File 'lib/queries/biological_association/filter.rb', line 621

def subject_type_facet
  return nil if subject_type.nil?
  table[:biological_association_subject_type].eq(subject_type)
end

#target_scope(target = 'subject') ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/queries/biological_association/filter.rb', line 461

def target_scope(target = 'subject')
  a = send((target + '_otu_query').to_sym)
  b = send((target + '_collection_object_query').to_sym)

  a_sql, b_sql = nil, nil

  if !a&.all(true).nil?
    a.project_id = project_id
    a_sql = a.all.to_sql
  end

  if !b&.all(true).nil?
    b.project_id = project_id
    b_sql = b.all.to_sql
  end

  return nil if a_sql.nil? and b_sql.nil?

  # Setup for "WITH" use
  a_with = "WITH a_#{target}_objects AS (" + a_sql + ') ' if a_sql
  b_with = "WITH b_#{target}_objects AS (" + b_sql + ') ' if b_sql

  d, e = nil, nil

  if a_with
    d = a_with + ::BiologicalAssociation
      .joins("JOIN a_#{target}_objects as a_#{target}_objects1 on a_#{target}_objects1.id = biological_associations.biological_association_#{target}_id AND biological_associations.biological_association_#{target}_type = 'Otu'").to_sql
  end

  if b_with
    e = b_with + ::BiologicalAssociation
      .joins("JOIN b_#{target}_objects as b_#{target}_objects1 on b_#{target}_objects1.id = biological_associations.biological_association_#{target}_id AND biological_associations.biological_association_#{target}_type = 'CollectionObject'").to_sql
  end

  ::BiologicalAssociation.from(
    '(' + [d,e].compact.collect{|q| '(' + q + ')'}.join(' UNION ') + ') as biological_associations'
  )
end

#taxon_name_query_facetObject

Brute-force style



688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/queries/biological_association/filter.rb', line 688

def taxon_name_query_facet
  return nil if taxon_name_query.nil?

  s = 'WITH query_tn_ba AS (' + taxon_name_query.all.to_sql + ') '

  a = ::BiologicalAssociation
    .joins("JOIN otus on otus.id = biological_associations.biological_association_subject_id AND biological_associations.biological_association_subject_type = 'Otu'")
    .joins('JOIN query_tn_ba as query_tn_ba1 on otus.taxon_name_id = query_tn_ba1.id')

  b = ::BiologicalAssociation
    .joins("JOIN otus on otus.id = biological_associations.biological_association_object_id AND biological_associations.biological_association_object_type = 'Otu'")
    .joins('JOIN query_tn_ba as query_tn_ba2 on otus.taxon_name_id = query_tn_ba2.id')

  c = ::BiologicalAssociation
    .joins("JOIN collection_objects on collection_objects.id = biological_associations.biological_association_subject_id AND biological_associations.biological_association_subject_type = 'CollectionObject'")
    .joins("JOIN taxon_determinations on taxon_determinations.taxon_determination_object_id = collection_objects.id AND taxon_determinations.taxon_determination_object_type = 'CollectionObject'")
    .joins('JOIN otus on otus.id = taxon_determinations.otu_id')
    .joins('JOIN query_tn_ba as query_tn_ba3 on otus.taxon_name_id = query_tn_ba3.id')
    .where('taxon_determinations.position = 1')

  d = ::BiologicalAssociation
    .joins("JOIN collection_objects on collection_objects.id = biological_associations.biological_association_object_id AND biological_associations.biological_association_object_type = 'CollectionObject'")
    .joins("JOIN taxon_determinations on taxon_determinations.taxon_determination_object_id = collection_objects.id AND taxon_determinations.taxon_determination_object_type = 'CollectionObject'")
    .joins('JOIN otus on otus.id = taxon_determinations.otu_id')
    .joins('JOIN query_tn_ba as query_tn_ba4 on otus.taxon_name_id = query_tn_ba4.id')
    .where('taxon_determinations.position = 1')

  s << referenced_klass_union([a,b,c,d]).to_sql

  ::BiologicalAssociation.from('(' + s + ') as biological_associations')
end