Skip to content

Commit

Permalink
complete introduction of common checks into snipping
Browse files Browse the repository at this point in the history
  • Loading branch information
sergpolly committed Nov 3, 2021
1 parent bb8163a commit d378147
Showing 1 changed file with 34 additions and 51 deletions.
85 changes: 34 additions & 51 deletions cooltools/snipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,24 +223,22 @@ def pair_sites(sites, separation, slop):
class CoolerSnipper:
def __init__(self, clr, cooler_opts=None, view_df=None):

# get chromosomes from bins, if view_df not specified:
# get chromosomes from cooler, if view_df not specified:
if view_df is None:
view_df = bioframe.make_viewframe(
[(chrom, 0, l, chrom) for chrom, l in clr.chromsizes.items()]
)
view_df = make_cooler_view(clr)
else:
# appropriate viewframe checks:
if not bioframe.is_viewframe(view_df):
raise ValueError("view_df is not a valid viewframe.")
if not bioframe.is_contained(
view_df, bioframe.make_viewframe(clr.chromsizes)
):
raise ValueError(
"view_df is out of the bounds of chromosomes in cooler."
)
# Make sure view_df is a proper viewframe
try:
_ = is_compatible_viewframe(
view_df,
clr,
check_sorting=True,
raise_errors=True,
)
except Exception as e:
raise ValueError("view_df is not a valid viewframe or incompatible") from e

self.view_df = view_df.set_index("name")

self.clr = clr
self.binsize = self.clr.binsize
self.offsets = {}
Expand Down Expand Up @@ -313,49 +311,34 @@ def __init__(self, clr, expected, cooler_opts=None, view_df=None):
self.clr = clr
self.expected = expected

# Detecting the columns for the detection of regions
columns = expected.columns
assert len(columns) > 0
if ("region1" not in columns) or ("region2" not in columns):
if ("chrom" in columns) or ("region" in columns):
raise ValueError(
"Provided expected appears to have old format, it has to comply with the format of expected v1.0"
)
else:
raise ValueError(
"Please check the expected dataframe, it has to comply with the format of expected v1.0"
)

# get chromosomes from cooler, if view_df not specified:
if view_df is None:
view_df = bioframe.make_viewframe(
[(chrom, 0, l, chrom) for chrom, l in clr.chromsizes.items()]
)
view_df = make_cooler_view(clr)
else:
# appropriate viewframe checks:
if not bioframe.is_viewframe(view_df):
raise ValueError("view_df is not a valid viewframe.")
if not bioframe.is_contained(
view_df, bioframe.make_viewframe(clr.chromsizes)
):
raise ValueError(
"view_df is out of the bounds of chromosomes in cooler."
# Make sure view_df is a proper viewframe
try:
_ = is_compatible_viewframe(
view_df,
clr,
check_sorting=True,
raise_errors=True,
)
except Exception as e:
raise ValueError("view_df is not a valid viewframe or incompatible") from e
# make sure expected is compatible
try:
_ = is_compatible_expected(
expected,
"cis",
view_df,
verify_cooler=clr,
expected_value_cols=["balanced.avg", ],
raise_errors=True
)
except Exception as e:
raise ValueError("provided expected is not valid") from e

self.view_df = view_df.set_index("name")

for (name1, name2), group in self.expected.groupby(["region1", "region2"]):
if name1 != name2:
raise ValueError("Only symmetric regions a supported, e.g. chromosomes, arms, etc")
n_diags = group.shape[0]
region = self.view_df.loc[name1]
lo, hi = self.clr.extent(region)
if n_diags != (hi - lo):
raise ValueError(
"Region shape mismatch between expected and cooler. "
"Are they using the same resolution?"
)

self.binsize = self.clr.binsize
self.offsets = {}
self.pad = True
Expand Down

0 comments on commit d378147

Please sign in to comment.