Class: HTS::Bcf
- Includes:
- Enumerable
- Defined in:
- lib/hts/bcf.rb,
lib/hts/bcf/info.rb,
lib/hts/bcf/format.rb,
lib/hts/bcf/header.rb,
lib/hts/bcf/record.rb
Defined Under Namespace
Classes: Format, Header, Info, 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
- #create_index(index_name = nil) ⇒ Object
-
#each ⇒ Object
Iterate over each record.
-
#each_copy ⇒ Object
Iterate over each record.
- #index_loaded? ⇒ Boolean
-
#initialize(file_name, mode = "r", index: nil, fai: nil, threads: nil, create_index: false) ⇒ Bcf
constructor
A new instance of Bcf.
- #load_index(index_name = nil) ⇒ Object
-
#nsamples ⇒ Object
Close the current file.
- #samples ⇒ Object
- #write(var) ⇒ Object
- #write_header ⇒ Object
Methods inherited from Hts
#close, #closed?, #format, #format_version, #rewind, #seek, #struct, #tell, #to_ptr
Constructor Details
#initialize(file_name, mode = "r", index: nil, fai: nil, threads: nil, create_index: false) ⇒ Bcf
Returns a new instance of Bcf.
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 |
# File 'lib/hts/bcf.rb', line 29 def initialize(file_name, mode = "r", index: nil, fai: nil, threads: nil, create_index: false) if block_given? = "HTS::Bcf.new() dose 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 @hts_file = LibHTS.hts_open(@file_name, mode) raise Errno::ENOENT, "Failed to open #{@file_name}" if @hts_file.null? 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 = Bcf::Header.new(@hts_file) create_index(index) if create_index @idx = load_index(index) @start_position = tell end |
Instance Attribute Details
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
15 16 17 |
# File 'lib/hts/bcf.rb', line 15 def file_name @file_name end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
15 16 17 |
# File 'lib/hts/bcf.rb', line 15 def header @header end |
#index_name ⇒ Object (readonly)
Returns the value of attribute index_name.
15 16 17 |
# File 'lib/hts/bcf.rb', line 15 def index_name @index_name end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
15 16 17 |
# File 'lib/hts/bcf.rb', line 15 def mode @mode end |
Class Method Details
.open(*args, **kw) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/hts/bcf.rb', line 17 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
#create_index(index_name = nil) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/hts/bcf.rb', line 61 def create_index(index_name = nil) warn "Create index for #{@file_name} to #{index_name}" if index LibHTS.bcf_index_build2(@hts_file, index_name, -1) else LibHTS.bcf_index_build(@hts_file, -1) end end |
#each ⇒ Object
Iterate over each record. Record object is reused. Faster than each_copy.
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/hts/bcf.rb', line 125 def each raise IOError, "closed stream" if closed? return to_enum(__method__) unless block_given? bcf1 = LibHTS.bcf_init record = Record.new(bcf1, header) yield record while LibHTS.bcf_read(@hts_file, header, bcf1) != -1 self end |
#each_copy ⇒ Object
Iterate over each record. Generate a new Record object each time. Slower than each.
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/hts/bcf.rb', line 110 def each_copy raise IOError, "closed stream" if closed? return to_enum(__method__) unless block_given? while LibHTS.bcf_read(@hts_file, header, bcf1 = LibHTS.bcf_init) != -1 record = Record.new(bcf1, header) yield record end self end |
#index_loaded? ⇒ Boolean
78 79 80 |
# File 'lib/hts/bcf.rb', line 78 def index_loaded? !@idx.null? end |
#load_index(index_name = nil) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/hts/bcf.rb', line 70 def load_index(index_name = nil) if index_name LibHTS.bcf_index_load2(@file_name, index_name) else LibHTS.bcf_index_load3(@file_name, nil, 2) end end |
#nsamples ⇒ Object
Close the current file.
99 100 101 |
# File 'lib/hts/bcf.rb', line 99 def nsamples header.nsamples end |
#samples ⇒ Object
103 104 105 |
# File 'lib/hts/bcf.rb', line 103 def samples header.samples end |
#write(var) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/hts/bcf.rb', line 90 def write(var) raise IOError, "closed stream" if closed? var_dup = var.dup = var.dup LibHTS.bcf_write(@hts_file, header, var_dup) > 0 || raise end |
#write_header ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/hts/bcf.rb', line 82 def write_header raise IOError, "closed stream" if closed? @header = header.dup LibHTS.hts_set_fai_filename(header, @file_name) LibHTS.bcf_hdr_write(@hts_file, header.struct) end |