Class: SampleManifestExcel::Upload::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/sample_manifest_excel/sample_manifest_excel/upload/base.rb

Overview

An upload will Find the start row based on the Sanger Sample Id column header cell Create a Data object based on the file. Extract the columns based on the headings in the spreadsheet Find the sanger sample id column Create some Rows Retrieve the sample manifest *Create a processor based on the sample manifest The Upload is only valid if the file, columns, sample manifest and processor are valid.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

rubocop:todo Metrics/AbcSize



39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 39

def initialize(attributes = {}) # rubocop:todo Metrics/AbcSize
  super
  @data = Upload::Data.new(file)
  @start_row = @data.start_row
  @columns = column_list.extract(data.header_row.reject(&:blank?) || [])
  @sanger_sample_id_column = columns.find_by(:name, :sanger_sample_id)
  @cache = Cache.new(self)
  @rows = Upload::Rows.new(data, columns, @cache)
  @sample_manifest = derive_sample_manifest
  @override = override || false
  @processor = create_processor
end

Instance Attribute Details

#cacheObject (readonly)

rubocop:todo Layout/LineLength



23
24
25
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 23

def cache
  @cache
end

#column_listObject

Returns the value of attribute column_list.



20
21
22
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 20

def column_list
  @column_list
end

#columnsObject (readonly)

rubocop:todo Layout/LineLength



23
24
25
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 23

def columns
  @columns
end

#dataObject (readonly)

rubocop:todo Layout/LineLength



23
24
25
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 23

def data
  @data
end

#fileObject

Returns the value of attribute file.



20
21
22
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 20

def file
  @file
end

#overrideObject

Returns the value of attribute override.



20
21
22
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 20

def override
  @override
end

#processorObject (readonly)

rubocop:todo Layout/LineLength



23
24
25
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 23

def processor
  @processor
end

#rowsObject (readonly)

rubocop:todo Layout/LineLength



23
24
25
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 23

def rows
  @rows
end

#sample_manifestObject (readonly)

rubocop:todo Layout/LineLength



23
24
25
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 23

def sample_manifest
  @sample_manifest
end

#sanger_sample_id_columnObject (readonly)

rubocop:todo Layout/LineLength



23
24
25
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 23

def sanger_sample_id_column
  @sanger_sample_id_column
end

#spreadsheetObject (readonly)

rubocop:todo Layout/LineLength



23
24
25
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 23

def spreadsheet
  @spreadsheet
end

#start_rowObject

Returns the value of attribute start_row.



20
21
22
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 20

def start_row
  @start_row
end

Instance Method Details

#broadcast_sample_manifest_updated_event(user) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 92

def broadcast_sample_manifest_updated_event(user)
  # Send to event warehouse
  sample_manifest.updated_broadcast_event(user, changed_samples.map(&:id))

  # Log legacy events: Show on history page, and may be used by reports.
  # We can get rid of these when:
  # - History page is updated with event warehouse viewer
  # - We've confirmed that no external reports use these events
  changed_samples.each { |sample| sample.handle_update_event(user) }
  changed_labware.each { |labware| labware.events.updated_using_sample_manifest!(user) }
end

#data_at(column_name) ⇒ Object



87
88
89
90
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 87

def data_at(column_name)
  required_column = columns.find_by(:name, column_name)
  rows.data_at(required_column.number) if required_column.present?
end

#derive_sample_manifestObject

The sample manifest is retrieved by taking the sanger sample id from the first row and retrieving its sample manifest. If it can’t be found the upload will fail.



62
63
64
65
66
67
68
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 62

def derive_sample_manifest
  return unless start_row.present? && sanger_sample_id_column.present?

  sanger_sample_id = data.cell(1, sanger_sample_id_column.number)
  SampleManifestAsset.find_by(sanger_sample_id:)&.sample_manifest ||
    Sample.find_by(sanger_sample_id:)&.sample_manifest
end

#failObject



113
114
115
116
117
118
119
120
121
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 113

def fail
  # If we've failed, do not update the manifest file, trying to do so
  # causes exceptions
  sample_manifest.association(:uploaded_document).reset
  # Errs here because sample_manifest.samples is a collection that's not empty in Rails 6.1,
  # but is empty in Rails 5.0. Therefore, reloaded the samples.
  sample_manifest.samples.reload
  sample_manifest.fail!
end

#inspectObject



52
53
54
55
56
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 52

def inspect
  # rubocop:todo Layout/LineLength
  "<#{self.class}: @file=#{file}, @columns=#{columns.inspect}, @start_row=#{start_row}, @sanger_sample_id_column=#{sanger_sample_id_column}, @data=#{data.inspect}>"
  # rubocop:enable Layout/LineLength
end

#process(tag_group) ⇒ Object

An upload can only be processed if the upload is valid. Processing involves updating the sample manifest and all of its associated samples.



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 73

def process(tag_group)
  # Temporarily disable accessioning until we invoke it explicitly
  # If we don't do this, then any accidental triggering of sample
  # saves will result in duplicate accessions
  Sample::Current.processing_manifest = true
  sample_manifest.last_errors = nil
  @cache.populate!
  processor.run(tag_group)

  processed?
ensure
  Sample::Current.processing_manifest = false
end

#register_stock_resourcesObject

If samples have been created, and it’s not a library plate/tube, register a stock_resource record in the MLWH



109
110
111
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 109

def register_stock_resources
  stock_receptacles_to_be_registered.each(&:register_stock!)
end

#trigger_accessioningObject



104
105
106
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 104

def trigger_accessioning
  changed_samples.each(&:accession)
end