Class: HTS::Bam::Record

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

Constant Summary collapse

SEQ_NT16_STR =
"=ACMGRSVTWYHKDBN"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bam1_t, header) ⇒ Record

Returns a new instance of Record.



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

def initialize(bam1_t, header)
  @bam1 = bam1_t
  @header = header
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



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

def header
  @header
end

Instance Method Details

#aux(key = nil) ⇒ Object

retruns the auxillary fields.



206
207
208
209
210
211
212
213
# File 'lib/hts/bam/record.rb', line 206

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

#base(n) ⇒ Object

return only the base of the requested index “i” of the query sequence.



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

def base(n)
  n += @bam1[:core][:l_qseq] if n < 0
  return "." if (n >= @bam1[:core][:l_qseq]) || (n < 0) # eg. base(-1000)

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

#base_qual(n) ⇒ Object

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



181
182
183
184
185
186
187
# File 'lib/hts/bam/record.rb', line 181

def base_qual(n)
  n += @bam1[:core][:l_qseq] if n < 0
  return 0 if (n >= @bam1[:core][:l_qseq]) || (n < 0) # eg. base_qual(-1000)

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

#binObject



73
74
75
# File 'lib/hts/bam/record.rb', line 73

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

#bin=(bin) ⇒ Object



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

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

#chromObject Also known as: contig

returns the chromosome or ” if not mapped.



87
88
89
90
91
# File 'lib/hts/bam/record.rb', line 87

def chrom
  return "" if tid == -1

  LibHTS.sam_hdr_tid2name(@header, tid)
end

#cigarObject

returns a ‘Cigar` object.



132
133
134
# File 'lib/hts/bam/record.rb', line 132

def cigar
  Cigar.new(LibHTS.bam_get_cigar(@bam1), @bam1[:core][:n_cigar])
end

#endposObject

returns end position of the read.



82
83
84
# File 'lib/hts/bam/record.rb', line 82

def endpos
  LibHTS.bam_endpos @bam1
end

#flagObject

returns a ‘Flag` object.



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

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

#flag=(flag) ⇒ Object



194
195
196
197
198
199
200
201
202
203
# File 'lib/hts/bam/record.rb', line 194

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_sizeObject Also known as: isize

insert size



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

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

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



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

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

#lenObject



161
162
163
# File 'lib/hts/bam/record.rb', line 161

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

#mapqObject

mapping quality



123
124
125
# File 'lib/hts/bam/record.rb', line 123

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

#mapq=(mapq) ⇒ Object



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

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

#mate_chromObject Also known as: mate_contig

returns the chromosome of the mate or ” if not mapped.



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

def mate_chrom
  return "" if mtid == -1

  LibHTS.sam_hdr_tid2name(@header, mtid)
end

#mposObject

returns 0-based mate position



65
66
67
# File 'lib/hts/bam/record.rb', line 65

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

#mpos=(mpos) ⇒ Object



69
70
71
# File 'lib/hts/bam/record.rb', line 69

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

#mtidObject

returns the tid of the mate or -1 if not mapped.



47
48
49
# File 'lib/hts/bam/record.rb', line 47

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

#mtid=(mtid) ⇒ Object



51
52
53
# File 'lib/hts/bam/record.rb', line 51

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

#posObject

returns 0-based start position.



56
57
58
# File 'lib/hts/bam/record.rb', line 56

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

#pos=(pos) ⇒ Object



60
61
62
# File 'lib/hts/bam/record.rb', line 60

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

#qlenObject



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

def qlen
  LibHTS.bam_cigar2qlen(
    @bam1[:core][:n_cigar],
    LibHTS.bam_get_cigar(@bam1)
  )
end

#qnameObject

returns the query name.



28
29
30
# File 'lib/hts/bam/record.rb', line 28

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

#qualObject

return the base qualities



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

def qual
  q_ptr = LibHTS.bam_get_qual(@bam1)
  q_ptr.read_array_of_uint8(@bam1[:core][:l_qseq])
end

#rlenObject



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

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

#seqObject Also known as: sequence

return the read sequence



151
152
153
154
155
156
157
158
# File 'lib/hts/bam/record.rb', line 151

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

#strandObject

Get strand information.



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

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

#structObject



19
20
21
# File 'lib/hts/bam/record.rb', line 19

def struct
  @bam1
end

#tidObject

returns the tid of the record or -1 if not mapped.



38
39
40
# File 'lib/hts/bam/record.rb', line 38

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

#tid=(tid) ⇒ Object



42
43
44
# File 'lib/hts/bam/record.rb', line 42

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

#to_ptrObject



23
24
25
# File 'lib/hts/bam/record.rb', line 23

def to_ptr
  @bam1.to_ptr
end

#to_sObject



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

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