Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 16, 2024
1 parent 145b24c commit bd05749
Show file tree
Hide file tree
Showing 29 changed files with 1,546 additions and 1,308 deletions.
21 changes: 10 additions & 11 deletions notebooks/spatialdata_tutorials/0_format_Xenium_sdata.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"metadata": {},
"outputs": [],
"source": [
"import spatialdata_io\n",
"import spatialdata as sd"
"import spatialdata as sd\n",
"import spatialdata_io"
]
},
{
Expand All @@ -39,8 +39,8 @@
}
],
"source": [
"path='/media/sergio/Discovair_final/Xenium_Prime_Mouse_Brain_Coronal_FF_outs'\n",
"sdata=spatialdata_io.xenium(path)"
"path = \"/media/sergio/Discovair_final/Xenium_Prime_Mouse_Brain_Coronal_FF_outs\"\n",
"sdata = spatialdata_io.xenium(path)"
]
},
{
Expand All @@ -66,7 +66,7 @@
}
],
"source": [
"outpath='/media/sergio/Discovair_final/mousebrain_prime.zarr'\n",
"outpath = \"/media/sergio/Discovair_final/mousebrain_prime.zarr\"\n",
"sdata.write(outpath)"
]
},
Expand Down Expand Up @@ -130,7 +130,7 @@
}
],
"source": [
"xenium_path='/media/sergio/Discovair_final/mousebrain_prime.zarr'\n",
"xenium_path = \"/media/sergio/Discovair_final/mousebrain_prime.zarr\"\n",
"sdata = sd.read_zarr(xenium_path)\n",
"sdata"
]
Expand Down Expand Up @@ -179,7 +179,7 @@
" axes=[\"x\", \"y\"],\n",
" min_coordinate=[17500, 0],\n",
" max_coordinate=[35000, 15000],\n",
" target_coordinate_system='global',\n",
" target_coordinate_system=\"global\",\n",
")\n",
"\n",
"cropped_sdata"
Expand Down Expand Up @@ -209,8 +209,7 @@
}
],
"source": [
"import spatialdata_plot\n",
"cropped_sdata.pl.render_images(\"morphology_focus\").pl.show( title=\"Morphology image\")"
"cropped_sdata.pl.render_images(\"morphology_focus\").pl.show(title=\"Morphology image\")"
]
},
{
Expand All @@ -232,8 +231,8 @@
}
],
"source": [
"xenium_path_cropped='/media/sergio/Discovair_final/mousebrain_prime_half.zarr'\n",
"cropped_sdata.write(xenium_path_cropped,overwrite=True)"
"xenium_path_cropped = \"/media/sergio/Discovair_final/mousebrain_prime_half.zarr\"\n",
"cropped_sdata.write(xenium_path_cropped, overwrite=True)"
]
}
],
Expand Down
141 changes: 72 additions & 69 deletions notebooks/spatialdata_tutorials/10_apply_sc_signatures_to_exrna.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,23 @@
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import numpy as np\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"# while not pip installable, add path to file\n",
"import sys\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import spatialdata_io\n",
"import spatialdata as sd\n",
"import pandas as pd\n",
"import scanpy as sc\n",
"import spatialdata as sd\n",
"\n",
"# while not pip installable, add path to file \n",
"import sys \n",
"sys.path.append(\"../../exrna\")\n",
"from formatting import *\n",
"from quantify_overexpression import *\n",
"from spatial_variability import *\n",
"from compute_target import *\n",
"from plotting import *\n",
"from compute_source_cell import *\n",
"from compute_target import *\n",
"from estimate_density import *\n",
"from NMF import *"
"from formatting import *\n",
"from NMF import *\n",
"from plotting import *\n",
"from quantify_overexpression import *\n",
"from spatial_variability import *"
]
},
{
Expand Down Expand Up @@ -54,9 +50,9 @@
}
],
"source": [
"xenium_path_cropped='/media/sergio/Discovair_final/mousebrain_prime_crop_points2regions_annotated.zarr'\n",
"output_path='/media/sergio/Discovair_final/analysis_crop'\n",
"sdata=sd.read_zarr(xenium_path_cropped)"
"xenium_path_cropped = \"/media/sergio/Discovair_final/mousebrain_prime_crop_points2regions_annotated.zarr\"\n",
"output_path = \"/media/sergio/Discovair_final/analysis_crop\"\n",
"sdata = sd.read_zarr(xenium_path_cropped)"
]
},
{
Expand All @@ -81,8 +77,8 @@
}
],
"source": [
"key='cell type'\n",
"sdata['table'].X=sdata['table'].X.todense()"
"key = \"cell type\"\n",
"sdata[\"table\"].X = sdata[\"table\"].X.todense()"
]
},
{
Expand All @@ -100,9 +96,9 @@
}
],
"source": [
"exp=sdata['table'].to_df()\n",
"exp[key]=sdata['table'].obs[key]\n",
"meansignature=exp.groupby(key).mean()\n"
"exp = sdata[\"table\"].to_df()\n",
"exp[key] = sdata[\"table\"].obs[key]\n",
"meansignature = exp.groupby(key).mean()"
]
},
{
Expand All @@ -111,8 +107,8 @@
"metadata": {},
"outputs": [],
"source": [
"layer='segmentation_free_table'\n",
"adata_extracellular=sdata[layer]"
"layer = \"segmentation_free_table\"\n",
"adata_extracellular = sdata[layer]"
]
},
{
Expand All @@ -125,7 +121,7 @@
"genes_extracellular = adata_extracellular.var_names\n",
"\n",
"# Get intersection of genes between the two datasets\n",
"common_genes = genes_cells.intersection(genes_extracellular)\n"
"common_genes = genes_cells.intersection(genes_extracellular)"
]
},
{
Expand Down Expand Up @@ -535,10 +531,10 @@
"metadata": {},
"outputs": [],
"source": [
"exp=pd.DataFrame(sdata['table'].layers['raw'].todense(),columns=sdata['table'].var.index)\n",
"exp[key]=list(sdata['table'].obs[key])\n",
"meansignature=exp.groupby(key).mean()\n",
"meansignature=meansignature.div(meansignature.sum(axis=0),axis=1)"
"exp = pd.DataFrame(sdata[\"table\"].layers[\"raw\"].todense(), columns=sdata[\"table\"].var.index)\n",
"exp[key] = list(sdata[\"table\"].obs[key])\n",
"meansignature = exp.groupby(key).mean()\n",
"meansignature = meansignature.div(meansignature.sum(axis=0), axis=1)"
]
},
{
Expand All @@ -547,12 +543,12 @@
"metadata": {},
"outputs": [],
"source": [
"def apply_exrnaH_to_cellular_to_create_cellularW(sdata,key='cell type',layer='segmentation_free_table'):\n",
" adata_extracellular=sdata[layer]\n",
" exp=pd.DataFrame(sdata['table'].layers['raw'].todense(),columns=sdata['table'].var.index)\n",
" exp[key]=list(sdata['table'].obs[key])\n",
" meansignature=exp.groupby(key).mean()\n",
" meansignature=meansignature.div(meansignature.sum(axis=0),axis=1)\n",
"def apply_exrnaH_to_cellular_to_create_cellularW(sdata, key=\"cell type\", layer=\"segmentation_free_table\"):\n",
" adata_extracellular = sdata[layer]\n",
" exp = pd.DataFrame(sdata[\"table\"].layers[\"raw\"].todense(), columns=sdata[\"table\"].var.index)\n",
" exp[key] = list(sdata[\"table\"].obs[key])\n",
" meansignature = exp.groupby(key).mean()\n",
" meansignature = meansignature.div(meansignature.sum(axis=0), axis=1)\n",
"\n",
" # Check the number of genes in adata_annotated and spots2region_output to match gene loadings (H)\n",
" genes_cells = meansignature.columns\n",
Expand All @@ -564,17 +560,19 @@
" # Filter both datasets to keep only common genes\n",
" adata_extracellular = adata_extracellular[:, common_genes]\n",
" H_filtered = meansignature.loc[:, common_genes] # Filtered NMF gene loadings for common genes\n",
" #H_filtered=H_filtered.loc[:,common_genes]\n",
" # H_filtered=H_filtered.loc[:,common_genes]\n",
" # Apply the NMF factors to the annotated dataset\n",
" # Calculate the new W matrix by multiplying the annotated data with the filtered H\n",
" W_annotated = adata_extracellular.X @ H_filtered.T\n",
" adata_extracellular.obsm['factors']=pd.DataFrame(W_annotated,columns=meansignature.index,index=adata_extracellular.obs.index)\n",
" #print(W_annotated[:, 0].shape)\n",
" adata_extracellular.obsm[\"factors\"] = pd.DataFrame(\n",
" W_annotated, columns=meansignature.index, index=adata_extracellular.obs.index\n",
" )\n",
" # print(W_annotated[:, 0].shape)\n",
" # Add the factors as new columns in adata_annotated.obs\n",
" #for factor in range(W_annotated.shape[1]):\n",
" # for factor in range(W_annotated.shape[1]):\n",
" # adata_annotated_cellular.obs[f'NMF_factor_{factor + 1}'] = W_annotated[:, factor]\n",
" #sdata['table']=adata_annotated_cellular\n",
" return adata_extracellular\n"
" # sdata['table']=adata_annotated_cellular\n",
" return adata_extracellular"
]
},
{
Expand All @@ -583,14 +581,13 @@
"metadata": {},
"outputs": [],
"source": [
"def apply_exrnaH_to_cellular_to_create_cellularW(sdata, key='cell type', layer='segmentation_free_table'):\n",
"def apply_exrnaH_to_cellular_to_create_cellularW(sdata, key=\"cell type\", layer=\"segmentation_free_table\"):\n",
" # Extracting extracellular and expression data\n",
" adata_extracellular = sdata[layer]\n",
" exp=pd.DataFrame(sdata['table'].layers['raw'].todense(),columns=sdata['table'].var.index)\n",
" exp[key]=list(sdata['table'].obs[key])\n",
" meansignature=exp.groupby(key).mean()\n",
" meansignature=meansignature.div(meansignature.sum(axis=0),axis=1)\n",
"\n",
" exp = pd.DataFrame(sdata[\"table\"].layers[\"raw\"].todense(), columns=sdata[\"table\"].var.index)\n",
" exp[key] = list(sdata[\"table\"].obs[key])\n",
" meansignature = exp.groupby(key).mean()\n",
" meansignature = meansignature.div(meansignature.sum(axis=0), axis=1)\n",
"\n",
" # Checking for matching genes\n",
" genes_cells = meansignature.columns\n",
Expand All @@ -599,16 +596,18 @@
"\n",
" if len(common_genes) == 0:\n",
" raise ValueError(\"No common genes found between cellular and extracellular datasets.\")\n",
" \n",
"\n",
" # Filter datasets to keep only common genes\n",
" adata_extracellular = adata_extracellular[:, common_genes]\n",
" H_filtered = meansignature.loc[:, common_genes]\n",
"\n",
" # Calculate the new W matrix\n",
" W_annotated = adata_extracellular.X @ H_filtered.T\n",
" adata_extracellular.obsm['factors'] = pd.DataFrame(W_annotated, columns=meansignature.index, index=adata_extracellular.obs.index)\n",
" \n",
" return adata_extracellular\n"
" adata_extracellular.obsm[\"factors\"] = pd.DataFrame(\n",
" W_annotated, columns=meansignature.index, index=adata_extracellular.obs.index\n",
" )\n",
"\n",
" return adata_extracellular"
]
},
{
Expand All @@ -626,7 +625,9 @@
}
],
"source": [
"adata_extracellular=apply_exrnaH_to_cellular_to_create_cellularW(sdata,key='cell type',layer='segmentation_free_table')"
"adata_extracellular = apply_exrnaH_to_cellular_to_create_cellularW(\n",
" sdata, key=\"cell type\", layer=\"segmentation_free_table\"\n",
")"
]
},
{
Expand All @@ -635,9 +636,9 @@
"metadata": {},
"outputs": [],
"source": [
"layer='extracellular_transcripts'\n",
"feature_key='feature_name'\n",
"bin_key='bin_id'"
"layer = \"extracellular_transcripts\"\n",
"feature_key = \"feature_name\"\n",
"bin_key = \"bin_id\""
]
},
{
Expand All @@ -646,8 +647,8 @@
"metadata": {},
"outputs": [],
"source": [
"adata_scores=sc.AnnData(adata_extracellular.obsm['factors'],obs=adata_extracellular.obs)\n",
"adata_scores.obsm['spatial']=adata_extracellular.obsm['spatial']"
"adata_scores = sc.AnnData(adata_extracellular.obsm[\"factors\"], obs=adata_extracellular.obs)\n",
"adata_scores.obsm[\"spatial\"] = adata_extracellular.obsm[\"spatial\"]"
]
},
{
Expand All @@ -656,7 +657,7 @@
"metadata": {},
"outputs": [],
"source": [
"sc.pp.subsample(adata_scores,0.1)"
"sc.pp.subsample(adata_scores, 0.1)"
]
},
{
Expand All @@ -665,8 +666,8 @@
"metadata": {},
"outputs": [],
"source": [
"adata_scores.X=adata_scores.X.astype(float)\n",
"adata_scores.obs.index=adata_scores.obs.index.astype(int)"
"adata_scores.X = adata_scores.X.astype(float)\n",
"adata_scores.obs.index = adata_scores.obs.index.astype(int)"
]
},
{
Expand All @@ -675,8 +676,8 @@
"metadata": {},
"outputs": [],
"source": [
"adata_scores.obs['x']=[i[0] for i in adata_scores.obsm['spatial']]\n",
"adata_scores.obs['y']=[i[1] for i in adata_scores.obsm['spatial']]"
"adata_scores.obs[\"x\"] = [i[0] for i in adata_scores.obsm[\"spatial\"]]\n",
"adata_scores.obs[\"y\"] = [i[1] for i in adata_scores.obsm[\"spatial\"]]"
]
},
{
Expand Down Expand Up @@ -848,9 +849,9 @@
],
"source": [
"for c in adata_scores.var.index:\n",
" plt.scatter(adata_scores.obs['x'],adata_scores.obs['y'],c=adata_scores.to_df()[c],s=0.1,cmap='YlGnBu',vmax=5)\n",
" plt.title(c)\n",
" plt.show()\n"
" plt.scatter(adata_scores.obs[\"x\"], adata_scores.obs[\"y\"], c=adata_scores.to_df()[c], s=0.1, cmap=\"YlGnBu\", vmax=5)\n",
" plt.title(c)\n",
" plt.show()"
]
},
{
Expand All @@ -870,7 +871,9 @@
}
],
"source": [
"plot_nmf_factors_exrna_genes_H(sdata['nmf_data'], vmin=0.0, vmax=0.02,saving_path=output_path,save=False,figsize=(4,5))"
"plot_nmf_factors_exrna_genes_H(\n",
" sdata[\"nmf_data\"], vmin=0.0, vmax=0.02, saving_path=output_path, save=False, figsize=(4, 5)\n",
")"
]
},
{
Expand All @@ -895,7 +898,7 @@
}
],
"source": [
"sdata = apply_exrnaH_to_cellular_to_create_cellularW(sdata,layer_factors='nmf_data')"
"sdata = apply_exrnaH_to_cellular_to_create_cellularW(sdata, layer_factors=\"nmf_data\")"
]
},
{
Expand All @@ -904,7 +907,7 @@
"metadata": {},
"outputs": [],
"source": [
"sdata['nmf_data']"
"sdata[\"nmf_data\"]"
]
},
{
Expand Down Expand Up @@ -1014,7 +1017,7 @@
}
],
"source": [
"plot_nmf_factors(sdata,figsize=(7,7),n_factors=10)"
"plot_nmf_factors(sdata, figsize=(7, 7), n_factors=10)"
]
},
{
Expand Down
Loading

0 comments on commit bd05749

Please sign in to comment.