Class: HTS::Bam

Inherits:
Hts
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hts/bam.rb,
lib/hts/bam/auxi.rb,
lib/hts/bam/flag.rb,
lib/hts/bam/cigar.rb,
lib/hts/bam/header.rb,
lib/hts/bam/record.rb,
lib/hts/bam/header_record.rb

Overview

A class for working with SAM, BAM, CRAM files.

Defined Under Namespace

Classes: Aux, Cigar, Flag, Header, HeaderRecord, Record

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, build_index: false) ⇒ Bam

Returns a new instance of Bam.

Raises:

  • (Errno::ENOENT)


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,
               build_index: false)
  if block_given?
    message = "HTS::Bam.new() dose not take block; Please use HTS::Bam.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
  @nthreads   = threads
  @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)
  build_index(index) if build_index
  @idx = load_index(index)
  @start_position = tell
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



16
17
18
# File 'lib/hts/bam.rb', line 16

def file_name
  @file_name
end

#headerObject (readonly)

Returns the value of attribute header.



16
17
18
# File 'lib/hts/bam.rb', line 16

def header
  @header
end

#index_nameObject (readonly)

Returns the value of attribute index_name.



16
17
18
# File 'lib/hts/bam.rb', line 16

def index_name
  @index_name
end

#modeObject (readonly)

Returns the value of attribute mode.



16
17
18
# File 'lib/hts/bam.rb', line 16

def mode
  @mode
end

#nthreadsObject (readonly)

Returns the value of attribute nthreads.



16
17
18
# File 'lib/hts/bam.rb', line 16

def nthreads
  @nthreads
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



153
154
155
156
157
158
159
160
# File 'lib/hts/bam.rb', line 153

def aux(tag)
  warn "experimental"
  check_closed
  position = tell
  ary = map { |r| r.aux(tag) }
  seek(position)
  ary
end

#build_index(index_name = nil, min_shift: 0) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hts/bam.rb', line 62

def build_index(index_name = nil, min_shift: 0)
  check_closed

  if index_name
    warn "Create index for #{@file_name} to #{index_name}"
  else
    warn "Create index for #{@file_name}"
  end
  r = LibHTS.sam_index_build3(@file_name, index_name, min_shift, @nthreads)
  raise "Failed to build index for #{@file_name}" if r < 0

  self
end

#chromArray

Get chrom array

Returns:

  • (Array)

    the chrom array



140
# File 'lib/hts/bam.rb', line 140

define_getter :chrom

#cigarArray

Get cigar array

Returns:

  • (Array)

    the cigar array



143
# File 'lib/hts/bam.rb', line 143

define_getter :cigar

#closeObject



92
93
94
95
96
# File 'lib/hts/bam.rb', line 92

def close
  LibHTS.hts_idx_destroy(@idx) if @idx&.null?
  @idx = nil
  super
end

#each(copy: false, &block) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/hts/bam.rb', line 118

def each(copy: false, &block)
  if copy
    each_record_copy(&block)
  else
    each_record_reuse(&block)
  end
end

#each_aux(tag) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/hts/bam.rb', line 180

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_chromObject

Get chrom iterator



167
# File 'lib/hts/bam.rb', line 167

define_iterator :chrom

#each_cigarObject

Get cigar iterator



170
# File 'lib/hts/bam.rb', line 170

define_iterator :cigar

#each_flagObject

Get flag iterator



166
# File 'lib/hts/bam.rb', line 166

define_iterator :flag

#each_insert_sizeObject Also known as: each_isize

Get insert_size iterator



173
# File 'lib/hts/bam.rb', line 173

define_iterator :insert_size

#each_mapqObject

Get mapq iterator



169
# File 'lib/hts/bam.rb', line 169

define_iterator :mapq

#each_mate_chromObject

Get mate_chrom iterator



171
# File 'lib/hts/bam.rb', line 171

define_iterator :mate_chrom

#each_mate_posObject Also known as: each_mpos

Get mate_pos iterator



172
# File 'lib/hts/bam.rb', line 172

define_iterator :mate_pos

#each_posObject

Get pos iterator



168
# File 'lib/hts/bam.rb', line 168

define_iterator :pos

#each_qnameObject

Get qname iterator



165
# File 'lib/hts/bam.rb', line 165

define_iterator :qname

#each_qualObject

Get qual iterator



175
# File 'lib/hts/bam.rb', line 175

define_iterator :qual

#each_seqObject

Get seq iterator



174
# File 'lib/hts/bam.rb', line 174

define_iterator :seq

#fai=(fai) ⇒ Object



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

def fai=(fai)
  check_closed
  LibHTS.hts_set_fai_filename(@hts_file, fai) > 0 || raise
end

#flagArray

Get flag array

Returns:

  • (Array)

    the flag array



139
# File 'lib/hts/bam.rb', line 139

define_getter :flag

#index_loaded?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
# File 'lib/hts/bam.rb', line 86

def index_loaded?
  check_closed

  !@idx.null?
end

#insert_sizeArray Also known as: isize

Get insert_size array

Returns:

  • (Array)

    the insert_size array



146
# File 'lib/hts/bam.rb', line 146

define_getter :insert_size

#load_index(index_name = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/hts/bam.rb', line 76

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

#mapqArray

Get mapq array

Returns:

  • (Array)

    the mapq array



142
# File 'lib/hts/bam.rb', line 142

define_getter :mapq

#mate_chromArray

Get mate_chrom array

Returns:

  • (Array)

    the mate_chrom array



144
# File 'lib/hts/bam.rb', line 144

define_getter :mate_chrom

#mate_posArray Also known as: mpos

Get mate_pos array

Returns:

  • (Array)

    the mate_pos array



145
# File 'lib/hts/bam.rb', line 145

define_getter :mate_pos

#posArray

Get pos array

Returns:

  • (Array)

    the pos array



141
# File 'lib/hts/bam.rb', line 141

define_getter :pos

#qnameArray

Get qname array

Returns:

  • (Array)

    the qname array



138
# File 'lib/hts/bam.rb', line 138

define_getter :qname

#qualArray

Get qual array

Returns:

  • (Array)

    the qual array



148
# File 'lib/hts/bam.rb', line 148

define_getter :qual

#query(region, copy: false, &block) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/hts/bam.rb', line 126

def query(region, copy: false, &block)
  if copy
    query_copy(region, &block)
  else
    query_reuse(region, &block)
  end
end

#seqArray

Get seq array

Returns:

  • (Array)

    the seq array



147
# File 'lib/hts/bam.rb', line 147

define_getter :seq

#write(aln) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/hts/bam.rb', line 110

def write(aln)
  check_closed

  aln_dup = aln.dup
  r = LibHTS.sam_write1(@hts_file, header, aln_dup)
  raise "Failed to write record" if r < 0
end

#write_header(header) ⇒ Object



103
104
105
106
107
108
# File 'lib/hts/bam.rb', line 103

def write_header(header)
  check_closed

  @header = header.dup
  LibHTS.sam_hdr_write(@hts_file, header)
end