Skip to content

Commit

Permalink
Merge pull request #337 from taoliu/fix_BedGraph_keys
Browse files Browse the repository at this point in the history
Fix BedGraph, Python3 compatibility
  • Loading branch information
taoliu authored Oct 30, 2019
2 parents d28843f + 907ac53 commit 0e871a8
Show file tree
Hide file tree
Showing 17 changed files with 11,237 additions and 110,954 deletions.
6,221 changes: 3,051 additions & 3,170 deletions MACS2/IO/BedGraph.c

Large diffs are not rendered by default.

28 changes: 17 additions & 11 deletions MACS2/IO/BedGraph.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# cython: language_level=3
# Time-stamp: <2019-10-02 11:05:35 taoliu>
# Time-stamp: <2019-10-30 12:12:53 taoliu>

"""Module for BedGraph data class.
Expand Down Expand Up @@ -221,12 +221,11 @@ cdef class bedGraphTrackI:
else:
return None

def get_chr_names (self):
cpdef set get_chr_names (self):
"""Return all the chromosome names stored.
"""
l = set(self.__data.keys())
return l
return set(sorted(self.__data.keys()))

def write_bedGraph (self, fhd, str name, str description, bool trackline=True):
"""Write all data to fhd in Wiggle Format.
Expand All @@ -240,6 +239,7 @@ cdef class bedGraphTrackI:
int pre, pos, i
double value
bytes chrom
set chrs

if trackline:
trackcontents = (name.replace("\"", "\\\""), description.replace("\"", "\\\""))
Expand Down Expand Up @@ -278,8 +278,9 @@ cdef class bedGraphTrackI:
int new_pre_pos, pos, i
double new_pre_value, value
bytes chrom
set chrs

chrs = set(self.__data.keys())
chrs = self.get_chr_names()
for chrom in chrs:
(p,v) = self.__data[chrom]
pnext = iter(p).__next__
Expand Down Expand Up @@ -319,8 +320,9 @@ cdef class bedGraphTrackI:
int new_pre_pos, pos, i
double new_pre_value, value
bytes chrom
set chrs

chrs = set(self.__data.keys())
chrs = self.get_chr_names()
for chrom in chrs:
(p,v) = self.__data[chrom]
pnext = iter(p).__next__
Expand Down Expand Up @@ -414,6 +416,7 @@ cdef class bedGraphTrackI:
int peak_length, x, pre_p, p, i, summit, tstart, tend
double v, summit_value, tvalue
bytes chrom
set chrs

#if call_summits: close_peak = self.__close_peak2
#else: close_peak = self.__close_peak
Expand Down Expand Up @@ -512,6 +515,7 @@ cdef class bedGraphTrackI:
"""
cdef bytes chrom
cdef int i, j
cdef set chrs
#cdef int tmp_n

assert lvl1_cutoff > lvl2_cutoff, "level 1 cutoff should be larger than level 2."
Expand Down Expand Up @@ -763,7 +767,7 @@ cdef class bedGraphTrackI:
double t0, t1, t

# calculate frequencies of each p-score
for chrom in self.__data.keys():
for chrom in self.get_chr_names():
pre_p = 0

[pos_array, pscore_array] = self.__data[ chrom ]
Expand Down Expand Up @@ -807,7 +811,7 @@ cdef class bedGraphTrackI:
nhcal += 1

# convert pscore to qscore
for chrom in self.__data.keys():
for chrom in self.get_chr_names():
[pos_array, pscore_array] = self.__data[ chrom ]

for i in range( len( pos_array ) ):
Expand Down Expand Up @@ -982,15 +986,16 @@ cdef class bedGraphTrackI:

cpdef str cutoff_analysis ( self, int max_gap, int min_length, int steps = 100 ):
cdef:
list chrs, tmplist, peak_content
set chrs
list tmplist, peak_content
bytes chrom
str ret
float cutoff
long total_l, total_p, i, n, ts, te, lastp, tl, peak_length
dict cutoff_npeaks, cutoff_lpeaks
float s, midvalue
chrs = self.__data.keys()

chrs = self.get_chr_names()

midvalue = self.minvalue/2 + self.maxvalue/2
s = float(self.minvalue - midvalue)/steps
Expand Down Expand Up @@ -1065,6 +1070,7 @@ def scoreTracktoBedGraph (scoretrack, str colname):
cdef:
int pre, i
bytes chrom
set chrs

bdgtrack = bedGraphTrackI( baseline_value = 0 )
if colname not in ['sample','control','-100logp','-100logq']:
Expand Down
Loading

0 comments on commit 0e871a8

Please sign in to comment.