<jupyter_start><jupyter_text>Playing around with pyvis to visualise cell-cell communication network<jupyter_code>import pandas as pd
import numpy as np
# playing around with pyvis to visualise cell-cell communication network
# reading in cellphone results table, abnormal vs healthy glomeruli ROIs
save_path = '/warehouse/team292_wh01/aa22/data/KidneyDataset/'
# cellphone results were produced only for glomeruli (abnormal and helathy), so excluding tubules here
df_output_downreg = pd.read_csv(save_path + '20210430_cellphone_interactions_table_with_gene_stats_downreg_in_abnormal_vs_healthy_no_logFC_cutoff_all.csv',
                               index_col=0)

df_output_downreg
df_output_upreg = pd.read_csv(save_path + '20210430_cellphone_interactions_table_with_gene_stats_upreg_in_abnormal_vs_healthy_no_logFC_cutoff_all.csv',
                               index_col=0)

df_output_upreg
# get the original cell type labels
path_data = '/warehouse/team292_wh01/aa22/data/KidneyDataset/'
Cell_Types_for_Spatial_Decon = pd.read_csv(path_data + 'Cell_Types_for_Spatial_Decon.txt',
                                          sep='\t')
np.unique(Cell_Types_for_Spatial_Decon['cell_type_general'])
# cell type deconvolution of the ROIs
stats = pd.read_csv('../../table_for_visualisation_ROIs.csv', index_col=0)
cols2keep = [elem for elem in stats.columns if ('ct_deconv_' in elem)]
stats = stats.loc[:,cols2keep]
stats
stats.columns
# get back to how the cell types were named in Cell_Types_for_Spatial_Decon
np.unique(Cell_Types_for_Spatial_Decon['cell_type_general'])
len(np.unique(Cell_Types_for_Spatial_Decon['cell_type_general']))
# get back to how the cell types were named in Cell_Types_for_Spatial_Decon
stats.columns = ['ct_deconv_B cell', 
                 'ct_deconv_CD8 T cell', 
                 'ct_deconv_DC',
                 'ct_deconv_Distal_tubules_and_collecting_duct', 
                 'ct_deconv_Endothelium',
                 'ct_deconv_Loop_of_Henle', 
                 'ct_deconv_MNP', 
                 'ct_deconv_Mast cell',
                 'ct_deconv_Mesangial_cells', 
                 'ct_deconv_NK cell', 
                 'ct_deconv_NKT cell',
                 'ct_deconv_Nephron_epithelium', 
                 'ct_deconv_Neutrophil',
                 'ct_deconv_Podocytes', 
                 'ct_deconv_Private',
                 'ct_deconv_Proximal_tubular_cells', 
                 'ct_deconv_Th cell']
# cellphone results were produced only for glomeruli (abnormal and helathy), so excluding tubules here
idx2keep = [elem for elem in stats.index if 'Geometric Segment' in elem]
len(idx2keep)
stats = stats.loc[idx2keep,:]
# (n_ROI (glomeruli), n_cell_types)
stats.shape
cell_types = [elem[10:] for elem in stats.columns]
cell_types
# make a dict of cell types present in each ROI
cell_types_in_ROI = {}

for ROI in stats.index:
    print(ROI)
    curr_df = pd.DataFrame(stats.loc[ROI,:])
    # cutoff for minimum presence in ROI
    curr_df = curr_df[curr_df[ROI] > 0.01]
    
    # get rid of 'ct_deconv_'
    curr_df.index = [elem[10:] for elem in curr_df.index]
    
    cell_types_in_ROI[ROI] = list(curr_df.index)
cell_types_in_ROI['disease3_scan | 007 | Geometric Segment']
# all possible cell type pairs, ignoring self pairs
import itertools
cell_type_pairs = list(itertools.combinations(cell_types, 2))
cell_type_pairs[:10]
len(cell_type_pairs)
pairs_coloc = []

for pair in cell_type_pairs:
    #print(pair)
    
    ct1 = pair[0]
    ct2 = pair[1]
    
    for ROI in stats.index:
        #print(ROI)
        if ct1 in cell_types_in_ROI[ROI] and ct2 in cell_types_in_ROI[ROI]:
            pairs_coloc.append(pair)
    
pairs_coloc = list(set(pairs_coloc))   
    
len(pairs_coloc)
pairs_coloc
# cell type pairs that are not colocated in any ROIs
cell_type_pairs_not_coloc = list(set(cell_type_pairs) - set(pairs_coloc))
cell_type_pairs_not_coloc
np.unique(stats[stats['ct_deconv_MNP'] > 0]['ct_deconv_NKT cell'])
df_output_upreg
# discard cellphone results for cell_type_pairs_not_coloc

idx_to_discard_upreg = []

for idx in df_output_upreg.index:
    curr_ct_A = df_output_upreg.loc[idx, 'celltype_A']
    curr_ct_B = df_output_upreg.loc[idx, 'celltype_B']
    
    for pair in cell_type_pairs_not_coloc:
        ct1 = pair[0]
        ct2 = pair[1]
        
        if (curr_ct_A == ct1 and curr_ct_B == ct2) or (curr_ct_A == ct2 and curr_ct_B == ct1):
            idx_to_discard_upreg.append(idx)
            
    
idx_to_keep_upreg = set(df_output_upreg.index) - set(idx_to_discard_upreg)
len(idx_to_keep_upreg)
df_output_upreg = df_output_upreg.loc[idx_to_keep_upreg,:]
df_output_upreg
len(pairs_coloc)
# adding self pairs
pairs_coloc_all = pairs_coloc + [(elem, elem) for elem in cell_types]
len(pairs_coloc_all)
# subset tables for each cell type pair, including self interactions

df_output_upreg_pair = {}

for pair in pairs_coloc_all:
    ct1 = pair[0]
    ct2 = pair[1]
    df_output_upreg_pair[pair] = df_output_upreg[((df_output_upreg['celltype_A'] == ct1) & (df_output_upreg['celltype_B'] == ct2))
                                                | ((df_output_upreg['celltype_B'] == ct1) & (df_output_upreg['celltype_A'] == ct2))]

df_output_upreg_pair[('B cell','B cell')]
df_output_upreg[df_output_upreg['celltype_A'] == 'Distal_tubules_and_collecting_duct']<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup