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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bam1_t, header) ⇒ Record

Returns a new instance of Record.



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

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

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



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

def header
  @header
end

Class Method Details

.rom_sam_strObject

def initialize_copy

super

end



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

def self.rom_sam_str; end

Instance Method Details

#base_at(n) ⇒ Object

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



138
139
140
141
142
143
144
# File 'lib/hts/bam/record.rb', line 138

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

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

#base_qualitiesObject

return the base qualities



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

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

#base_quality_at(n) ⇒ Object

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



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

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

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

#chromObject

returns the chromosome or ” if not mapped.



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

def chrom
  return "" if tid == -1

  LibHTS.sam_hdr_tid2name(@header, tid)
end

#cigarObject

returns a ‘Cigar` object.



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

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

#contigObject

returns the chromosome or ” if not mapped.



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

def contig
  chrom
end

#flagObject

returns a ‘Flag` object.



166
167
168
# File 'lib/hts/bam/record.rb', line 166

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

#flag_strObject



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

def flag_str
  LibHTS.bam_flag2str(@bam1[:core][:flag])
end

#insert_sizeObject

insert size



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

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

#mapping_qualityObject

mapping quality



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

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

#mate_chromObject

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



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

def mate_chrom
  mtid = mate_tid
  return "" if mtid == -1

  LibHTS.sam_hdr_tid2name(@header, mtid)
end

#mate_startObject Also known as: mate_pos

returns 0-based mate position



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

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

#mate_tidObject

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



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

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

#qlenObject



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

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

#qnameObject

returns the query name.



35
36
37
# File 'lib/hts/bam/record.rb', line 35

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

#rlenObject



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

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

#sequenceObject

return the read sequence



128
129
130
131
132
133
134
135
# File 'lib/hts/bam/record.rb', line 128

def sequence
  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

#startObject

returns 0-based start position.



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

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

#stopObject

returns end position of the read.



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

def stop
  LibHTS.bam_endpos @bam1
end

#strandObject



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

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

#structObject



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

def struct
  @bam1
end

#tag(str) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/hts/bam/record.rb', line 170

def tag(str)
  aux = LibHTS.bam_aux_get(@bam1, str)
  return nil if aux.null?

  t = aux.read_string(1)

  # A (character), B (general array),
  # f (real number), H (hexadecimal array),
  # i (integer), or Z (string).

  case t
  when "i", "I", "c", "C", "s", "S"
    LibHTS.bam_aux2i(aux)
  when "f", "d"
    LibHTS.bam_aux2f(aux)
  when "Z", "H"
    LibHTS.bam_aux2Z(aux)
  when "A" # char
    LibHTS.bam_aux2A(aux).chr
  end
end

#tagsObject



32
# File 'lib/hts/bam/record.rb', line 32

def tags; end

#tidObject

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



45
46
47
# File 'lib/hts/bam/record.rb', line 45

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

#to_ptrObject



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

def to_ptr
  @bam1.to_ptr
end

#to_sObject



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

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