Step4: Visualization
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
# Hyperparameter
InputFolderName = "./MERFISH_Brain_KNNgraph_Input/"
# Import graph index.
GraphIndex_filename = "./Run1/GraphIdx.csv"
graph_index = np.loadtxt(GraphIndex_filename, dtype='int', delimiter="\t")
# Import region name list.
Region_filename = InputFolderName + "ImageNameList.txt"
region_name_list = pd.read_csv(
Region_filename,
sep="\t", # tab-separated
header=None, # no heading row
names=["Image"], # set our own names for the columns
)
# Import target graph x/y coordinates.
region_name = region_name_list.Image[graph_index]
GraphCoord_filename = InputFolderName + region_name + "_Coordinates.txt"
x_y_coordinates = pd.read_csv(
GraphCoord_filename,
sep="\t", # tab-separated
header=None, # no heading row
names=["x_coordinate", "y_coordinate"], # set our own names for the columns
)
target_graph_map = x_y_coordinates
#target_graph_map["y_coordinate"] = 0 - target_graph_map["y_coordinate"] # for consistent with original paper. Don't do this is also ok.
# Import cell type label.
CellType_filename = InputFolderName + region_name + "_CellTypeLabel.txt"
cell_type_label = pd.read_csv(
CellType_filename,
sep="\t", # tab-separated
header=None, # no heading row
names=["cell_type"], # set our own names for the columns
)
# Add cell type labels to target graph x/y coordinates.
target_graph_map["CellType"] = cell_type_label.cell_type
#!!! Add consensus cluster labels to target graph x/y coordinates.
target_graph_map["PredictedLabel"] = np.loadtxt("ConsensusLabel_MajorityVoting.csv", dtype='int', delimiter=",")
# Converting integer list to string list for making color scheme discrete.
target_graph_map.PredictedLabel = target_graph_map.PredictedLabel.astype(str)
#-----------------------------------------Generate plots-------------------------------------------------#
# Plot x/y map with "TCN" coloring. Note that consensus clustering result is generated by R with 1-indexing.
dict_color_TCN = {"1": "#5a3b1c", "2": "#939396", "3": "#2c663b", "4": "#d63189", \
"5": "#54a9dd", "6": "#813188", "7": "Orange", "8": "#231f1f", "9": "#912b61"}
TCN_MajorityVoting_fig = sns.lmplot(x="x_coordinate", y="y_coordinate", data=target_graph_map, fit_reg=False, hue='PredictedLabel', legend=False, palette=dict_color_TCN, scatter_kws={"s": 6.0})
TCN_MajorityVoting_fig.set(xticks=[]) #remove ticks and also tick labels.
TCN_MajorityVoting_fig.set(yticks=[])
TCN_MajorityVoting_fig.set(xlabel=None) #remove axis label.
TCN_MajorityVoting_fig.set(ylabel=None)
TCN_MajorityVoting_fig.despine(left=True, bottom=True) #remove x(bottom) and y(left) axis.
TCN_MajorityVoting_fig.add_legend(label_order = ["5", "2", "4", "9", "7", "3", "6", "1", "8"])
for lh in TCN_MajorityVoting_fig._legend.legendHandles:
#lh.set_alpha(1)
lh._sizes = [15] # You can also use lh.set_sizes([15])
#plt.show()
# Save the figure.
TCN_fig_filename = "./TCN_MajorityVoting.pdf"
TCN_MajorityVoting_fig.savefig(TCN_fig_filename)
# Plot x/y map with "CellType" coloring.
dict_color_CellType = {"Astrocyte": "#4daf4a", "Endothelial": "#984ea3", "Ependymal": "#ff7f00", "Excitatory": "#e41a1c", \
"Inhibitory": "#377eb8", "Microglia": "#ffff33", "OD Immature": "#f781bf", "OD Mature": "#a65628", "Pericytes": "#999999"}
CellType_fig = sns.lmplot(x="x_coordinate", y="y_coordinate", data=target_graph_map, fit_reg=False, hue='CellType', legend=False, palette=dict_color_CellType, scatter_kws={"s": 6.0})
CellType_fig.set(xticks=[]) #remove ticks and also tick labels.
CellType_fig.set(yticks=[])
CellType_fig.set(xlabel=None) #remove axis label.
CellType_fig.set(ylabel=None)
CellType_fig.despine(left=True, bottom=True) #remove x(bottom) and y(left) axis.
CellType_fig.add_legend(label_order = ["Astrocyte", "Endothelial", "Ependymal", \
"Excitatory", "Inhibitory", "Microglia", "OD Immature", "OD Mature", "Pericytes"])
for lh in CellType_fig._legend.legendHandles:
#lh.set_alpha(1)
lh._sizes = [15] # You can also use lh.set_sizes([15])
# Save the figure.
CellType_fig_filename = "./CellType.pdf"
CellType_fig.savefig(CellType_fig_filename)
# Export dataframe: "target_graph_map".
TargetGraph_dataframe_filename = "./TargetGraphDF.csv"
target_graph_map.to_csv(TargetGraph_dataframe_filename, na_rep="NULL", index=False) #remove row index.
Output
After this step, we will obtain a single-cell saptial map colored by identified TCNs.
── TCN_MajorityVoting.pdf
── TargetGraphDF.csv
── CellType.pdf
TCN plot:
Cell type plot: