class Array
Ruby core Array
class, with additional methods.
Method to create a RGFA::ByteArray
from an Array
Extensions to the Array
core class.
Method to create a numeric array from an array
Public Instance Methods
@!macro gfa_datatype
# File lib/rgfa/field_writer.rb, line 90 def default_gfa_datatype (all?{|i|i.kind_of?(Integer)} or all?{|i|i.kind_of?(Float)}) ? :B : :J end
Is this possibly a {RGFA::FieldArray} instance?
(i.e. are the two last elements a datatype symbol and a zero byte?) @return [Boolean]
# File lib/rgfa/field_array.rb, line 71 def rgfa_field_array? self[-1] == "\0" and RGFA::Line::OPTFIELD_DATATYPE.include?(self[-2].to_sym) end
Create a RGFA::ByteArray
from an Array
instance @return [RGFA::ByteArray] the byte array
# File lib/rgfa/byte_array.rb, line 55 def to_byte_array RGFA::ByteArray.new(self) end
Create a {RGFA::CIGAR} instance from the content of the array. @return [RGFA::CIGAR]
# File lib/rgfa/cigar.rb, line 139 def to_cigar RGFA::CIGAR.new(self) end
Create a {RGFA::CIGAR::Operation} instance from the content of the array. @return [RGFA::CIGAR::Operation]
# File lib/rgfa/cigar.rb, line 144 def to_cigar_operation RGFA::CIGAR::Operation.new(Integer(self[0]), self[1].to_sym) end
@!macro to_gfa_field
# File lib/rgfa/field_writer.rb, line 70 def to_gfa_field(datatype: default_gfa_datatype) case datatype when :B to_numeric_array.to_s when :J to_json when :cig to_cigar.to_s when :cgs map{|cig|cig.to_cigar.to_s}.join(",") when :lbs map{|os|os.to_oriented_segment.to_s}.join(",") when :H to_byte_array.to_s else map(&:to_s).join(",") end end
Create a numeric array from an Array
instance @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
@return [RGFA::NumericArray] the numeric array
# File lib/rgfa/numeric_array.rb, line 148 def to_numeric_array(validate: true) na = RGFA::NumericArray.new(self) na.validate! if validate na end
Create and validate a segment end from an array @!macro segment_info_validation_errors @return [RGFA::OrientedSegment]
# File lib/rgfa/segment_info.rb, line 145 def to_oriented_segment to_segment_info(RGFA::OrientedSegment) end
Converts an Array
of strings or RGFA::Line
instances into a RGFA
instance. @return [RGFA] @!macro validate
# File lib/rgfa.rb, line 369 def to_rgfa(validate: 2) gfa = RGFA.new(validate: validate) each {|line| gfa << line} gfa.validate! if validate >= 1 return gfa end
Create a {RGFA::FieldArray} from an array @param datatype [RGFA::Line::OPTFIELD_DATATYPE, nil] the datatype to use
# File lib/rgfa/field_array.rb, line 78 def to_rgfa_field_array(datatype=nil) if self.rgfa_field_array? RGFA::FieldArray.new(self[-2].to_sym, self[0..-3]) elsif datatype.nil? raise RGFA::FieldArray::Error, "no datatype specified" else RGFA::FieldArray.new(datatype, self) end end
Parses an array containing the fields of a RGFA
file line and creates an object of the correct record type child class of {RGFA::Line} @note
This method modifies the content of the array; if you still need the array, you must create a copy before calling it
@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 723 def to_rgfa_line(validate: 2) RGFA::Line.subclass(shift).new(self, validate: validate) end
Create and validate a segment end from an array @!macro segment_info_validation_errors @return [RGFA::SegmentEnd]
# File lib/rgfa/segment_info.rb, line 138 def to_segment_end to_segment_info(RGFA::SegmentEnd) end
@!macro validate_gfa_field
# File lib/rgfa/field_validator.rb, line 116 def validate_gfa_field!(datatype, fieldname=nil) begin case datatype when :J return when :Z return when :lbs map!(&:to_oriented_segment).each(&:validate!) return when :cig to_cigar.validate! return when :cgs map(&:to_cigar).each(&:validate!) return when :B to_numeric_array.validate! return when :H to_byte_array.validate! return end rescue => err raise RGFA::FieldParser::FormatError, "Invalid content for field #{fieldname}\n"+ "Content: #{self.inspect}\n"+ "Datatype: #{datatype}\n"+ "Error: #{err}" end raise RGFA::FieldParser::FormatError, "Wrong type (#{self.class}) for field #{fieldname}\n"+ "Content: #{self.inspect}\n"+ "Datatype: #{datatype}" end
Protected Instance Methods
# File lib/rgfa/segment_info.rb, line 151 def to_segment_info(subclass) return self if self.kind_of?(subclass) # support converting from gfa gem GraphVertex objects: if respond_to?(:segment) and respond_to?(:orient) return RGFA::OrientedSegment.new([segment.to_sym, orient.to_sym]) end se = subclass.new(map {|e| e.kind_of?(String) ? e.to_sym : e}) se.validate! return se end