class RGFA::NumericArray

A numeric array representable using the data type B of the GFA specification

Constants

FLOAT_SUBTYPE

Subtypes for floats

INT_SUBTYPE

Subtypes for integers

SIGNED_INT_SUBTYPE

Subtypes for signed integers, from the smallest to the largest

SUBTYPE

Subtypes

SUBTYPE_BITS

Number of bits of unsigned integer subtypes

SUBTYPE_RANGE

Range for integer subtypes

UNSIGNED_INT_SUBTYPE

Subtypes for unsigned integers, from the smallest to the largest

Public Class Methods

integer_type(range) click to toggle source

Computes the subtype for integers in a given range.

If all elements are non-negative, an unsigned subtype is selected, otherwise a signed subtype.

@param range [Range] the integer range

@raise [RGFA::NumericArray::ValueError] if the integer range is outside

all subtype ranges

@return [RGFA::NumericArray::INT_SUBTYPE] subtype code

# File lib/rgfa/numeric_array.rb, line 89
def self.integer_type(range)
  if range.min < 0
    SIGNED_INT_SUBTYPE.each do |st|
      st_range = RGFA::NumericArray::SUBTYPE_RANGE[st]
      if st_range.include?(range.min) and st_range.include?(range.max)
        return st
      end
    end
  else
    UNSIGNED_INT_SUBTYPE.each do |st|
      return st if range.max < RGFA::NumericArray::SUBTYPE_RANGE[st].max
    end
  end
  raise RGFA::NumericArray::ValueError,
    "NumericArray: values are outside of all integer subtype ranges\n"+
    "Content: #{inspect}"
end

Public Instance Methods

compute_subtype() click to toggle source

Computes the subtype of the array from its content.

If all elements are float, then the computed subtype is “f”. If all elements are integer, the smallest possible numeric subtype is computed; thereby, if all elements are non-negative, an unsigned subtype is selected, otherwise a signed subtype. In all other cases an exception is raised.

@raise [RGFA::NumericArray::ValueError] if the array is not a valid numeric

array

@return [RGFA::NumericArray::SUBTYPE]

# File lib/rgfa/numeric_array.rb, line 59
def compute_subtype
  if all? {|f|f.kind_of?(Float)}
    return "f"
  else
    e_max = nil
    e_min = nil
    each do |e|
      if !e.kind_of?(Integer)
        raise RGFA::NumericArray::ValueError,
          "NumericArray does not contain homogenous numeric values\n"+
          "Content: #{inspect}"
      end
      e_max = e if e_max.nil? or e > e_max
      e_min = e if e_min.nil? or e < e_min
    end
    return RGFA::NumericArray.integer_type(e_min..e_max)
  end
end
default_gfa_datatype() click to toggle source

@!macro gfa_datatype

# File lib/rgfa/field_writer.rb, line 102
def default_gfa_datatype; :B; end
to_numeric_array(validate: false) click to toggle source

Return self @param validate [Boolean] (default: false)

if +true+, validate the range of the numeric values, according
to the array subtype

@raise [RGFA::NumericArray::ValueError] if validate is set and

any value is not compatible with the subtype

@return [RGFA::NumericArray]

# File lib/rgfa/numeric_array.rb, line 114
def to_numeric_array(validate: false)
  validate! if validate
  self
end
to_s() click to toggle source

GFA datatype B representation of the numeric array @raise [RGFA::NumericArray::ValueError] if the array

if not a valid numeric array

@return [String]

# File lib/rgfa/numeric_array.rb, line 123
def to_s
  subtype = compute_subtype
  "#{subtype},#{join(",")}"
end
validate!() click to toggle source

Validate the numeric array

@raise [RGFA::NumericArray::ValueError] if the array is not valid

# File lib/rgfa/numeric_array.rb, line 43
def validate!
  compute_subtype
end
validate_gfa_field!(datatype, fieldname=nil) click to toggle source

@!macro validate_gfa_field

# File lib/rgfa/field_validator.rb, line 197
def validate_gfa_field!(datatype, fieldname=nil)
  if datatype != :B
    raise RGFA::FieldParser::FormatError,
        "Wrong type (#{self.class}) for field #{fieldname}\n"+
        "Content: #{self.inspect}\n"+
        "Datatype: #{datatype}"
  end
  begin
    validate!
  rescue => err
    raise RGFA::FieldParser::FormatError,
      "Invalid content for field #{fieldname}\n"+
      "Content: #{self.inspect}\n"+
      "Datatype: #{datatype}\n"+
      "Error: #{err}"
  end
end