Class: SampleManifestExcel::Upload::Processor::Plate

Inherits:
Base
  • Object
show all
Defined in:
app/sample_manifest_excel/sample_manifest_excel/upload/processor/plate.rb

Overview

TODO: had to explicitly specify the namespace for Base here otherwise it picks up Upload::Base

Processor to handle plate manifest uploads.

Instance Attribute Summary

Attributes inherited from Base

#upload

Instance Method Summary collapse

Methods inherited from Base

#aliquots_updated?, #downstream_aliquots_updated?, #initialize, #processed?, #run, #sample_manifest_updated?, #samples_updated?, #substitutions, #type, #update_sample_manifest, #update_samples_and_aliquots

Constructor Details

This class inherits a constructor from SampleManifestExcel::Upload::Processor::Base

Instance Method Details

#check_for_barcodes_uniqueObject

For plate manifests the barcodes (sanger plate id column) should be the same for each well from the same plate, and different for each plate. Uniqueness of foreign barcodes in the database is checked in the specialised field sanger_plate_id.


14
15
16
17
18
19
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/processor/plate.rb', line 14

def check_for_barcodes_unique
  duplicated_barcode_row, err_msg = duplicate_barcodes
  return if duplicated_barcode_row.nil?

  errors.add(:base, "Barcode duplicated at row: #{duplicated_barcode_row.number}. #{err_msg}")
end

#duplicate_barcodesObject

Return the row of the first encountered barcode mismatch


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/processor/plate.rb', line 22

def duplicate_barcodes
  return nil, nil unless upload.respond_to?('rows')

  unique_bcs = {}
  unique_plates = {}
  upload.rows.each do |row|
    next if row.columns.blank? || row.data.blank?

    plate_barcode = row.value('sanger_plate_id')
    sample_id = row.value('sanger_sample_id')
    next if plate_barcode.nil? || sample_id.nil?

    plate_id_for_sample = find_plate_id_for_sample_id(sample_id)
    next if plate_id_for_sample.nil?

    # Check that a barcode is used for only one plate
    if unique_bcs.key?(plate_barcode)
      err_msg = 'Barcode is used in multiple plates.'
      return row, err_msg unless unique_bcs[plate_barcode] == plate_id_for_sample
    else
      unique_bcs[plate_barcode] = plate_id_for_sample
    end
    # Check that a plate has only one barcode
    if unique_plates.key?(plate_id_for_sample)
      err_msg = 'Plate has multiple barcodes.'
      return row, err_msg unless unique_plates[plate_id_for_sample] == plate_barcode
    else
      unique_plates[plate_id_for_sample] = plate_barcode
    end
  end
  [nil, nil]
end

#find_plate_id_for_sample_id(sample_id) ⇒ Object


55
56
57
58
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/processor/plate.rb', line 55

def find_plate_id_for_sample_id(sample_id)
  sample_manifest_asset = SampleManifestAsset.find_by(sanger_sample_id: sample_id)
  sample_manifest_asset.labware&.id
end