Class: HTS::Bam::Record
- Inherits:
-
Object
- Object
- HTS::Bam::Record
- 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
-
#header ⇒ Object
readonly
Returns the value of attribute header.
Instance Method Summary collapse
-
#aux(key = nil) ⇒ String
Get the auxiliary data.
-
#base(n) ⇒ String
Get the base of the requested index “i” of the query sequence.
-
#base_qual(n) ⇒ Integer
Get the base quality of the requested index “i” of the query sequence.
-
#bin ⇒ Integer
Get the bin calculated by bam_reg2bin().
- #bin=(bin) ⇒ Object
-
#chrom ⇒ String
(also: #contig)
Get the reference sequence name of the alignment.
-
#cigar ⇒ Bam::Cigar
Get the Bam::Cigar object.
-
#endpos ⇒ Integer
Get the rightmost base position of the alignment on the reference genome.
-
#flag ⇒ Bam::Flag
Get Bam::Flag object of the alignment.
- #flag=(flag) ⇒ Object
-
#initialize(bam1_t, header) ⇒ Record
constructor
A new instance of Record.
-
#insert_size ⇒ Integer
(also: #isize)
Get the observed template length.
- #insert_size=(isize) ⇒ Object (also: #isize=)
-
#len ⇒ Integer
Get the length of the query sequence.
-
#mapq ⇒ Integer
Get the mapping quality of the alignment.
- #mapq=(mapq) ⇒ Object
-
#mate_chrom ⇒ String
(also: #mate_contig)
Get the reference sequence name of the mate.
-
#mate_pos ⇒ Integer
(also: #mpos)
Get the 0-based leftmost coordinate of the mate.
- #mate_pos=(mpos) ⇒ Object (also: #mpos=)
-
#mate_strand ⇒ String
Get whether the query’s mate is on the reverse strand.
-
#mtid ⇒ Integer
Get the chromosome ID of the mate.
- #mtid=(mtid) ⇒ Object
-
#pos ⇒ Integer
Get the 0-based leftmost coordinate of the alignment.
- #pos=(pos) ⇒ Object
-
#qlen ⇒ Integer
Calculate query length from CIGAR.
-
#qname ⇒ String
Get the read name.
-
#qual ⇒ Array
Get the base qualities.
-
#qual_string ⇒ String
Get the base qualities as a string.
-
#rlen ⇒ Integer
Calculate reference length from CIGAR.
-
#seq ⇒ String
(also: #sequence)
Get the sequence.
-
#strand ⇒ String
Get whether the query is on the reverse strand.
-
#struct ⇒ Object
Return the FFI::Struct object.
-
#tid ⇒ Integer
Get the chromosome ID of the alignment.
- #tid=(tid) ⇒ Object
- #to_ptr ⇒ Object
-
#to_s ⇒ String
A string representation of the alignment.
Constructor Details
#initialize(bam1_t, header) ⇒ Record
Returns a new instance of Record.
15 16 17 18 |
# File 'lib/hts/bam/record.rb', line 15 def initialize(bam1_t, header) @bam1 = bam1_t @header = header end |
Instance Attribute Details
#header ⇒ Object (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.
250 251 252 253 254 255 256 257 |
# File 'lib/hts/bam/record.rb', line 250 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.
197 198 199 200 201 202 203 |
# File 'lib/hts/bam/record.rb', line 197 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) ⇒ Integer
Get the base quality of the requested index “i” of the query sequence.
222 223 224 225 226 227 228 |
# File 'lib/hts/bam/record.rb', line 222 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 |
#bin ⇒ Integer
Get the bin calculated by bam_reg2bin().
80 81 82 |
# File 'lib/hts/bam/record.rb', line 80 def bin @bam1[:core][:bin] end |
#bin=(bin) ⇒ Object
84 85 86 |
# File 'lib/hts/bam/record.rb', line 84 def bin=(bin) @bam1[:core][:bin] = bin end |
#chrom ⇒ String Also known as: contig
Get the reference sequence name of the alignment. (a.k.a RNAME) ” if not mapped.
97 98 99 100 101 |
# File 'lib/hts/bam/record.rb', line 97 def chrom return "" if tid == -1 LibHTS.sam_hdr_tid2name(@header, tid) end |
#cigar ⇒ Bam::Cigar
Get the Bam::Cigar object.
153 154 155 |
# File 'lib/hts/bam/record.rb', line 153 def cigar Cigar.new(self) end |
#endpos ⇒ Integer
Get the rightmost base position of the alignment on the reference genome.
90 91 92 |
# File 'lib/hts/bam/record.rb', line 90 def endpos LibHTS.bam_endpos @bam1 end |
#flag ⇒ Bam::Flag
Get Bam::Flag object of the alignment.
232 233 234 |
# File 'lib/hts/bam/record.rb', line 232 def flag Flag.new(@bam1[:core][:flag]) end |
#flag=(flag) ⇒ Object
236 237 238 239 240 241 242 243 244 245 |
# File 'lib/hts/bam/record.rb', line 236 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_size ⇒ Integer Also known as: isize
Get the observed template length. (a.k.a TLEN)
130 131 132 |
# File 'lib/hts/bam/record.rb', line 130 def insert_size @bam1[:core][:isize] end |
#insert_size=(isize) ⇒ Object Also known as: isize=
134 135 136 |
# File 'lib/hts/bam/record.rb', line 134 def insert_size=(isize) @bam1[:core][:isize] = isize end |
#len ⇒ Integer
Get the length of the query sequence.
190 191 192 |
# File 'lib/hts/bam/record.rb', line 190 def len @bam1[:core][:l_qseq] end |
#mapq ⇒ Integer
Get the mapping quality of the alignment. (a.k.a MAPQ)
143 144 145 |
# File 'lib/hts/bam/record.rb', line 143 def mapq @bam1[:core][:qual] end |
#mapq=(mapq) ⇒ Object
147 148 149 |
# File 'lib/hts/bam/record.rb', line 147 def mapq=(mapq) @bam1[:core][:qual] = mapq end |
#mate_chrom ⇒ String Also known as: mate_contig
Get the reference sequence name of the mate. ” if not mapped.
108 109 110 111 112 |
# File 'lib/hts/bam/record.rb', line 108 def mate_chrom return "" if mtid == -1 LibHTS.sam_hdr_tid2name(@header, mtid) end |
#mate_pos ⇒ Integer Also known as: mpos
Get the 0-based leftmost coordinate of the mate.
67 68 69 |
# File 'lib/hts/bam/record.rb', line 67 def mate_pos @bam1[:core][:mpos] end |
#mate_pos=(mpos) ⇒ Object Also known as: mpos=
71 72 73 |
# File 'lib/hts/bam/record.rb', line 71 def mate_pos=(mpos) @bam1[:core][:mpos] = mpos end |
#mate_strand ⇒ String
Get whether the query’s mate is on the reverse strand.
124 125 126 |
# File 'lib/hts/bam/record.rb', line 124 def mate_strand LibHTS.bam_is_mrev(@bam1) ? "-" : "+" end |
#mtid ⇒ Integer
Get the chromosome ID of the mate. -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 |
#pos ⇒ Integer
Get the 0-based leftmost coordinate of the alignment.
57 58 59 |
# File 'lib/hts/bam/record.rb', line 57 def pos @bam1[:core][:pos] end |
#pos=(pos) ⇒ Object
61 62 63 |
# File 'lib/hts/bam/record.rb', line 61 def pos=(pos) @bam1[:core][:pos] = pos end |
#qlen ⇒ Integer
Calculate query length from CIGAR.
159 160 161 162 163 164 165 |
# File 'lib/hts/bam/record.rb', line 159 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 |
#qname ⇒ String
Get the read name. (a.k.a QNAME)
31 32 33 |
# File 'lib/hts/bam/record.rb', line 31 def qname LibHTS.bam_get_qname(@bam1).read_string end |
#qual ⇒ Array
Get the base qualities.
207 208 209 210 |
# File 'lib/hts/bam/record.rb', line 207 def qual q_ptr = LibHTS.bam_get_qual(@bam1) q_ptr.read_array_of_uint8(@bam1[:core][:l_qseq]) end |
#qual_string ⇒ String
Get the base qualities as a string. (a.k.a QUAL) ASCII of base quality + 33.
215 216 217 |
# File 'lib/hts/bam/record.rb', line 215 def qual_string qual.map { |q| (q + 33).chr }.join end |
#rlen ⇒ Integer
Calculate reference length from CIGAR.
169 170 171 172 173 174 |
# File 'lib/hts/bam/record.rb', line 169 def rlen LibHTS.bam_cigar2rlen( @bam1[:core][:n_cigar], LibHTS.bam_get_cigar(@bam1) ) end |
#seq ⇒ String Also known as: sequence
Get the sequence. (a.k.a SEQ)
178 179 180 181 182 183 184 185 |
# File 'lib/hts/bam/record.rb', line 178 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 |
#strand ⇒ String
Get whether the query is on the reverse strand.
118 119 120 |
# File 'lib/hts/bam/record.rb', line 118 def strand LibHTS.bam_is_rev(@bam1) ? "-" : "+" end |
#struct ⇒ Object
Return the FFI::Struct object.
21 22 23 |
# File 'lib/hts/bam/record.rb', line 21 def struct @bam1 end |
#tid ⇒ Integer
Get the chromosome ID of the alignment. -1 if not mapped.
37 38 39 |
# File 'lib/hts/bam/record.rb', line 37 def tid @bam1[:core][:tid] end |
#tid=(tid) ⇒ Object
41 42 43 |
# File 'lib/hts/bam/record.rb', line 41 def tid=(tid) @bam1[:core][:tid] = tid end |
#to_ptr ⇒ Object
25 26 27 |
# File 'lib/hts/bam/record.rb', line 25 def to_ptr @bam1.to_ptr end |
#to_s ⇒ String
Returns a string representation of the alignment.
275 276 277 278 279 280 |
# File 'lib/hts/bam/record.rb', line 275 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 |