Module: WellHelpers
- Defined in:
- lib/well_helpers.rb
Overview
Provides a series of helper methods to assist with generating and processing well names
Defined Under Namespace
Modules: Extensions
Constant Summary collapse
- HORIZONTAL_RATIO =
3
- VERTICAL_RATIO =
2
Class Method Summary collapse
-
.column_order(size = 96, rows: nil, columns: nil) ⇒ Array
Returns an array of all well names in column order.
- .columns_range(size = nil, columns: nil) ⇒ Object
- .default_columns(size) ⇒ Object
- .default_rows(size) ⇒ Object
-
.first_and_last_in_columns(wells) ⇒ Array<string>
Extracts the first and last well (as sorted in column order) from the array.
-
.formatted_range(wells, size = 96) ⇒ String
Compacts the provided well range into an easy to read summary.
-
.index_of(well, size = 96) ⇒ Int
Returns the index of the well by column.
-
.row_order(size = 96, rows: nil, columns: nil) ⇒ Array
Returns an array of all well names in row order.
- .rows_range(size = nil, rows: nil) ⇒ Object
-
.sort_in_column_order(wells) ⇒ Array<String>
Returns a new array sorted into column order e.g..
-
.stamp_hash(size, rows: nil, columns: nil) ⇒ Hash
Returns a hash suitable for stamping an entire plate.
-
.well_at_column_index(index, size = 96) ⇒ String
Returns the name of the well at the provided index.
-
.well_coordinate(well) ⇒ Array<Integer>
Converts a well name to its co-ordinates.
-
.well_name(row, column) ⇒ String
Returns the name of the well at the given co-ordinates e.g..
-
.well_quadrant(well) ⇒ Integer
Converts a well name to its quadrant.
Class Method Details
.column_order(size = 96, rows: nil, columns: nil) ⇒ Array
Returns an array of all well names in column order
45 46 47 48 49 |
# File 'lib/well_helpers.rb', line 45 def self.column_order(size = 96, rows: nil, columns: nil) columns_range(size, columns: columns) .each_with_object([]) { |c, wells| rows_range(size, rows: rows).each { |r| wells << "#{r}#{c}" } } .freeze end |
.columns_range(size = nil, columns: nil) ⇒ Object
32 33 34 35 |
# File 'lib/well_helpers.rb', line 32 def self.columns_range(size = nil, columns: nil) number_columns = columns || default_columns(size) (1..number_columns) end |
.default_columns(size) ⇒ Object
24 25 26 |
# File 'lib/well_helpers.rb', line 24 def self.default_columns(size) ((size / (VERTICAL_RATIO * HORIZONTAL_RATIO))**0.5).to_i * HORIZONTAL_RATIO end |
.default_rows(size) ⇒ Object
28 29 30 |
# File 'lib/well_helpers.rb', line 28 def self.default_rows(size) ((size / (VERTICAL_RATIO * HORIZONTAL_RATIO))**0.5).to_i * VERTICAL_RATIO end |
.first_and_last_in_columns(wells) ⇒ Array<string>
Extracts the first and last well (as sorted in column order) from the array
134 135 136 137 |
# File 'lib/well_helpers.rb', line 134 def self.first_and_last_in_columns(wells) sorted = sort_in_column_order(wells) [sorted.first, sorted.last] end |
.formatted_range(wells, size = 96) ⇒ String
Compacts the provided well range into an easy to read summary. e.g.. formatted_range([‘A1’, ‘B1’, ‘C1’,‘A2’,‘A5’,‘B5’]) => ‘A1-C1,A2,A5-B5’ Mostly this will just be start_well-end_well
120 121 122 123 124 125 |
# File 'lib/well_helpers.rb', line 120 def self.formatted_range(wells, size = 96) sort_in_column_order(wells) .slice_when { |previous_well, next_well| index_of(next_well, size) - index_of(previous_well, size) > 1 } .map { |range| [range.first, range.last].uniq.join('-') } .join(', ') end |
.index_of(well, size = 96) ⇒ Int
Returns the index of the well by column
75 76 77 |
# File 'lib/well_helpers.rb', line 75 def self.index_of(well, size = 96) column_order(size).index(well) || raise("Unknown well #{well} on plate of size 96") end |
.row_order(size = 96, rows: nil, columns: nil) ⇒ Array
Returns an array of all well names in row order
55 56 57 58 59 |
# File 'lib/well_helpers.rb', line 55 def self.row_order(size = 96, rows: nil, columns: nil) rows_range(size, rows: rows) .each_with_object([]) { |r, wells| columns_range(size, columns: columns).each { |c| wells << "#{r}#{c}" } } .freeze end |
.rows_range(size = nil, rows: nil) ⇒ Object
37 38 39 40 |
# File 'lib/well_helpers.rb', line 37 def self.rows_range(size = nil, rows: nil) number_rows = rows || default_rows(size) ('A'..).take(number_rows) end |
.sort_in_column_order(wells) ⇒ Array<String>
Returns a new array sorted into column order e.g.. sort_in_column_order([‘A1’, ‘A2’, ‘B1’]) => [‘A1’, ‘B1’, ‘A2’]
107 108 109 |
# File 'lib/well_helpers.rb', line 107 def self.sort_in_column_order(wells) wells.sort_by { |well| well_coordinate(well) } end |
.stamp_hash(size, rows: nil, columns: nil) ⇒ Hash
Returns a hash suitable for stamping an entire plate
68 69 70 |
# File 'lib/well_helpers.rb', line 68 def self.stamp_hash(size, rows: nil, columns: nil) column_order(size, rows: rows, columns: columns).each_with_object({}) { |well, hash| hash[well] = well } end |
.well_at_column_index(index, size = 96) ⇒ String
Returns the name of the well at the provided index. e.g.. ‘WellHelpers.column_index(2) #=> ’C1’‘
95 96 97 |
# File 'lib/well_helpers.rb', line 95 def self.well_at_column_index(index, size = 96) column_order(size)[index] end |
.well_coordinate(well) ⇒ Array<Integer>
Converts a well name to its co-ordinates
146 147 148 |
# File 'lib/well_helpers.rb', line 146 def self.well_coordinate(well) [well[1..].to_i - 1, well.upcase.getbyte(0) - 'A'.getbyte(0)] end |
.well_name(row, column) ⇒ String
Returns the name of the well at the given co-ordinates e.g.. ‘WellHelpers.well_name(2,3) #=> ’D3’‘
85 86 87 88 |
# File 'lib/well_helpers.rb', line 85 def self.well_name(row, column) row_name = ('A'.getbyte(0) + row).chr "#{row_name}#{column + 1}" end |
.well_quadrant(well) ⇒ Integer
Converts a well name to its quadrant
157 158 159 160 |
# File 'lib/well_helpers.rb', line 157 def self.well_quadrant(well) col, row = well_coordinate(well) (2 * (col % 2)) + (row % 2) end |