Class: HTS::Bam::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/hts/bam/record.rb

Overview

A class for working with alignment records.

Constant Summary collapse

SEQ_NT16_STR =
"=ACMGRSVTWYHKDBN"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, bam1 = nil, qname: nil, flag: nil, tid: nil, pos: nil, mapq: nil, cigar: nil, mtid: nil, mpos: nil, isize: nil, seq: nil, qual: nil, l_aux: nil) ⇒ Record

Initialization API is experimental.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hts/bam/record.rb', line 17

def initialize(
  header,
  bam1 = nil,
  qname: nil,
  flag: nil,
  tid: nil,
  pos: nil,
  mapq: nil,
  cigar: nil,
  mtid: nil,
  mpos: nil,
  isize: nil,
  seq: nil,
  qual: nil,
  l_aux: nil
)
  @bam1 = bam1 || LibHTS.bam_init1
  @header = header

  params = [qname, flag, tid, pos, mapq, cigar, mtid, mpos, isize, seq, qual, l_aux]
  return if params.all? { |x| x.nil? }

  if params.all?
    c = FFI::MemoryPointer.new(:pointer)
    m = FFI::MemoryPointer.new(:size_t)
    LibHTS.sam_parse_cigar(cigar, FFI::Pointer::NULL, c, m)
    cigar_array = c.read_pointer.read_array_of_uint32(m.read(:size_t))
    cigar_pointer = FFI::MemoryPointer.new(:uint32, cigar_array.length)
    cigar_pointer.write_array_of_uint32(cigar_array)
    if qual.is_a?(Array)
      qual = qual.pack("C*")
    elsif qual.is_a?(String)
      raise "Which implementation is better? +33 or not? Please tell me."
    end
    r = LibHTS.bam_set1(
      @bam1,
      qname.length,
      qname,
      flag,
      tid,
      pos,
      mapq,
      cigar_array.length,
      cigar_pointer,
      mtid,
      mpos,
      isize,
      seq.length,
      seq,
      qual,
      l_aux
    )
    raise "bam_set1 failed: #{r}" if r < 0
  else
    warn "Ignore bam_set1 because some arguments are missing."
  end
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



13
14
15
# File 'lib/hts/bam/record.rb', line 13

def header
  @header
end

Instance Method Details

#aux(key = nil) ⇒ String

Get the auxiliary data.

Parameters:

  • key (String) (defaults to: nil)

    tag name

Returns:

  • (String)

    value



320
321
322
323
324
325
326
327
# File 'lib/hts/bam/record.rb', line 320

def aux(key = nil)
  aux = Aux.new(self)
  if key
    aux.get(key)
  else
    aux
  end
end

#base(n) ⇒ String

Get the base of the requested index “i” of the query sequence.

Parameters:

  • i (Integer)

    index

Returns:

  • (String)

    base



267
268
269
270
271
272
273
# File 'lib/hts/bam/record.rb', line 267

def base(n)
  n += len if n < 0
  return "." if (n >= len) || (n < 0) # eg. base(-1000)

  r = LibHTS.bam_get_seq(@bam1)
  SEQ_NT16_STR[LibHTS.bam_seqi(r, n)]
end

#base_qual(n) ⇒ Integer

Get the base quality of the requested index “i” of the query sequence.

Parameters:

  • i (Integer)

    index

Returns:

  • (Integer)

    base quality



292
293
294
295
296
297
298
# File 'lib/hts/bam/record.rb', line 292

def base_qual(n)
  n += len if n < 0
  return 0 if (n >= len) || (n < 0) # eg. base_qual(-1000)

  q_ptr = LibHTS.bam_get_qual(@bam1)
  q_ptr.get_uint8(n)
end

#binInteger

Get the bin calculated by bam_reg2bin().

Returns:

  • (Integer)

    bin



139
140
141
# File 'lib/hts/bam/record.rb', line 139

def bin
  @bam1[:core][:bin]
end

#bin=(bin) ⇒ Object



143
144
145
# File 'lib/hts/bam/record.rb', line 143

def bin=(bin)
  @bam1[:core][:bin] = bin
end

#chromString Also known as: contig

Get the reference sequence name of the alignment. (a.k.a RNAME) ” if not mapped.

Returns:

  • (String)

    reference sequence name



