|
| 1 | +import anndata as ad |
| 2 | +import numpy as np |
| 3 | +import pandas as pd |
| 4 | +import scanpy as sc |
| 5 | +from scanpy.metrics import morans_i |
| 6 | + |
| 7 | +## VIASH START |
| 8 | +# Note: this section is auto-generated by viash at runtime. To edit it, make changes |
| 9 | +# in config.vsh.yaml and then run `viash config inject config.vsh.yaml`. |
| 10 | +par = { |
| 11 | + 'input_solution': 'resources_test/task_spatial_trajectory_inference/dlpfc_151673/solution.h5ad', |
| 12 | + 'input_prediction': 'resources_test/task_spatial_trajectory_inference/dlpfc_151673/prediction.h5ad', |
| 13 | + 'output': 'output.h5ad', |
| 14 | + 'n_neighbors': 6, |
| 15 | +} |
| 16 | +meta = { |
| 17 | + 'name': 'morans_i' |
| 18 | +} |
| 19 | +## VIASH END |
| 20 | + |
| 21 | + |
| 22 | +def calc_morans_i(adata, pt_col, coords_key, n_neighbors=6): |
| 23 | + """Spatial autocorrelation of pseudotime via Moran's I.""" |
| 24 | + try: |
| 25 | + sc.pp.neighbors(adata, use_rep=coords_key, n_neighbors=n_neighbors, key_added="spatial_neighbors") |
| 26 | + pt = pd.to_numeric(adata.obs[pt_col], errors="coerce").values |
| 27 | + |
| 28 | + return float(morans_i(adata.obsp["spatial_neighbors_connectivities"], pt)) |
| 29 | + except Exception as e: |
| 30 | + print(f"Moran's I skipped for {pt_col}: {e}") |
| 31 | + return np.nan |
| 32 | + |
| 33 | +# read input data |
| 34 | +print('Reading input files', flush=True) |
| 35 | +input_solution = ad.read_h5ad(par['input_solution']) |
| 36 | +input_prediction = ad.read_h5ad(par['input_prediction']) |
| 37 | + |
| 38 | +assert (input_prediction.obs_names == input_solution.obs_names).all(), "obs_names not the same in prediction and solution inputs" |
| 39 | + |
| 40 | +# inferred pseudotime and spatial coordinates |
| 41 | +INFERRED_COL = "pseudotime_inferred" |
| 42 | +COORDS_KEY = "X_spatial" |
| 43 | + |
| 44 | +# spatial coordinates in the solution, the pseudotime in the prediction |
| 45 | +adata = ad.AnnData( |
| 46 | + obs=pd.DataFrame( |
| 47 | + {INFERRED_COL: input_prediction.obs[INFERRED_COL].values}, |
| 48 | + index=input_solution.obs_names, |
| 49 | + ), |
| 50 | + obsm={COORDS_KEY: np.asarray(input_solution.obsm[COORDS_KEY])}, |
| 51 | +) |
| 52 | + |
| 53 | +# generate results |
| 54 | +print('Compute metrics', flush=True) |
| 55 | +# metric_ids and metric_values can have length > 1 |
| 56 | +# but should be of equal length |
| 57 | + |
| 58 | +score = calc_morans_i(adata, INFERRED_COL, COORDS_KEY, n_neighbors=par['n_neighbors']) |
| 59 | + |
| 60 | +uns_metric_ids = [ 'morans_i' ] |
| 61 | +uns_metric_values = [ score ] |
| 62 | + |
| 63 | +# Write output data to file |
| 64 | +print("Write output AnnData to file...", flush=True) |
| 65 | + |
| 66 | +output = ad.AnnData( |
| 67 | + obs=pd.DataFrame(index=pd.Index(np.array([], dtype=str))), |
| 68 | + var=pd.DataFrame(index=pd.Index(np.array([], dtype=str))), |
| 69 | + uns={ |
| 70 | + 'dataset_id': input_solution.uns.get('dataset_id', 'unknown'), |
| 71 | + 'normalization_id': input_solution.uns.get('normalization_id', 'unknown'), |
| 72 | + 'method_id': input_prediction.uns.get('method_id', 'unknown'), |
| 73 | + 'metric_ids': uns_metric_ids, |
| 74 | + 'metric_values': uns_metric_values, |
| 75 | + } |
| 76 | +) |
| 77 | + |
| 78 | +output.write_h5ad(par['output'], compression='gzip') |
0 commit comments