class String
Ruby core String
class, with additional methods.
Method to parse the string representation of a RGFA::ByteArray
Extensions to the String
core class.
Method to create a numeric array from a string
Public Instance Methods
Convert a GFA string representation of a byte array to a byte array @return [RGFA::ByteArray] the byte array @raise [RGFA::ByteArray::FormatError] if the string size is not > 0
and even
# 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
Parse CIGAR string and return an array of CIGAR operations @return [RGFA::CIGAR] CIGAR operations (empty if string is “*”) @raise [RGFA::CIGAR::ValueError] if the string is not a valid CIGAR string
# File lib/rgfa/cigar.rb, line 153 def to_cigar RGFA::CIGAR.from_string(self) end
Create a numeric array from a string @param validate [Boolean] (default: true
)
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
@raise [RGFA::NumericArray::TypeError] if the subtype code is invalid @return [RGFA::NumericArray] the numeric array
# 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
Converts a String
into a RGFA
instance. Each line of the string is added separately to the gfa. @return [RGFA] @!macro validate
# 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
Parses a line of a RGFA
file and creates an object of the correct
record type child class of {RGFA::Line}
@return [subclass of RGFA::Line] @raise [RGFA::Error] if the fields do not comply to the RGFA
specification @param validate [Integer] (defaults to: 2)
see RGFA::Line#initialize
# 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