Skip to content

Commit def4cec

Browse files
Merge pull request #15 from theislab/sergio_dev
Adding cell scores functions
2 parents 0200ae6 + af35ae1 commit def4cec

24 files changed

Lines changed: 9203 additions & 481 deletions

notebooks/spatialdata_tutorials/.ipynb_checkpoints/4_quantify_exRNA-checkpoint.ipynb

Lines changed: 3058 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/spatialdata_tutorials/.ipynb_checkpoints/5_explore_comunication-checkpoint.ipynb

Lines changed: 718 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/spatialdata_tutorials/.ipynb_checkpoints/run_sainsc_4_campa-checkpoint.ipynb

Lines changed: 1736 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/spatialdata_tutorials/2_run_segmentation_free.ipynb

Lines changed: 441 additions & 20 deletions
Large diffs are not rendered by default.

notebooks/spatialdata_tutorials/4_quantify_exRNA.ipynb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,6 @@
929929
"source": [
930930
"import numpy as np\n",
931931
"\n",
932-
"\n",
933932
"def compute_projection_score(sdata):\n",
934933
" \"\"\"\n",
935934
" Compute a segmentation score for each cell based on the expression of genes weighted by their intracellular proportion (1 - extracellular proportion).\n",
@@ -994,7 +993,6 @@
994993
" # Store the score in the AnnData object under obs\n",
995994
" adata.obs[\"projection_score\"] = score\n",
996995
"\n",
997-
"\n",
998996
"# return sdata\n",
999997
"\n",
1000998
"# Example usage:\n",
@@ -2959,6 +2957,13 @@
29592957
" adata.obs[\"extracellularly_enriched_proportion\"] = proportion"
29602958
]
29612959
},
2960+
{
2961+
"cell_type": "code",
2962+
"execution_count": null,
2963+
"metadata": {},
2964+
"outputs": [],
2965+
"source": []
2966+
},
29622967
{
29632968
"cell_type": "code",
29642969
"execution_count": 210,
@@ -3031,7 +3036,7 @@
30313036
],
30323037
"metadata": {
30333038
"kernelspec": {
3034-
"display_name": "exrna",
3039+
"display_name": "Python 3 (ipykernel)",
30353040
"language": "python",
30363041
"name": "python3"
30373042
},
@@ -3045,9 +3050,9 @@
30453050
"name": "python",
30463051
"nbconvert_exporter": "python",
30473052
"pygments_lexer": "ipython3",
3048-
"version": "3.10.15"
3053+
"version": "3.10.16"
30493054
}
30503055
},
30513056
"nbformat": 4,
3052-
"nbformat_minor": 2
3057+
"nbformat_minor": 4
30533058
}

