Class: CompoundAliquot

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, CompoundSampleHelper
Defined in:
app/models/compound_aliquot.rb

Overview

Factory class for creating Aliquots with compound samples in them. At time of writing, called from Request::SampleCompoundAliquotTransfer, in the context of a Request.

The inputs are: request A Request for transferring source_aliquots into a target receptacle. At time of writing, only used for a SequencingRequest, from a multiplex tube into a lane. source_aliquots A list of Aliquots that should be transferred into a single Aliquot in a target Receptacle, that contains a single compound sample. The list of component samples is derived from source_aliquots. Some attributes are transferred from the source aliquots onto the compound aliquot.

Constant Summary collapse

DUPLICATE_TAG_DEPTH_ERROR_MSG =
"Cannot create compound sample from following samples due to duplicate 'tag depth'"
MULTIPLE_PROJECTS_ERROR_MSG =
'Cannot create compound sample due to the component samples being under different projects.'
MULTIPLE_STUDIES_ERROR_MSG =
'Cannot create compound sample due to the component samples being under different studies.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CompoundSampleHelper

#find_or_create_compound_sample

Instance Attribute Details

#requestObject

Returns the value of attribute request.



24
25
26
# File 'app/models/compound_aliquot.rb', line 24

def request
  @request
end

#source_aliquotsObject

Returns the value of attribute source_aliquots.



25
26
27
# File 'app/models/compound_aliquot.rb', line 25

def source_aliquots
  @source_aliquots
end

Instance Method Details

#aliquot_attributesObject



60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/compound_aliquot.rb', line 60

def aliquot_attributes
  {
    tag_id: tag_id,
    tag2_id: tag2_id,
    library_type: default_library_type,
    study_id: default_compound_study.id,
    project_id: default_compound_project_id,
    library_id: default_library_id,
    sample: find_or_create_compound_sample(default_compound_study, component_samples)
  }
end

#component_samplesObject



27
28
29
# File 'app/models/compound_aliquot.rb', line 27

def component_samples
  @component_samples ||= source_aliquots.map(&:sample)
end

#default_compound_project_idObject



80
81
82
# File 'app/models/compound_aliquot.rb', line 80

def default_compound_project_id
  request.initial_project_id || source_aliquots.first.project_id
end

#default_compound_studyObject

Default Study & Project: Use the one from the request if present, Otherwise use the one from the source aliquots if it’s consistent Error if inconsistent (see validation)



76
77
78
# File 'app/models/compound_aliquot.rb', line 76

def default_compound_study
  request.initial_study || source_aliquots.first.study
end

#default_library_idObject



92
93
94
95
# File 'app/models/compound_aliquot.rb', line 92

def default_library_id
  library_ids = source_aliquots.map(&:library_id).uniq
  library_ids.size == 1 ? library_ids.first : nil
end

#default_library_typeObject

Less dangerous attributes: Use the one from the source aliquots if it’s consistent Otherwise, set it to nil for now, as the behaviour hasn’t been specified if it’s inconsistent



87
88
89
90
# File 'app/models/compound_aliquot.rb', line 87

def default_library_type
  library_types = source_aliquots.map(&:library_type).uniq
  library_types.size == 1 ? library_types.first : nil
end

#source_aliquots_have_same_projectObject



54
55
56
57
58
# File 'app/models/compound_aliquot.rb', line 54

def source_aliquots_have_same_project
  return if request.initial_project || source_aliquots.map(&:project_id).uniq.count == 1

  errors.add(:base, "#{MULTIPLE_PROJECTS_ERROR_MSG}: #{component_samples.map(&:name)}")
end

#source_aliquots_have_same_studyObject



48
49
50
51
52
# File 'app/models/compound_aliquot.rb', line 48

def source_aliquots_have_same_study
  return if request.initial_study || source_aliquots.map(&:study_id).uniq.count == 1

  errors.add(:base, "#{MULTIPLE_STUDIES_ERROR_MSG}: #{component_samples.map(&:name)}")
end

#tag2_idObject



105
106
107
# File 'app/models/compound_aliquot.rb', line 105

def tag2_id
  source_aliquots.first.tag2_id
end

#tag_depth_is_uniqueObject

Check that the component samples in the compound sample will be able to be distinguished - this is represented by them all having a unique ‘tag_depth’



42
43
44
45
46
# File 'app/models/compound_aliquot.rb', line 42

def tag_depth_is_unique
  return unless source_aliquots.pluck(:tag_depth).uniq!

  errors.add(:base, "#{DUPLICATE_TAG_DEPTH_ERROR_MSG}: #{component_samples.map(&:name)}")
end

#tag_idObject

Tags: We can assume that the tags will be the same for all source aliquots, as that’s essentially the definition of a compound sample - they all have the same tag1 and tag2 but different tag_depths.



101
102
103
# File 'app/models/compound_aliquot.rb', line 101

def tag_id
  source_aliquots.first.tag_id
end