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

Pre-filter batches in hvg overlap metric #9

Merged
merged 6 commits into from
Nov 8, 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
2 changes: 1 addition & 1 deletion common
70 changes: 40 additions & 30 deletions src/metrics/hvg_overlap/script.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,66 @@
import sys

import anndata as ad
import numpy as np
import scanpy as sc
from scib.metrics import hvg_overlap
from scib.utils import split_batches

## VIASH START
par = {
'input_integrated': 'resources_test/task_batch_integration/cxg_immune_cell_atlas/integrated_full.h5ad',
'input_solution': 'resources_test/task_batch_integration/cxg_immune_cell_atlas/solution.h5ad',
'output': 'output.h5ad',
}
meta = {
'name': 'foo',
"resources_dir": "src/utils"
"input_integrated": "resources_test/task_batch_integration/cxg_immune_cell_atlas/integrated_full.h5ad",
"input_solution": "resources_test/task_batch_integration/cxg_immune_cell_atlas/solution.h5ad",
"output": "output.h5ad",
}
meta = {"name": "foo", "resources_dir": "src/utils"}
## VIASH END

sys.path.append(meta["resources_dir"])
from read_anndata_partial import read_anndata

print('Read input', flush=True)
print("Read input", flush=True)
adata_solution = read_anndata(
par['input_solution'],
X='layers/normalized',
obs='obs',
var='var',
uns='uns'
par["input_solution"], X="layers/normalized", obs="obs", var="var", uns="uns"
)
adata_integrated = read_anndata(
par['input_integrated'],
X='layers/corrected_counts',
obs='obs',
var='var',
uns='uns'
par["input_integrated"],
X="layers/corrected_counts",
obs="obs",
var="var",
uns="uns",
)

print("Copy batch information", flush=True)
adata_integrated.obs['batch'] = adata_solution.obs['batch']
adata_integrated.obs["batch"] = adata_solution.obs["batch"]

print('Compute score', flush=True)
score = hvg_overlap(
adata_solution,
adata_integrated,
batch_key="batch"
)
print("Remove batches with insufficient genes", flush=True)
adata_list = split_batches(adata_solution, "batch", hvg=adata_integrated.var_names)
skip_batches = []
for adata_batch in adata_list:
sc.pp.filter_genes(adata_batch, min_cells=1)
n_hvg_tmp = np.minimum(500, int(0.5 * adata_batch.n_vars))
if n_hvg_tmp < 500:
batch = adata_batch.obs["batch"][0]
print(
f"Batch '{batch}' has insufficient genes (0.5 * {adata_batch.n_vars} < 500) and will be skipped",
flush=True,
)
skip_batches.append(batch)

adata_solution = adata_solution[~adata_solution.obs["batch"].isin(skip_batches)]
adata_integrated = adata_integrated[~adata_integrated.obs["batch"].isin(skip_batches)]

print("Compute score", flush=True)
score = hvg_overlap(adata_solution, adata_integrated, batch_key="batch")

print("Create output AnnData object", flush=True)
output = ad.AnnData(
uns={
"dataset_id": adata_solution.uns['dataset_id'],
'normalization_id': adata_solution.uns['normalization_id'],
"method_id": adata_integrated.uns['method_id'],
"metric_ids": [meta['name']],
"metric_values": [score]
"dataset_id": adata_solution.uns["dataset_id"],
"normalization_id": adata_solution.uns["normalization_id"],
"method_id": adata_integrated.uns["method_id"],
"metric_ids": [meta["name"]],
"metric_values": [score],
}
)

Expand Down
Loading