|
23 | 23 | }, |
24 | 24 | { |
25 | 25 | "cell_type": "code", |
26 | | - "execution_count": 1, |
| 26 | + "execution_count": null, |
27 | 27 | "metadata": {}, |
28 | 28 | "outputs": [ |
29 | 29 | { |
|
439 | 439 | "source": [ |
440 | 440 | "import pandas as pd\n", |
441 | 441 | "\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'])" |
443 | 443 | ] |
444 | 444 | }, |
445 | 445 | { |
|
524 | 524 | } |
525 | 525 | ], |
526 | 526 | "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)" |
528 | 532 | ] |
529 | 533 | }, |
530 | 534 | { |
|
719 | 723 | } |
720 | 724 | ], |
721 | 725 | "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\")" |
725 | 732 | ] |
726 | 733 | }, |
727 | 734 | { |
|
832 | 839 | ], |
833 | 840 | "source": [ |
834 | 841 | "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", |
843 | 846 | ")" |
844 | 847 | ] |
845 | 848 | }, |
|
861 | 864 | ], |
862 | 865 | "source": [ |
863 | 866 | "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", |
872 | 870 | ")" |
873 | 871 | ] |
874 | 872 | }, |
|
909 | 907 | } |
910 | 908 | ], |
911 | 909 | "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)" |
915 | 914 | ] |
916 | 915 | }, |
917 | 916 | { |
|
929 | 928 | "source": [ |
930 | 929 | "import numpy as np\n", |
931 | 930 | "\n", |
| 931 | + "\n", |
932 | 932 | "def compute_projection_score(sdata):\n", |
933 | 933 | " \"\"\"\n", |
934 | 934 | " Compute a segmentation score for each cell based on the expression of genes weighted by their intracellular proportion (1 - extracellular proportion).\n", |
|
937 | 937 | " ----------\n", |
938 | 938 | " sdata : dict\n", |
939 | 939 | " 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", |
944 | 940 | "\n", |
945 | 941 | " Returns\n", |
946 | 942 | " -------\n", |
947 | 943 | " sdata : dict\n", |
948 | 944 | " The same sdata object with a new column 'segmentation_score' in sdata['table'].obs.\n", |
949 | 945 | " \"\"\"\n", |
950 | 946 | " # Retrieve the AnnData object with cells in .obs and genes in .var\n", |
951 | | - " adata = sdata[\"table\"]\n", |
| 947 | + " adata = sdata['table']\n", |
952 | 948 | "\n", |
953 | 949 | " # 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", |
955 | 951 | "\n", |
956 | 952 | " # If raw_expr is a sparse matrix, convert to a dense array\n", |
957 | 953 | " if hasattr(raw_expr, \"toarray\"):\n", |
|
961 | 957 | " genes = adata.var_names\n", |
962 | 958 | "\n", |
963 | 959 | " # Retrieve gene metadata containing the extracellular proportions\n", |
964 | | - " gene_meta = sdata[\"xrna_metadata\"].var\n", |
| 960 | + " gene_meta = sdata['xrna_metadata'].var\n", |
965 | 961 | "\n", |
966 | 962 | " # Identify the genes common to both the expression data and the metadata\n", |
967 | 963 | " common_genes = gene_meta.index.intersection(genes)\n", |
|
976 | 972 | " # Reorder gene_meta so that it matches the ordering in the expression data.\n", |
977 | 973 | " # This assumes that the order of genes in adata.var_names is the desired order.\n", |
978 | 974 | " 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", |
980 | 976 | "\n", |
981 | 977 | " # Convert extracellular proportion to intracellular weight (1 - extracellular proportion)\n", |
982 | 978 | " intracellular_weights = 1 - gene_weights.values # numpy array\n", |
|
991 | 987 | " score = np.divide(numerator, denominator, out=np.full_like(numerator, np.nan), where=denominator != 0)\n", |
992 | 988 | "\n", |
993 | 989 | " # Store the score in the AnnData object under obs\n", |
994 | | - " adata.obs[\"projection_score\"] = score\n", |
| 990 | + " adata.obs['projection_score'] = score\n", |
995 | 991 | "\n", |
996 | 992 | "# return sdata\n", |
997 | 993 | "\n", |
|
2882 | 2878 | "source": [ |
2883 | 2879 | "import scanpy as sc\n", |
2884 | 2880 | "\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')" |
2886 | 2882 | ] |
2887 | 2883 | }, |
2888 | 2884 | { |
|
2891 | 2887 | "metadata": {}, |
2892 | 2888 | "outputs": [], |
2893 | 2889 | "source": [ |
| 2890 | + "\n", |
2894 | 2891 | "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", |
2896 | 2894 | "\n", |
2897 | 2895 | " Parameters\n", |
2898 | 2896 | " ----------\n", |
2899 | 2897 | " sdata : dict\n", |
2900 | 2898 | " 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", |
2905 | 2899 | "\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", |
2909 | 2902 | "\n", |
2910 | 2903 | " Returns\n", |
2911 | 2904 | " -------\n", |
2912 | 2905 | " 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", |
2915 | 2908 | " \"\"\"\n", |
2916 | 2909 | " # 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", |
2919 | 2912 | "\n", |
2920 | 2913 | " # Convert to dense if necessary\n", |
2921 | 2914 | " if hasattr(raw_expr, \"toarray\"):\n", |
2922 | 2915 | " raw_expr = raw_expr.toarray()\n", |
2923 | 2916 | "\n", |
2924 | 2917 | " # Get gene names from AnnData and the corresponding gene metadata\n", |
2925 | 2918 | " genes = adata.var_names\n", |
2926 | | - " gene_meta = sdata[\"xrna_metadata\"].var\n", |
| 2919 | + " gene_meta = sdata['xrna_metadata'].var\n", |
2927 | 2920 | "\n", |
2928 | 2921 | " # Find common genes between the expression data and metadata\n", |
2929 | 2922 | " common_genes = gene_meta.index.intersection(genes)\n", |
|
2936 | 2929 | "\n", |
2937 | 2930 | " # Reorder the gene metadata to match the ordering in the expression data\n", |
2938 | 2931 | " 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", |
2940 | 2933 | "\n", |
2941 | 2934 | " # Create a boolean mask for genes that are extracellularly enriched (above threshold)\n", |
2942 | 2935 | " enriched_mask = extracellular_props > threshold\n", |
|
2951 | 2944 | " enriched_count = (expressed & enriched_mask).sum(axis=1)\n", |
2952 | 2945 | "\n", |
2953 | 2946 | " # 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", |
2955 | 2953 | "\n", |
2956 | 2954 | " # Store the computed metric in the AnnData object under .obs\n", |
2957 | 2955 | " adata.obs[\"extracellularly_enriched_proportion\"] = proportion" |
|
3023 | 3021 | "import scanpy as sc\n", |
3024 | 3022 | "\n", |
3025 | 3023 | "# 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" |
3027 | 3026 | ] |
3028 | 3027 | }, |
3029 | 3028 | { |
|
3036 | 3035 | ], |
3037 | 3036 | "metadata": { |
3038 | 3037 | "kernelspec": { |
3039 | | - "display_name": "exrna", |
| 3038 | + "display_name": "Python 3 (ipykernel)", |
3040 | 3039 | "language": "python", |
3041 | 3040 | "name": "python3" |
3042 | 3041 | }, |
|
3050 | 3049 | "name": "python", |
3051 | 3050 | "nbconvert_exporter": "python", |
3052 | 3051 | "pygments_lexer": "ipython3", |
3053 | | - "version": "3.10.15" |
| 3052 | + "version": "3.10.16" |
3054 | 3053 | } |
3055 | 3054 | }, |
3056 | 3055 | "nbformat": 4, |
3057 | | - "nbformat_minor": 2 |
| 3056 | + "nbformat_minor": 4 |
3058 | 3057 | } |
0 commit comments