"""Module.
Examples
--------
pass
"""
__author__ = "Yeremia Gunawan Adhisantoso"
__license__ = "Clear BSD"
__email__ = "adhisant@tnt.uni-hannover.de"
import typing as t
import warnings
import numpy as np
import pandas as pd
from scipy import stats
from ... import loaders as cm_loaders
from ...consts import DataStructure
from ...structs.conflict_policy import ConflictPolicy
from ..mds.mds_numpy import comp_edm_from_p
from ...converters import convert_to_cm_coo
SUPERREC_COOR_COL_NAMES = ["X", "Y", "Z"]
COO_DELIM = " "
def gen_superrec_coo(
chr_region: str,
bin_size_bp: int,
balancing: str,
input_fpath: str,
output_fpath: str,
on_conflict: ConflictPolicy = "error",
) -> None:
"""Generate a COO format file for SHNeigh.
Notes
-----
This function converts the input data to a COO format file suitable for SHNeigh.
It uses the `convert_to_cm_coo` function with specific parameters tailored for SHNeigh's requirements.
Parameters
----------
chr_region : str
The chromosome region.
bin_size_bp : int
The bin size (in base pairs).
balancing : str
The balancing method.
input_fpath : str
The path to the input file.
output_fpath : str
The path to the output file.
on_conflict : ConflictPolicy, optional
What to do when ``output_fpath`` already exists. One of
``"error"`` (raise), ``"overwrite"`` (replace),
``"skip"`` (return without writing). Defaults to ``"error"``.
Returns
-------
None
"""
convert_to_cm_coo(
input_fpath,
output_fpath,
chr_region,
bin_size_bp,
balancing,
on_conflict=on_conflict,
gen_pseudo_weights=False,
output_delimiter=COO_DELIM,
res_to_one=True,
)
[docs]def comp_superrec_obj_perf(
region1: str,
bin_size_bp: int,
balancing: str,
input_fpath: str,
points_fpath: str,
region2: str | None = None,
) -> dict:
"""Compute the performance metrics (Spearman and Pearson correlation) for Euclidean distances predicted by SuperRec.
Notes
-----
This function loads the count data for a specified region and bin size, computes the Euclidean distance matrix
from the points file, and then calculates the Spearman and Pearson correlation coefficients between the counts and
the distances. The function assumes that the points file contains valid points and that the row and column IDs
are mapped to these valid points.
Parameters
----------
region1 : str
The first region for which to compute the performance metrics.
bin_size_bp : int
The bin size (in base pairs).
balancing : str
The balancing method to use when loading the data.
input_fpath : str
The file path to the input data.
points_fpath : str
The file path to the points data.
region2 : Optional[str], optional
The second region for which to compute the performance metrics, by default None.
Returns
-------
dict
A dictionary containing the region, Spearman correlation coefficient, Pearson correlation coefficient, and data ratio.
"""
count_df = cm_loaders.load_cm_data(
input_fpath,
bin_size_bp,
region1,
balancing=balancing,
region2=region2,
output_format=DataStructure.DF
)
row_ids = count_df[cm_loaders.ROW_IDS_COLNAME].to_numpy()
col_ids = count_df[cm_loaders.COL_IDS_COLNAME].to_numpy()
counts = count_df[cm_loaders.COUNTS_COLNAME].to_numpy()
#? Create mapping from row/col ids to points
#? Reason: not all loci are valid, yet the points contains only valid points
unique_ids = np.unique([row_ids, col_ids])
mapping = np.searchsorted(unique_ids, np.arange(unique_ids.max()+1))
new_row_ids = mapping[row_ids]
new_col_ids = mapping[col_ids]
points_fpath = pd.read_csv(
points_fpath,
delim_whitespace=True, #? Delimiter is all possible whitespace
header=None,
names=SUPERREC_COOR_COL_NAMES,
index_col=None,
).to_numpy()
edm = comp_edm_from_p(
points_fpath,
new_row_ids,
new_col_ids
)
res = stats.spearmanr(counts, edm)
spearman_r = res.correlation
res = stats.pearsonr(counts, edm)
pearson_r = res.correlation
DATA_RATIO = 1.0 #? No filtering of valid ids, thus ratio is 1.0
output_dict = {
'region': region1,
'spearman_r': spearman_r,
'pearson_r': pearson_r,
'data_ratio': DATA_RATIO,
}
return output_dict