|
| 1 | +import anndata as ad |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import json |
| 5 | +import pandas as pd |
| 6 | +from pathlib import Path |
| 7 | +## VIASH START |
| 8 | +par = { |
| 9 | + 'input_spatial_normalized_counts': 'resources_test/task_ist_preprocessing/mouse_brain_combined/spatial_normalized_counts.h5ad', |
| 10 | + 'input_scrnaseq_reference': 'resources_test/task_ist_preprocessing/mouse_brain_combined/scrnaseq_reference.h5ad', |
| 11 | + 'celltype_key': 'cell_type', |
| 12 | + "output": 'spatial_with_celltypes.h5ad' |
| 13 | +} |
| 14 | +meta = { "temp_dir": './tmp/'} |
| 15 | + |
| 16 | +## VIASH END |
| 17 | + |
| 18 | +TMP_DIR = Path(meta["temp_dir"] or "/tmp/") |
| 19 | +TMP_DIR.mkdir(parents=True, exist_ok=True) |
| 20 | + |
| 21 | +adata_sp = ad.read_h5ad(par['input_spatial_normalized_counts']) |
| 22 | +adata_sc = ad.read_h5ad(par['input_scrnaseq_reference']) |
| 23 | + |
| 24 | +if "counts" in adata_sc.layers: |
| 25 | + adata_sc.X = adata_sc.layers["counts"] |
| 26 | + |
| 27 | +adata_sp.var_names = adata_sp.var_names.astype(str) |
| 28 | +adata_sc.var_names = adata_sc.var_names.astype(str) |
| 29 | +adata_sp.var_names_make_unique() |
| 30 | +adata_sc.var_names_make_unique() |
| 31 | + |
| 32 | +common_genes = list(set(adata_sp.var.index).intersection(adata_sc.var.index)) |
| 33 | + |
| 34 | +adata_sc = adata_sc[:, common_genes] |
| 35 | +sc_path = os.path.join(meta["temp_dir"],"sc_adata_processed.h5ad") |
| 36 | +adata_sc.write_h5ad(sc_path) |
| 37 | +sp_path = os.path.join(meta["temp_dir"],"sp_processed.h5ad") |
| 38 | +adata_sp[:, common_genes].write_h5ad(sp_path) |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +precomputed_path = os.path.join(meta["temp_dir"],"precomputed_stats.h5ad") |
| 43 | + |
| 44 | +command = [ |
| 45 | + "python", |
| 46 | + "-m", |
| 47 | + "cell_type_mapper.cli.precompute_stats_scrattch", |
| 48 | + "--h5ad_path", |
| 49 | + sc_path, |
| 50 | + "--hierarchy", |
| 51 | + "['cell_type']", |
| 52 | + "--output_path", |
| 53 | + precomputed_path |
| 54 | +] |
| 55 | + |
| 56 | +subprocess.run(command) |
| 57 | + |
| 58 | +data = {"None": common_genes} |
| 59 | +genes_file_path = os.path.join(meta["temp_dir"],"genes.json") |
| 60 | +with open(genes_file_path, "w") as json_file: |
| 61 | + json.dump(data, json_file, indent=2) |
| 62 | + |
| 63 | +command = [ |
| 64 | + "python", |
| 65 | + "-m", |
| 66 | + "cell_type_mapper.cli.from_specified_markers", |
| 67 | + "--query_path", |
| 68 | + sp_path, |
| 69 | + "--type_assignment.normalization", |
| 70 | + "log2CPM", |
| 71 | + "--precomputed_stats.path", |
| 72 | + precomputed_path, |
| 73 | + "--query_markers.serialized_lookup", |
| 74 | + genes_file_path, |
| 75 | + "--csv_result_path", |
| 76 | + os.path.join(meta["temp_dir"],"results.csv"), |
| 77 | + "--extended_result_path", |
| 78 | + os.path.join(meta["temp_dir"], "extended_results.json"), |
| 79 | + "--flatten", |
| 80 | + "True", |
| 81 | + "--type_assignment.bootstrap_iteration", |
| 82 | + "1", |
| 83 | + "--type_assignment.bootstrap_factor", |
| 84 | + "1.0" |
| 85 | +] |
| 86 | + |
| 87 | +subprocess.run(command) |
| 88 | +annotation_df = pd.read_csv(os.path.join(meta["temp_dir"],"results.csv"), skiprows=3) |
| 89 | +adata_sp.obs[par['celltype_key']] = list(annotation_df['cell_type_label']) |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +# Delete all temporary files |
| 94 | +for file_path in [ |
| 95 | + sc_path, |
| 96 | + sp_path, |
| 97 | + precomputed_path, |
| 98 | + genes_file_path, |
| 99 | + os.path.join(meta["temp_dir"],"results.csv"), |
| 100 | + os.path.join(meta["temp_dir"], "extended_results.json") |
| 101 | +]: |
| 102 | + if os.path.isfile(file_path): |
| 103 | + os.remove(file_path) |
| 104 | + |
| 105 | + |
| 106 | +adata_sp.write_h5ad(par['output']) |
0 commit comments