Class: RGFA::CIGAR

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

Overview

Array of CIGAR operations. Represents the contents of a CIGAR string.

Defined Under Namespace

Classes: Operation, ValueError

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Array

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

Class Method Details

.from_string(str) ⇒ RGFA::CIGAR

Parse a CIGAR string into an array of CIGAR operations.

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

Returns:

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
# 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

Instance Method Details

#cloneRGFA::CIGAR

Create a copy

Returns:



83
84
85
# File 'lib/rgfa/cigar.rb', line 83

def clone
  map{|x|x.clone}
end

#complementRGFA::CIGAR

Compute the CIGAR for the segments when these are switched.

Examples:

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

Returns:



25
26
27
28
29
30
31
32
33
34
# 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_cigarRGFA::CIGAR

Returns self.

Returns:



77
78
79
# File 'lib/rgfa/cigar.rb', line 77

def to_cigar
  self
end

#to_sString

String representation of the CIGAR

Returns:



59
60
61
62
63
64
65
# File 'lib/rgfa/cigar.rb', line 59

def to_s
  if empty?
    return "*"
  else
    map(&:to_s).join
  end
end

#validate!void

This method returns an undefined value.

Validate the instance

Raises:

  • if any component of the CIGAR array is invalid.



70
71
72
73
74
# 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) ⇒ 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:



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# 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