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

Fix annotate for chunked operations like in insulation #422

Merged
merged 7 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 17 additions & 8 deletions src/cooler/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def chroms(
lo: int = 0,
hi: int | None = None,
fields: list[str] | None = None,
**kwargs
**kwargs,
) -> pd.DataFrame:
"""
Table describing the chromosomes/scaffolds/contigs used.
Expand Down Expand Up @@ -473,7 +473,7 @@ def bins(
lo: int = 0,
hi: int | None = None,
fields: list[str] | None = None,
**kwargs
**kwargs,
) -> pd.DataFrame:
"""
Table describing the genomic bins that make up the axes of the heatmap.
Expand Down Expand Up @@ -530,7 +530,7 @@ def pixels(
hi: int | None = None,
fields: list[str] | None = None,
join: bool = True,
**kwargs
**kwargs,
) -> pd.DataFrame:
"""
Table describing the nonzero upper triangular pixels of the Hi-C contact
Expand Down Expand Up @@ -570,9 +570,7 @@ def pixels(


def annotate(
pixels: pd.DataFrame,
bins: pd.DataFrame | RangeSelector1D,
replace: bool = False
pixels: pd.DataFrame, bins: pd.DataFrame | RangeSelector1D, replace: bool = False
) -> pd.DataFrame:
"""
Add bin annotations to a data frame of pixels.
Expand Down Expand Up @@ -609,6 +607,7 @@ def annotate(
def _loc_slice(sel, beg, end):
# slicing a range selector is end-exclusive like iloc
return sel[beg : end + 1 if end is not None else None]

else:

def _loc_slice(df, beg, end):
Expand All @@ -631,7 +630,7 @@ def _loc_slice(df, beg, end):
bmin, bmax = 0, None
ann1 = _loc_slice(bins, bmin, bmax)
anns.append(
ann1.iloc[bin1 - bmin]
ann1.iloc[bin1 - ann1.index[0]]
.rename(columns=lambda x: x + "1")
.reset_index(drop=True)
)
Expand All @@ -647,7 +646,7 @@ def _loc_slice(df, beg, end):
bmin, bmax = 0, None
ann2 = _loc_slice(bins, bmin, bmax)
anns.append(
ann2.iloc[bin2 - bmin]
ann2.iloc[bin2 - ann2.index[0]]
.rename(columns=lambda x: x + "2")
.reset_index(drop=True)
)
Expand All @@ -662,6 +661,16 @@ def _loc_slice(df, beg, end):
out.index = pixels.index
return out

# Drop original bin IDs if not wanted
if replace:
cols_to_drop = [col for col in ("bin1_id", "bin2_id") if col in columns]
pixels = pixels.drop(cols_to_drop, axis=1)

# Concatenate bin annotations with pixels
out = pd.concat([*anns, pixels.reset_index(drop=True)], axis=1)
out.index = pixels.index
return out

Phlya marked this conversation as resolved.
Show resolved Hide resolved

def matrix(
h5: h5py.Group,
Expand Down
2 changes: 1 addition & 1 deletion src/cooler/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
region_to_extent,
region_to_offset,
)
from ._selectors import RangeSelector1D, RangeSelector2D
from ._selectors import RangeSelector1D, RangeSelector2D, _IndexingMixin
from ._tableops import delete, get, put

__all__ = [
Expand Down