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

Fix problems with small batches during doublet detection #52

Merged
merged 1 commit into from
Mar 4, 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 modules/dedoublet_adata.nf
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ process DEDOUBLET_ADATA {
solo_annotation = solo_annotation.reindex(adata.obs_names)

# Keep only cells with "singlet" in the "doublet_label" column
adata = adata[solo_annotation["doublet_label"] == "singlet", :]
adata = adata[~solo_annotation["doublet_label"] == "doublet", :]

# Save the AnnData object
adata.write_h5ad("${meta.id}.dedup.h5ad")
Expand Down
19 changes: 7 additions & 12 deletions modules/solo.nf
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ process SOLO {
adata = sc.read_h5ad("${adata}")

adata_batch = adata[adata.obs.batch == "${batch}"]
batch_size = len(adata_batch)

if batch_size < 128:
print(f"Batch size too small ({batch_size}), skipping SOLO")
solo_res = pd.DataFrame(index=adata_batch.obs.index)
solo_res["doublet_label"] = "Unknown"
solo_res.to_pickle("${new_meta.id}.solo.pkl")
exit(0)

if ${has_celltypes ? "True" : "False"}:
scvi.model.SCANVI.setup_anndata(adata, batch_key="batch", labels_key="cell_type", unlabeled_category="Unknown")
Expand All @@ -46,7 +38,7 @@ process SOLO {

solo = scvi.external.SOLO.from_scvi_model(scvi_model, restrict_to_batch="${batch}")

minibatch_size = 128
minibatch_size = min(128, len(adata_batch))
worked = False
while not worked and minibatch_size > 100:
try:
Expand All @@ -55,10 +47,13 @@ process SOLO {
except ValueError:
print("Minibatch size did not work, trying again with smaller minibatch size")
minibatch_size -= 1
pass

solo_res = solo.predict()
solo_res["doublet_label"] = solo.predict(False)
if worked:
solo_res = solo.predict()
solo_res["doublet_label"] = solo.predict(False)
else:
solo_res = pd.DataFrame(index=adata_batch.obs.index)
solo_res["doublet_label"] = "Unknown"

solo_res.to_pickle("${new_meta.id}.solo.pkl")
"""
Expand Down