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



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_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



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

Parameters:

  • oligo_index (Hash)

    A hash of oligo sequences indexed by oligo id.

Returns:

  • (String)

    A description of the substitution



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_aliquotsArray<Integer>

All aliquots which match the criteria

Returns:

  • (Array<Integer>)

    An array of aliquot ids.



90
91
92
# File 'app/models/tag_substitution/substitution.rb', line 90

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



96
97
98
99
100
101
102
103
# File 'app/models/tag_substitution/substitution.rb', line 96

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



84
85
86
# File 'app/models/tag_substitution/substitution.rb', line 84

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

#substitute_tagsVoid

Applies the new tags to the aliquots.

Returns:

  • (Void)


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

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.



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_pairArray<Integer>

A two value array representing the tag ids AFTER substitution.

Returns:

  • (Array<Integer>)

    Array of tag_id, followed by tag2_id.



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

Returns:

  • (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

Returns:

  • (Boolean)


161
162
163
# File 'app/models/tag_substitution/substitution.rb', line 161

def updated?
  substitute_tag? || substitute_tag2? || @other_attributes.present?
end