Skip to content

Commit

Permalink
Merge pull request #3 from aertslab/add_pybigtools_0.2.0_support
Browse files Browse the repository at this point in the history
Update code to new pybigtools 0.2.0 API.
  • Loading branch information
LukasMahieu authored Jul 10, 2024
2 parents 8cbb85e + c8011f5 commit 9e68b6d
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/crested/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,41 @@ def _read_chromsizes(chromsizes_file: PathLike) -> dict[str, int]:
return chromsizes_dict


def _extract_values_from_bigwig(bw_file, bed_file, target, target_region_width):
def _extract_values_from_bigwig(
bw_file: PathLike, bed_file: PathLike, target: str
) -> np.ndarray:
"""Extract target values from a bigWig file for regions specified in a BED file."""
if isinstance(bed_file, Path):
bed_file = str(bed_file)
if isinstance(bw_file, Path):
bw_file = str(bw_file)

if target == "mean":
values = list(
pybigtools.bigWigAverageOverBed(bw_file, bed=bed_file, names=None)
)
with pybigtools.open(bw_file, "r") as bw:
values = np.fromiter(
bw.average_over_bed(bed=bed_file, names=None, stats="mean0"),
dtype=np.float32,
)
elif target == "max":
values = list(
pybigtools.bigWigAverageOverBed(bw_file, bed=bed_file, names=None)
)
with pybigtools.open(bw_file, "r") as bw:
values = np.fromiter(
bw.average_over_bed(bed=bed_file, names=None, stats="max"),
dtype=np.float32,
)
elif target == "count":
values = list(
pybigtools.bigWigAverageOverBed(bw_file, bed=bed_file, names=None)
)
with pybigtools.open(bw_file, "r") as bw:
values = np.fromiter(
bw.average_over_bed(bed=bed_file, names=None, stats="sum"),
dtype=np.float32,
)
elif target == "logcount":
values = list(
pybigtools.bigWigAverageOverBed(bw_file, bed=bed_file, names=None)
)
with pybigtools.open(bw_file, "r") as bw:
values = np.log1p(
np.fromiter(
bw.average_over_bed(bed=bed_file, names=None, stats="sum"),
dtype=np.float32,
)
)
else:
raise ValueError(f"Unsupported target '{target}'")

Expand Down Expand Up @@ -427,7 +439,6 @@ def import_bigwigs(
bw_file,
bed_file,
target,
target_region_width,
)
for bw_file in bw_files
]
Expand All @@ -437,7 +448,7 @@ def import_bigwigs(
if target_region_width is not None:
os.remove(bed_file)

data_matrix = np.array(all_results)
data_matrix = np.vstack(all_results)

# Create DataFrame for AnnData
df = pd.DataFrame(
Expand Down

0 comments on commit 9e68b6d

Please sign in to comment.