@@ -6,6 +6,22 @@ class Bam < Hts
66 class Mpileup
77 include Enumerable
88
9+ # Usage:
10+ # HTS::Bam::Mpileup.open([bam1, bam2], region: "chr1:1-100") do |mpl|
11+ # mpl.each { |cols| ... }
12+ # end
13+ def self . open ( *args , **kw )
14+ m = new ( *args , **kw )
15+ return m unless block_given?
16+
17+ begin
18+ yield m
19+ ensure
20+ m . close
21+ end
22+ m
23+ end
24+
925 # Normalize inputs to HTS::Bam instances
1026 # Accepts array of HTS::Bam or filenames (String)
1127 def initialize ( inputs , region : nil , beg : nil , end_ : nil , maxcnt : nil , overlaps : false )
@@ -107,39 +123,35 @@ def each
107123 plp1_size = HTS ::LibHTS ::BamPileup1 . size
108124 headers = @bams . map ( &:header )
109125
110- begin
111- while HTS ::LibHTS . bam_mplp64_auto ( @iter , tid_ptr , pos_ptr , n_ptr , plp_ptr ) > 0
112- tid = tid_ptr . read_int
113- pos = pos_ptr . read_long_long
114-
115- counts = n_ptr . read_array_of_int ( n )
116- plp_arr = plp_ptr . read_array_of_pointer ( n )
117-
118- cols = Array . new ( n )
119- i = 0
120- while i < n
121- c = counts [ i ]
122- if c <= 0 || plp_arr [ i ] . null?
123- cols [ i ] = HTS ::Bam ::Pileup ::PileupColumn . new ( tid : tid , pos : pos , alignments : [ ] )
124- else
125- base_ptr = plp_arr [ i ]
126- aligns = Array . new ( c )
127- j = 0
128- while j < c
129- e_ptr = base_ptr + ( j * plp1_size )
130- entry = HTS ::LibHTS ::BamPileup1 . new ( e_ptr )
131- aligns [ j ] = HTS ::Bam ::Pileup ::PileupRecord . new ( entry , headers [ i ] )
132- j += 1
133- end
134- cols [ i ] = HTS ::Bam ::Pileup ::PileupColumn . new ( tid : tid , pos : pos , alignments : aligns )
126+ while HTS ::LibHTS . bam_mplp64_auto ( @iter , tid_ptr , pos_ptr , n_ptr , plp_ptr ) > 0
127+ tid = tid_ptr . read_int
128+ pos = pos_ptr . read_long_long
129+
130+ counts = n_ptr . read_array_of_int ( n )
131+ plp_arr = plp_ptr . read_array_of_pointer ( n )
132+
133+ cols = Array . new ( n )
134+ i = 0
135+ while i < n
136+ c = counts [ i ]
137+ if c <= 0 || plp_arr [ i ] . null?
138+ cols [ i ] = HTS ::Bam ::Pileup ::PileupColumn . new ( tid : tid , pos : pos , alignments : [ ] )
139+ else
140+ base_ptr = plp_arr [ i ]
141+ aligns = Array . new ( c )
142+ j = 0
143+ while j < c
144+ e_ptr = base_ptr + ( j * plp1_size )
145+ entry = HTS ::LibHTS ::BamPileup1 . new ( e_ptr )
146+ aligns [ j ] = HTS ::Bam ::Pileup ::PileupRecord . new ( entry , headers [ i ] )
147+ j += 1
135148 end
136- i += 1
149+ cols [ i ] = HTS :: Bam :: Pileup :: PileupColumn . new ( tid : tid , pos : pos , alignments : aligns )
137150 end
138-
139- yield cols
151+ i += 1
140152 end
141- ensure
142- close
153+
154+ yield cols
143155 end
144156
145157 self
0 commit comments