Class: Descriptor::Gene

Inherits:
Descriptor
  • Object
show all
Defined in:
app/models/descriptor/gene.rb

Overview

A Descriptor::Gene defines a set of sequeces, i.e. column in a “matrix” whose cells contains sequences that match the a set (logical AND) of GeneAttributes.

The column (conceptually set of sequences) is populated by things that match (all) of the GeneAttibutes attached to the Descriptor::Gene.

Constant Summary

Constant Summary

Constants inherited from Descriptor

ALTERNATE_VALUES_FOR

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Method Summary (collapse)

Methods inherited from Descriptor

human_name, #qualitative?, #short_name_is_shorter, #sv_short_name_is_short, #type_is_subclassed

Methods included from SoftValidation

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

Methods included from Housekeeping

#has_polymorphic_relationship?

Instance Method Details

- (Array) gene_attribute_pairs

Returns of arrays, like [[id, type], [id, type]]

Returns:

  • (Array)

    of arrays, like [[id, type], [id, type]]



56
57
58
# File 'app/models/descriptor/gene.rb', line 56

def gene_attribute_pairs
   gene_attributes.pluck(:sequence_id, :sequence_relationship_type)
end

- (Object) gene_attribute_sequence_ids



60
61
62
# File 'app/models/descriptor/gene.rb', line 60

def gene_attribute_sequence_ids
  gene_attribute_pairs.collect{|id, z| id}
end

- (Object) gene_attribute_sequence_retlationship_types



64
65
66
# File 'app/models/descriptor/gene.rb', line 64

def gene_attribute_sequence_retlationship_types
  gene_attribute_pairs.collect{|id, z| z}
end

- (Object) sequences

z = 'SELECT s.* FROM sequences s' +

' INNER JOIN sequence_relationships sr ON sr.object_sequence_id = s.id' +
js + 
' GROUP BY s.id' +
" HAVING COUNT(sr.object_sequence_id) = #{data.count};"


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/descriptor/gene.rb', line 23

def sequences
  return Sequence.none if !gene_attributes.any?

  data = gene_attribute_pairs

  s = Sequence.arel_table
  sr = SequenceRelationship.arel_table

  j = s.alias('j') # required for group/having purposes

  b = s.project(j[Arel.star]).from(j)
    .join(sr )
    .on(sr['object_sequence_id'].eq(j['id']))

  # Build an aliased join for each set of attributes
  data.each do |id, type|
    sr_a = sr.alias("b#{id}")
    b = b.join(sr_a).on(
      sr_a['object_sequence_id'].eq(j['id']),
      sr_a['type'].eq(type), 
      sr_a['subject_sequence_id'].eq(id)
    )
  end

  b = b.group(j['id']).having(sr['object_sequence_id'].count.eq(data.count))
  b = b.as('foo')

  # AHA from http://stackoverflow.com/questions/28568205/rails-4-arel-join-on-subquery
  Sequence.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(s['id']))))
end