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.



33
34
35
36
37
38
39
40
41
42
# File 'lib/hts/bam/cigar.rb', line 33

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
24
25
26
27
28
29
30
31
# File 'lib/hts/bam/cigar.rb', line 15

def self.parse(str)
  c = FFI::MemoryPointer.new(:pointer)
  m = FFI::MemoryPointer.new(:size_t)
  c.write_pointer(FFI::Pointer::NULL)
  m.write(:size_t, 0)
  ptr = nil
  n_cigar = LibHTS.sam_parse_cigar(str, FFI::Pointer::NULL, c, m)
  raise "sam_parse_cigar failed: #{n_cigar}" if n_cigar.negative?

  ptr = c.read_pointer
  cigar_array = ptr.null? ? [] : ptr.read_array_of_uint32(n_cigar)
  obj = new
  obj.array = cigar_array
  obj
ensure
  LibHTS.hts_free(ptr) if ptr && !ptr.null?
end

Instance Method Details

#==(other) ⇒ Object



70
71
72
# File 'lib/hts/bam/cigar.rb', line 70

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

#eachObject



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

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)


74
75
76
# File 'lib/hts/bam/cigar.rb', line 74

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

#qlenObject



58
59
60
61
62
# File 'lib/hts/bam/cigar.rb', line 58

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

#rlenObject



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

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

#to_sObject



44
45
46
# File 'lib/hts/bam/cigar.rb', line 44

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