Class: Aliquot

Inherits:
ApplicationRecord show all
Includes:
DataForSubstitution, AliquotIndexer::AliquotScopes, Api::AliquotIO::Extensions, Api::Messages::FlowcellIO::AliquotExtensions, Api::Messages::QcResultIO::AliquotExtensions, Uuid::Uuidable
Defined in:
app/models/aliquot.rb

Overview

A note on tags: Aliquots can have up to two tags attached, the i7 (tag) and the i5(tag2) Tags are short DNA sequences which can be used to track samples following pooling. If two samples with the same tags are pooled together it becomes impossible to distinguish between them. To avoid this we have an index which ensures unique tags are maintained per pool. (Limitation: This restriction assumes that each oligo sequence is represented only once in the database. This is not the case, so additional slower checks are required where cross tag group pools are possible) MySQL indexes treat NULL values as non identical, so -1 (UNASSIGNED_TAG) is used to represent an untagged well. We have some performance optimizations in place to avoid trying to look up tag -1

See Also:

Defined Under Namespace

Modules: Aliquotable, DataForSubstitution, DeprecatedBehaviours, Remover Classes: InsertSize

Constant Summary collapse

TagClash =

TagClash = Class.new(ActiveRecord::RecordInvalid)

Class.new(StandardError)
TAG_COUNT_NAMES =
%w[Untagged Single Dual].freeze
UNASSIGNED_TAG =

It may have a tag but not necessarily. If it does, however, that tag needs to be unique within the receptacle. To ensure that there can only be one untagged aliquot present in a receptacle we use a special value for tag_id, rather than NULL which does not work in MySQL. It also works because the unassigned tag ID never gets matched for a Tag and so the result is nil!

-1

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataForSubstitution

#changes, #generate_substitution_hash, #original_tag2_id, #original_tag_id, #other_attributes_for_substitution, #substitute_tag2_id, #substitute_tag_id, #substitution_hash, #tag2_id_substitution, #tag_id_substitution

Methods included from Api::AliquotIO::Extensions

included

Methods included from AliquotIndexer::AliquotScopes

included

Methods included from Uuid::Uuidable

included, #unsaved_uuid!, #uuid

Methods inherited from ApplicationRecord

convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Methods included from Warren::BroadcastMessages

#broadcast, included, #queue_associated_for_broadcast, #queue_for_broadcast, #warren

Class Method Details

.count_by_project_cost_codeObject

returns a hash, where keys are cost_codes and values are number of aliquots related to particular cost code {'cost_code_1' => 20, 'cost_code_2' => 3, 'cost_code_3' => 8 } this one does not work, as project is not always there: joins(project: :project_metadata).group(“project_metadata.project_cost_code”).count


94
95
96
97
98
99
# File 'app/models/aliquot.rb', line 94

def self.count_by_project_cost_code
  joins('LEFT JOIN projects ON aliquots.project_id = projects.id')
    .joins('LEFT JOIN project_metadata ON project_metadata.project_id = projects.id')
    .group('project_metadata.project_cost_code')
    .count
end

.equivalent_attributesObject

Returns a list of attributes which must be the same for two Aliquots to be considered #equivalent? Generated dynamically to avoid accidental introduction of false positives when new columns are added


104
105
106
# File 'app/models/aliquot.rb', line 104

def self.equivalent_attributes
  @equivalent_attributes ||= attribute_names - %w[id receptacle_id created_at updated_at]
end

Instance Method Details

#aliquot_index_valueObject


108
109
110
# File 'app/models/aliquot.rb', line 108

def aliquot_index_value
  aliquot_index.try(:aliquot_index)
end

#created_with_request_optionsObject


112
113
114
115
116
117
118
# File 'app/models/aliquot.rb', line 112

def created_with_request_options
  {
    fragment_size_required_from: insert_size_from,
    fragment_size_required_to: insert_size_to,
    library_type: library_type
  }
end

#dup(params = {}) ⇒ Object

Cloning an aliquot should unset the receptacle ID because otherwise it won't get reassigned. We should also reset the timestamp information as this is a new aliquot really. Any options passed in as parameters will override the aliquot defaults


