Skip to content

Commit

Permalink
Fix for by_distance
Browse files Browse the repository at this point in the history
  • Loading branch information
Phlya committed Apr 12, 2023
1 parent 06a4706 commit e773db9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
19 changes: 11 additions & 8 deletions coolpuppy/coolpup.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,6 @@ def get_combinations(
groupby=[],
modify_2Dintervals_func=None,
):

if intervals is None:
intervals = self.intervals
if not len(intervals) >= 1:
Expand Down Expand Up @@ -734,7 +733,6 @@ def get_intervals_stream(
groupby=[],
modify_2Dintervals_func=None,
):

if intervals is None:
intervals = self.intervals
intervals = filter_func1(intervals)
Expand Down Expand Up @@ -927,8 +925,10 @@ def __init__(
except Exception as e:
raise ValueError("provided expected is not valid") from e
self.ExpSnipper = snipping.ExpectedSnipper(
self.clr, self.expected, view_df=self.view_df,
expected_value_col=self.expected_value_col
self.clr,
self.expected,
view_df=self.view_df,
expected_value_col=self.expected_value_col,
)
self.expected_selections = {
region_name: self.ExpSnipper.select(region_name, region_name)
Expand Down Expand Up @@ -2006,12 +2006,17 @@ def pileup(
pileup_df - pandas DataFrame containing the pileups and their grouping information,
if any, all possible annotations from the arguments of this function.
"""
if by_distance:
if by_distance is not False:
if by_distance is True or by_distance == "default":
distance_edges = "default"
by_distance = True
elif len(by_distance) > 0:
distance_edges = by_distance
try:
distance_edges = [int(i) for i in by_distance]
except Exception as e:
raise ValueError(
"Distance bin edges have to be an iterable of integers or convertable to integers"
) from e
by_distance = True
else:
raise ValueError(
Expand All @@ -2021,8 +2026,6 @@ def pileup(
raise ValueError(
"Can't do local pileups by distance, please specify only one of those arguments"
)
else:
by_distance = False

if not rescale:
rescale_flank = None
Expand Down
32 changes: 25 additions & 7 deletions tests/test_coolpup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from matplotlib.pyplot import ioff
from coolpuppy.coolpup import PileUpper, CoordCreator
from coolpuppy.coolpup import PileUpper, CoordCreator, pileup
from cooltools.lib import io, common
import pandas as pd
import numpy as np
Expand Down Expand Up @@ -101,16 +101,34 @@ def test_bystrand_bydistance_pileups_with_controls(request):
features = bf.read_table(
op.join(request.fspath.dirname, "data/toy_features.bed"), schema="bed"
)
cc = CoordCreator(
features,
1_000_000,

pup = pileup(
clr=clr,
features=features,
features_format="bed",
local=False,
view_df=regions,
mindist=0,
flank=2_000_000,
nshifts=1,
by_strand=True,
by_distance=True,
)
assert np.all(
pup.sort_values(["orientation", "distance_band"])["n"] == [1, 2, 1, 1, 1, 6]
)

distance_bins = np.append([0], 50000 * 2 ** np.arange(30))
pup = pileup(
clr=clr,
features=features,
features_format="bed",
view_df=regions,
mindist=0,
flank=2_000_000,
nshifts=1,
by_strand=True,
by_distance=distance_bins,
)
pu = PileUpper(clr, cc, expected=False, view_df=regions, control=True)
pup = pu.pileupsByStrandByDistanceWithControl()
assert np.all(
pup.sort_values(["orientation", "distance_band"])["n"] == [1, 2, 1, 1, 1, 6]
)

0 comments on commit e773db9

Please sign in to comment.