Class: HTS::Bam
- Inherits:
-
Object
- Object
- HTS::Bam
- Includes:
- Enumerable
- Defined in:
- lib/hts/bam.rb,
lib/hts/bam/flag.rb,
lib/hts/bam/cigar.rb,
lib/hts/bam/header.rb,
lib/hts/bam/record.rb
Defined Under Namespace
Classes: Cigar, Flag, Header, Record
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Class Method Summary collapse
Instance Method Summary collapse
-
#close ⇒ Object
Close the current file.
- #closed? ⇒ Boolean
- #create_index ⇒ Object
-
#each ⇒ Object
Iterate over each record.
-
#each_copy ⇒ Object
Iterate over each record.
-
#flush ⇒ Object
Flush the current file.
-
#initialize(filename, mode = "r", fai: nil, threads: nil, index: nil) ⇒ Bam
constructor
A new instance of Bam.
-
#query(region) ⇒ Object
query [WIP].
- #struct ⇒ Object
- #to_ptr ⇒ Object
- #write(aln) ⇒ Object
- #write_header(header) ⇒ Object
Constructor Details
#initialize(filename, mode = "r", fai: nil, threads: nil, index: nil) ⇒ Bam
Returns a new instance of Bam.
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 |
# File 'lib/hts/bam.rb', line 29 def initialize(filename, mode = "r", fai: nil, threads: nil, index: nil) raise "HTS::Bam.new() dose not take block; Please use HTS::Bam.open() instead" if block_given? @file_path = filename == "-" ? "-" : File.(filename) if mode[0] == "r" && !File.exist?(file_path) = "No such SAM/BAM file - #{file_path}" raise end @mode = mode @hts_file = LibHTS.hts_open(file_path, mode) if fai fai_path = File.(fai) r = LibHTS.hts_set_fai_filename(@hts_file, fai_path) raise "Failed to load fasta index: #{fai}" if r < 0 end if threads&.> 0 r = LibHTS.hts_set_threads(@hts_file, threads) raise "Failed to set number of threads: #{threads}" if r < 0 end return if mode[0] == "w" @header = Bam::Header.new(@hts_file) create_index if index # load index @idx = LibHTS.sam_index_load(@hts_file, file_path) end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
15 16 17 |
# File 'lib/hts/bam.rb', line 15 def file_path @file_path end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
15 16 17 |
# File 'lib/hts/bam.rb', line 15 def header @header end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
15 16 17 |
# File 'lib/hts/bam.rb', line 15 def mode @mode end |
Class Method Details
.open ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/hts/bam.rb', line 17 def self.open(...) file = new(...) return file unless block_given? begin yield file ensure file.close end file end |
Instance Method Details
#close ⇒ Object
Close the current file.
79 80 81 82 83 84 |
# File 'lib/hts/bam.rb', line 79 def close LibHTS.hts_idx_destroy(@idx) if @idx @idx = nil LibHTS.hts_close(@hts_file) @hts_file = nil end |
#closed? ⇒ Boolean
86 87 88 |
# File 'lib/hts/bam.rb', line 86 def closed? @hts_file.nil? end |
#create_index ⇒ Object
63 64 65 66 67 68 |
# File 'lib/hts/bam.rb', line 63 def create_index warn "Create index for #{file_path}" LibHTS.sam_index_build(file_path, -1) idx = LibHTS.sam_index_load(@hts_file, file_path) raise "Failed to load index: #{file_path}" if idx.null? end |
#each ⇒ Object
Iterate over each record. Record object is reused. Faster than each_copy.
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/hts/bam.rb', line 109 def each # Each does not always start at the beginning of the file. # This is the common behavior of IO objects in Ruby. # This may change in the future. return to_enum(__method__) unless block_given? bam1 = LibHTS.bam_init1 record = Record.new(bam1, header) yield record while LibHTS.sam_read1(@hts_file, header, bam1) > 0 end |
#each_copy ⇒ Object
Iterate over each record. Generate a new Record object each time. Slower than each.
123 124 125 126 127 128 129 130 |
# File 'lib/hts/bam.rb', line 123 def each_copy return to_enum(__method__) unless block_given? while LibHTS.sam_read1(@hts_file, header, bam1 = LibHTS.bam_init1) > 0 record = Record.new(bam1, header) yield record end end |
#flush ⇒ Object
Flush the current file.
102 103 104 |
# File 'lib/hts/bam.rb', line 102 def flush # LibHTS.bgzf_flush(@@hts_file.fp.bgzf) end |
#query(region) ⇒ Object
query [WIP]
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/hts/bam.rb', line 133 def query(region) # FIXME: when @idx is nil qiter = LibHTS.sam_itr_querys(@idx, header, region) begin bam1 = LibHTS.bam_init1 slen = LibHTS.sam_itr_next(@hts_file, qiter, bam1) while slen > 0 yield Record.new(bam1, header) bam1 = LibHTS.bam_init1 slen = LibHTS.sam_itr_next(@hts_file, qiter, bam1) end ensure LibHTS.hts_itr_destroy(qiter) end end |
#struct ⇒ Object
70 71 72 |
# File 'lib/hts/bam.rb', line 70 def struct @hts_file end |
#to_ptr ⇒ Object
74 75 76 |
# File 'lib/hts/bam.rb', line 74 def to_ptr @hts_file.to_ptr end |
#write(aln) ⇒ Object
96 97 98 99 |
# File 'lib/hts/bam.rb', line 96 def write(aln) aln_dup = aln.dup LibHTS.sam_write1(@hts_file, header, aln_dup) > 0 || raise end |