













import torch
import numpy as np
import scanpy as sc
import SpaHDmap as hdmap
rank = 20
seed = 123
verbose = True
np.random.seed(seed)
torch.manual_seed(seed)
root_path = '../experiments/'
project = 'MPBS01'
results_path = f'{root_path}/{project}/Results_Rank{rank}/'
section_id = 'V1_Mouse_Brain_Sagittal_Posterior'
# Download the data from the 10X website (set include_hires_tiff=True to download the hires image)
adata = sc.datasets.visium_sge(section_id, include_hires_tiff=True)
image_path = adata.uns["spatial"][section_id]["metadata"]["source_image_path"]
# or load the data from a local folder
# adata = sc.read_visium(f'data/{section_id}', count_file='filtered_feature_bc_matrix.h5')
# image_path = f'data/{section_id}/image.tif'
# Load the data from the 10X Visium folder
mouse_posterior = hdmap.prepare_stdata(adata=adata,
section_name='mouse_posterior',
image_path=image_path,
scale_rate=1)
hdmap.select_svgs(mouse_posterior, n_top_genes=3000, method='moran')
mouse_posterior.show()
# Load the data from file paths
mouse_posterior = hdmap.prepare_stdata(section_name='mouse_posterior',
image_path='data/V1_Mouse_Brain_Sagittal_Posterior/image.tif',
spot_coord_path='data/V1_Mouse_Brain_Sagittal_Posterior/spatial/tissue_positions_list.csv',
spot_exp_path='data/V1_Mouse_Brain_Sagittal_Posterior/filtered_feature_bc_matrix.h5',
scale_rate=1,
radius=45) # Has to be provided if load from file paths
hdmap.select_svgs(mouse_posterior, n_top_genes=3000, method='moran')
###Run SpaHDmap
# Initialize the SpaHDmap runner
mapper = hdmap.Mapper(mouse_posterior, results_path=results_path, rank=rank, verbose=verbose)
# Run all steps in one function
mapper.run_SpaHDmap(save_score=False, save_model=True, load_model=True, visualize=True, format='png')
# If you want to lower the GPU memory usage, you can set `batch_size` to a smaller number
# mapper.args.batch_size = 16 (default 32)
# mapper.run_SpaHDmap(save_score=False, save_model=True, visualize=False)
# Run NMF on the gene expression data
mapper.get_NMF_score(save_score=False)
print(mouse_posterior.scores['NMF'].shape)
mapper.visualize(mouse_posterior, use_score='NMF', index=2)
# mapper.visualize('mouse_posterior', use_score='NMF', index=2) # visualize given the name
# mapper.visualize(use_score='NMF', index=2) # ignore the section name if only one section
# Save all NMF scores into `results_path/section_name/NMF`
mapper.visualize(use_score='NMF', format='png') # default is 'png'
# Pre-train the SpaHDmap model via reconstructing the HE image
mapper.pretrain(save_model=True)
# Train the GCN model and get GCN score
mapper.get_GCN_score(save_score=False)
print(mouse_posterior.scores['GCN'].shape)
# Visualize the GCN score
mapper.visualize(mouse_posterior, use_score='GCN', index=2)
# Save all GCN scores into `results_path/section_name/GCN`
mapper.visualize(use_score='GCN', format='png')
# The refined metagene matrix based on the GCN score
print(mapper.metagene_GCN.shape)
# Get the VD score
mapper.get_VD_score(use_score='GCN')
# Train the SpaHDmap model
# If the model has been trained and saved, mapper will load the model directly
mapper.train(save_model=True, load_model=True)
# Get the SpaHDmap score
mapper.get_SpaHDmap_score(save_score=False)
print(mouse_posterior.scores['SpaHDmap'].shape)
# Visualize the SpaHDmap score
mapper.visualize(mouse_posterior, use_score='SpaHDmap', index=2)
mapper.visualize(use_score='SpaHDmap')
# Load the STData object
mouse_posterior = hdmap.prepare_stdata(st_path=(results_path + 'mouse_posterior.st'))
# Initialize the SpaHDmap runner with the loaded STData object
mapper = hdmap.Mapper(mouse_posterior, results_path=results_path, rank=rank, verbose=verbose)
# Cluster pixels based on the SpaHDmap score
mapper.cluster(use_score='SpaHDmap', resolution=0.8, show=True)
# Visualize the clustering results of SpaHDmap
mapper.visualize(target='cluster', use_score='SpaHDmap', format='pdf')
mouse_posterior.genes[:5]
# Load the metagene matrix from the results path
mapper.load_metagene()
# Recover and visualize the high-resolution gene expression for a specific gene
mapper.recovery(gene=mouse_posterior.genes[:5], use_score='SpaHDmap')
mapper.visualize(gene='Pcp2')
mapper.visualize(gene='Mbp')
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。