Class: HTS::Bcf

Inherits:
Hts
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.

Raises:

  • (Errno::ENOENT)


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?
    message = "HTS::Bcf.new() dose not take block; Please use HTS::Bcf.open() instead"
    raise message
  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_nameObject (readonly)

Returns the value of attribute file_name.



15
16
17
# File 'lib/hts/bcf.rb', line 15

def file_name
  @file_name
end

#headerObject (readonly)

Returns the value of attribute header.



15
16
17
# File 'lib/hts/bcf.rb', line 15

def header
  @header
end

#index_nameObject (readonly)

Returns the value of attribute index_name.



15
16
17
# File 'lib/hts/bcf.rb', line 15

def index_name
  @index_name
end

#modeObject (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

#eachObject

Iterate over each record. Record object is reused. Faster than each_copy.

Raises:

  • (IOError)


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_copyObject

Iterate over each record. Generate a new Record object each time. Slower than each.

Raises:

  • (IOError)


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

Returns:

  • (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

#nsamplesObject

Close the current file.



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

def nsamples
  header.nsamples
end

#samplesObject



103
104
105
# File 'lib/hts/bcf.rb', line 103

def samples
  header.samples
end

#write(var) ⇒ Object

Raises:

  • (IOError)


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_headerObject

Raises:

  • (IOError)


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