notebooks/spatialdata_tutorials/5_explore_comunication.ipynb

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
{
2525
"cell_type": "code",
26-
"execution_count": null,
26+
"execution_count": 1,
2727
"metadata": {},
2828
"outputs": [
2929
{
@@ -64,6 +64,27 @@
6464
"sdata = sd.read_zarr(xenium_path_cropped)"
6565
]
6666
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"metadata": {},
71+
"outputs": [],
72+
"source": []
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": []
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"metadata": {},
85+
"outputs": [],
86+
"source": []
87+
},
6788
{
6889
"cell_type": "markdown",
6990
"metadata": {},
@@ -146,6 +167,142 @@
146167
"**PLOTTING FUNCTION 2 (troutpy.pl) Sorted scatterplot/barplot**"
147168
]
148169
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": null,
173+
"metadata": {},
174+
"outputs": [],
175+
"source": [
176+
"# Compute contribution source score"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": null,
182+
"metadata": {},
183+
"outputs": [],
184+
"source": [
185+
"import numpy as np\n",
186+
"\n",
187+
"def compute_projection_score(sdata):\n",
188+
" \"\"\"\n",
189+
" Compute a segmentation score for each cell based on the expression of genes weighted by their intracellular proportion (1 - extracellular proportion).\n",
190+
"\n",
191+
" Parameters\n",
192+
" ----------\n",
193+
" sdata : dict\n",
194+
" A spatialdata object with keys 'table' and 'xrna_metadata'.\n",
195+
" - sdata['table'] is an AnnData object containing expression data in layers['raw']\n",
196+
" and cell metadata in .obs.\n",
197+
" - sdata['xrna_metadata'].var is a DataFrame with gene names as the index and\n",
198+
" an 'extracellular proportion' column.\n",
199+
"\n",
200+
" Returns\n",
201+
" -------\n",
202+
" sdata : dict\n",
203+
" The same sdata object with a new column 'segmentation_score' in sdata['table'].obs.\n",
204+
" \"\"\"\n",
205+
" # Retrieve the AnnData object with cells in .obs and genes in .var\n",
206+
" adata = sdata[\"table\"]\n",
207+
"\n",
208+
" # Retrieve raw expression data; assume shape (n_cells, n_genes)\n",
209+
" raw_expr = adata.layers[\"raw\"]\n",
210+
"\n",
211+
" # If raw_expr is a sparse matrix, convert to a dense array\n",
212+
" if hasattr(raw_expr, \"toarray\"):\n",
213+
" raw_expr = raw_expr.toarray()\n",
214+
"\n",
215+
" # Get gene names from the AnnData object\n",
216+
" genes = adata.var_names\n",
217+
"\n",
218+
" # Retrieve gene metadata containing the extracellular proportions\n",
219+
" gene_meta = sdata[\"xrna_metadata\"].var\n",
220+
"\n",
221+
" # Identify the genes common to both the expression data and the metadata\n",
222+
" common_genes = gene_meta.index.intersection(genes)\n",
223+
" if len(common_genes) == 0:\n",
224+
" raise ValueError(\"No common genes found between adata and gene metadata.\")\n",
225+
"\n",
226+
" # Subset the expression matrix to only those common genes\n",
227+
" # Here we assume adata.var_names preserves order; get indices corresponding to common genes.\n",
228+
" common_idx = [i for i, gene in enumerate(genes) if gene in common_genes]\n",
229+
" raw_expr = raw_expr[:, common_idx]\n",
230+
"\n",
231+
" # Reorder gene_meta so that it matches the ordering in the expression data.\n",
232+
" # This assumes that the order of genes in adata.var_names is the desired order.\n",
233+
" ordered_genes = [gene for gene in genes if gene in common_genes]\n",
234+
" gene_weights = gene_meta.loc[ordered_genes, \"extracellular_proportion\"]\n",
235+
"\n",
236+
" # Convert extracellular proportion to intracellular weight (1 - extracellular proportion)\n",
237+
" intracellular_weights = 1 - gene_weights.values # numpy array\n",
238+
"\n",
239+
" # Compute the numerator and denominator for the weighted average per cell.\n",
240+
" # Numerator: dot product of cell expression with intracellular weights.\n",
241+
" # Denominator: total expression (for the common genes) per cell.\n",
242+
" numerator = raw_expr.dot(intracellular_weights)\n",
243+
" denominator = raw_expr.sum(axis=1)\n",
244+
"\n",
245+
" # Avoid division by zero (if a cell has zero expression for these genes)\n",
246+
" score = np.divide(numerator, denominator, out=np.full_like(numerator, np.nan), where=denominator != 0)\n",
247+
"\n",
248+
" # Store the score in the AnnData object under obs\n",
249+
" adata.obs[\"projection_score\"] = score\n",
250+
"\n",
251+
"# return sdata\n",
252+
"\n",
253+
"# Example usage:\n",
254+
"# sdata = compute_segmentation_score(sdata)"
255+
]
256+
},
257+
{
258+
"cell_type": "code",
259+
"execution_count": null,
260+
"metadata": {},
261+
"outputs": [],
262+
"source": []
263+
},
264+
{
265+
"cell_type": "code",
266+
"execution_count": null,
267+
"metadata": {},
268+
"outputs": [],
269+
"source": []
270+
},
271+
{
272+
"cell_type": "code",
273+
"execution_count": null,
274+
"metadata": {},
275+
"outputs": [],
276+
"source": []
277+
},
278+
{
279+
"cell_type": "code",
280+
"execution_count": null,
281+
"metadata": {},
282+
"outputs": [],
283+
"source": []
284+
},
285+
{
286+
"cell_type": "code",
287+
"execution_count": null,
288+
"metadata": {},
289+
"outputs": [],
290+
"source": []
291+
},
292+
{
293+
"cell_type": "code",
294+
"execution_count": null,
295+
"metadata": {},
296+
"outputs": [],
297+
"source": []
298+
},
299+
{
300+
"cell_type": "code",
301+
"execution_count": null,
302+
"metadata": {},
303+
"outputs": [],
304+
"source": []
305+
},
149306
{
150307
"cell_type": "markdown",
151308
"metadata": {},
@@ -714,5 +871,5 @@
714871
}
715872
},
716873
"nbformat": 4,
717-
"nbformat_minor": 2
874+
"nbformat_minor": 4
718875
}

0 commit comments

Comments
 (0)