Class: HTS::Bam::Mpileup

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hts/bam/mpileup.rb

Defined Under Namespace

Classes: ColumnsView, DepthView

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inputs, region: nil, beg: nil, end_: nil, maxcnt: nil, overlaps: false) ⇒ Mpileup

Returns a new instance of Mpileup.

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hts/bam/mpileup.rb', line 70

def initialize(inputs, region: nil, beg: nil, end_: nil, maxcnt: nil, overlaps: false)
  raise ArgumentError, "inputs must be non-empty" if inputs.nil? || inputs.empty?

  @owned_bams = []
  @bams = inputs.map do |input|
    case input
    when HTS::Bam then input
    when String
      HTS::Bam.open(input).tap { |bam| @owned_bams << bam }
    else raise ArgumentError, "Unsupported input type: #{input.class}"
    end
  end
  if region
    missing = @bams.find { |bam| !bam.__send__(:ensure_index_loaded) }
    raise "Index file is required to use region mpileup: #{missing.file_name}" if missing
  end
  files = @bams.map { |bam| bam.__send__(:native_handle) }
  headers = @bams.map { |bam| bam.header.__send__(:native_handle) }
  @native = Native::MpileupHandle.open(files, headers, region, beg, end_, maxcnt, overlaps)
  @closed = false
end

Class Method Details

.open(*args, **keywords) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hts/bam/mpileup.rb', line 58

def self.open(*args, **keywords)
  mpileup = new(*args, **keywords)
  return mpileup unless block_given?

  begin
    yield mpileup
  ensure
    mpileup.close
  end
  mpileup
end

Instance Method Details

#closeObject



170
171
172
173
174
175
176
177
178
# File 'lib/hts/bam/mpileup.rb', line 170

def close
  return if @closed

  @native.close
  @owned_bams.each(&:close)
  @owned_bams.clear
  @closed = true
  nil
end

#eachObject



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hts/bam/mpileup.rb', line 92

def each
  return to_enum(__method__) unless block_given?

  headers = @bams.map(&:header)
  each_column_raw do |tid, pos, _depths, rows_by_input, _|
    columns = rows_by_input.each_with_index.map do |rows, index|
      alignments = rows.map { |row| Pileup::PileupRecord.new(row, headers[index]) }
      Pileup::PileupColumn.new(tid:, pos:, alignments:)
    end
    yield columns
  end
  self
end

#each_base_counts(min_base_quality: 0, min_mapping_quality: 0) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/hts/bam/mpileup.rb', line 133

def each_base_counts(min_base_quality: 0, min_mapping_quality: 0)
  return to_enum(__method__, min_base_quality:, min_mapping_quality:) unless block_given?

  min_base_quality = Integer(min_base_quality)
  min_mapping_quality = Integer(min_mapping_quality)
  if min_base_quality.negative? || min_mapping_quality.negative?
    raise ArgumentError,
          "quality thresholds must be non-negative"
  end

  counts = Array.new(@bams.length) { Array.new(Pileup::BASE_COUNT_FIELDS.length, 0) }
  each_column_raw do |tid, pos, _, rows_by_input, _|
    rows_by_input.each_with_index do |rows, input_index|
      aggregate_counts(rows, counts[input_index], min_base_quality, min_mapping_quality)
    end
    yield tid, pos, counts
  end
  self
end

#each_column_rawObject

The fourth value is an Array of opaque native-entry rows. It remains borrowed until the next iteration.



155
156
157
158
159
160
161
162
163
# File 'lib/hts/bam/mpileup.rb', line 155

def each_column_raw
  return to_enum(__method__) unless block_given?

  while (column = @native.next)
    tid, pos, depths, rows = column
    yield tid, pos, depths, rows, @bams.length
  end
  self
end

#each_depthObject



106
107
108
109
110
111
112
# File 'lib/hts/bam/mpileup.rb', line 106

def each_depth
  return to_enum(__method__) unless block_given?

  view = DepthView.new
  each_column_raw { |tid, pos, depths, _, _| yield tid, pos, view.reset(depths) }
  self
end

#each_entry_rawObject



122
123
124
125
126
127
128
129
130
131
# File 'lib/hts/bam/mpileup.rb', line 122

def each_entry_raw
  return to_enum(__method__) unless block_given?

  each_column_raw do |tid, pos, _, rows_by_input, _|
    rows_by_input.each_with_index do |rows, input_index|
      rows.each { |row| yield input_index, tid, pos, row[1], row[7], row[8], row[9] }
    end
  end
  self
end

#each_viewObject



114
115
116
117
118
119
120
# File 'lib/hts/bam/mpileup.rb', line 114

def each_view
  return to_enum(__method__) unless block_given?

  view = ColumnsView.new
  each_column_raw { |tid, pos, _, rows, _| yield view.reset(tid, pos, rows) }
  self
end

#resetObject



165
166
167
168
# File 'lib/hts/bam/mpileup.rb', line 165

def reset
  @native.reset
  self
end