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

Returns a new instance of Base.


35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 35

def initialize(attributes = {})
  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)

TODO: probably shouldn't add the cache here, do it another way


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

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)

TODO: probably shouldn't add the cache here, do it another way


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

def columns
  @columns
end

#dataObject (readonly)

TODO: probably shouldn't add the cache here, do it another way


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

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)

TODO: probably shouldn't add the cache here, do it another way


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

def processor
  @processor
end

#rowsObject (readonly)

TODO: probably shouldn't add the cache here, do it another way


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

def rows
  @rows
end

#sample_manifestObject (readonly)

TODO: probably shouldn't add the cache here, do it another way


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

def sample_manifest
  @sample_manifest
end

#sanger_sample_id_columnObject (readonly)

TODO: probably shouldn't add the cache here, do it another way


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

def sanger_sample_id_column
  @sanger_sample_id_column
end

#spreadsheetObject (readonly)

TODO: probably shouldn't add the cache here, do it another way


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

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


86
87
88
89
90
91
92
93
94
95
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 86

def broadcast_sample_manifest_updated_event(user)
  # Send to event warehouse
  sample_manifest.updated_broadcast_event(user, samples_to_be_broadcasted.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 updates with event warehouse viewer
  # - We've confirmed that no external reports use these events
  samples_to_be_broadcasted.each { |sample| sample.handle_update_event(user) }
  labware_to_be_broadcasted.each { |labware| labware.events.updated_using_sample_manifest!(user) }
end

#completeObject


97
98
99
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 97

def complete
  sample_manifest.finished!
end

#data_at(column_name) ⇒ Object


81
82
83
84
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 81

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.


56
57
58
59
60
61
62
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 56

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


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

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


48
49
50
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 48

def inspect
  "<#{self.class}: @file=#{file}, @columns=#{columns.inspect}, @start_row=#{start_row}, @sanger_sample_id_column=#{sanger_sample_id_column}, @data=#{data.inspect}>"
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.


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/base.rb', line 67

def process(tag_group)
  ActiveRecord::Base.transaction do
    sample_manifest.last_errors = nil
    sample_manifest.start!
    @cache.populate!
    processor.run(tag_group)
    return true if processed?

    # One of out post processing checks failed, something went wrong, so we
    # roll everything back
    raise ActiveRecord::Rollback
  end
end