173
174
175
176
177
# File 'app/models/aliquot.rb', line 173

def dup(params = {})
  super().tap do |cloned_aliquot|
    cloned_aliquot.assign_attributes(params)
  end
end

#equivalent?(other) ⇒ Boolean

Unlike the above methods, which allow untagged to match with tagged, this looks for exact matches only only id, timestamps and receptacles are excluded

Returns:

  • (Boolean)

200
201
202
203
204
# File 'app/models/aliquot.rb', line 200

def equivalent?(other)
  Aliquot.equivalent_attributes.all? do |attrib|
    send(attrib) == other.send(attrib)
  end
end

#matches?(object) ⇒ Boolean

Returns:

  • (Boolean)

184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/models/aliquot.rb', line 184

def matches?(object)
  # NOTE: This function is directional, and assumes that the downstream aliquot
  # is checking the upstream aliquot
  case
  when sample_id != object.sample_id                                                   then false # The samples don't match
  when object.library_id.present?      && (library_id      != object.library_id)       then false # Our libraries don't match.
  when object.bait_library_id.present? && (bait_library_id != object.bait_library_id)  then false # We have different bait libraries
  when (no_tag1? && object.tag1?) || (no_tag2? && object.tag2?)
    raise StandardError, 'Tag missing from downstream aliquot' # The downstream aliquot is untagged, but is tagged upstream. Something is wrong!
  when object.no_tags? then true # The upstream aliquot was untagged, we don't need to check tags
  else (object.no_tag1? || (tag_id == object.tag_id)) && (object.no_tag2? || (tag2_id == object.tag2_id)) # Both aliquots are tagged, we need to check if they match
  end
end

#no_tag1?Boolean

Validating the uniqueness of tags in rails was causing issues, as it was resulting the in the preform_transfer_of_contents in transfer request to fail, without any visible sign that something had gone wrong. This essentially meant that tag clashes would result in sample dropouts. (presumably because << triggers save not save!)

Returns:

  • (Boolean)

124
125
126
# File 'app/models/aliquot.rb', line 124

def no_tag1?
  tag_id == UNASSIGNED_TAG || tag.nil?
end

#no_tag2?Boolean

Returns:

  • (Boolean)

132
133
134
# File 'app/models/aliquot.rb', line 132

def no_tag2?
  tag2_id == UNASSIGNED_TAG || tag2.nil?
end

#no_tags?Boolean

Returns:

  • (Boolean)

144
145
146
# File 'app/models/aliquot.rb', line 144

def no_tags?
  no_tag1? && no_tag2?
end

#set_library(force: false) ⇒ Object

Cop disabled as this isn't a setter


166
167
168
# File 'app/models/aliquot.rb', line 166

def set_library(force: false) # rubocop:disable Naming/AccessorMethodName
  self.library = receptacle if library.nil? || force
end

#tagObject

Optimization: Avoids us hitting the database for untagged aliquots


157
158
159
# File 'app/models/aliquot.rb', line 157

def tag
  super unless tag_id == UNASSIGNED_TAG
end

#tag1?Boolean

Returns:

  • (Boolean)

128
129
130
# File 'app/models/aliquot.rb', line 128

def tag1?
  !no_tag1?
end

#tag2Object


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

def tag2
  super unless tag2_id == UNASSIGNED_TAG
end

#tag2?Boolean

Returns:

  • (Boolean)

136
137
138
# File 'app/models/aliquot.rb', line 136

def tag2?
  !no_tag2?
end

#tag_count_nameObject


152
153
154
# File 'app/models/aliquot.rb', line 152

def tag_count_name
  TAG_COUNT_NAMES[tag_count]
end

#tags?Boolean

Returns:

  • (Boolean)

140
141
142
# File 'app/models/aliquot.rb', line 140

def tags?
  !no_tags?
end

#tags_combinationObject


148
149
150
# File 'app/models/aliquot.rb', line 148

def tags_combination
  [tag.try(:oligo), tag2.try(:oligo)]
end

#update_quality(suboptimal_quality) ⇒ Object


179
180
181
182
# File 'app/models/aliquot.rb', line 179

def update_quality(suboptimal_quality)
  self.suboptimal = suboptimal_quality
  save!
end