Class: TagSubstitution::Substitution

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/tag_substitution/substitution.rb

Overview

Used by TagSubstitution to handle the individual substitutions per library.

Author:

  • grl

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, tag_substituter = nil) ⇒ Substitution

Generate a tag substitutes for a single library

Parameters:

  • attributes (Hash)

    Details describing the library and its new state sample_id: The sample_id for the aliquots to update library_id: The library_id for the aliquots to update original_tag_id: The current tag_id of the aliquots to update substitute_tag_id: The new tag_id of the aliquots to update original_tag2_id: The current tag2_id of the aliquots to update substitute_tag2_id: The new tag2_id of the aliquots to update OR aliquot: Provide an aliquot to act as a template. Useful for pre-populating forms

  • tag_substituter (TagSubstitution) (defaults to: nil)

    The parent tag substituter


33
34
35
36
37
# File 'app/models/tag_substitution/substitution.rb', line 33

def initialize(attributes, tag_substituter = nil)
  super(attributes.extract!(:sample_id, :library_id, :original_tag_id, :substitute_tag_id, :original_tag2_id, :substitute_tag2_id, :aliquot))
  @other_attributes = attributes
  @tag_substituter = tag_substituter
end

Instance Attribute Details

#library_idObject

Returns the value of attribute library_id


11
12
13
# File 'app/models/tag_substitution/substitution.rb', line 11

def library_id
  @library_id
end

#original_tag2_idObject

Returns the value of attribute original_tag2_id


12
13
14
# File 'app/models/tag_substitution/substitution.rb', line 12

def original_tag2_id
  @original_tag2_id
end

#original_tag_idObject

Returns the value of attribute original_tag_id


12
13
14
# File 'app/models/tag_substitution/substitution.rb', line 12

def original_tag_id
  @original_tag_id
end

#sample_idObject

Returns the value of attribute sample_id


11
12
13
# File 'app/models/tag_substitution/substitution.rb', line 11

def sample_id
  @sample_id
end

#substitute_tag2_idObject

Returns the value of attribute substitute_tag2_id


12
13
14
# File 'app/models/tag_substitution/substitution.rb', line 12

def substitute_tag2_id
  @substitute_tag2_id
end

#substitute_tag_idObject

Returns the value of attribute substitute_tag_id


12
13
14
# File 'app/models/tag_substitution/substitution.rb', line 12

def substitute_tag_id
  @substitute_tag_id
end

#tag_substituterObject (readonly)

Returns the value of attribute tag_substituter


12
13
14
# File 'app/models/tag_substitution/substitution.rb', line 12

def tag_substituter
  @tag_substituter
end

#tag_substitutionObject (readonly)

Returns the value of attribute tag_substitution


12
13
14
# File 'app/models/tag_substitution/substitution.rb', line 12

def tag_substitution
  @tag_substitution
end

Instance Method Details

#aliquot=(aliquot) ⇒ Object

Used when seeding from a template asset Lets us populate web forms


57
58
59
60
61
62
63
64
65
# File 'app/models/tag_substitution/substitution.rb', line 57

def aliquot=(aliquot)
  @sample_id = aliquot.sample_id
  @sample = aliquot.sample
  @library_id = aliquot.library_id
  @original_tag_id = aliquot.tag_id
  @substitute_tag_id = aliquot.tag_id
  @original_tag2_id = aliquot.tag2_id
  @substitute_tag2_id = aliquot.tag2_id
end

#comment(oligo_index) ⇒ String

Generates a comment to describe the substitutions performed The oligo index is passed in as part of a performance optimiztion to avoid repeated hits to the database to fetch oligo sequences

Parameters:

  • oligo_index (Hash)

    A hash of oligo sequences indexed by oligo id.

Returns:

  • (String)

    A description of the substitution


107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/tag_substitution/substitution.rb', line 107

def comment(oligo_index)
  return '' unless updated?

  comment = +"Sample #{sample_id}:"
  comment << " Tag changed from #{oligo_index[original_tag_id]} to #{oligo_index[substitute_tag_id]};" if substitute_tag?
  comment << " Tag2 changed from #{oligo_index[original_tag2_id]} to #{oligo_index[substitute_tag2_id]};" if substitute_tag2?
  @other_attributes.each do |k, v|
    comment << " #{k} changed to #{v};"
  end
  comment
end

#matching_aliquotsArray<Integer>

All aliquots which match the criteria

Returns:

  • (Array<Integer>)

    An array of aliquot ids.


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

def matching_aliquots
  @matching_aliquots ||= find_matching_aliquots
end

#nullify_tagsObject

Nullify tags sets all tags to null. We need to do this first as otherwise we introduce tag clashes while performing substitutions


80
81
82
83
84
85
86
# File 'app/models/tag_substitution/substitution.rb', line 80

def nullify_tags
  tags_hash = {}
  tags_hash[:tag_id]  = nil if substitute_tag?
  tags_hash[:tag2_id] = nil if substitute_tag2?
  # We DO NOT want to trigger validations here
  Aliquot.where(id: matching_aliquots).update_all(tags_hash) if tags_hash.present? # rubocop:disable Rails/SkipsModelValidations
end

#sampleObject

Returns the sample. Caution! Will be slow if not populated by aliquot


68
69
70
# File 'app/models/tag_substitution/substitution.rb', line 68

def sample
  @sample ||= Sample.find(@sample_id)
end

#substitute_tagsVoid

Applies the new tags to the aliquots.

Returns:

  • (Void)

92
93
94
95
96
97
98
99
# File 'app/models/tag_substitution/substitution.rb', line 92

def substitute_tags
  Aliquot.where(id: matching_aliquots).find_each do |aliquot|
    aliquot.tag_id = substitute_tag_id if substitute_tag?
    aliquot.tag2_id = substitute_tag2_id if substitute_tag2?
    aliquot.update(@other_attributes) if @other_attributes.present?
    aliquot.save!
  end
end

#tag_idsArray<Integer>

Returns an array of all associated tag ids. Excludes untagged representation (typically -1)

Returns:

  • (Array<Integer>)

    All tag ids which should correspond to actual tags. -1, nil etc. are ignored.


123
124
125
# File 'app/models/tag_substitution/substitution.rb', line 123

def tag_ids
  [original_tag_id, substitute_tag_id, original_tag2_id, substitute_tag2_id].select { |id| id&.positive? }
end

#tag_pairArray<Integer>

A two value array representing the tag ids AFTER substitution.

Returns:

  • (Array<Integer>)

    Array of tag_id, followed by tag2_id.


131
132
133
# File 'app/models/tag_substitution/substitution.rb', line 131

def tag_pair
  [substitute_tag_id, substitute_tag2_id]
end

#tag_substitutions?Boolean

Returns:

  • (Boolean)

135
136
137
# File 'app/models/tag_substitution/substitution.rb', line 135

def tag_substitutions?
  original_tag_id || original_tag2_id
end