156
157
158
159
160
# File 'lib/hts/bam/record.rb', line 156

def chrom
  return "" if tid == -1

  LibHTS.sam_hdr_tid2name(@header, tid)
end

#cigarBam::Cigar

Get the Bam::Cigar object.

Returns:



212
213
214
# File 'lib/hts/bam/record.rb', line 212

def cigar
  Cigar.new(self)
end

#cigar=(str) ⇒ Object



216
217
218
219
220
221
222
223
224
225
# File 'lib/hts/bam/record.rb', line 216

def cigar=(str)
  if cigar.is_a? Cigar
    raise "Not implemented yet."
  elsif cigar.is_a? String
    r = LibHTS.bam_parse_cigar(str, FFI::Pointer::NULL, @bam1)
    raise "bam_parse_cigar failed: #{r}" if r != 0
  else
    raise ArgumentError, "cigar must be a String or Bam::Cigar"
  end
end

#endposInteger

Get the rightmost base position of the alignment on the reference genome.

Returns:

  • (Integer)

    0-based rightmost coordinate



149
150
151
# File 'lib/hts/bam/record.rb', line 149

def endpos
  LibHTS.bam_endpos @bam1
end

#flagBam::Flag

Get Bam::Flag object of the alignment.

Returns:



302
303
304
# File 'lib/hts/bam/record.rb', line 302

def flag
  Flag.new(@bam1[:core][:flag])
end

#flag=(flag) ⇒ Object



306
307
308
309
310
311
312
313
314
315
# File 'lib/hts/bam/record.rb', line 306

def flag=(flag)
  case flag
  when Integer
    @bam1[:core][:flag] = flag
  when Flag
    @bam1[:core][:flag] = flag.value
  else
    raise "Invalid flag type: #{flag.class}"
  end
end

#insert_sizeInteger Also known as: isize

Get the observed template length. (a.k.a TLEN)

Returns:

  • (Integer)

    isize



189
190
191
# File 'lib/hts/bam/record.rb', line 189

def insert_size
  @bam1[:core][:isize]
end

#insert_size=(isize) ⇒ Object Also known as: isize=



193
194
195
# File 'lib/hts/bam/record.rb', line 193

def insert_size=(isize)
  @bam1[:core][:isize] = isize
end

#lenInteger

Get the length of the query sequence.

Returns:

  • (Integer)

    query length



260
261
262
# File 'lib/hts/bam/record.rb', line 260

def len
  @bam1[:core][:l_qseq]
end

#mapqInteger

Get the mapping quality of the alignment. (a.k.a MAPQ)

Returns:

  • (Integer)

    mapping quality



202
203
204
# File 'lib/hts/bam/record.rb', line 202

def mapq
  @bam1[:core][:qual]
end

#mapq=(mapq) ⇒ Object



206
207
208
# File 'lib/hts/bam/record.rb', line 206

def mapq=(mapq)
  @bam1[:core][:qual] = mapq
end

#mate_chromString Also known as: mate_contig

Get the reference sequence name of the mate. ” if not mapped.

Returns:

  • (String)

    reference sequence name



167
168
169
170
171
# File 'lib/hts/bam/record.rb', line 167

def mate_chrom
  return "" if mtid == -1

  LibHTS.sam_hdr_tid2name(@header, mtid)
end

#mate_posInteger Also known as: mpos

Get the 0-based leftmost coordinate of the mate.

Returns:

  • (Integer)

    0-based leftmost coordinate



126
127
128
# File 'lib/hts/bam/record.rb', line 126

def mate_pos
  @bam1[:core][:mpos]
end

#mate_pos=(mpos) ⇒ Object Also known as: mpos=



130
131
132
# File 'lib/hts/bam/record.rb', line 130

def mate_pos=(mpos)
  @bam1[:core][:mpos] = mpos
end

#mate_strandString

Get whether the query’s mate is on the reverse strand.

Returns:

  • (String)

    strand “+” or “-”



183
184
185
# File 'lib/hts/bam/record.rb', line 183

def mate_strand
  LibHTS.bam_is_mrev(@bam1) ? "-" : "+"
end

#mtidInteger

Get the chromosome ID of the mate. -1 if not mapped.

Returns:

  • (Integer)

    chromosome ID



106
107
108
# File 'lib/hts/bam/record.rb', line 106

