Class: SampleManifest::PlateBehaviour::Base

Inherits:
Object
  • Object
show all
Includes:
CoreBehaviour::NoSpecializedValidation, CoreBehaviour::Shared
Defined in:
app/models/sample_manifest/plate_behaviour.rb

Direct Known Subclasses

LibraryPlateBehaviour::Core, Core

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CoreBehaviour::NoSpecializedValidation

#specialized_fields, #validate_specialized_fields

Methods included from CoreBehaviour::Shared

#details, #generate_sanger_ids, included

Constructor Details

#initialize(manifest) ⇒ Base

Returns a new instance of Base


8
9
10
11
# File 'app/models/sample_manifest/plate_behaviour.rb', line 8

def initialize(manifest)
  @manifest = manifest
  @plates = []
end

Instance Attribute Details

#platesObject (readonly)

Returns the value of attribute plates


6
7
8
# File 'app/models/sample_manifest/plate_behaviour.rb', line 6

def plates
  @plates
end

Instance Method Details

#acceptable_purposesObject


17
18
19
# File 'app/models/sample_manifest/plate_behaviour.rb', line 17

def acceptable_purposes
  PlatePurpose.for_submissions
end

#default_purposeObject


21
22
23
# File 'app/models/sample_manifest/plate_behaviour.rb', line 21

def default_purpose
  PlatePurpose.stock_plate_purpose
end

#details_arrayObject


74
75
76
77
78
79
80
81
82
# File 'app/models/sample_manifest/plate_behaviour.rb', line 74

def details_array
  @details_array ||= sample_manifest_assets.includes(asset: [:map, :aliquots, { plate: :barcodes }]).map do |sample_manifest_asset|
    {
      barcode: sample_manifest_asset.asset.plate.human_barcode,
      position: sample_manifest_asset.asset.map_description,
      sample_id: sample_manifest_asset.sanger_sample_id
    }
  end
end

#generateObject


13
14
15
# File 'app/models/sample_manifest/plate_behaviour.rb', line 13

def generate
  @plates = generate_plates(purpose)
end

#generate_wells(well_data, plates) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/sample_manifest/plate_behaviour.rb', line 29

def generate_wells(well_data, plates)
  # Generate the wells, samples & requests asynchronously.
  generate_wells_for_plates(well_data, plates) do |this_plates_well_data, plate|
    generate_wells_asynchronously(
      this_plates_well_data.map { |map, sample_id| [map.id, sample_id] },
      plate.id
    )
  end

  # Ensure we maintain the information we need for printing labels and generating
  # the CSV file
  @plates = plates.sort_by(&:human_barcode)

  @details_array = plates.flat_map do |plate|
    well_data.slice!(0, plate.size).map do |map, sample_id|
      {
        barcode: plate.human_barcode,
        position: map.description,
        sample_id: sample_id
      }
    end
  end
end

#generate_wells_job(wells_for_plate, plate) ⇒ Object

Called by GenerateWellsJob and builds the wells


95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/sample_manifest/plate_behaviour.rb', line 95

def generate_wells_job(wells_for_plate, plate)
  wells_for_plate.map do |map, sanger_sample_id|
    plate.wells.create!(map: map) do |well|
      SampleManifestAsset.create(sanger_sample_id: sanger_sample_id,
                                 asset: well,
                                 sample_manifest: @manifest)
    end
  end
  RequestFactory.create_assets_requests(plate.wells, study)
  plate.events.created_using_sample_manifest!(@manifest.user)
end

#included_resourcesObject


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

def included_resources
  [{ sample: :sample_metadata, asset: { plate: :barcodes } }]
end

#io_samplesObject


53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/sample_manifest/plate_behaviour.rb', line 53

def io_samples
  samples.map do |sample|
    container = sample.primary_receptacle
    {
      sample: sample,
      container: {
        barcode: container.plate.human_barcode,
        position: container.map.description.sub(/^([^\d]+)(\d)$/, '\10\2')
      }
    }
  end
end

#labwareObject Also known as: printables

We use the barcodes here as we may need to reference the plates before the delayed job has passed


89
90
91
# File 'app/models/sample_manifest/plate_behaviour.rb', line 89

def labware
  plates | labware_from_barcodes
end

#labware=(labware) ⇒ Object


84
85
86
# File 'app/models/sample_manifest/plate_behaviour.rb', line 84

def labware=(labware)
  @plates = labware
end

#updated_by!(user, samples) ⇒ Object


66
67
68
69
70
71
72
# File 'app/models/sample_manifest/plate_behaviour.rb', line 66

def updated_by!(user, samples)
  # It's more efficient to look for the wells with the samples than to look for the assets from the samples
  # themselves as the former can use named_scopes where as the latter is an array that needs iterating over.
  Plate.with_sample(samples).each do |plate|
    plate.events.updated_using_sample_manifest!(user)
  end
end