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: Pixel annotation with partial bin table #426

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release notes #

## [v0.10.1](https://github.com/open2c/cooler/compare/v0.10.0...v0.10.1)

## Bug fixes
* fix: Pixel annotation with partial bin table in https://github.com/open2c/cooler/pull/422

## [v0.10.0](https://github.com/open2c/cooler/compare/v0.9.3...v0.10.0)

### New features
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "cooler"
version = "0.10.0"
version = "0.10.1"
description = "Sparse binary format for genomic interaction matrices."
requires-python = ">=3.8"
license = {text = "BSD-3-Clause"}
Expand Down
15 changes: 7 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 Down
15 changes: 14 additions & 1 deletion src/cooler/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,18 @@
region_to_extent,
region_to_offset,
)
from ._selectors import RangeSelector1D, RangeSelector2D, _IndexingMixin
from ._selectors import RangeSelector1D, RangeSelector2D, _IndexingMixin # noqa
from ._tableops import delete, get, put

__all__ = [
"CSRReader",
"DirectRangeQuery2D",
"FillLowerRangeQuery2D",
"RangeSelector1D",
"RangeSelector2D",
"region_to_extent",
"region_to_offset",
"delete",
"get",
"put",
]
22 changes: 22 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ def test_annotate_with_partial_bins():
assert out[col].notnull().all()


def test_annotate_with_partial_bins_and_partial_pixels():
# Addresses a bug where annotating only certain pixels with a partial
# bin-table dataframe would lead to IndexError

clr = api.Cooler(op.join(datadir, "hg19.GM12878-MboI.matrix.2000kb.cool"))
region = "chr2:0-1000000"

pix = clr.matrix(as_pixels=True, balance=False).fetch(region)

# Use partial bin table
bins_region = clr.bins().fetch(region)
out = api.annotate(pix, bins_region)
for col in ["chrom1", "start1", "end1", "chrom2", "start2", "end2"]:
assert out[col].notnull().all()

# Compare with full bin table as selector
pd.testing.assert_frame_equal(out, api.annotate(pix, clr.bins()))

# Compare with full bin table materialized
pd.testing.assert_frame_equal(out, api.annotate(pix, clr.bins()[:]))


def test_matrix():
clr = api.Cooler(op.join(datadir, "yeast.10kb.cool"))
region = ("chrI:100345-220254", "chrII:200789-813183")
Expand Down