Class: HTS::Bcf
- Includes:
- Enumerable
- Defined in:
- lib/hts/bcf.rb,
lib/hts/bcf/info.rb,
lib/hts/bcf/errors.rb,
lib/hts/bcf/format.rb,
lib/hts/bcf/header.rb,
lib/hts/bcf/record.rb,
lib/hts/bcf/header_record.rb
Overview
A class for working with VCF, BCF files.
Defined Under Namespace
Classes: Error, FieldError, Format, FormatDefinitionError, FormatError, FormatReadError, FormatTypeError, FormatUpdateError, Header, HeaderError, HeaderRecord, IndexError, Info, InfoError, InfoReadError, InfoTypeError, InfoUpdateError, MissingIndexError, OpenError, QueryError, Record, SubsetError, UnknownSampleError, UnsupportedFormatOperationError, UnsupportedInfoOperationError
Instance Attribute Summary collapse
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
-
#header ⇒ Object
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.
-
#nthreads ⇒ Object
readonly
Returns the value of attribute nthreads.
Class Method Summary collapse
- .build_index(file_name, index_name = nil, min_shift = 14, threads = 0, verbose = true) ⇒ Object
- .open(*args, **kw) ⇒ Object
Instance Method Summary collapse
- #<<(var) ⇒ Object
-
#alt ⇒ Array
Get alt array.
- #build_index(index_name = nil, min_shift: 14, verbose: true) ⇒ Object
-
#chrom ⇒ Array
Get chrom array.
- #close ⇒ Object
- #each(copy: false, &block) ⇒ Object
-
#each_alt ⇒ Object
Get alt iterator.
-
#each_chrom ⇒ Object
Get chrom iterator.
-
#each_endpos ⇒ Object
Get endpos iterator.
-
#each_filter ⇒ Object
Get filter iterator.
- #each_format(key) ⇒ Object
-
#each_id ⇒ Object
Get id iterator.
- #each_info(key) ⇒ Object
-
#each_pos ⇒ Object
Get pos iterator.
-
#each_qual ⇒ Object
Get qual iterator.
-
#each_ref ⇒ Object
Get ref iterator.
-
#endpos ⇒ Array
Get endpos array.
-
#filter ⇒ Array
Get filter array.
- #format(key = nil) ⇒ Object
-
#id ⇒ Array
Get id array.
- #index_loaded? ⇒ Boolean
- #info(key = nil) ⇒ Object
-
#initialize(file_name, mode = "r", index: nil, threads: nil, build_index: false, subset: nil) ⇒ Bcf
constructor
A new instance of Bcf.
- #load_index(index_name = nil) ⇒ Object
- #nsamples ⇒ Object
-
#pos ⇒ Array
Get pos array.
-
#qual ⇒ Array
Get qual array.
- #query(region, beg = nil, end_ = nil, copy: false, &block) ⇒ Object
-
#ref ⇒ Array
Get ref array.
- #samples ⇒ Object
- #write(record) ⇒ Object
- #write_header(header) ⇒ Object
Methods inherited from Hts
#closed?, #fai=, #file_format, #file_format_version, #rewind, #seek, #set_threads, #struct, #tell, #to_ptr
Constructor Details
#initialize(file_name, mode = "r", index: nil, threads: nil, build_index: false, subset: nil) ⇒ Bcf
Returns a new instance of Bcf.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/hts/bcf.rb', line 50 def initialize(file_name, mode = "r", index: nil, threads: nil, build_index: false, subset: nil) if block_given? = "HTS::Bcf.new() does not take block; Please use HTS::Bcf.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 @nthreads = threads @hts_file = LibHTS.hts_open(@file_name, mode) raise OpenError, "Failed to open #{@file_name}" if @hts_file.null? set_threads(threads) if threads raise SubsetError, "Sample subsetting is only available when reading BCF/VCF files" if subset && @mode[0] == "w" return if @mode[0] == "w" @read_header = Bcf::Header.new(@hts_file) @header = subset ? @read_header.subset(subset) : @read_header build_index(index) if build_index @idx = load_index(index) @start_position = tell end |
Instance Attribute Details
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
17 18 19 |
# File 'lib/hts/bcf.rb', line 17 def file_name @file_name end |
#header ⇒ Object
Returns the value of attribute header.
17 18 19 |
# File 'lib/hts/bcf.rb', line 17 def header @header end |
#index_name ⇒ Object (readonly)
Returns the value of attribute index_name.
17 18 19 |
# File 'lib/hts/bcf.rb', line 17 def index_name @index_name end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
17 18 19 |
# File 'lib/hts/bcf.rb', line 17 def mode @mode end |
#nthreads ⇒ Object (readonly)
Returns the value of attribute nthreads.
17 18 19 |
# File 'lib/hts/bcf.rb', line 17 def nthreads @nthreads end |
Class Method Details
.build_index(file_name, index_name = nil, min_shift = 14, threads = 0, verbose = true) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/hts/bcf.rb', line 31 def self.build_index(file_name, index_name = nil, min_shift = 14, threads = 0, verbose = true) if verbose if index_name warn "Create index for #{file_name} to #{index_name}" else warn "Create index for #{file_name}" end end case LibHTS.bcf_index_build3(file_name, index_name, min_shift, threads) when 0 # successful when -1 then raise IndexError, "Indexing failed for #{file_name}" when -2 then raise IndexError, "Opening #{file_name} failed while building the index" when -3 then raise IndexError, "#{file_name} is not in an indexable format" when -4 then raise IndexError, "Failed to create or save the index for #{file_name}" else raise IndexError, "Unknown index build error for #{file_name}" end end |
.open(*args, **kw) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/hts/bcf.rb', line 19 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
#<<(var) ⇒ Object
128 129 130 |
# File 'lib/hts/bcf.rb', line 128 def <<(var) write(var) end |
#alt ⇒ Array
Get alt array
153 |
# File 'lib/hts/bcf.rb', line 153 define_getter :alt |
#build_index(index_name = nil, min_shift: 14, verbose: true) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/hts/bcf.rb', line 80 def build_index(index_name = nil, min_shift: 14, verbose: true) check_closed self.class.build_index(@file_name, index_name, min_shift, @nthreads || 0, verbose) self # for method chaining end |
#chrom ⇒ Array
Get chrom array
148 |
# File 'lib/hts/bcf.rb', line 148 define_getter :chrom |
#close ⇒ Object
103 104 105 106 107 |
# File 'lib/hts/bcf.rb', line 103 def close LibHTS.hts_idx_destroy(@idx) if @idx && !@idx.null? @idx = nil super end |
#each(copy: false, &block) ⇒ Object
215 216 217 218 219 220 221 |
# File 'lib/hts/bcf.rb', line 215 def each(copy: false, &block) if copy each_record_copy(&block) else each_record_reuse(&block) end end |
#each_alt ⇒ Object
Get alt iterator
193 |
# File 'lib/hts/bcf.rb', line 193 define_iterator :alt |
#each_chrom ⇒ Object
Get chrom iterator
188 |
# File 'lib/hts/bcf.rb', line 188 define_iterator :chrom |
#each_endpos ⇒ Object
Get endpos iterator
190 |
# File 'lib/hts/bcf.rb', line 190 define_iterator :endpos |
#each_filter ⇒ Object
Get filter iterator
195 |
# File 'lib/hts/bcf.rb', line 195 define_iterator :filter |
#each_format(key) ⇒ Object
206 207 208 209 210 211 212 213 |
# File 'lib/hts/bcf.rb', line 206 def each_format(key) check_closed return to_enum(__method__, key) unless block_given? each do |r| yield r.format(key) end end |
#each_id ⇒ Object
Get id iterator
191 |
# File 'lib/hts/bcf.rb', line 191 define_iterator :id |
#each_info(key) ⇒ Object
197 198 199 200 201 202 203 204 |
# File 'lib/hts/bcf.rb', line 197 def each_info(key) check_closed return to_enum(__method__, key) unless block_given? each do |r| yield r.info(key) end end |
#each_pos ⇒ Object
Get pos iterator
189 |
# File 'lib/hts/bcf.rb', line 189 define_iterator :pos |
#each_qual ⇒ Object
Get qual iterator
194 |
# File 'lib/hts/bcf.rb', line 194 define_iterator :qual |
#each_ref ⇒ Object
Get ref iterator
192 |
# File 'lib/hts/bcf.rb', line 192 define_iterator :ref |
#endpos ⇒ Array
Get endpos array
150 |
# File 'lib/hts/bcf.rb', line 150 define_getter :endpos |
#filter ⇒ Array
Get filter array
155 |
# File 'lib/hts/bcf.rb', line 155 define_getter :filter |
#format(key = nil) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/hts/bcf.rb', line 171 def format(key = nil) check_closed position = tell raise NotImplementedError unless key ary = map { |r| r.format(key) } # ary = each_copy.map { |r| r.format } # ary = map { |r| r.format.clone } seek(position) ary end |
#id ⇒ Array
Get id array
151 |
# File 'lib/hts/bcf.rb', line 151 define_getter :id |
#index_loaded? ⇒ Boolean
97 98 99 100 101 |
# File 'lib/hts/bcf.rb', line 97 def index_loaded? check_closed !@idx.null? end |
#info(key = nil) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/hts/bcf.rb', line 157 def info(key = nil) check_closed position = tell raise NotImplementedError unless key ary = map { |r| r.info(key) } # ary = each_copy.map { |r| r.info } # ary = map { |r| r.info.clone } seek(position) ary end |
#load_index(index_name = nil) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/hts/bcf.rb', line 87 def load_index(index_name = nil) check_closed if index_name LibHTS.bcf_index_load2(@file_name, index_name) else LibHTS.bcf_index_load3(@file_name, nil, 2) end end |
#nsamples ⇒ Object
132 133 134 135 136 |
# File 'lib/hts/bcf.rb', line 132 def nsamples check_closed header.nsamples end |
#pos ⇒ Array
Get pos array
149 |
# File 'lib/hts/bcf.rb', line 149 define_getter :pos |
#qual ⇒ Array
Get qual array
154 |
# File 'lib/hts/bcf.rb', line 154 define_getter :qual |
#query(region, beg = nil, end_ = nil, copy: false, &block) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/hts/bcf.rb', line 223 def query(region, beg = nil, end_ = nil, copy: false, &block) check_closed raise QueryError, "Query is only available for BCF files" unless file_format == "bcf" raise MissingIndexError, "Index file is required to call the query method for #{@file_name}" unless index_loaded? case region when Array raise ArgumentError, "beg and end must not be specified when region is an Array" unless beg.nil? && end_.nil? query_regions(region, copy:, &block) else if beg && end_ tid = header.name2id(region) queryi(tid, beg, end_, copy:, &block) elsif beg.nil? && end_.nil? querys(region, copy:, &block) else raise ArgumentError, "beg and end must be specified together" end end end |
#ref ⇒ Array
Get ref array
152 |
# File 'lib/hts/bcf.rb', line 152 define_getter :ref |
#samples ⇒ Object
138 139 140 141 142 |
# File 'lib/hts/bcf.rb', line 138 def samples check_closed header.samples end |
#write(record) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/hts/bcf.rb', line 120 def write(record) check_closed # record = record.dup r = LibHTS.bcf_write(@hts_file, header, record) raise "Failed to write record" if r < 0 end |
#write_header(header) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/hts/bcf.rb', line 109 def write_header(header) check_closed @header = header.dup LibHTS.bcf_hdr_write(@hts_file, header) end |