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


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

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


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

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


89
90
91
92
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 89

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.


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

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: sanger_sample_id)&.sample_manifest ||
    Sample.find_by(sanger_sample_id: sanger_sample_id)&.sample_manifest
end

#failObject


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

def fail
  # If we've failed, do not update the manifest file, trying to do so
  # causes exceptions
  sample_manifest.association(:uploaded_document).reset
  sample_manifest.fail!
end

#inspectObject


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

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.


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

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
  sample_manifest.start!
  @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


111
112
113
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 111

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

#trigger_accessioningObject


106
107
108
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 106

def trigger_accessioning
  changed_samples.each(&:accession)
end