"""Writers for Higashi / FastHigashi input formats.
Higashi v2 expects:
- Per-cell text files of binned contacts with columns
``chrom1, pos1, chrom2, pos2, count`` (counts collapsed to one row per
bin-pair).
- A ``filelist.txt`` with one line per cell: ``cell_name<TAB>path``.
- ``label_info.pickle``: ``dict[str, np.ndarray]``, one entry per metadata
column, aligned to filelist order.
- ``config.JSON``: genome / resolution / chrom_list + the paths above.
See https://github.com/ma-compbio/Higashi/wiki/Input-Files for the full spec.
"""
from __future__ import annotations
import gzip
import json
import pickle
from pathlib import Path
from typing import Any, Mapping
import numpy as np
import pandas as pd
[docs]
def pairs_df_to_higashi(
pairs: pd.DataFrame,
*,
resolution: int,
) -> pd.DataFrame:
"""Convert a ``read_pairs``-style DataFrame to Higashi's per-cell format.
Input columns: ``chr1, pos1, chr2, pos2`` (extra columns are ignored).
Output columns: ``chrom1, pos1, chrom2, pos2, count``.
Positions are binned to ``resolution`` (floor), intra-chromosomal pairs
are ordered so ``pos1 <= pos2``, and duplicates are collapsed by summing
into ``count``.
"""
df = pd.DataFrame({
"chrom1": pairs["chr1"].astype(str),
"pos1": (pairs["pos1"].astype("int64") // resolution) * resolution,
"chrom2": pairs["chr2"].astype(str),
"pos2": (pairs["pos2"].astype("int64") // resolution) * resolution,
})
intra = df["chrom1"] == df["chrom2"]
swap = intra & (df["pos1"] > df["pos2"])
if swap.any():
df.loc[swap, ["pos1", "pos2"]] = df.loc[swap, ["pos2", "pos1"]].values
out = (
df.groupby(["chrom1", "pos1", "chrom2", "pos2"], sort=False)
.size()
.reset_index(name="count")
)
return out
[docs]
def make_higashi_config(
*,
data_dir: str,
genome_reference: str,
resolution: int,
chrom_list: list[str],
temp_dir: str | None = None,
cell_feats: list[str] | None = None,
overrides: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""Build a Higashi ``config.JSON`` dict.
Fields follow the v2 schema from the Higashi wiki and FastHigashi's
runtime expectations (``input_format``, ``contact_header``,
``header_included``). ``overrides`` are merged last, so callers can
tweak any field (including the required ones) without forking this
helper.
"""
cfg: dict[str, Any] = {
"data_dir": data_dir,
"temp_dir": temp_dir if temp_dir is not None else data_dir,
"genome_reference": genome_reference,
"resolution": int(resolution),
# FastHigashi tries multiple resolutions; default to the same one.
"resolution_fh": [int(resolution)],
"chrom_list": list(chrom_list),
"input_format": "higashi_v2",
"cell_feats": list(cell_feats) if cell_feats else [],
# FastHigashi reads per-cell files according to these:
"header_included": False,
"contact_header": ["chrom1", "pos1", "chrom2", "pos2", "count"],
}
if overrides:
cfg.update(overrides)
return cfg
[docs]
def write_label_info(
cells: pd.DataFrame,
out_path: str | Path,
) -> Path:
"""Write a per-cell metadata DataFrame to Higashi's ``label_info.pickle``.
Higashi expects a ``dict[str, np.ndarray]``, one entry per metadata
column, aligned row-by-row to the filelist order. Row order of
``cells`` is preserved.
"""
out_path = Path(out_path)
out_path.parent.mkdir(parents=True, exist_ok=True)
payload: dict[str, np.ndarray] = {
col: cells[col].to_numpy() for col in cells.columns
}
with open(out_path, "wb") as f:
pickle.dump(payload, f)
return out_path
def _read_pairs_file(path: str | Path) -> pd.DataFrame:
"""Read the minimal .pairs columns needed for Higashi conversion."""
path = Path(path)
opener = gzip.open if path.suffix == ".gz" else open
with opener(path, "rt") as f:
df = pd.read_csv(f, sep="\t", header=None, comment="#")
columns = ["read", "chr1", "pos1", "chr2", "pos2", "strand1", "strand2"]
df = df.iloc[:, :len(columns)]
df.columns = columns[:len(df.columns)]
return df
def _materialise_chromsizes(
chromsizes: Mapping[str, int] | str | Path | None,
out_dir: Path,
) -> Path | None:
"""Return a path to a chromsizes.tsv on disk, writing it if needed."""
if chromsizes is None:
return None
if isinstance(chromsizes, (str, Path)):
return Path(chromsizes)
out_path = out_dir / "chromsizes.tsv"
with open(out_path, "w") as f:
for chrom, size in chromsizes.items():
f.write(f"{chrom}\t{int(size)}\n")
return out_path