|
23 | 23 | }, |
24 | 24 | { |
25 | 25 | "cell_type": "code", |
26 | | - "execution_count": null, |
| 26 | + "execution_count": 1, |
27 | 27 | "metadata": {}, |
28 | 28 | "outputs": [ |
29 | 29 | { |
|
64 | 64 | "sdata = sd.read_zarr(xenium_path_cropped)" |
65 | 65 | ] |
66 | 66 | }, |
| 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 | + }, |
67 | 88 | { |
68 | 89 | "cell_type": "markdown", |
69 | 90 | "metadata": {}, |
|
146 | 167 | "**PLOTTING FUNCTION 2 (troutpy.pl) Sorted scatterplot/barplot**" |
147 | 168 | ] |
148 | 169 | }, |
| 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 | + }, |
149 | 306 | { |
150 | 307 | "cell_type": "markdown", |
151 | 308 | "metadata": {}, |
|
714 | 871 | } |
715 | 872 | }, |
716 | 873 | "nbformat": 4, |
717 | | - "nbformat_minor": 2 |
| 874 | + "nbformat_minor": 4 |
718 | 875 | } |
0 commit comments