Module: Map::Coordinate

Defined in:
app/models/map.rb

Constant Summary collapse

PLATE_DIMENSIONS =

TODO: These methods are only valid for standard plates. Moved them here to make that more explicit (even if its not strictly appropriate) They could do with refactoring/removing.

Hash.new { |_h, _k| [] }.merge(
  96 => [12, 8],
  384 => [24, 16]
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#asset_sizeInteger

Returns the plate size for which the Map corresponds.

Returns:

  • (Integer)

    the plate size for which the Map corresponds


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/map.rb', line 16

module Coordinate
  # TODO: These methods are only valid for standard plates. Moved them here to make that more explicit
  # (even if its not strictly appropriate) They could do with refactoring/removing.

  PLATE_DIMENSIONS = Hash.new { |_h, _k| [] }.merge(
    96 => [12, 8],
    384 => [24, 16]
  )

  # Seems to expect row to be zero-indexed but column to be 1 indexed
  def self.location_from_row_and_column(row, column, _ = nil, __ = nil)
    "#{('A'.getbyte(0) + row).chr}#{column}"
  end

  def self.description_to_horizontal_plate_position(well_description, plate_size)
    return nil unless Map.valid_well_description_and_plate_size?(well_description, plate_size)

    split_well = Map.split_well_description(well_description)
    width = plate_width(plate_size)
    return nil if width.nil?

    (width * split_well[:row]) + split_well[:col]
  end

  def self.description_to_vertical_plate_position(well_description, plate_size)
    return nil unless Map.valid_well_description_and_plate_size?(well_description, plate_size)

    split_well = Map.split_well_description(well_description)
    length = plate_length(plate_size)
    return nil if length.nil?

    (length * (split_well[:col] - 1)) + split_well[:row] + 1
  end

  def self.horizontal_plate_position_to_description(well_position, plate_size)
    return nil unless Map.valid_plate_position_and_plate_size?(well_position, plate_size)

    width = plate_width(plate_size)
    return nil if width.nil?

    horizontal_position_to_description(well_position, width)
  end

  def self.vertical_plate_position_to_description(well_position, plate_size)
    return nil unless Map.valid_plate_position_and_plate_size?(well_position, plate_size)

    length = plate_length(plate_size)
    return nil if length.nil?

    vertical_position_to_description(well_position, length)
  end

  def self.descriptions_for_row(row, size)
    (1..plate_width(size)).map { |column| "#{row}#{column}" }
  end

  def self.descriptions_for_column(column, size)
    (0...plate_length(size)).map { |row| Map.location_from_row_and_column(row, column) }
  end

  def self.plate_width(plate_size)
    PLATE_DIMENSIONS[plate_size].first
  end

  def self.plate_length(plate_size)
    PLATE_DIMENSIONS[plate_size].last
  end

  def self.vertical_position_to_description(well_position, length)
    desc_letter = (((well_position - 1) % length) + 65).chr
    desc_number = ((well_position - 1) / length) + 1
    (desc_letter + (desc_number.to_s))
  end

  def self.horizontal_position_to_description(well_position, width)
    desc_letter = (((well_position - 1) / width) + 65).chr
    desc_number = ((well_position - 1) % width) + 1
    (desc_letter + (desc_number.to_s))
  end

  def self.horizontal_to_vertical(well_position, plate_size)
    alternate_position(well_position, plate_size, :width, :length)
  end

  def self.vertical_to_horizontal(well_position, plate_size)
    alternate_position(well_position, plate_size, :length, :width)
  end

  def self.location_from_index(index, size)
    horizontal_plate_position_to_description(index + 1, size)
  end

  class << self
    # Given the well position described in terms of a direction (vertical or horizontal) this function
    # will map it to the alternate positional representation, i.e. a vertical position will be mapped
    # to a horizontal one.  It does this with the divisor and multiplier, which will be reversed for
    # the alternate.
    #
    # NOTE: I don't like this, it just makes things clearer than it was!
    # NOTE: I hate the nil returns but external code would take too long to change this time round
    def alternate_position(well_position, size, *dimensions)
      return nil unless Map.valid_well_position?(well_position)

      divisor, multiplier = dimensions.map { |n| send("plate_#{n}", size) }
      return nil if divisor.nil? || multiplier.nil?

      column, row = (well_position - 1).divmod(divisor)
      return nil unless (0...multiplier).cover?(column)
      return nil unless (0...divisor).cover?(row)

      (row * multiplier) + column + 1
    end
    private :alternate_position
  end
end

#column_orderInteger

Returns zero indexed order of the well when sorted by column.

Returns:

  • (Integer)

    zero indexed order of the well when sorted by column


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/map.rb', line 16

module Coordinate
  # TODO: These methods are only valid for standard plates. Moved them here to make that more explicit
  # (even if its not strictly appropriate) They could do with refactoring/removing.

  PLATE_DIMENSIONS = Hash.new { |_h, _k| [] }.merge(
    96 => [12, 8],
    384 => [24, 16]
  )

  # Seems to expect row to be zero-indexed but column to be 1 indexed
  def self.location_from_row_and_column(row, column, _ = nil, __ = nil)
    "#{('A'.getbyte(0) + row).chr}#{column}"
  end

  def self.description_to_horizontal_plate_position(well_description, plate_size)
    return nil unless Map.valid_well_description_and_plate_size?(well_description, plate_size)

    split_well = Map.split_well_description(well_description)
    width = plate_width(plate_size)
    return nil if width.nil?

    (width * split_well[:row]) + split_well[:col]
  end

  def self.description_to_vertical_plate_position(well_description, plate_size)
    return nil unless Map.valid_well_description_and_plate_size?(well_description, plate_size)

    split_well = Map.split_well_description(well_description)
    length = plate_length(plate_size)
    return nil if length.nil?

    (length * (split_well[:col] - 1)) + split_well[:row] + 1
  end

  def self.horizontal_plate_position_to_description(well_position, plate_size)
    return nil unless Map.valid_plate_position_and_plate_size?(well_position, plate_size)

    width = plate_width(plate_size)
    return nil if width.nil?

    horizontal_position_to_description(well_position, width)
  end

  def self.vertical_plate_position_to_description(well_position, plate_size)
    return nil unless Map.valid_plate_position_and_plate_size?(well_position, plate_size)

    length = plate_length(plate_size)
    return nil if length.nil?

    vertical_position_to_description(well_position, length)
  end

  def self.descriptions_for_row(row, size)
    (1..plate_width(size)).map { |column| "#{row}#{column}" }
  end

  def self.descriptions_for_column(column, size)
    (0...plate_length(size)).map { |row| Map.location_from_row_and_column(row, column) }
  end

  def self.plate_width(plate_size)
    PLATE_DIMENSIONS[plate_size].first
  end

  def self.plate_length(plate_size)
    PLATE_DIMENSIONS[plate_size].last
  end

  def self.vertical_position_to_description(well_position, length)
    desc_letter = (((well_position - 1) % length) + 65).chr
    desc_number = ((well_position - 1) / length) + 1
    (desc_letter + (desc_number.to_s))
  end

  def self.horizontal_position_to_description(well_position, width)
    desc_letter = (((well_position - 1) / width) + 65).chr
    desc_number = ((well_position - 1) % width) + 1
    (desc_letter + (desc_number.to_s))
  end

  def self.horizontal_to_vertical(well_position, plate_size)
    alternate_position(well_position, plate_size, :width, :length)
  end

  def self.vertical_to_horizontal(well_position, plate_size)
    alternate_position(well_position, plate_size, :length, :width)
  end

  def self.location_from_index(index, size)
    horizontal_plate_position_to_description(index + 1, size)
  end

  class << self
    # Given the well position described in terms of a direction (vertical or horizontal) this function
    # will map it to the alternate positional representation, i.e. a vertical position will be mapped
    # to a horizontal one.  It does this with the divisor and multiplier, which will be reversed for
    # the alternate.
    #
    # NOTE: I don't like this, it just makes things clearer than it was!
    # NOTE: I hate the nil returns but external code would take too long to change this time round
    def alternate_position(well_position, size, *dimensions)
      return nil unless Map.valid_well_position?(well_position)

      divisor, multiplier = dimensions.map { |n| send("plate_#{n}", size) }
      return nil if divisor.nil? || multiplier.nil?

      column, row = (well_position - 1).divmod(divisor)
      return nil unless (0...multiplier).cover?(column)
      return nil unless (0...divisor).cover?(row)

      (row * multiplier) + column + 1
    end
    private :alternate_position
  end
end

#descriptionString

Returns the name of the well. In most cases this will be in the along the lines of A1 or H12.

Returns:

  • (String)

    the name of the well. In most cases this will be in the along the lines of A1 or H12


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/map.rb', line 16

module Coordinate
  # TODO: These methods are only valid for standard plates. Moved them here to make that more explicit
  # (even if its not strictly appropriate) They could do with refactoring/removing.

  PLATE_DIMENSIONS = Hash.new { |_h, _k| [] }.merge(
    96 => [12, 8],
    384 => [24, 16]
  )

  # Seems to expect row to be zero-indexed but column to be 1 indexed
  def self.location_from_row_and_column(row, column, _ = nil, __ = nil)
    "#{('A'.getbyte(0) + row).chr}#{column}"
  end

  def self.description_to_horizontal_plate_position(well_description, plate_size)
    return nil unless Map.valid_well_description_and_plate_size?(well_description, plate_size)

    split_well = Map.split_well_description(well_description)
    width = plate_width(plate_size)
    return nil if width.nil?

    (width * split_well[:row]) + split_well[:col]
  end

  def self.description_to_vertical_plate_position(well_description, plate_size)
    return nil unless Map.valid_well_description_and_plate_size?(well_description, plate_size)

    split_well = Map.split_well_description(well_description)
    length = plate_length(plate_size)
    return nil if length.nil?

    (length * (split_well[:col] - 1)) + split_well[:row] + 1
  end

  def self.horizontal_plate_position_to_description(well_position, plate_size)
    return nil unless Map.valid_plate_position_and_plate_size?(well_position, plate_size)

    width = plate_width(plate_size)
    return nil if width.nil?

    horizontal_position_to_description(well_position, width)
  end

  def self.vertical_plate_position_to_description(well_position, plate_size)
    return nil unless Map.valid_plate_position_and_plate_size?(well_position, plate_size)

    length = plate_length(plate_size)
    return nil if length.nil?

    vertical_position_to_description(well_position, length)
  end

  def self.descriptions_for_row(row, size)
    (1..plate_width(size)).map { |column| "#{row}#{column}" }
  end

  def self.descriptions_for_column(column, size)
    (0...plate_length(size)).map { |row| Map.location_from_row_and_column(row, column) }
  end

  def self.plate_width(plate_size)
    PLATE_DIMENSIONS[plate_size].first
  end

  def self.plate_length(plate_size)
    PLATE_DIMENSIONS[plate_size].last
  end

  def self.vertical_position_to_description(well_position, length)
    desc_letter = (((well_position - 1) % length) + 65).chr
    desc_number = ((well_position - 1) / length) + 1
    (desc_letter + (desc_number.to_s))
  end

  def self.horizontal_position_to_description(well_position, width)
    desc_letter = (((well_position - 1) / width) + 65).chr
    desc_number = ((well_position - 1) % width) + 1
    (desc_letter + (desc_number.to_s))
  end

  def self.horizontal_to_vertical(well_position, plate_size)
    alternate_position(well_position, plate_size, :width, :length)
  end

  def self.vertical_to_horizontal(well_position, plate_size)
    alternate_position(well_position, plate_size, :length, :width)
  end

  def self.location_from_index(index, size)
    horizontal_plate_position_to_description(index + 1, size)
  end

  class << self
    # Given the well position described in terms of a direction (vertical or horizontal) this function
    # will map it to the alternate positional representation, i.e. a vertical position will be mapped
    # to a horizontal one.  It does this with the divisor and multiplier, which will be reversed for
    # the alternate.
    #
    # NOTE: I don't like this, it just makes things clearer than it was!
    # NOTE: I hate the nil returns but external code would take too long to change this time round
    def alternate_position(well_position, size, *dimensions)
      return nil unless Map.valid_well_position?(well_position)

      divisor, multiplier = dimensions.map { |n| send("plate_#{n}", size) }
      return nil if divisor.nil? || multiplier.nil?

      column, row = (well_position - 1).divmod(divisor)
      return nil unless (0...multiplier).cover?(column)
      return nil unless (0...divisor).cover?(row)

      (row * multiplier) + column + 1
    end
    private :alternate_position
  end
end

#row_orderInteger

Returns zero indexed order of the well when sorted by row.

Returns:

  • (Integer)

    zero indexed order of the well when sorted by row


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/map.rb', line 16

module Coordinate
  # TODO: These methods are only valid for standard plates. Moved them here to make that more explicit
  # (even if its not strictly appropriate) They could do with refactoring/removing.

  PLATE_DIMENSIONS = Hash.new { |_h, _k| [] }.merge(
    96 => [12, 8],
    384 => [24, 16]
  )

  # Seems to expect row to be zero-indexed but column to be 1 indexed
  def self.location_from_row_and_column(row, column, _ = nil, __ = nil)
    "#{('A'.getbyte(0) + row).chr}#{column}"
  end

  def self.description_to_horizontal_plate_position(well_description, plate_size)
    return nil unless Map.valid_well_description_and_plate_size?(well_description, plate_size)

    split_well = Map.split_well_description(well_description)
    width = plate_width(plate_size)
    return nil if width.nil?

    (width * split_well[:row]) + split_well[:col]
  end

  def self.description_to_vertical_plate_position(well_description, plate_size)
    return nil unless Map.valid_well_description_and_plate_size?(well_description, plate_size)

    split_well = Map.split_well_description(well_description)
    length = plate_length(plate_size)
    return nil if length.nil?

    (length * (split_well[:col] - 1)) + split_well[:row] + 1
  end

  def self.horizontal_plate_position_to_description(well_position, plate_size)
    return nil unless Map.valid_plate_position_and_plate_size?(well_position, plate_size)

    width = plate_width(plate_size)
    return nil if width.nil?

    horizontal_position_to_description(well_position, width)
  end

  def self.vertical_plate_position_to_description(well_position, plate_size)
    return nil unless Map.valid_plate_position_and_plate_size?(well_position, plate_size)

    length = plate_length(plate_size)
    return nil if length.nil?

    vertical_position_to_description(well_position, length)
  end

  def self.descriptions_for_row(row, size)
    (1..plate_width(size)).map { |column| "#{row}#{column}" }
  end

  def self.descriptions_for_column(column, size)
    (0...plate_length(size)).map { |row| Map.location_from_row_and_column(row, column) }
  end

  def self.plate_width(plate_size)
    PLATE_DIMENSIONS[plate_size].first
  end

  def self.plate_length(plate_size)
    PLATE_DIMENSIONS[plate_size].last
  end

  def self.vertical_position_to_description(well_position, length)
    desc_letter = (((well_position - 1) % length) + 65).chr
    desc_number = ((well_position - 1) / length) + 1
    (desc_letter + (desc_number.to_s))
  end

  def self.horizontal_position_to_description(well_position, width)
    desc_letter = (((well_position - 1) / width) + 65).chr
    desc_number = ((well_position - 1) % width) + 1
    (desc_letter + (desc_number.to_s))
  end

  def self.horizontal_to_vertical(well_position, plate_size)
    alternate_position(well_position, plate_size, :width, :length)
  end

  def self.vertical_to_horizontal(well_position, plate_size)
    alternate_position(well_position, plate_size, :length, :width)
  end

  def self.location_from_index(index, size)
    horizontal_plate_position_to_description(index + 1, size)
  end

  class << self
    # Given the well position described in terms of a direction (vertical or horizontal) this function
    # will map it to the alternate positional representation, i.e. a vertical position will be mapped
    # to a horizontal one.  It does this with the divisor and multiplier, which will be reversed for
    # the alternate.
    #
    # NOTE: I don't like this, it just makes things clearer than it was!
    # NOTE: I hate the nil returns but external code would take too long to change this time round
    def alternate_position(well_position, size, *dimensions)
      return nil unless Map.valid_well_position?(well_position)

      divisor, multiplier = dimensions.map { |n| send("plate_#{n}", size) }
      return nil if divisor.nil? || multiplier.nil?

      column, row = (well_position - 1).divmod(divisor)
      return nil unless (0...multiplier).cover?(column)
      return nil unless (0...divisor).cover?(row)

      (row * multiplier) + column + 1
    end
    private :alternate_position
  end
end

Class Method Details

.alternate_position(well_position, size, *dimensions) ⇒ Object

Given the well position described in terms of a direction (vertical or horizontal) this function will map it to the alternate positional representation, i.e. a vertical position will be mapped to a horizontal one. It does this with the divisor and multiplier, which will be reversed for the alternate.

NOTE: I don't like this, it just makes things clearer than it was! NOTE: I hate the nil returns but external code would take too long to change this time round


116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/map.rb', line 116

def alternate_position(well_position, size, *dimensions)
  return nil unless Map.valid_well_position?(well_position)

  divisor, multiplier = dimensions.map { |n| send("plate_#{n}", size) }
  return nil if divisor.nil? || multiplier.nil?

  column, row = (well_position - 1).divmod(divisor)
  return nil unless (0...multiplier).cover?(column)
  return nil unless (0...divisor).cover?(row)

  (row * multiplier) + column + 1
end

.description_to_horizontal_plate_position(well_description, plate_size) ⇒ Object


30
31
32
33
34
35
36
37
38
# File 'app/models/map.rb', line 30

def self.description_to_horizontal_plate_position(well_description, plate_size)
  return nil unless Map.valid_well_description_and_plate_size?(well_description, plate_size)

  split_well = Map.split_well_description(well_description)
  width = plate_width(plate_size)
  return nil if width.nil?

  (width * split_well[:row]) + split_well[:col]
end

.description_to_vertical_plate_position(well_description, plate_size) ⇒ Object


40
41
42
43
44
45
46
47
48
# File 'app/models/map.rb', line 40

def self.description_to_vertical_plate_position(well_description, plate_size)
  return nil unless Map.valid_well_description_and_plate_size?(well_description, plate_size)

  split_well = Map.split_well_description(well_description)
  length = plate_length(plate_size)
  return nil if length.nil?

  (length * (split_well[:col] - 1)) + split_well[:row] + 1
end

.descriptions_for_column(column, size) ⇒ Object


72
73
74
# File 'app/models/map.rb', line 72

def self.descriptions_for_column(column, size)
  (0...plate_length(size)).map { |row| Map.location_from_row_and_column(row, column) }
end

.descriptions_for_row(row, size) ⇒ Object


68
69
70
# File 'app/models/map.rb', line 68

def self.descriptions_for_row(row, size)
  (1..plate_width(size)).map { |column| "#{row}#{column}" }
end

.horizontal_plate_position_to_description(well_position, plate_size) ⇒ Object


50
51
52
53
54
55
56
57
# File 'app/models/map.rb', line 50

def self.horizontal_plate_position_to_description(well_position, plate_size)
  return nil unless Map.valid_plate_position_and_plate_size?(well_position, plate_size)

  width = plate_width(plate_size)
  return nil if width.nil?

  horizontal_position_to_description(well_position, width)
end

.horizontal_position_to_description(well_position, width) ⇒ Object


90
91
92
93
94
# File 'app/models/map.rb', line 90

def self.horizontal_position_to_description(well_position, width)
  desc_letter = (((well_position - 1) / width) + 65).chr
  desc_number = ((well_position - 1) % width) + 1
  (desc_letter + (desc_number.to_s))
end

.horizontal_to_vertical(well_position, plate_size) ⇒ Object


96
97
98
# File 'app/models/map.rb', line 96

def self.horizontal_to_vertical(well_position, plate_size)
  alternate_position(well_position, plate_size, :width, :length)
end

.location_from_index(index, size) ⇒ Object


104
105
106
# File 'app/models/map.rb', line 104

def self.location_from_index(index, size)
  horizontal_plate_position_to_description(index + 1, size)
end

.location_from_row_and_column(row, column, _ = nil, __ = nil) ⇒ Object

Seems to expect row to be zero-indexed but column to be 1 indexed


26
27
28
# File 'app/models/map.rb', line 26

def self.location_from_row_and_column(row, column, _ = nil, __ = nil)
  "#{('A'.getbyte(0) + row).chr}#{column}"
end

.plate_length(plate_size) ⇒ Object


80
81
82
# File 'app/models/map.rb', line 80

def self.plate_length(plate_size)
  PLATE_DIMENSIONS[plate_size].last
end

.plate_width(plate_size) ⇒ Object


76
77
78
# File 'app/models/map.rb', line 76

def self.plate_width(plate_size)
  PLATE_DIMENSIONS[plate_size].first
end

.vertical_plate_position_to_description(well_position, plate_size) ⇒ Object


59
60
61
62
63
64
65
66
# File 'app/models/map.rb', line 59

def self.vertical_plate_position_to_description(well_position, plate_size)
  return nil unless Map.valid_plate_position_and_plate_size?(well_position, plate_size)

  length = plate_length(plate_size)
  return nil if length.nil?

  vertical_position_to_description(well_position, length)
end

.vertical_position_to_description(well_position, length) ⇒ Object


84
85
86
87
88
# File 'app/models/map.rb', line 84

def self.vertical_position_to_description(well_position, length)
  desc_letter = (((well_position - 1) % length) + 65).chr
  desc_number = ((well_position - 1) / length) + 1
  (desc_letter + (desc_number.to_s))
end

.vertical_to_horizontal(well_position, plate_size) ⇒ Object


100
101
102
# File 'app/models/map.rb', line 100

def self.vertical_to_horizontal(well_position, plate_size)
  alternate_position(well_position, plate_size, :length, :width)
end