Class: TagSubstitution::Substitution
- Inherits:
-
Object
- Object
- TagSubstitution::Substitution
- Includes:
- ActiveModel::Model
- Defined in:
- app/models/tag_substitution/substitution.rb
Overview
Used by TagSubstitution to handle the individual substitutions per library.
Instance Attribute Summary collapse
-
#library_id ⇒ Object
Returns the value of attribute library_id.
-
#original_tag2_id ⇒ Object
Returns the value of attribute original_tag2_id.
-
#original_tag_id ⇒ Object
Returns the value of attribute original_tag_id.
-
#sample_id ⇒ Object
Returns the value of attribute sample_id.
-
#substitute_tag2_id ⇒ Object
Returns the value of attribute substitute_tag2_id.
-
#substitute_tag_id ⇒ Object
Returns the value of attribute substitute_tag_id.
-
#tag_substituter ⇒ Object
readonly
Returns the value of attribute tag_substituter.
-
#tag_substitution ⇒ Object
readonly
Returns the value of attribute tag_substitution.
Instance Method Summary collapse
-
#aliquot=(aliquot) ⇒ Object
Used when seeding from a template asset Lets us populate web forms.
-
#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.
-
#initialize(attributes, tag_substituter = nil) ⇒ Substitution
constructor
Generate a tag substitutes for a single library.
-
#matching_aliquots ⇒ Array<Integer>
All aliquots which match the criteria.
-
#nullify_tags ⇒ Object
Nullify tags sets all tags to null.
-
#sample ⇒ Object
Returns the sample.
-
#substitute_tags ⇒ Void
Applies the new tags to the aliquots.
-
#tag_ids ⇒ Array<Integer>
Returns an array of all associated tag ids.
-
#tag_pair ⇒ Array<Integer>
A two value array representing the tag ids AFTER substitution.
- #tag_substitutions? ⇒ Boolean
- #updated? ⇒ Boolean
Constructor Details
#initialize(attributes, tag_substituter = nil) ⇒ Substitution
Generate a tag substitutes for a single library
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/models/tag_substitution/substitution.rb', line 39 def initialize(attributes, tag_substituter = nil) # rubocop:todo Metrics/MethodLength 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_id ⇒ Object
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_id ⇒ Object
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_id ⇒ Object
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_id ⇒ Object
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_id ⇒ Object
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_id ⇒ Object
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_substituter ⇒ Object (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_substitution ⇒ Object (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
73 74 75 76 77 78 79 80 81 |
# File 'app/models/tag_substitution/substitution.rb', line 73 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
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'app/models/tag_substitution/substitution.rb', line 127 def comment(oligo_index) return '' unless updated? comment = "Sample #{sample_id}:" if substitute_tag? comment << " Tag changed from #{oligo_index[original_tag_id]} to #{oligo_index[substitute_tag_id]};" end if substitute_tag2? comment << " Tag2 changed from #{oligo_index[original_tag2_id]} to #{oligo_index[substitute_tag2_id]};" end @other_attributes.each { |k, v| comment << " #{k} changed to #{v};" } comment end |
#matching_aliquots ⇒ Array<Integer>
All aliquots which match the criteria
90 91 92 |
# File 'app/models/tag_substitution/substitution.rb', line 90 def matching_aliquots @matching_aliquots ||= find_matching_aliquots end |
#nullify_tags ⇒ Object
Nullify tags sets all tags to null. We need to do this first as otherwise we introduce tag clashes while performing substitutions
96 97 98 99 100 101 102 103 |
# File 'app/models/tag_substitution/substitution.rb', line 96 def = {} [:tag_id] = nil if substitute_tag? [:tag2_id] = nil if substitute_tag2? # We DO NOT want to trigger validations here Aliquot.where(id: matching_aliquots).update_all() if .present? # rubocop:disable Rails/SkipsModelValidations end |
#sample ⇒ Object
Returns the sample. Caution! Will be slow if not populated by aliquot
84 85 86 |
# File 'app/models/tag_substitution/substitution.rb', line 84 def sample @sample ||= Sample.find(@sample_id) end |
#substitute_tags ⇒ Void
Applies the new tags to the aliquots.
109 110 111 112 113 114 115 116 117 118 |
# File 'app/models/tag_substitution/substitution.rb', line 109 def 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_ids ⇒ Array<Integer>
Returns an array of all associated tag ids. Excludes untagged representation (typically -1)
145 146 147 |
# File 'app/models/tag_substitution/substitution.rb', line 145 def tag_ids [original_tag_id, substitute_tag_id, original_tag2_id, substitute_tag2_id].select { |id| id&.positive? } end |
#tag_pair ⇒ Array<Integer>
A two value array representing the tag ids AFTER substitution.
153 154 155 |
# File 'app/models/tag_substitution/substitution.rb', line 153 def tag_pair [substitute_tag_id, substitute_tag2_id] end |
#tag_substitutions? ⇒ Boolean
157 158 159 |
# File 'app/models/tag_substitution/substitution.rb', line 157 def tag_substitutions? original_tag_id || original_tag2_id end |
#updated? ⇒ Boolean
161 162 163 |
# File 'app/models/tag_substitution/substitution.rb', line 161 def updated? substitute_tag? || substitute_tag2? || @other_attributes.present? end |