def mtid
  @bam1[:core][:mtid]
end

#mtid=(mtid) ⇒ Object



110
111
112
# File 'lib/hts/bam/record.rb', line 110

def mtid=(mtid)
  @bam1[:core][:mtid] = mtid
end

#posInteger

Get the 0-based leftmost coordinate of the alignment.

Returns:

  • (Integer)

    0-based leftmost coordinate



116
117
118
# File 'lib/hts/bam/record.rb', line 116

def pos
  @bam1[:core][:pos]
end

#pos=(pos) ⇒ Object



120
121
122
# File 'lib/hts/bam/record.rb', line 120

def pos=(pos)
  @bam1[:core][:pos] = pos
end

#qlenInteger

Calculate query length from CIGAR.

Returns:

  • (Integer)

    query length



229
230
231
232
233
234
235
# File 'lib/hts/bam/record.rb', line 229

def qlen
  # cigar.qlen will be slower because it converts to a Ruby array.
  LibHTS.bam_cigar2qlen(
    @bam1[:core][:n_cigar],
    LibHTS.bam_get_cigar(@bam1)
  )
end

#qnameString

Get the read name. (a.k.a QNAME)

Returns:

  • (String)

    query template name



86
87
88
# File 'lib/hts/bam/record.rb', line 86

def qname
  LibHTS.bam_get_qname(@bam1).read_string
end

#qname=(name) ⇒ Object



90
91
92
# File 'lib/hts/bam/record.rb', line 90

def qname=(name)
  LibHTS.bam_set_qname(@bam1, name)
end

#qualArray

Get the base qualities.

Returns:

  • (Array)

    base qualities



277
278
279
280
# File 'lib/hts/bam/record.rb', line 277

def qual
  q_ptr = LibHTS.bam_get_qual(@bam1)
  q_ptr.read_array_of_uint8(len)
end

#qual_stringString

Get the base qualities as a string. (a.k.a QUAL) ASCII of base quality + 33.

Returns:

  • (String)

    base qualities



285
286
287
# File 'lib/hts/bam/record.rb', line 285

def qual_string
  qual.map { |q| (q + 33).chr }.join
end

#rlenInteger

Calculate reference length from CIGAR.

Returns:

  • (Integer)

    reference length



239
240
241
242
243
244
# File 'lib/hts/bam/record.rb', line 239

def rlen
  LibHTS.bam_cigar2rlen(
    @bam1[:core][:n_cigar],
    LibHTS.bam_get_cigar(@bam1)
  )
end

#seqString Also known as: sequence

Get the sequence. (a.k.a SEQ)

Returns:

  • (String)

    sequence



248
249
250
251
252
253
254
255
# File 'lib/hts/bam/record.rb', line 248

def seq
  r = LibHTS.bam_get_seq(@bam1)
  seq = String.new
  len.times do |i|
    seq << SEQ_NT16_STR[LibHTS.bam_seqi(r, i)]
  end
  seq
end

#strandString

Get whether the query is on the reverse strand.

Returns:

  • (String)

    strand “+” or “-”



177
178
179
# File 'lib/hts/bam/record.rb', line 177

def strand
  LibHTS.bam_is_rev(@bam1) ? "-" : "+"
end

#structObject

Return the FFI::Struct object.



76
77
78
# File 'lib/hts/bam/record.rb', line 76

def struct
  @bam1
end

#tidInteger

Get the chromosome ID of the alignment. -1 if not mapped.

Returns:

  • (Integer)

    chromosome ID



96
97
98
# File 'lib/hts/bam/record.rb', line 96

def tid
  @bam1[:core][:tid]
end

#tid=(tid) ⇒ Object



100
101
102
# File 'lib/hts/bam/record.rb', line 100

def tid=(tid)
  @bam1[:core][:tid] = tid
end

#to_ptrObject



80
81
82
# File 'lib/hts/bam/record.rb', line 80

def to_ptr
  @bam1.to_ptr
end

#to_sString

Returns a string representation of the alignment.

Returns:

  • (String)

    a string representation of the alignment.



345
346
347
348
349
350
# File 'lib/hts/bam/record.rb', line 345

def to_s
  kstr = LibHTS::KString.new
  raise "Failed to format bam record" if LibHTS.sam_format1(@header.struct, @bam1, kstr) == -1

  kstr[:s]
end