Skip to content

Annotate Visium HD with cell2location #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions scripts/visium_hd_brain/0_download_visium_HD_and_metadata.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Cell type metadata
wget https://allen-brain-cell-atlas.s3-us-west-2.amazonaws.com/metadata/WMB-10X/20231215/views/cell_metadata_with_cluster_annotation.csv

curl -O https://cf.10xgenomics.com/samples/spatial-exp/3.0.0/Visium_HD_Mouse_Brain/Visium_HD_Mouse_Brain_binned_outputs.tar.gz
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

module load R-bundle-Bioconductor/3.18-foss-2023a-R-4.3.2
cd $PBS_O_WORKDIR
Rscript --vanilla 2_downsample_ABA_meta.R
Rscript --vanilla 2_downsample_ABA_metadata.R
5 changes: 3 additions & 2 deletions scripts/visium_hd_brain/3_downsample_ABA_sc.pbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/bin/bash -l
#PBS -l nodes=1:ppn=7
#PBS -l nodes=1:ppn=5
#PBS -l mem=64gb
#PBS -l walltime=01:00:00
#PBS -e logs/
#PBS -o logs/

module load Single-cell-python-bundle/2024.0
module load Single-cell-python-bundle/2024.02-foss-2023a
cd $PBS_O_WORKDIR
python 3_downsample_ABA_sc.py ${PBS_ARRAYID}

Expand Down
10 changes: 10 additions & 0 deletions scripts/visium_hd_brain/4_prepare_sc_and_st.pbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash -l
#PBS -l nodes=1:ppn=1
#PBS -l walltime=00:20:00
#PBS -l mem=80gb
#PBS -e logs/
#PBS -o logs/

module load Single-cell-python-bundle/2024.02-foss-2023a
cd $PBS_O_WORKDIR
python 4_prepare_sc_and_st.py
58 changes: 58 additions & 0 deletions scripts/visium_hd_brain/4_prepare_sc_and_st.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os
import anndata as ad
import pandas as pd

# Define the path to the subset folder
subset_folder = os.environ['VSC_DATA_VO_USER'] + '/ABA_data/subset/'
output_file = os.environ['VSC_DATA_VO_USER'] + '/ABA_data/WMB-10Xv3_subset.h5ad'

# List all .h5ad files in the subset folder
subset_files = sorted([f for f in os.listdir(subset_folder) if f.endswith('.h5ad')])

# Initialize an empty list to hold the AnnData objects
adatas = []

# Read each .h5ad file and append to the list
for subset_file in subset_files:
file_path = os.path.join(subset_folder, subset_file)
adata = ad.read_h5ad(file_path)
adatas.append(adata)

# Concatenate all AnnData objects into a single AnnData object
combined_adata = ad.concat(adatas, join='outer', merge='same')

# Download metadata
downsampled_csv = os.environ['VSC_DATA_VO_USER'] + '/ABA_data/cell_metadata_with_cluster_annotation_downsampled.csv'
metadata = pd.read_csv(downsampled_csv)

# Ensure that the index of the metadata dataframe is the 'cell_label' column
metadata.set_index('cell_label', inplace=True)

# Identify overlapping columns
overlapping_columns = metadata.columns.intersection(combined_adata.obs.columns)
print(f"Overlapping columns: {overlapping_columns}")

# Rename overlapping columns in the metadata dataframe
metadata.rename(columns={col: f"{col}_metadata" for col in overlapping_columns}, inplace=True)

# Align the metadata indices with the obs_names of the AnnData object
metadata = metadata.loc[combined_adata.obs_names]

# Check if all indices are aligned
assert all(metadata.index == combined_adata.obs_names)

# Add metadata columns to the AnnData object
# Join using the indices
combined_adata.obs = combined_adata.obs.join(metadata)

# Save the combined AnnData object to a file
combined_adata.write(output_file)
print(f"Combined data saved to {output_file}")

##### VISIUM HD #####
import scanpy as sc
file_path = os.environ['VSC_DATA_VO_USER'] + '/spatialnichenet/Visium_HD_Mouse_Brain/square_008um/'

# Read anndata from 10x output
adata = sc.read_10x_h5(file_path + 'filtered_feature_bc_matrix.h5')
adata.write(os.environ['VSC_DATA_VO_USER'] + '/rds/Visium_HD_Mouse_Brain_008um.h5ad')
10 changes: 10 additions & 0 deletions scripts/visium_hd_brain/5_deconvolve.pbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash -l
#PBS -l nodes=1:ppn=1
#PBS -l walltime=04:00:00
#PBS -l mem=64gb
#PBS -e logs/
#PBS -o logs/

module load Single-cell-python-bundle/2024.02-foss-2023a
cd $PBS_O_WORKDIR
python 4_prepare_sc_and_st.py
6 changes: 6 additions & 0 deletions scripts/visium_hd_brain/5_deconvolve.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
nextflow run main.nf -profile hpc \
-params-file $VSC_DATA_VO_USER/spatial_ccc_experiments/scripts/visium_hd_brain/cell2location_params.yaml \
--mode run_dataset \
--sc_input $VSC_DATA_VO_USER/ABA_data/WMB-10Xv3_subset.h5ad \
--sp_input $VSC_DATA_VO_USER/rds/Visium_HD_Mouse_Brain_008um.h5ad \
--annot class --methods cell2location --skip_metrics --gpu
25 changes: 25 additions & 0 deletions scripts/visium_hd_brain/run_cell2location.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
params {
file_type = "h5ad"
sc_input = "/data/gent/vo/000/gvo00070/vsc43831/ABA_data/WMB-10Xv3_subset.h5ad"
sp_input = "/data/gent/vo/000/gvo00070/vsc43831/rds/Visium_HD_Mouse_Brain_008um.h5ad"
annot = "class"
methods = "cell2location"
sampleID = "donor_label"
verbose = true
skip_metrics = true
deconv_args = [cell2location: [build: "", fit:"-n 2"]]
gpu = true

}

process {
withName: buildCell2locationModel {
memory = { "${32 + (task.attempt * 32)} GB" }
time = { "2h" }
}

withName: fitCell2locationModel {
memory = { "${64 + (task.attempt * 32)} GB" }
time = { "${task.attempt * 4}h" }
}
}