








import nico
from nico import Annotations as sann
from nico import Interactions as sint
from nico import Covariations as scov
import numpy as np
import os
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
# if you did not install the nico package and downloaded the nico related scripts into the current directory
#import Annotations as sann
#import Interactions as sint
#import Covariations as scov
#import scanpy as sc
#import gseapy
#import xlsxwriter
plt.rcParams['pdf.fonttype'] = 42
plt.rcParams['ps.fonttype'] = 42
plt.rcParams['axes.linewidth'] = 0.1 #set the value globally
# please use Helvetica font according to your OS to ensure compatibility with Adobe Illustrator.
plt.rcParams['font.family'] = 'Helvetica'
plt.rcParams['font.sans-serif'] = ['Helvetica']
# Use the default font for all the figures
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = ['Tahoma', 'DejaVu Sans','Lucida Grande', 'Verdana']
import warnings
warnings.filterwarnings("ignore")
#parameters for saving plots
saveas='png'
transparent_mode=False
dpi=300
showit=True
ref_datapath='./inputRef/'
query_datapath='./inputQuery/'
output_nico_dir='./nico_out/'
output_annotation_dir=None #uses default location
#output_annotation_dir=output_nico_dir+'annotations/'
annotation_save_fname= 'nico_celltype_annotation.h5ad'
inputRadius=0
ref_cluster_tag='cluster' #scRNAseq cell type slot
annotation_slot='nico_ct' #spatial cell type slot
####Perform cell type annotation of spatial data
anchors_and_neighbors_info=sann.find_anchor_cells_between_ref_and_query(
refpath=ref_datapath,
quepath=query_datapath,
output_nico_dir=output_nico_dir,
output_annotation_dir=output_annotation_dir)
output_info=sann.nico_based_annotation(anchors_and_neighbors_info,
guiding_spatial_cluster_resolution_tag='leiden0.4',
across_spatial_clusters_dispersion_cutoff=0.15,
ref_cluster_tag=ref_cluster_tag,
resolved_tie_issue_with_weighted_nearest_neighbor='No')
sann.delete_files(output_info)
sann.save_annotations_in_spatial_object(output_info,
anndata_object_name=annotation_save_fname)
####Annotations from different computational methods such cell2location or TACCO
####Visualize the spatial annotations of all cell types
sann.visualize_umap_and_cell_coordinates_with_all_celltypes(
output_nico_dir=output_nico_dir,
output_annotation_dir=output_annotation_dir,
anndata_object_name=annotation_save_fname,
#spatial_cluster_tag='nico_ct',
spatial_cluster_tag=annotation_slot,
spatial_coordinate_tag='spatial',
umap_tag='X_umap',
showit=showit,
saveas=saveas,transparent_mode=transparent_mode)
###Visualize spatial annotations of selected pairs (or larger sets) of cell types
choose_celltypes=[['Stem/TA','Paneth'],['Paneth','Goblet']]
sann.visualize_umap_and_cell_coordinates_with_selected_celltypes(
choose_celltypes=choose_celltypes,
output_nico_dir=output_nico_dir,
output_annotation_dir=output_annotation_dir,
anndata_object_name=annotation_save_fname,
spatial_cluster_tag=annotation_slot,spatial_coordinate_tag='spatial',
umap_tag='X_umap',
showit=showit,
saveas=saveas,transparent_mode=transparent_mode)
image

image
do_not_use_following_CT_in_niche=['Basophils','Cycling/GC B cell','pDC']
niche_pred_output=sint.spatial_neighborhood_analysis(
Radius=inputRadius,
output_nico_dir=output_nico_dir,
anndata_object_name=annotation_save_fname,
spatial_cluster_tag='nico_ct',
removed_CTs_before_finding_CT_CT_interactions=do_not_use_following_CT_in_niche)
# this cutoff is use for the visualization of cell type interactions network
celltype_niche_interaction_cutoff=0.1
###Plot the niche interaction network without any edge weight details for cutoff 0.1
sint.plot_niche_interactions_without_edge_weight(niche_pred_output,
niche_cutoff=celltype_niche_interaction_cutoff,
saveas=saveas,
transparent_mode=transparent_mode,
showit=showit,
figsize=(10,7),
dpi=dpi, #Resolution in dots per inch for saving the figure.
input_colormap='jet', #Colormap for node colors, from matplotlib colormaps.
with_labels=True, #Display cell type labels on the nodes, if True.
node_size=500, #Size of the nodes.
linewidths=0.5, #Width of the node border lines.
node_font_size=6, #Font size for node labels.
alpha=0.5, #Opacity level for nodes and edges. 1 is fully opaque, and 0 is fully transparent.
font_weight='bold' #Font weight for node labels; 'bold' for emphasis, 'normal' otherwise.
)
###Using edge weights included in the niche interaction plot can be done as shown below
sint.plot_niche_interactions_with_edge_weight(niche_pred_output,
niche_cutoff=celltype_niche_interaction_cutoff,
saveas=saveas,
transparent_mode=transparent_mode,
showit=showit,
figsize=(10,7),
dpi=dpi,
input_colormap='jet',
with_labels=True,
node_size=500,
linewidths=1,
node_font_size=8,
alpha=0.5,
font_weight='normal',
edge_label_pos=0.35, #Relative position of the weight label along the edge.
edge_font_size=3 #Font size for edge labels.
)
sint.find_interacting_cell_types(niche_pred_output,
choose_celltypes=['Stem/TA','Paneth'],
celltype_niche_interaction_cutoff=celltype_niche_interaction_cutoff,
saveas=saveas,transparent_mode=transparent_mode,dpi=dpi,showit=showit,figsize=(4.0,2.0))

###Plot the average confusion matrix of the classifier from cross-folds
sint.plot_confusion_matrix(niche_pred_output,
saveas=saveas,transparent_mode=transparent_mode,showit=showit,dpi=dpi)
cov_out=scov.gene_covariation_analysis(iNMFmode=True,
Radius=inputRadius,
no_of_factors=3,
refpath=ref_datapath,
quepath=query_datapath,
spatial_integration_modality='double',
output_niche_prediction_dir=output_nico_dir,
ref_cluster_tag=ref_cluster_tag,LRdbFilename='NiCoLRdb.txt')
scov.plot_cosine_and_spearman_correlation_to_factors(cov_out,
choose_celltypes=['Paneth'],
NOG_Fa=30,saveas=saveas,transparent_mode=transparent_mode,showit=showit,
figsize=(15,10),dpi=dpi)
# Cosine and spearman correlation: visualize the correlation of genes from NMF
scov.plot_cosine_and_spearman_correlation_to_factors(cov_out,
choose_celltypes=['Stem/TA'],
NOG_Fa=30,saveas=saveas,transparent_mode=transparent_mode,showit=showit,
figsize=(15,10),dpi=dpi)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。