From fbe69b1c3942d337fc92034545d86786b8a16788 Mon Sep 17 00:00:00 2001 From: Eachan Johnson Date: Mon, 11 May 2026 16:42:01 +0100 Subject: [PATCH] Deal with situations where reference barcodes have zero counts --- bartab/transforms.py | 16 +++++++++++++--- pyproject.toml | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/bartab/transforms.py b/bartab/transforms.py index b0d94d8..2838450 100644 --- a/bartab/transforms.py +++ b/bartab/transforms.py @@ -56,8 +56,18 @@ def compute_log_ratios( raise ValueError("No t0 samples found") log_X = np.log(X) # (n_strains, n_samples) - log_ref = np.log(np.sum(X[ref_mask, :], axis=0, keepdims=True)) # (1, n_samples,) + ref_counts = np.nansum(X[ref_mask, :], axis=0, keepdims=True) + zero_ref_counts = ref_counts == 0. + if zero_ref_counts.all(): + raise ValueError( + f"Reference counts in all samples are zero." + ) + if zero_ref_counts.any(): + raise ValueError( + f"The following samples have zero reference counts:\n{adata.var.loc[zero_ref_counts]}" + ) + log_ref = np.log(ref_counts) # (1, n_samples,) # log(c_i / c_wt) at every sample log_ratio_to_ref = log_X - log_ref # (n_strains, n_samples) log_ratio = log_ratio_to_ref.copy() @@ -106,8 +116,8 @@ def compute_log_ratios( growth_t0_mean = growth[t0_idx].mean() if growth_type == "density": expansion[:, c_idx] = np.log(growth[c_idx]) - np.log(growth_t0_mean) - else: - expansion[:, c_idx] = growth[c_idx] * np.log(2.) + else: # generations + expansion[:, c_idx] = (growth[c_idx] - growth_t0_mean) * np.log(2.) expansion = -expansion else: raise ValueError( diff --git a/pyproject.toml b/pyproject.toml index 4a635a2..504f89e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "bartab" -version = "0.0.9" +version = "0.0.10" authors = [ { name="Eachan Johnson", email="eachan.johnson@crick.ac.uk" }, ]