|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Author_and_contribution: Niklas Mueller-Boetticher; created script, |
| 4 | +# Author_and_contribution: Qirong Mao; implemented method |
| 5 | + |
| 6 | + |
| 7 | +import argparse |
| 8 | + |
| 9 | +# TODO adjust description |
| 10 | +parser = argparse.ArgumentParser( |
| 11 | + description="Neighbor definition based on number of rings of neighbors (only for grid coordinates)" |
| 12 | +) |
| 13 | + |
| 14 | +parser.add_argument( |
| 15 | + "-c", "--coordinates", help="Path to coordinates (as tsv).", required=True |
| 16 | +) |
| 17 | + |
| 18 | +parser.add_argument( |
| 19 | + "-m", "--matrix", help="Path to (transformed) counts (as mtx).", required=True |
| 20 | +) |
| 21 | + |
| 22 | +parser.add_argument( |
| 23 | + "-f", "--features", help="Path to features (as tsv).", required=True |
| 24 | +) |
| 25 | + |
| 26 | +parser.add_argument( |
| 27 | + "-o", "--observations", help="Path to observations (as tsv).", required=True |
| 28 | +) |
| 29 | + |
| 30 | +parser.add_argument("-d", "--out_dir", help="Output directory.", required=True) |
| 31 | + |
| 32 | +parser.add_argument( |
| 33 | + "--config", |
| 34 | + help="Optional config file (json) used to pass additional parameters.", |
| 35 | + required=False, |
| 36 | +) |
| 37 | + |
| 38 | +args = parser.parse_args() |
| 39 | + |
| 40 | +# Output files |
| 41 | +from pathlib import Path |
| 42 | + |
| 43 | +out_dir = Path(args.out_dir) |
| 44 | + |
| 45 | +spatial_connectivities_file = out_dir / "spatial_connectivities.mtx" |
| 46 | +##spatial_distances_file = out_dir / "spatial_distances.mtx" |
| 47 | + |
| 48 | +# Use these filepaths and inputs ... |
| 49 | +coord_file = args.coordinates |
| 50 | +matrix_file = args.matrix |
| 51 | +feature_file = args.features |
| 52 | +observation_file = args.observations |
| 53 | + |
| 54 | + |
| 55 | +## Loading delaunay parameters from config_file |
| 56 | +if args.config is not None: |
| 57 | + config_file = args.config |
| 58 | + |
| 59 | +import json |
| 60 | + |
| 61 | +with open(config_file) as f: |
| 62 | + parameters = json.load(f) |
| 63 | + |
| 64 | +n_rings = parameters["n_rings"] |
| 65 | + |
| 66 | + |
| 67 | +# ... or AnnData if you want |
| 68 | +def get_anndata(args): |
| 69 | + # Untested template |
| 70 | + import anndata as ad |
| 71 | + import pandas as pd |
| 72 | + import scipy as sp |
| 73 | + |
| 74 | + X = sp.io.mmread(args.matrix) |
| 75 | + if sp.sparse.issparse(X): |
| 76 | + X = X.tocsr() |
| 77 | + observations = pd.read_table(args.observations, index_col=0) |
| 78 | + features = pd.read_table(args.features, index_col=0) |
| 79 | + coordinates = ( |
| 80 | + pd.read_table(args.coordinates, index_col=0) |
| 81 | + .loc[observations.index, :] |
| 82 | + .to_numpy() |
| 83 | + ) |
| 84 | + |
| 85 | + adata = ad.AnnData( |
| 86 | + X=X, obs=observations, var=features, obsm={"spatial": coordinates} |
| 87 | + ) |
| 88 | + |
| 89 | + return adata |
| 90 | + |
| 91 | + |
| 92 | +adata = get_anndata(args) |
| 93 | + |
| 94 | +## Your code goes here |
| 95 | +import squidpy as sq |
| 96 | + |
| 97 | +sq.gr.spatial_neighbors(adata,n_rings=n_rings, coord_type="grid") |
| 98 | + |
| 99 | +neighbors = adata.obsp["spatial_connectivities"].astype(int) |
| 100 | +##distance = adata.obsp["spatial_distances"].astype(float) |
| 101 | + |
| 102 | +## Write output |
| 103 | +import scipy as sp |
| 104 | + |
| 105 | +out_dir.mkdir(parents=True, exist_ok=True) |
| 106 | + |
| 107 | +sp.io.mmwrite(spatial_connectivities_file, neighbors) |
| 108 | +##sp.io.mmwrite(spatial_distances_file, distance) |
0 commit comments