Skip to content

Commit

Permalink
Fix empty selections
Browse files Browse the repository at this point in the history
  • Loading branch information
Phlya committed Jul 17, 2020
1 parent 41d5ed4 commit 898d060
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions coolpuppy/coolpup.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,12 +658,13 @@ def control_regions(self, filter_func, pos_pairs=None):
source = self.pos_stream(filter_func)
else:
source = map(lambda x: x[1:], pos_pairs.itertuples())
row1 = source.__next__()
if row1[0] is None: # Checking if empty selection
logging.debug("Empty selection")
yield row1
else:
source = itertools.chain([row1], source)
# try:
# row1 = next(source)
# except StopIteration:
# logging.debug("Empty selection")
# raise StopIteration
# else:
# source = itertools.chain([row1], source)
for start, end, p1, p2 in source:
for i in range(self.nshifts):
shift = np.random.randint(minbin, maxbin)
Expand Down Expand Up @@ -970,7 +971,19 @@ def get_coverage(self, data):
def _do_pileups(
self, mids, chrom, expected=False,
):

mymap = self.make_outmap()
cov_start = np.zeros(mymap.shape[0])
cov_end = np.zeros(mymap.shape[1])
num = np.zeros_like(mymap)
n = 0
try:
mids_row1 = next(mids)
except StopIteration:
logging.info(f"Nothing to sum up in chromosome {chrom}")
return mymap, mymap, cov_start, cov_end, n
else:
mids = itertools.chain([mids_row1], mids)

if expected:
data = None
logging.debug("Doing expected")
Expand All @@ -979,13 +992,10 @@ def _do_pileups(
chrom
) # self.CoolSnipper.select(self.regions[chrom], self.regions[chrom])
max_right = self.matsizes[chrom]
mymap = self.make_outmap()

if self.coverage_norm:
coverage = self.get_coverage(data)
cov_start = np.zeros(mymap.shape[0])
cov_end = np.zeros(mymap.shape[1])
num = np.zeros_like(mymap)
n = 0

for stBin, endBin, stPad, endPad in mids:
rot_flip = False
rot = False
Expand Down Expand Up @@ -1117,13 +1127,6 @@ def pileup_chrom(
mids = self.CC.control_regions(filter_func)
else:
mids = self.CC.pos_stream(filter_func)
try:
mids_row1 = mids.__next__()
except StopIteration:
logging.info(f"Nothing to sum up in chromosome {chrom}")
return mymap, mymap, cov_start, cov_end, 0
else:
mids = itertools.chain([mids_row1], mids)
mymap, num, cov_start, cov_end, n = self._do_pileups(
mids=mids, chrom=chrom, expected=expected,
)
Expand Down

0 comments on commit 898d060

Please sign in to comment.