Class: RGFA::NumericArray

Inherits:
Array show all
Defined in:
lib/rgfa/numeric_array.rb,
lib/rgfa/field_writer.rb,
lib/rgfa/field_validator.rb

Overview

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

Defined Under Namespace

Classes: TypeError, ValueError

Constant Summary collapse

SIGNED_INT_SUBTYPE =

Subtypes for signed integers, from the smallest to the largest

%W[c s i]
UNSIGNED_INT_SUBTYPE =

Subtypes for unsigned integers, from the smallest to the largest

SIGNED_INT_SUBTYPE.map{|st|st.upcase}
INT_SUBTYPE =

Subtypes for integers

UNSIGNED_INT_SUBTYPE + SIGNED_INT_SUBTYPE
FLOAT_SUBTYPE =

Subtypes for floats

["f"]
SUBTYPE =

Subtypes

INT_SUBTYPE + FLOAT_SUBTYPE
SUBTYPE_BITS =

Number of bits of unsigned integer subtypes

{"c" => 8, "s" => 16, "i" => 32}
SUBTYPE_RANGE =

Range for integer subtypes

Hash[
      INT_SUBTYPE.map do |subtype|
        [
         subtype,
         if subtype == subtype.upcase
0..((2**SUBTYPE_BITS[subtype.downcase])-1)
         else
(-(2**(SUBTYPE_BITS[subtype]-1)))..((2**(SUBTYPE_BITS[subtype]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#rgfa_field_array?, #to_byte_array, #to_cigar, #to_cigar_operation, #to_gfa_field, #to_oriented_segment, #to_rgfa, #to_rgfa_field_array, #to_rgfa_line, #to_segment_end

Class Method Details

.integer_type(range) ⇒ RGFA::NumericArray::INT_SUBTYPE

Computes the subtype for integers in a given range.

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

Parameters:

  • range (Range)

    the integer range

Returns:

Raises:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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

Instance Method Details

#compute_subtypeRGFA::NumericArray::SUBTYPE

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.

Returns:

Raises:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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_datatypeRGFA::Line::FIELD_DATATYPE

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Optional field GFA datatype to use, if none is provided



102
# File 'lib/rgfa/field_writer.rb', line 102

def default_gfa_datatype; :B; end

#to_numeric_array(validate: false) ⇒ RGFA::NumericArray

Return self

Parameters:

  • validate (Boolean) (defaults to: false)

    (default: false) if true, validate the range of the numeric values, according to the array subtype

Returns:

Raises:



114
115
116
117
# File 'lib/rgfa/numeric_array.rb', line 114

def to_numeric_array(validate: false)
  validate! if validate
  self
end

#to_sString

GFA datatype B representation of the numeric array

Returns:

Raises:



123
124
125
126
# File 'lib/rgfa/numeric_array.rb', line 123

def to_s
  subtype = compute_subtype
  "#{subtype},#{join(",")}"
end

#validate!Object

Validate the numeric array

Raises:



43
44
45
# File 'lib/rgfa/numeric_array.rb', line 43

def validate!
  compute_subtype
end

#validate_gfa_field!(datatype, fieldname = nil) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Validates the object according to the provided datatype

Parameters:

Raises:



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# 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