Class: String

Inherits:
Object show all
Includes:
RGFA::FieldParser, RGFA::FieldValidator, RGFA::Sequence
Defined in:
lib/rgfa.rb,
lib/rgfa/line.rb,
lib/rgfa/cigar.rb,
lib/rgfa/sequence.rb,
lib/rgfa/byte_array.rb,
lib/rgfa/field_parser.rb,
lib/rgfa/numeric_array.rb,
lib/rgfa/field_validator.rb

Overview

Method to create a numeric array from a string

Constant Summary

Constants included from RGFA::FieldValidator

RGFA::FieldValidator::DATASTRING_VALIDATION_REGEXP

Constants included from RGFA::Sequence

RGFA::Sequence::WCC

Instance Method Summary collapse

Methods included from RGFA::FieldValidator

#validate_gfa_field!, #validate_segment_name!

Methods included from RGFA::FieldParser

#parse_gfa_field, #parse_gfa_optfield

Methods included from RGFA::Sequence

#rc

Instance Method Details

#to_byte_arrayRGFA::ByteArray

Convert a GFA string representation of a byte array to a byte array

Returns:

  • the byte array

Raises:

  • if the string size is not > 0 and even



66
67
68
69
70
71
72
73
# File 'lib/rgfa/byte_array.rb', line 66

def to_byte_array
  if (size < 2) or (size % 2 == 1)
    raise RGFA::ByteArray::FormatError,
      "Invalid byte array string #{self}; "+
      "each element must be represented by two letters [0-9A-F]"
  end
  scan(/..?/).map {|x|Integer(x,16)}.to_byte_array
end

#to_cigarRGFA::CIGAR

Parse CIGAR string and return an array of CIGAR operations

Returns:

  • CIGAR operations (empty if string is “*”)

Raises:

  • if the string is not a valid CIGAR string



153
154
155
# File 'lib/rgfa/cigar.rb', line 153

def to_cigar
  RGFA::CIGAR.from_string(self)
end

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

Create a numeric array from a string

Parameters:

  • (defaults to: true)

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

Returns:

  • the numeric array

Raises:

  • if validate is set and any value is not compatible with the subtype

  • if the subtype code is invalid



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rgfa/numeric_array.rb', line 167

def to_numeric_array(validate: true)
  elems = split(",")
  subtype = elems.shift
  integer = (subtype != "f")
  if integer
    range = RGFA::NumericArray::SUBTYPE_RANGE[subtype]
  elsif !RGFA::NumericArray::SUBTYPE.include?(subtype)
    raise RGFA::NumericArray::TypeError, "Subtype #{subtype} unknown"
  end
  elems.map do |e|
    begin
      if integer
        e = Integer(e)
        if validate and not range.include?(e)
          raise "NumericArray: "+
                "value is outside of subtype #{subtype} range\n"+
                "Value: #{e}\n"+
                "Range: #{range.inspect}\n"+
                "Content: #{inspect}"
        end
        e
      else
        Float(e)
      end
    rescue => msg
      raise RGFA::NumericArray::ValueError, msg
    end
  end
end

#to_rgfa(validate: 2) ⇒ RGFA

Converts a String into a RGFA instance. Each line of the string is added separately to the gfa.

Parameters:

  • (defaults to: 2)

    (defaults to: 2) the validation level; see “Validation level” under RGFA::Line#initialize.

Returns:



353
354
355
356
357
358
# File 'lib/rgfa.rb', line 353

def to_rgfa(validate: 2)
  gfa = RGFA.new(validate: validate)
  split("\n").each {|line| gfa << line}
  gfa.validate! if validate >= 1
  return gfa
end

#to_rgfa_line(validate: 2) ⇒ subclass of RGFA::Line

Parses a line of a RGFA file and creates an object of the correct

record type child class of {RGFA::Line}

Parameters:

  • (defaults to: 2)

    (defaults to: 2) see RGFA::Line#initialize

Returns:

Raises:

  • if the fields do not comply to the RGFA specification



700
701
702
703
704
705
706
# File 'lib/rgfa/line.rb', line 700

def to_rgfa_line(validate: 2)
  if self[0] == "#"
    return RGFA::Line::Comment.new([self[1..-1]], validate: 0)
  else
    split(RGFA::Line::SEPARATOR).to_rgfa_line(validate: validate)
  end
end