class RGFA::CIGAR

Array of {RGFA::CIGAR::Operation CIGAR operations}. Represents the contents of a CIGAR string.

Public Class Methods

from_string(str) click to toggle source

Parse a CIGAR string into an array of CIGAR operations.

Each operation is represented by a {RGFA::CIGAR::Operation}, i.e. a tuple of operation length and operation symbol (one of MIDNSHPX=).

@return [RGFA::CIGAR] (empty if string is *) @raise [RGFA::CIGAR::ValueError] if the string is not a valid CIGAR string

# File lib/rgfa/cigar.rb, line 44
def self.from_string(str)
  a = RGFA::CIGAR.new
  if str != "*"
    raise RGFA::CIGAR::ValueError if str !~ /^([0-9]+[MIDNSHPX=])+$/
    str.scan(/[0-9]+[MIDNSHPX=]/).each do |op|
      len = op[0..-2].to_i
      code = op[-1..-1].to_sym
      a << RGFA::CIGAR::Operation.new(len, code)
    end
  end
  return a
end

Public Instance Methods

clone() click to toggle source

Create a copy @return [RGFA::CIGAR]

# File lib/rgfa/cigar.rb, line 83
def clone
  map{|x|x.clone}
end
complement() click to toggle source

Compute the CIGAR for the segments when these are switched.

@example Computing the complement CIGAR

RGFA::CIGAR.from_string("2M1D3M").complement.to_s
# => "3M1I2M"

# S1 + S2 + 2M1D3M
#
# S1+  ACGACTGTGA
# S2+      CT-TGACGG
#
# S2-  CCGTCA-AG
# S1-     TCACAGTCGT
#
# S2 - S1 - 3M1I2M

@return [RGFA::CIGAR] (empty if CIGAR string is *)

# File lib/rgfa/cigar.rb, line 25
def complement
  reverse.map do |op|
    if op.code == :I
      op.code = :D
    elsif op.code == :D
      op.code = :I
    end
    op
  end
end
to_cigar() click to toggle source

@return [RGFA::CIGAR] self

# File lib/rgfa/cigar.rb, line 77
def to_cigar
  self
end
to_s() click to toggle source

String representation of the CIGAR @return [String] CIGAR string

# File lib/rgfa/cigar.rb, line 59
def to_s
  if empty?
    return "*"
  else
    map(&:to_s).join
  end
end
validate!() click to toggle source

Validate the instance @raise if any component of the CIGAR array is invalid. @return [void]

# File lib/rgfa/cigar.rb, line 70
def validate!
  any? do |op|
    op.to_cigar_operation.validate!
  end
end
validate_gfa_field!(datatype, fieldname=nil) click to toggle source

@!macro validate_gfa_field

# File lib/rgfa/field_validator.rb, line 176
def validate_gfa_field!(datatype, fieldname=nil)
  if datatype != :cig
    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