Class: HTS::Bam::Cigar

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hts/bam/cigar.rb

Overview

CIGAR string

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record = nil) ⇒ Cigar

Returns a new instance of Cigar.



25
26
27
28
29
30
31
32
33
34
# File 'lib/hts/bam/cigar.rb', line 25

def initialize(record = nil)
  if record
    # The record is used at initialization and is not retained after that.
    bam1 = record.struct
    n_cigar = bam1[:core][:n_cigar]
    @array = LibHTS.bam_get_cigar(bam1).read_array_of_uint32(n_cigar)
  else
    @array = []
  end
end

Instance Attribute Details

#arrayObject

a uint32_t array (with 32 bits for every CIGAR op: length<<4|operation)



10
11
12
# File 'lib/hts/bam/cigar.rb', line 10

def array
  @array
end

Class Method Details

.parse(str) ⇒ Object

Create a new Cigar object from a string. The CIGAR string is converted to a uint32_t array in htslib.

Parameters:

  • cigar_str (String)


15
16
17
18
19
20
21
22
23
# File 'lib/hts/bam/cigar.rb', line 15

def self.parse(str)
  c = FFI::MemoryPointer.new(:pointer)
  m = FFI::MemoryPointer.new(:size_t)
  LibHTS.sam_parse_cigar(str, FFI::Pointer::NULL, c, m)
  cigar_array = c.read_pointer.read_array_of_uint32(m.read(:size_t))
  obj = new
  obj.array = cigar_array
  obj
end

Instance Method Details

#==(other) ⇒ Object



62
63
64
# File 'lib/hts/bam/cigar.rb', line 62

def ==(other)
  other.is_a?(Cigar) && (@array == other.array)
end

#eachObject



40
41
42
43
44
45
46
47
48
# File 'lib/hts/bam/cigar.rb', line 40

def each
  return to_enum(__method__) unless block_given?

  @array.each do |c|
    op =  LibHTS.bam_cigar_opchr(c)
    len = LibHTS.bam_cigar_oplen(c)
    yield [op, len]
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/hts/bam/cigar.rb', line 66

def eql?(other)
  other.is_a?(Cigar) && @array.eql?(other.array)
end

#qlenObject



50
51
52
53
54
# File 'lib/hts/bam/cigar.rb', line 50

def qlen
  a = FFI::MemoryPointer.new(:uint32, @array.size)
  a.write_array_of_uint32(@array)
  LibHTS.bam_cigar2qlen(@array.size, a)
end

#rlenObject



56
57
58
59
60
# File 'lib/hts/bam/cigar.rb', line 56

def rlen
  a = FFI::MemoryPointer.new(:uint32, @array.size)
  a.write_array_of_uint32(@array)
  LibHTS.bam_cigar2rlen(@array.size, a)
end

#to_sObject



36
37
38
# File 'lib/hts/bam/cigar.rb', line 36

def to_s
  map { |op, len| "#{len}#{op}" }.join
end