Class: HTS::Bam
- Includes:
- Enumerable
- Defined in:
- lib/hts/bam.rb,
lib/hts/bam/aux.rb,
lib/hts/bam/flag.rb,
lib/hts/bam/cigar.rb,
lib/hts/bam/header.rb,
lib/hts/bam/record.rb
Overview
A class for working with SAM, BAM, CRAM files.
Defined Under Namespace
Classes: Aux, Cigar, Flag, Header, Record
Instance Attribute Summary collapse
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#index_name ⇒ Object
readonly
Returns the value of attribute index_name.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Class Method Summary collapse
Instance Method Summary collapse
- #aux(tag) ⇒ Object
-
#chrom ⇒ Array
Get chrom array.
-
#cigar ⇒ Array
Get cigar array.
-
#close ⇒ Object
Close the current file.
- #create_index(index_name = nil) ⇒ Object
- #each(copy: false, &block) ⇒ Object
- #each_aux(tag) ⇒ Object
-
#each_chrom ⇒ Object
Get chrom iterator.
-
#each_cigar ⇒ Object
Get cigar iterator.
-
#each_flag ⇒ Object
Get flag iterator.
-
#each_insert_size ⇒ Object
(also: #each_isize)
Get insert_size iterator.
-
#each_mapq ⇒ Object
Get mapq iterator.
-
#each_mate_chrom ⇒ Object
Get mate_chrom iterator.
-
#each_mate_pos ⇒ Object
(also: #each_mpos)
Get mate_pos iterator.
-
#each_pos ⇒ Object
Get pos iterator.
-
#each_qname ⇒ Object
Get qname iterator.
-
#each_qual ⇒ Object
Get qual iterator.
-
#each_seq ⇒ Object
Get seq iterator.
-
#flag ⇒ Array
Get flag array.
- #index_loaded? ⇒ Boolean
-
#initialize(file_name, mode = "r", index: nil, fai: nil, threads: nil, create_index: false) ⇒ Bam
constructor
A new instance of Bam.
-
#insert_size ⇒ Array
(also: #isize)
Get insert_size array.
- #load_index(index_name = nil) ⇒ Object
-
#mapq ⇒ Array
Get mapq array.
-
#mate_chrom ⇒ Array
Get mate_chrom array.
-
#mate_pos ⇒ Array
(also: #mpos)
Get mate_pos array.
-
#pos ⇒ Array
Get pos array.
-
#qname ⇒ Array
Get qname array.
-
#qual ⇒ Array
Get qual array.
- #query(region, copy: false, &block) ⇒ Object
-
#seq ⇒ Array
Get seq array.
- #write(aln) ⇒ Object
- #write_header(header) ⇒ Object
Methods inherited from Hts
#closed?, #file_format, #file_format_version, #rewind, #seek, #set_threads, #struct, #tell, #to_ptr
Constructor Details
#initialize(file_name, mode = "r", index: nil, fai: nil, threads: nil, create_index: false) ⇒ Bam
Returns a new instance of Bam.
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 |
# File 'lib/hts/bam.rb', line 30 def initialize(file_name, mode = "r", index: nil, fai: nil, threads: nil, create_index: false) if block_given? = "HTS::Bam.new() dose not take block; Please use HTS::Bam.open() instead" raise end # NOTE: Do not check for the existence of local files, since file_names may be remote URIs. @file_name = file_name @index_name = index @mode = mode @hts_file = LibHTS.hts_open(@file_name, mode) raise Errno::ENOENT, "Failed to open #{@file_name}" if @hts_file.null? if fai r = LibHTS.hts_set_fai_filename(@hts_file, fai) raise "Failed to load fasta index: #{fai}" if r < 0 end set_threads(threads) if threads return if @mode[0] == "w" @header = Bam::Header.new(@hts_file) create_index(index) if create_index @idx = load_index(index) @start_position = tell super # do nothing end |
Instance Attribute Details
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
16 17 18 |
# File 'lib/hts/bam.rb', line 16 def file_name @file_name end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
16 17 18 |
# File 'lib/hts/bam.rb', line 16 def header @header end |
#index_name ⇒ Object (readonly)
Returns the value of attribute index_name.
16 17 18 |
# File 'lib/hts/bam.rb', line 16 def index_name @index_name end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
16 17 18 |
# File 'lib/hts/bam.rb', line 16 def mode @mode end |
Class Method Details
.open(*args, **kw) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/hts/bam.rb', line 18 def self.open(*args, **kw) file = new(*args, **kw) # do not yield return file unless block_given? begin yield file ensure file.close end file end |
Instance Method Details
#aux(tag) ⇒ Object
208 209 210 211 212 213 214 215 |
# File 'lib/hts/bam.rb', line 208 def aux(tag) warn "experimental" check_closed position = tell ary = map { |r| r.aux(tag) } seek(position) ary end |
#chrom ⇒ Array
Get chrom array
195 |
# File 'lib/hts/bam.rb', line 195 define_getter :chrom |
#cigar ⇒ Array
Get cigar array
198 |
# File 'lib/hts/bam.rb', line 198 define_getter :cigar |
#close ⇒ Object
Close the current file.
90 91 92 93 94 |
# File 'lib/hts/bam.rb', line 90 def close LibHTS.hts_idx_destroy(@idx) if @idx&.null? @idx = nil super end |
#create_index(index_name = nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/hts/bam.rb', line 62 def create_index(index_name = nil) check_closed warn "Create index for #{@file_name} to #{index_name}" if index LibHTS.sam_index_build2(@file_name, index_name, -1) else LibHTS.sam_index_build(@file_name, -1) end end |
#each(copy: false, &block) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/hts/bam.rb', line 111 def each(copy: false, &block) if copy each_record_copy(&block) else each_record_reuse(&block) end end |
#each_aux(tag) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/hts/bam.rb', line 235 def each_aux(tag) warn "experimental" check_closed return to_enum(__method__, tag) unless block_given? each do |record| yield record.aux(tag) end self end |
#each_chrom ⇒ Object
Get chrom iterator
222 |
# File 'lib/hts/bam.rb', line 222 define_iterator :chrom |
#each_cigar ⇒ Object
Get cigar iterator
225 |
# File 'lib/hts/bam.rb', line 225 define_iterator :cigar |
#each_flag ⇒ Object
Get flag iterator
221 |
# File 'lib/hts/bam.rb', line 221 define_iterator :flag |
#each_insert_size ⇒ Object Also known as: each_isize
Get insert_size iterator
228 |
# File 'lib/hts/bam.rb', line 228 define_iterator :insert_size |
#each_mapq ⇒ Object
Get mapq iterator
224 |
# File 'lib/hts/bam.rb', line 224 define_iterator :mapq |
#each_mate_chrom ⇒ Object
Get mate_chrom iterator
226 |
# File 'lib/hts/bam.rb', line 226 define_iterator :mate_chrom |
#each_mate_pos ⇒ Object Also known as: each_mpos
Get mate_pos iterator
227 |
# File 'lib/hts/bam.rb', line 227 define_iterator :mate_pos |
#each_pos ⇒ Object
Get pos iterator
223 |
# File 'lib/hts/bam.rb', line 223 define_iterator :pos |
#each_qname ⇒ Object
Get qname iterator
220 |
# File 'lib/hts/bam.rb', line 220 define_iterator :qname |
#each_qual ⇒ Object
Get qual iterator
230 |
# File 'lib/hts/bam.rb', line 230 define_iterator :qual |
#each_seq ⇒ Object
Get seq iterator
229 |
# File 'lib/hts/bam.rb', line 229 define_iterator :seq |
#flag ⇒ Array
Get flag array
194 |
# File 'lib/hts/bam.rb', line 194 define_getter :flag |
#index_loaded? ⇒ Boolean
83 84 85 86 87 |
# File 'lib/hts/bam.rb', line 83 def index_loaded? check_closed !@idx.null? end |
#insert_size ⇒ Array Also known as: isize
Get insert_size array
201 |
# File 'lib/hts/bam.rb', line 201 define_getter :insert_size |
#load_index(index_name = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/hts/bam.rb', line 73 def load_index(index_name = nil) check_closed if index_name LibHTS.sam_index_load2(@hts_file, @file_name, index_name) else LibHTS.sam_index_load3(@hts_file, @file_name, nil, 2) # should be 3 ? (copy remote file to local?) end end |
#mapq ⇒ Array
Get mapq array
197 |
# File 'lib/hts/bam.rb', line 197 define_getter :mapq |
#mate_chrom ⇒ Array
Get mate_chrom array
199 |
# File 'lib/hts/bam.rb', line 199 define_getter :mate_chrom |
#mate_pos ⇒ Array Also known as: mpos
Get mate_pos array
200 |
# File 'lib/hts/bam.rb', line 200 define_getter :mate_pos |
#pos ⇒ Array
Get pos array
196 |
# File 'lib/hts/bam.rb', line 196 define_getter :pos |
#qname ⇒ Array
Get qname array
193 |
# File 'lib/hts/bam.rb', line 193 define_getter :qname |
#qual ⇒ Array
Get qual array
203 |
# File 'lib/hts/bam.rb', line 203 define_getter :qual |
#query(region, copy: false, &block) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/hts/bam.rb', line 142 def query(region, copy: false, &block) if copy query_copy(region, &block) else query_reuse(region, &block) end end |
#seq ⇒ Array
Get seq array
202 |
# File 'lib/hts/bam.rb', line 202 define_getter :seq |
#write(aln) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/hts/bam.rb', line 104 def write(aln) check_closed aln_dup = aln.dup LibHTS.sam_write1(@hts_file, header, aln_dup) > 0 || raise end |
#write_header(header) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/hts/bam.rb', line 96 def write_header(header) check_closed @header = header.dup LibHTS.hts_set_fai_filename(@hts_file, @file_name) LibHTS.sam_hdr_write(@hts_file, header) end |