Class: SBCF::Checksum

Inherits:
Object
  • Object
show all
Defined in:
lib/sanger_barcode_format/checksum.rb

Overview

Checsum is an internally generated checksum used to validate user typed barcodes. In human readable format is consists of a single letter, in machine readable form, two digits. It appears at the end of the barcode.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(human_checksum: nil, machine_checksum: nil) ⇒ Checksum

Create a new checksum from either the huamn or machine form

Parameters:

  • human_checksum (String)

    A single letter representing the human readable checksum

  • machine_checksum (Int)

    Two digit integer representing the machine readable checksum

Raises:


44
45
46
47
48
# File 'lib/sanger_barcode_format/checksum.rb', line 44

def initialize(human_checksum: nil, machine_checksum: nil)
  raise BarcodeError, 'Must supply a human or machine checksum' unless human_checksum || machine_checksum
  @human = human_checksum
  @machine = machine_checksum.to_i if machine_checksum
end

Class Method Details

.from_human(human_checksum) ⇒ Checksum

Generate a Checksum from the single letter human form

Parameters:

  • human_checksum (String)

    A single letter representing the human readable checksum

Returns:


12
13
14
# File 'lib/sanger_barcode_format/checksum.rb', line 12

def self.from_human(human_checksum)
  new(human_checksum: human_checksum)
end

.from_machine(machine_checksum) ⇒ Type

Generate a Checksum from the two digit machine form

Parameters:

  • machine_checksum (Int)

    two digit integer representing the machine readable checksum

Returns:

  • (Type)

    A new checksum object


20
21
22
# File 'lib/sanger_barcode_format/checksum.rb', line 20

def self.from_machine(machine_checksum)
  new(machine_checksum: machine_checksum)
end

.from_prefix_and_number(prefix, number) ⇒ Checksum

Generate a checksum from the barcode prefix and number

Parameters:

  • prefix (Prefix)

    a barcode prefix object

  • number (Int)

    The barcode number

Returns:

  • (Checksum)

    Corresponding checksum object


29
30
31
32
33
34
35
36
37
# File 'lib/sanger_barcode_format/checksum.rb', line 29

def self.from_prefix_and_number(prefix, number)
  string = prefix.human + number.to_s
  list = string.reverse
  sum = 0
  list.bytes.each_with_index do |byte, i|
    sum += byte * (i + 1)
  end
  new(human_checksum: (sum % 23 + CHECKSUM_ASCII_OFFSET).chr)
end

Instance Method Details

#==(other) ⇒ Bool

Returns ture if the checksums match Checsums match if their value is the same, regardless of how they were calculated.

Parameters:

  • other_checkum (Checksum)

    the checksum with which to compare

Returns:

  • (Bool)

    description tru is the checsums match, false otherwise

Raises:

  • (ArgumentError)

70
71
72
73
# File 'lib/sanger_barcode_format/checksum.rb', line 70

def ==(other)
  raise ArgumentError, 'Can only compare a checksum with a checksum' unless other.is_a?(Checksum)
  human == other.human
end

#humanString

Return the human readable checksum

Returns:

  • (String)

    A single letter representing the human readable checksum


60
61
62
# File 'lib/sanger_barcode_format/checksum.rb', line 60

def human
  @human ||= @machine.chr
end

#machineInt

Return the machine readable checksum

Returns:

  • (Int)

    Two digit integer representing the machine readable checksum


53
54
55
# File 'lib/sanger_barcode_format/checksum.rb', line 53

def machine
  @machine ||= human.getbyte(0)
end