Skip to content

Commit 71f819a

Browse files
author
Sergio Salas
committed
ruff_mods
coomit
1 parent def4cec commit 71f819a

43 files changed

Lines changed: 7907 additions & 1045 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
{
2525
"cell_type": "code",
26-
"execution_count": 1,
26+
"execution_count": null,
2727
"metadata": {},
2828
"outputs": [
2929
{
@@ -439,7 +439,7 @@
439439
"source": [
440440
"import pandas as pd\n",
441441
"\n",
442-
"pd.crosstab(sdata[\"xrna_metadata\"].var[\"p_value_Poisson\"] < 0.00005, sdata[\"xrna_metadata\"].var[\"control_probe\"])"
442+
"pd.crosstab(sdata['xrna_metadata'].var['p_value_Poisson']<0.00005,sdata['xrna_metadata'].var['control_probe'])"
443443
]
444444
},
445445
{
@@ -524,7 +524,11 @@
524524
}
525525
],
526526
"source": [
527-
"troutpy.pl.metric_scatter(sdata, x_metric=\"proportion_of_colocalized\", y_metric=\"extracellular_proportion\", label_top_n_x=3, label_top_n_y=3)"
527+
528+
"troutpy.pl.metric_scatter(sdata,\n",
529+
"x_metric=\"proportion_of_colocalized\",\n",
530+
"y_metric=\"extracellular_proportion\",\n",
531+
"label_top_n_x=3,label_top_n_y=3)"
528532
]
529533
},
530534
{
@@ -719,9 +723,12 @@
719723
}
720724
],
721725
"source": [
722-
"troutpy.pl.global_distribution_from_source(\n",
723-
" sdata, cluster_key=\"kmeans_distribution\", feature_key=\"feature_name\", distance_key=\"distance\", n_bins=40, how=\"collapsed\"\n",
724-
")"
726+
"troutpy.pl.global_distribution_from_source(sdata,\n",
727+
" cluster_key= \"kmeans_distribution\",\n",
728+
" feature_key = \"feature_name\",\n",
729+
" distance_key= \"distance\",\n",
730+
" n_bins= 40,\n",
731+
" how= \"collapsed\")"
725732
]
726733
},
727734
{
@@ -832,14 +839,10 @@
832839
],
833840
"source": [
834841
"troutpy.pl.intra_extra_density(\n",
835-
" sdata,\n",
836-
" bottom_genes,\n",
837-
" layer=\"transcripts\",\n",
838-
" gene_key=\"feature_name\",\n",
839-
" coord_keys=[\"x\", \"y\"],\n",
840-
" intra_kde_kwargs={\"fill\": True, \"cmap\": \"Blues\", \"thresh\": 0.05, \"bw_adjust\": 0.2},\n",
841-
" extra_kde_kwargs={\"fill\": True, \"cmap\": \"Reds\", \"thresh\": 0.05, \"bw_adjust\": 0.2},\n",
842-
" figsize=(5, 7),\n",
842+
843+
" sdata, bottom_genes, layer=\"transcripts\", gene_key=\"feature_name\", coord_keys=[\"x\", \"y\"],\n",
844+
" intra_kde_kwargs = {\"fill\": True, \"cmap\": \"Blues\", \"thresh\": 0.05,\"bw_adjust\":0.2},\n",
845+
" extra_kde_kwargs = {\"fill\": True, \"cmap\": \"Reds\", \"thresh\": 0.05,\"bw_adjust\":0.2},figsize=(5,7)\n",
843846
")"
844847
]
845848
},
@@ -861,14 +864,9 @@
861864
],
862865
"source": [
863866
"troutpy.pl.intra_extra_density(\n",
864-
" sdata,\n",
865-
" top_genes,\n",
866-
" layer=\"transcripts\",\n",
867-
" gene_key=\"feature_name\",\n",
868-
" coord_keys=[\"x\", \"y\"],\n",
869-
" intra_kde_kwargs={\"fill\": True, \"cmap\": \"Blues\", \"thresh\": 0.05, \"bw_adjust\": 0.2},\n",
870-
" extra_kde_kwargs={\"fill\": True, \"cmap\": \"Reds\", \"thresh\": 0.05, \"bw_adjust\": 0.2},\n",
871-
" figsize=(5, 7),\n",
867+
" sdata, top_genes, layer=\"transcripts\", gene_key=\"feature_name\", coord_keys=[\"x\", \"y\"],\n",
868+
" intra_kde_kwargs = {\"fill\": True, \"cmap\": \"Blues\", \"thresh\": 0.05,\"bw_adjust\":0.2},\n",
869+
" extra_kde_kwargs = {\"fill\": True, \"cmap\": \"Reds\", \"thresh\": 0.05,\"bw_adjust\":0.2},figsize=(5,7)\n",
872870
")"
873871
]
874872
},
@@ -909,9 +907,10 @@
909907
}
910908
],
911909
"source": [
912-
"troutpy.pl.metric_scatter(\n",
913-
" sdata, x_metric=\"spatial_density_correlation\", y_metric=\"mean_displacement\", label_top_n_x=3, label_top_n_y=3, label_bottom_n_x=5\n",
914-
")"
910+
"troutpy.pl.metric_scatter(sdata,\n",
911+
"x_metric=\"spatial_density_correlation\",\n",
912+
"y_metric=\"mean_displacement\",\n",
913+
"label_top_n_x=3,label_top_n_y=3,label_bottom_n_x=5)"
915914
]
916915
},
917916
{
@@ -929,6 +928,7 @@
929928
"source": [
930929
"import numpy as np\n",
931930
"\n",
931+
"\n",
932932
"def compute_projection_score(sdata):\n",
933933
" \"\"\"\n",
934934
" Compute a segmentation score for each cell based on the expression of genes weighted by their intracellular proportion (1 - extracellular proportion).\n",
@@ -937,21 +937,17 @@
937937
" ----------\n",
938938
" sdata : dict\n",
939939
" A spatialdata object with keys 'table' and 'xrna_metadata'.\n",
940-
" - sdata['table'] is an AnnData object containing expression data in layers['raw']\n",
941-
" and cell metadata in .obs.\n",
942-
" - sdata['xrna_metadata'].var is a DataFrame with gene names as the index and\n",
943-
" an 'extracellular proportion' column.\n",
944940
"\n",
945941
" Returns\n",
946942
" -------\n",
947943
" sdata : dict\n",
948944
" The same sdata object with a new column 'segmentation_score' in sdata['table'].obs.\n",
949945
" \"\"\"\n",
950946
" # Retrieve the AnnData object with cells in .obs and genes in .var\n",
951-
" adata = sdata[\"table\"]\n",
947+
" adata = sdata['table']\n",
952948
"\n",
953949
" # Retrieve raw expression data; assume shape (n_cells, n_genes)\n",
954-
" raw_expr = adata.layers[\"raw\"]\n",
950+
" raw_expr = adata.layers['raw']\n",
955951
"\n",
956952
" # If raw_expr is a sparse matrix, convert to a dense array\n",
957953
" if hasattr(raw_expr, \"toarray\"):\n",
@@ -961,7 +957,7 @@
961957
" genes = adata.var_names\n",
962958
"\n",
963959
" # Retrieve gene metadata containing the extracellular proportions\n",
964-
" gene_meta = sdata[\"xrna_metadata\"].var\n",
960+
" gene_meta = sdata['xrna_metadata'].var\n",
965961
"\n",
966962
" # Identify the genes common to both the expression data and the metadata\n",
967963
" common_genes = gene_meta.index.intersection(genes)\n",
@@ -976,7 +972,7 @@
976972
" # Reorder gene_meta so that it matches the ordering in the expression data.\n",
977973
" # This assumes that the order of genes in adata.var_names is the desired order.\n",
978974
" ordered_genes = [gene for gene in genes if gene in common_genes]\n",
979-
" gene_weights = gene_meta.loc[ordered_genes, \"extracellular_proportion\"]\n",
975+
" gene_weights = gene_meta.loc[ordered_genes, 'extracellular_proportion']\n",
980976
"\n",
981977
" # Convert extracellular proportion to intracellular weight (1 - extracellular proportion)\n",
982978
" intracellular_weights = 1 - gene_weights.values # numpy array\n",
@@ -991,7 +987,7 @@
991987
" score = np.divide(numerator, denominator, out=np.full_like(numerator, np.nan), where=denominator != 0)\n",
992988
"\n",
993989
" # Store the score in the AnnData object under obs\n",
994-
" adata.obs[\"projection_score\"] = score\n",
990+
" adata.obs['projection_score'] = score\n",
995991
"\n",
996992
"# return sdata\n",
997993
"\n",
@@ -2882,7 +2878,7 @@
28822878
"source": [
28832879
"import scanpy as sc\n",
28842880
"\n",
2885-
"sc.pl.umap(sdata[\"table\"], color=[\"projection_score\", \"cell_type\", \"total_counts\"], vmax=\"p99.997\")"
2881+
"sc.pl.umap(sdata['table'],color=['projection_score','cell_type','total_counts'],vmax='p99.997')"
28862882
]
28872883
},
28882884
{
@@ -2891,39 +2887,36 @@
28912887
"metadata": {},
28922888
"outputs": [],
28932889
"source": [
2890+
"\n",
28942891
"def proportion_of_extracellularly_enriched_genes(sdata, threshold=0.5):\n",
2895-
" \"\"\"For cell, compute the proportion of expressed genes that are extracellularly enriched, i.e. whose extracellular transcript proportion is above a given threshold.\n",
2892+
" \"\"\"\n",
2893+
" For each cell, compute the proportion of expressed genes that are extracellularly enriched, i.e. whose extracellular transcript proportion is above a given threshold.\n",
28962894
"\n",
28972895
" Parameters\n",
28982896
" ----------\n",
28992897
" sdata : dict\n",
29002898
" A spatialdata object with keys 'table' and 'xrna_metadata'.\n",
2901-
" - sdata['table'] is an AnnData object containing expression data in layers['raw']\n",
2902-
" and cell metadata in .obs.\n",
2903-
" - sdata['xrna_metadata'].var is a DataFrame with gene names as the index and\n",
2904-
" an 'extracellular_proportion' column.\n",
29052899
"\n",
2906-
" threshold : float, default=0.5\n",
2907-
" The minimum extracellular proportion required for a gene to be considered\n",
2908-
" \"extracellularly enriched\".\n",
2900+
" threshold : float\n",
2901+
" The minimum extracellular proportion required for a gene to be considered \"extracellularly enriched\".\n",
29092902
"\n",
29102903
" Returns\n",
29112904
" -------\n",
29122905
" sdata : dict\n",
2913-
" The same sdata object with a new column 'extracellularly_enriched_proportion'\n",
2914-
" in sdata['table'].obs, which contains the computed metric for each cell.\n",
2906+
" The same sdata object with a new column 'extracellularly_enriched_proportion' in sdata['table'].obs, which contains the computed metric for each cell.\n",
2907+
"\n",
29152908
" \"\"\"\n",
29162909
" # Retrieve the AnnData object and raw expression data\n",
2917-
" adata = sdata[\"table\"]\n",
2918-
" raw_expr = adata.layers[\"raw\"]\n",
2910+
" adata = sdata['table']\n",
2911+
" raw_expr = adata.layers['raw']\n",
29192912
"\n",
29202913
" # Convert to dense if necessary\n",
29212914
" if hasattr(raw_expr, \"toarray\"):\n",
29222915
" raw_expr = raw_expr.toarray()\n",
29232916
"\n",
29242917
" # Get gene names from AnnData and the corresponding gene metadata\n",
29252918
" genes = adata.var_names\n",
2926-
" gene_meta = sdata[\"xrna_metadata\"].var\n",
2919+
" gene_meta = sdata['xrna_metadata'].var\n",
29272920
"\n",
29282921
" # Find common genes between the expression data and metadata\n",
29292922
" common_genes = gene_meta.index.intersection(genes)\n",
@@ -2936,7 +2929,7 @@
29362929
"\n",
29372930
" # Reorder the gene metadata to match the ordering in the expression data\n",
29382931
" ordered_genes = [gene for gene in genes if gene in common_genes]\n",
2939-
" extracellular_props = gene_meta.loc[ordered_genes, \"extracellular_proportion\"].values\n",
2932+
" extracellular_props = gene_meta.loc[ordered_genes, 'extracellular_proportion'].values\n",
29402933
"\n",
29412934
" # Create a boolean mask for genes that are extracellularly enriched (above threshold)\n",
29422935
" enriched_mask = extracellular_props > threshold\n",
@@ -2951,7 +2944,12 @@
29512944
" enriched_count = (expressed & enriched_mask).sum(axis=1)\n",
29522945
"\n",
29532946
" # Compute the proportion (handling potential division by zero)\n",
2954-
" proportion = np.divide(enriched_count, expressed_count, out=np.full_like(enriched_count, np.nan, dtype=float), where=expressed_count != 0)\n",
2947+
" proportion = np.divide(\n",
2948+
" enriched_count,\n",
2949+
" expressed_count,\n",
2950+
" out=np.full_like(enriched_count, np.nan, dtype=float),\n",
2951+
" where=expressed_count != 0\n",
2952+
" )\n",
29552953
"\n",
29562954
" # Store the computed metric in the AnnData object under .obs\n",
29572955
" adata.obs[\"extracellularly_enriched_proportion\"] = proportion"
@@ -3023,7 +3021,8 @@
30233021
"import scanpy as sc\n",
30243022
"\n",
30253023
"# Ensure your AnnData object (sdata['table']) has a 'cell type' annotation in obs.\n",
3026-
"sc.pl.dotplot(sdata[\"table\"], extranscripts, groupby=\"cell_type\", standard_scale=\"var\", title=\"Extracellularly Enriched Genes by Cell Type\")"
3024+
"sc.pl.dotplot(sdata['table'], extranscripts, groupby='cell_type',\n",
3025+
" standard_scale='var', title='Extracellularly Enriched Genes by Cell Type')\n"
30273026
]
30283027
},
30293028
{
@@ -3036,7 +3035,7 @@
30363035
],
30373036
"metadata": {
30383037
"kernelspec": {
3039-
"display_name": "exrna",
3038+
"display_name": "Python 3 (ipykernel)",
30403039
"language": "python",
30413040
"name": "python3"
30423041
},
@@ -3050,9 +3049,9 @@
30503049
"name": "python",
30513050
"nbconvert_exporter": "python",
30523051
"pygments_lexer": "ipython3",
3053-
"version": "3.10.15"
3052+
"version": "3.10.16"
30543053
}
30553054
},
30563055
"nbformat": 4,
3057-
"nbformat_minor": 2
3056+
"nbformat_minor": 4
30583057
}

0 commit comments

Comments
 (0)