Class: SampleManifestExcel::Upload::Row

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

Overview

A Row relates to a row in a sample manifest spreadsheet. Each Row relates to a sample Required fields: *number: Number of the row which is used for error tracking *data: An array of sample data *columns: The columns which relate to the data.

Constant Summary

Constants included from Converters

Converters::BLANK_CHARS, Converters::BLANK_CHARS_REGEXP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Converters

#strip_all_blanks

Constructor Details

#initialize(attributes = {}) ⇒ Row

Finds a sample based on the sanger_sample_id column. Must exist for row to be valid. Creates the specialised fields for updating the sample based on the passed columns


28
29
30
31
32
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 28

def initialize(attributes = {})
  super
  @cache ||= SampleManifestAsset
  @sanger_sample_id ||= value(:sanger_sample_id).presence if columns.present? && data.present?
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.


16
17
18
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 16

def cache
  @cache
end

#columnsObject

Returns the value of attribute columns.


16
17
18
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 16

def columns
  @columns
end

#dataObject

Returns the value of attribute data.


16
17
18
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 16

def data
  @data
end

#numberObject

Returns the value of attribute number.


16
17
18
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 16

def number
  @number
end

#sanger_sample_idObject (readonly)

Returns the value of attribute sanger_sample_id.


17
18
19
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 17

def sanger_sample_id
  @sanger_sample_id
end

Instance Method Details

#aliquotObject


58
59
60
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 58

def aliquot
  @aliquot ||= manifest_asset.aliquot
end

#aliquot_transferred?Boolean

Returns:

  • (Boolean)

148
149
150
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 148

def aliquot_transferred?
  @aliquot_transferred
end

#assetObject


62
63
64
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 62

def asset
  @asset ||= manifest_asset.asset
end

#at(col_num) ⇒ Object

Finds the data value for a particular column. Offset by 1. Columns have numbers data is an array


37
38
39
40
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 37

def at(col_num)
  val = data[col_num - 1]
  strip_all_blanks(val)
end

#changed?Boolean

rubocop:enable Metrics/MethodLength

Returns:

  • (Boolean)

101
102
103
104
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 101

def changed?
  @sample_updated && sample.previous_changes.present? || .previous_changes.present? ||
    aliquot.previous_changes.present?
end

#empty?Boolean

Returns:

  • (Boolean)

152
153
154
155
156
157
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 152

def empty?
  primary_column = 'supplier_name'
  return true unless columns.present? && columns.valid? && columns.names.include?(primary_column)

  value(primary_column).blank?
end

#first?Boolean

Returns:

  • (Boolean)

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

def first?
  number == 1
end

#labwareObject


159
160
161
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 159

def labware
  sample.primary_receptacle.labware
end

#metadataObject


66
67
68
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 66

def 
  @metadata ||= sample.
end

#reuploaded?Boolean

Returns:

  • (Boolean)

128
129
130
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 128

def reuploaded?
  @reuploaded || false
end

#row_titleObject

Used for errors


54
55
56
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 54

def row_title
  "Row #{number} -"
end

#sampleObject


132
133
134
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 132

def sample
  @sample ||= manifest_asset&.find_or_create_sample if sanger_sample_id.present? && !empty?
end

#sample_created?Boolean

Returns:

  • (Boolean)

144
145
146
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 144

def sample_created?
  sample_updated? && !reuploaded?
end

#sample_skipped_or_updated?Boolean

Returns:

  • (Boolean)

140
141
142
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 140

def sample_skipped_or_updated?
  @sample_skipped || sample_updated?
end

#sample_updated?Boolean

Returns:

  • (Boolean)

136
137
138
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 136

def sample_updated?
  @sample_updated || false
end

#specialised_fieldsObject


70
71
72
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 70

def specialised_fields
  @specialised_fields ||= create_specialised_fields
end

#transfer_aliquotObject

If it is a multiplexed library tube the aliquot is transferred from the library tube to a multiplexed library tube and stated set to passed.


120
121
122
123
124
125
126
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 120

def transfer_aliquot
  return unless valid?

  asset.external_library_creation_requests.each do |request|
    @aliquot_transferred = request.passed? || request.manifest_processed!
  end
end

#update_metadata_fieldsObject


110
111
112
113
114
115
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 110

def 
  columns..each do |column|
    value = at(column.number)
    column.(, value) if value.present?
  end
end

#update_sample(tag_group, override) ⇒ Object

Updating the sample involves: *Checking it is ok to update row *Updating all of the specialised fields in the aliquot *Updating the sample metadata *Saving the asset, metadata and sample rubocop:todo Metrics/MethodLength


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 81

def update_sample(tag_group, override) # rubocop:todo Metrics/AbcSize
  return unless valid?

  @reuploaded = sample.updated_by_manifest

  if sample.updated_by_manifest && !override
    @sample_skipped = true
  else
    update_specialised_fields(tag_group)
    asset.save!
    
    .save!
    sample.updated_by_manifest = true
    sample.empty_supplier_sample_name = false
    @sample_updated = sample.save
  end
end

#update_specialised_fields(tag_group) ⇒ Object


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

def update_specialised_fields(tag_group)
  specialised_fields.each { |specialised_field| specialised_field.update(aliquot: aliquot, tag_group: tag_group) }
end

#validate_sampleObject


163
164
165
166
167
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 163

def validate_sample
  check_sample_present
  sample_can_be_updated
  errors.empty?
end

#value(key) ⇒ Object

Find a value based on a column name


44
45
46
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/row.rb', line 44

def value(key)
  at(columns.find_column_or_null(:name, key).number)
end