Module: Api::Messages::FlowcellIO::LaneExtensions

Included in:
SequencingRequest
Defined in:
app/models/api/messages/flowcell_io.rb

Overview

Included in SequencingRequest

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/api/messages/flowcell_io.rb', line 8

def self.included(base)
  base.class_eval do
    delegate :position, to: :batch_request

    def mx_library
      asset.external_identifier
    end

    def manual_qc
      MANUAL_QC_BOOLS[target_asset.try(:qc_state)]
    end

    def flowcell_barcode
      detect_descriptor(flowcell_identifier)
    end

    def lane_samples
      target_asset.aliquots
    end

    def lane
      target_asset.labware
    end

    delegate :spiked_in_buffer, :external_release, to: :lane, allow_nil: true

    def controls
      spiked_in_buffer.present? ? spiked_in_buffer.aliquots : []
    end

    def lane_identifier
      target_asset_id
    end

    def request_purpose_key
      request_purpose.try(:key)
    end

    def workflow
      detect_descriptor('Workflow (Standard or Xp)')
    end

    def spiked_phix_barcode
      spiked_in_buffer&.human_barcode
    end

    def spiked_phix_percentage
      detect_float_descriptor('PhiX %', '%')
    end

    def loading_concentration
      detect_float_descriptor('Lane loading concentration (pM)', 'pM')
    end

    # Currently the tangled mass that is descriptors does little in the way of validation
    # This means non-float values have been entered in some case (such as ranges)
    # This extracts floats only until the data can be repaired, and validation added to prevent
    # bad data from being added in future
    def detect_float_descriptor(name, ignored_unit)
      value = detect_descriptor(name)
      return nil if value.nil?

      begin
        # If someone has added the units to the input, strip them off then convert to a float
        # We also strip whitespace.
        # However if float conversion fails, then the input is unsuitable.
        # Note: .to_f is too permissive here
        Float(value.gsub(ignored_unit, '').strip)
      rescue ArgumentError
        nil
      end
    end

    def detect_descriptor(name)
      lab_events.each do |e|
        e.descriptor_value_for(name).tap { |bc| return bc if bc.present? }
      end
      nil # We have no flowcell barcode
    end
  end
end