Class: SBCF::Prefix

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

Overview

Prefixes are found at the front of sanger_barcodable_spec They have a two character human form, or a three digit machine form

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(human_prefix: nil, machine_prefix: nil) ⇒ Prefix

Return a new instance of the prefix class, either from the supplier human_prefix or supplief machine_prefix

Parameters:

  • human_prefix (String)

    A two character prefix (eg. DN)

  • machine_prefix (Int)

    A two-three digit prefix (eg. 122)

Raises:


27
28
29
30
31
# File 'lib/sanger_barcode_format/prefix.rb', line 27

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

Class Method Details

.from_human(human_prefix) ⇒ Prefix

Create a prefix object from the character string

Parameters:

  • human_prefix (Sting)

    A two character prefix (eg. DN)

Returns:

  • (Prefix)

    A new instance of the prefix class


9
10
11
# File 'lib/sanger_barcode_format/prefix.rb', line 9

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

.from_machine(machine_prefix) ⇒ Prefix

Create a prefix object from the digit representation

Parameters:

  • machine_prefix (Int)

    A two-three digit prefix (eg. 122)

Returns:

  • (Prefix)

    A new instance of the prefix class


17
18
19
# File 'lib/sanger_barcode_format/prefix.rb', line 17

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

Instance Method Details

#humanString

A two character prefix (eg. DN)

Returns:

  • (String)

    A two character prefix (eg. DN)


52
53
54
# File 'lib/sanger_barcode_format/prefix.rb', line 52

def human
  @human ||= calculate_human
end

#machineInt

Returns the 3 digit encoded prefix as used in barcodes

Returns:

  • (Int)

    A two - three digit prefix (eg. 122)


36
37
38
# File 'lib/sanger_barcode_format/prefix.rb', line 36

def machine
  @machine ||= calculate_machine
end

#machine_fullInt

Deprecated.

This method will be made private in future versions

Returns the prefix mutliplied to allow addition of the numeric form

Returns:

  • (Int)

    barcode prefix multipled by 10000000


59
60
61
# File 'lib/sanger_barcode_format/prefix.rb', line 59

def machine_full
  machine * 10**(NUMBER_LENGTH + CHECKSUM_LENGTH)
end

#machine_sString

Returns the 3 digit encoded prefix as used in barcodes (zero padded)

Returns:

  • (String)

    A zero padded three digit prefix (eg. 122)


43
44
45
46
47
# File 'lib/sanger_barcode_format/prefix.rb', line 43

def machine_s
  machine_s = machine.to_s
  padding = above_zero(PREFIX_LENGTH - machine_s.length)
  machine_s.insert(0, '0' * padding)
end