Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overlap point segment patch #183 #184

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions bioframe/core/arrops.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,22 @@ def _overlap_intervals_legacy(starts1, ends1, starts2, ends2, closed=False, sort

return overlap_ids

def get_pseudo_segment(starts, ends):
smitkadvani marked this conversation as resolved.
Show resolved Hide resolved
"""
Get pseudo-segment for overlapping intervals.
smitkadvani marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
starts, ends : numpy.ndarray

Returns
-------
pseudo_ends : numpy.ndarray
An array of pseudo-ends for overlapping intervals.
"""
pseudo_ends = ends.copy()
pseudo_ends[ends == starts] += 1
return [starts, pseudo_ends]

def overlap_intervals(starts1, ends1, starts2, ends2, closed=False, sort=False):
"""
Expand Down Expand Up @@ -296,8 +312,11 @@ def overlap_intervals(starts1, ends1, starts2, ends2, closed=False, sort=False):

starts1 = np.asarray(starts1)
ends1 = np.asarray(ends1)
starts1, ends1 = get_pseudo_segment(starts1, ends1)
smitkadvani marked this conversation as resolved.
Show resolved Hide resolved

starts2 = np.asarray(starts2)
ends2 = np.asarray(ends2)
starts2, ends2 = get_pseudo_segment(starts2, ends2)

# Concatenate intervals lists
n1 = len(starts1)
Expand Down
156 changes: 155 additions & 1 deletion tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,94 @@ def test_overlap():
)
assert len(b) == 3

### test overlap with point and segment data
smitkadvani marked this conversation as resolved.
Show resolved Hide resolved
df1 = pd.DataFrame(
smitkadvani marked this conversation as resolved.
Show resolved Hide resolved
[
['chr1', 1, 1]
],
columns=['chrom','start','end']
).astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()})

df2 = pd.DataFrame(
[
['chr1', 1, 2]
],
columns=['chrom','start','end']
).astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()})

b = bioframe.overlap(
df1,
df2,
on=None,
how="left",
return_index=True,
return_input=False,
)
assert np.sum(pd.isna(b["index_"].values)) == 0
smitkadvani marked this conversation as resolved.
Show resolved Hide resolved
b = bioframe.overlap(
df2,
df1,
on=None,
how="left",
return_index=True,
return_input=False,
)
assert np.sum(pd.isna(b["index_"].values)) == 0

b = bioframe.overlap(
df1,
df2,
on=None,
how="right",
return_index=True,
return_input=False,
)
assert np.sum(pd.isna(b["index"].values)) == 0
b = bioframe.overlap(
df2,
df1,
on=None,
how="right",
return_index=True,
return_input=False,
)
assert np.sum(pd.isna(b["index"].values)) == 0

### Two adjacent point should not overlap with each other
df1 = pd.DataFrame(
smitkadvani marked this conversation as resolved.
Show resolved Hide resolved
[
['chr1', 1, 1]
],
columns=['chrom','start','end']
).astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()})

df2 = pd.DataFrame(
[
['chr1', 2, 2]
],
columns=['chrom','start','end']
).astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()})

b = bioframe.overlap(
df1,
df2,
on=None,
how="left",
return_index=True,
return_input=False,
)
assert np.sum(pd.isna(b["index_"].values)) == 1
b = bioframe.overlap(
df2,
df1,
on=None,
how="left",
return_index=True,
return_input=False,
)
assert np.sum(pd.isna(b["index_"].values)) == 1


### test keep_order and NA handling
df1 = pd.DataFrame(
[
Expand Down Expand Up @@ -1403,14 +1491,80 @@ def test_subtract():
.sort_values(["chrom", "start", "end"])
.reset_index(drop=True)
)

pd.testing.assert_frame_equal(
df_result.astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()}),
bioframe.subtract(df1, df2)
.sort_values(["chrom", "start", "end"])
.reset_index(drop=True),
)

# Test the case when substraction from point bioframe
df1 = pd.DataFrame(
[
['chr1', 1, 1]
],
columns=['chrom','start','end']
).astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()})

df2 = pd.DataFrame(
[
['chr1', 0, 2]
],
columns=['chrom','start','end']
).astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()})

df_result = (
pd.DataFrame(
[
["chr1", 0, 1],
["chr1", 1, 2],
],
columns=["chrom", "start", "end"],
)
.sort_values(["chrom", "start", "end"])
smitkadvani marked this conversation as resolved.
Show resolved Hide resolved
.reset_index(drop=True)
)
pd.testing.assert_frame_equal(
df_result.astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()}),
bioframe.subtract(df2, df1)
.sort_values(["chrom", "start", "end"])
.reset_index(drop=True),
)

# Test the case when substraction from point is at the beginning bioframe

df1 = pd.DataFrame(
[
['chr1', 1, 1]
],
columns=['chrom','start','end']
).astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()})

df2 = pd.DataFrame(
[
['chr1', 1, 2]
],
columns=['chrom','start','end']
).astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()})

df_result = (
pd.DataFrame(
[
["chr1", 1, 2],
],
columns=["chrom", "start", "end"],
)
.sort_values(["chrom", "start", "end"])
smitkadvani marked this conversation as resolved.
Show resolved Hide resolved
.reset_index(drop=True)
)

pd.testing.assert_frame_equal(
df_result.astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()}),
bioframe.subtract(df2, df1)
.sort_values(["chrom", "start", "end"])
.reset_index(drop=True),
)

# subtract should ignore null rows
df1 = pd.DataFrame(
[[pd.NA, pd.NA, pd.NA], ["chr1", 1, 5]],
Expand Down
Loading