Skip to content

Commit

Permalink
Related to issue macs3-project#122. Check ndarray size, then resize i…
Browse files Browse the repository at this point in the history
…t to half size

if the half size is less than 10K.
  • Loading branch information
taoliu committed May 19, 2016
1 parent a0603c7 commit 24a1eab
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 28 deletions.
26 changes: 19 additions & 7 deletions MACS2/IO/CallPeakUnit.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Time-stamp: <2016-02-15 15:23:38 Tao Liu>
# Time-stamp: <2016-05-19 10:22:22 Tao Liu>

"""Module for Calculate Scores.
Expand Down Expand Up @@ -85,6 +85,15 @@ def do_nothing(*args, **kwargs):

LOG10_E = 0.43429448190325176

cdef void clean_up_ndarray ( np.ndarray x ):
# clean numpy ndarray in two steps
cdef:
long i
i = x.shape[0] / 2
x.resize( 100000 if i > 100000 else i, refcheck=False)
x.resize( 0, refcheck=False)
return

cdef inline float chi2_k1_cdf ( float x ):
return erf( sqrt(x/2) )

Expand Down Expand Up @@ -472,12 +481,15 @@ cdef class CallerFromAlignments:

# reset or clean existing self.chr_pos_treat_ctrl
if self.chr_pos_treat_ctrl: # not a beautiful way to clean
self.chr_pos_treat_ctrl[0].resize(10000,refcheck=False)
self.chr_pos_treat_ctrl[1].resize(10000,refcheck=False)
self.chr_pos_treat_ctrl[2].resize(10000,refcheck=False)
self.chr_pos_treat_ctrl[0].resize(0,refcheck=False)
self.chr_pos_treat_ctrl[1].resize(0,refcheck=False)
self.chr_pos_treat_ctrl[2].resize(0,refcheck=False)
clean_up_ndarray( self.chr_pos_treat_ctrl[0] )
clean_up_ndarray( self.chr_pos_treat_ctrl[1] )
clean_up_ndarray( self.chr_pos_treat_ctrl[2] )
#self.chr_pos_treat_ctrl[0].resize(10000,refcheck=False)
#self.chr_pos_treat_ctrl[1].resize(10000,refcheck=False)
#self.chr_pos_treat_ctrl[2].resize(10000,refcheck=False)
#self.chr_pos_treat_ctrl[0].resize(0,refcheck=False)
#self.chr_pos_treat_ctrl[1].resize(0,refcheck=False)
#self.chr_pos_treat_ctrl[2].resize(0,refcheck=False)

if self.PE_mode:
treat_pv = self.treat.pileup_a_chromosome ( chrom, [self.treat_scaling_factor,], baseline_value = 0.0 )
Expand Down
61 changes: 40 additions & 21 deletions MACS2/Pileup.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Time-stamp: <2016-02-15 16:18:10 Tao Liu>
# Time-stamp: <2016-05-19 10:26:24 Tao Liu>

"""Module Description: For pileup functions.
Expand Down Expand Up @@ -50,6 +50,15 @@ cdef inline int int_max(int a, int b): return a if a >= b else b
cdef inline long long_max(long a, long b): return a if a >= b else b
cdef inline float float_max(float a, float b): return a if a >= b else b

cdef void clean_up_ndarray ( np.ndarray x ):
# clean numpy ndarray in two steps
cdef:
long i
i = x.shape[0] / 2
x.resize( 100000 if i > 100000 else i, refcheck=False)
x.resize( 0, refcheck=False)
return

# This function uses pure C code for pileup
cpdef pileup_and_write( trackI,
output_filename,
Expand Down Expand Up @@ -217,10 +226,12 @@ cdef pileup_bdg_se(object trackI, int d,
ret.add_a_chromosome( chrom, quick_pileup ( ends.startposs, ends.endposs, scale_factor, baseline_value ) )

# free mem
ends.startposs.resize(100000, refcheck=False)
ends.startposs.resize(0, refcheck=False)
ends.endposs.resize(100000, refcheck=False)
ends.endposs.resize(0, refcheck=False)
clean_up_ndarray( ends.startposs )
clean_up_ndarray( ends.endposs )
#ends.startposs.resize(100000, refcheck=False)
#ends.startposs.resize(0, refcheck=False)
#ends.endposs.resize(100000, refcheck=False)
#ends.endposs.resize(0, refcheck=False)

return ret

Expand Down Expand Up @@ -292,10 +303,12 @@ cdef pileup_w_multiple_d_bdg(object trackI, list d_s, list scale_factor_s = [],
tmp_pileup = quick_pileup ( ends.startposs, ends.endposs, scale_factor, baseline_value )

# free mem
ends.startposs.resize(100000, refcheck=False)
ends.startposs.resize(0, refcheck=False)
ends.endposs.resize(100000, refcheck=False)
ends.endposs.resize(0, refcheck=False)
clean_up_ndarray( ends.startposs )
clean_up_ndarray( ends.endposs )
#ends.startposs.resize(100000, refcheck=False)
#ends.startposs.resize(0, refcheck=False)
#ends.endposs.resize(100000, refcheck=False)
#ends.endposs.resize(0, refcheck=False)

if prev_pileup:
prev_pileup = max_over_two_pv_array ( prev_pileup, tmp_pileup )
Expand Down Expand Up @@ -377,10 +390,12 @@ cdef pileup_bdg_pe_w_ext (object trackI, int d, float scale_factor = 1.0,
ret.add_a_chromosome( chrom, pileup )

# free mem
start_poss.resize(100000, refcheck=False)
start_poss.resize(0, refcheck=False)
end_poss.resize(100000, refcheck=False)
end_poss.resize(0, refcheck=False)
clean_up_ndarray( start_poss )
clean_up_ndarray( end_poss)
#start_poss.resize(100000, refcheck=False)
#start_poss.resize(0, refcheck=False)
#end_poss.resize(100000, refcheck=False)
#end_poss.resize(0, refcheck=False)

return ret

Expand Down Expand Up @@ -436,10 +451,12 @@ cdef pileup_w_multiple_d_bdg_pe ( object trackI, list d_s = [],
baseline_value)

# free mem
start_poss.resize(100000, refcheck=False)
start_poss.resize(0, refcheck=False)
end_poss.resize(100000, refcheck=False)
end_poss.resize(0, refcheck=False)
clean_up_ndarray( start_poss )
clean_up_ndarray( end_poss )
#start_poss.resize(100000, refcheck=False)
#start_poss.resize(0, refcheck=False)
#end_poss.resize(100000, refcheck=False)
#end_poss.resize(0, refcheck=False)

prev_pileup = max_over_two_pv_array ( prev_pileup, tmp_pileup )

Expand Down Expand Up @@ -643,10 +660,12 @@ cpdef se_all_in_one_pileup ( np.ndarray[np.int32_t, ndim=1] plus_tags, np.ndarra
end_poss_ptr += 1

# clean mem
start_poss.resize(100000, refcheck=False)
start_poss.resize(0, refcheck=False)
end_poss.resize(100000, refcheck=False)
end_poss.resize(0, refcheck=False)
clean_up_ndarray( start_poss )
clean_up_ndarray( end_poss )
#start_poss.resize(100000, refcheck=False)
#start_poss.resize(0, refcheck=False)
#end_poss.resize(100000, refcheck=False)
#end_poss.resize(0, refcheck=False)

# resize
ret_p.resize( I, refcheck=False )
Expand Down

0 comments on commit 24a1eab

Please sign in to comment.