-
Notifications
You must be signed in to change notification settings - Fork 1
/
figure_5.py
336 lines (269 loc) · 14.9 KB
/
figure_5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import numpy as np
import csv
import pickle
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.colors import rgb2hex # LinearSegmentedColormap, to_hex,
from scipy.sparse import csr_matrix
from collections import defaultdict
import pandas as pd
import gzip
import argparse
import os
import scipy.stats
from scipy.sparse.csgraph import connected_components
from pyvis.network import Network
import networkx as nx
from networkx.drawing.nx_agraph import write_dot
import altair as alt
import altairThemes # assuming you have altairThemes.py at your current directoy or your system knows the path of this altairThemes.py.
import gc
import copy
alt.themes.register("publishTheme", altairThemes.publishTheme)
# enable the newly registered theme
alt.themes.enable("publishTheme")
#current_directory = ??
##########################################################
# preprocessDf, plot: these two functions are taken from GW's repository /mnt/data0/gw/research/notta_pancreatic_cancer_visium/plots/fatema_signaling/hist.py
def preprocessDf(df):
"""Transform ligand and receptor columns."""
df["ligand-receptor"] = df["ligand"] + '-' + df["receptor"]
df["component"] = df["component"] #.astype(str).str.zfill(2)
return df
def plot(df):
set1 = altairThemes.get_colour_scheme("Set1", len(df["component"].unique()))
set1[0] = '#000000'
base = alt.Chart(df).mark_bar().encode(
x=alt.X("ligand-receptor:N", axis=alt.Axis(labelAngle=45), sort='-y'),
y=alt.Y("count()"),
color=alt.Color("component:N", scale = alt.Scale(range=set1)),
order=alt.Order("component:N", sort="ascending"),
tooltip=["component"]
)
p = base
return p
####################### Set the name of the sample you want to visualize ###################################
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# parser.add_argument( '--data_name', type=str, help='The name of dataset', required=True) #
# parser.add_argument( '--model_name', type=str, help='Name of the trained model', required=True)
parser.add_argument( '--top_edge_count', type=int, default=1500 ,help='Number of the top communications to plot. To plot all insert -1') #
# parser.add_argument( '--top_percent', type=int, default=20, help='Top N percentage communications to pick')
# parser.add_argument( '--metadata_from', type=str, default='metadata/', help='Path to grab the metadata')
# parser.add_argument( '--output_path', type=str, default='output/', help='Path to save the visualization results, e.g., histograms, graph etc.')
parser.add_argument( '--barcode_info_file', type=str, default='', help='Path to load the barcode information file produced during data preprocessing step')
parser.add_argument( '--annotation_file_path', type=str, default='', help='Path to load the annotation file in csv format (if available) ')
parser.add_argument( '--selfloop_info_file', type=str, default='', help='Path to load the selfloop information file produced during data preprocessing step')
parser.add_argument( '--top_ccc_file', type=str, default='', help='Path to load the selected top CCC file produced during data postprocessing step')
parser.add_argument( '--output_name', type=str, default='', help='Output file name prefix according to user\'s choice')
args = parser.parse_args()
output_name = args.output_name
##################### make cell metadata: barcode_info ###################################
with gzip.open(args.barcode_info_file, 'rb') as fp: #b, a:[0:5]
barcode_info = pickle.load(fp)
############################### read which spots have self loops ###############################################################
with gzip.open(args.selfloop_info_file, 'rb') as fp: #b, a:[0:5] _filtered
self_loop_found = pickle.load(fp)
####### load annotations ##############################################
annotation_data = pd.read_csv(args.annotation_file_path, sep=",")
pathologist_label=[]
for i in range (0, len(annotation_data)):
pathologist_label.append([annotation_data['Barcode'][i], annotation_data['Type'][i]])
barcode_type=dict() # record the type (annotation) of each spot (barcode)
for i in range (0, len(pathologist_label)):
barcode_type[pathologist_label[i][0]] = pathologist_label[i][1]
######################### read the NEST output in csv format ####################################################
inFile = args.top_ccc_file
df = pd.read_csv(inFile, sep=",")
#################################################################################################################
csv_record = df.values.tolist() # barcode_info[i][0], barcode_info[j][0], ligand, receptor, edge_rank, label, i, j, score
## sort the edges based on their rank (column 4), low to high, low being higher attention score
csv_record = sorted(csv_record, key = lambda x: x[4])
## add the column names and take first top_edge_count edges
# columns are: from_cell, to_cell, ligand_gene, receptor_gene, rank, attention_score, component, from_id, to_id
df_column_names = list(df.columns)
# print(df_column_names)
print(len(csv_record))
if args.top_edge_count != -1:
csv_record_final = [df_column_names] + csv_record [0:min(args.top_edge_count, len(csv_record))]
## add a dummy row at the end for the convenience of histogram preparation (to keep the color same as altair plot)
i=0
j=0
csv_record_final.append([barcode_info[i][0], barcode_info[j][0], 'no-ligand', 'no-receptor', 0, 0, i, j, 0]) # dummy for histogram
csv_record = 0
gc.collect()
######################## connected component finding #################################
print('Finding connected component')
connecting_edges = np.zeros((len(barcode_info),len(barcode_info)))
for k in range (1, len(csv_record_final)-1): # last record is a dummy for histogram preparation
i = csv_record_final[k][6]
j = csv_record_final[k][7]
connecting_edges[i][j]=1
graph = csr_matrix(connecting_edges)
n_components, labels = connected_components(csgraph=graph,directed=True, connection = 'weak', return_labels=True) # It assigns each SPOT to a component based on what pair it belongs to
print('Number of connected components %d'%n_components)
count_points_component = np.zeros((n_components))
for i in range (0, len(labels)):
count_points_component[labels[i]] = count_points_component[labels[i]] + 1
id_label = 2 # initially all are zero. =1 those who have self edge but above threshold. >= 2 who belong to some component
index_dict = dict()
for i in range (0, count_points_component.shape[0]):
if count_points_component[i]>1:
index_dict[i] = id_label
id_label = id_label+1
print('Unique component count %d'%id_label)
for i in range (0, len(barcode_info)):
if count_points_component[labels[i]] > 1:
barcode_info[i][3] = index_dict[labels[i]] #2
elif connecting_edges[i][i] == 1 and (i in self_loop_found and i in self_loop_found[i]): # that is: self_loop_found[i][i] do exist
barcode_info[i][3] = 1
else:
barcode_info[i][3] = 0
# update the label based on found component numbers
#max opacity
for record in range (1, len(csv_record_final)-1):
i = csv_record_final[record][6]
label = barcode_info[i][3]
csv_record_final[record][5] = label
#####################################
component_list = dict()
for record_idx in range (1, len(csv_record_final)-1): #last entry is a dummy for histograms, so ignore it.
record = csv_record_final[record_idx]
i = record[6]
j = record[7]
component_label = record[5]
barcode_info[i][3] = component_label #?
barcode_info[j][3] = component_label #?
component_list[component_label] = ''
component_list[0] = ''
unique_component_count = max(len(component_list.keys()), id_label)
##################################### Altair Plot ##################################################################
## dictionary of those spots who are participating in CCC ##
active_spot = defaultdict(list)
for record_idx in range (1, len(csv_record_final)-1): #last entry is a dummy for histograms, so ignore it.
record = csv_record_final[record_idx]
i = record[6]
pathology_label = barcode_type[barcode_info[i][0]]
component_label = record[5]
X = barcode_info[i][1]
Y = -barcode_info[i][2]
opacity = np.float(record[8])
active_spot[i].append([pathology_label, component_label, X, Y, opacity])
j = record[7]
pathology_label = barcode_type[barcode_info[j][0]]
component_label = record[5]
X = barcode_info[j][1]
Y = -barcode_info[j][2]
opacity = np.float(record[8])
active_spot[j].append([pathology_label, component_label, X, Y, opacity])
''''''
######### color the spots in the plot with opacity = attention score #################
opacity_list = []
for i in active_spot:
sum_opacity = []
for edges in active_spot[i]:
sum_opacity.append(edges[4])
avg_opacity = np.max(sum_opacity) #np.mean(sum_opacity)
opacity_list.append(avg_opacity)
active_spot[i]=[active_spot[i][0][0], active_spot[i][0][1], active_spot[i][0][2], active_spot[i][0][3], avg_opacity]
min_opacity = np.min(opacity_list)
max_opacity = np.max(opacity_list)
#### making dictionary for converting to pandas dataframe to draw altair plot ###########
data_list=dict()
data_list['pathology_label']=[]
data_list['component_label']=[]
data_list['X']=[]
data_list['Y']=[]
data_list['opacity']=[]
for i in range (0, len(barcode_info)):
if i in active_spot:
data_list['pathology_label'].append(active_spot[i][0])
data_list['component_label'].append(active_spot[i][1])
data_list['X'].append(active_spot[i][2])
data_list['Y'].append(active_spot[i][3])
data_list['opacity'].append((active_spot[i][4]-min_opacity)/(max_opacity-min_opacity))
else:
data_list['pathology_label'].append(barcode_type[barcode_info[i][0]])
data_list['component_label'].append(0) # make it zero so it is black
data_list['X'].append(barcode_info[i][1])
data_list['Y'].append(-barcode_info[i][2])
data_list['opacity'].append(0.1)
# barcode_info[i][3] = 0
# converting to pandas dataframe
data_list_pd = pd.DataFrame(data_list)
id_label = len(list(set(data_list['component_label']))) # unique_component_count
set1 = altairThemes.get_colour_scheme("Set1", id_label)
set1[0] = '#000000'
chart = alt.Chart(data_list_pd).mark_point(filled=True, opacity = 1).encode(
alt.X('X', scale=alt.Scale(zero=False)),
alt.Y('Y', scale=alt.Scale(zero=False)),
shape = alt.Shape('pathology_label:N'), #shape = "pathology_label",
color=alt.Color('component_label:N', scale=alt.Scale(range=set1)),
#opacity=alt.Opacity('opacity:N'), #"opacity",
tooltip=['component_label'] #,'opacity'
)
chart.save(output_name +'_altair_plot.html')
print('Altair plot generation done')
################################### Histogram plotting #################################################################################
df = pd.DataFrame(csv_record_final)
df.to_csv('temp_csv.csv', index=False, header=False)
df = pd.read_csv('temp_csv.csv', sep=",")
os.remove('temp_csv.csv') # delete the intermediate file
print('len of loaded csv for histogram generation is %d'%len(df))
df = preprocessDf(df)
p = plot(df)
outPath = output_name +'_histogram_test.html'
p.save(outPath)
print('Histogram plot generation done')
############################ Network/edge graph plot ######################
set1 = altairThemes.get_colour_scheme("Set1", unique_component_count)
colors = set1
colors[0] = '#000000' # black means no CCC
ids = []
x_index=[]
y_index=[]
colors_point = []
for i in range (0, len(barcode_info)):
ids.append(i)
x_index.append(barcode_info[i][1])
y_index.append(barcode_info[i][2])
colors_point.append(colors[barcode_info[i][3]])
max_x = np.max(x_index)
max_y = np.max(y_index)
g = nx.MultiDiGraph(directed=True)
for i in range (0, len(barcode_info)):
marker_size = 'circle'
label_str = str(i)+'_c:'+str(barcode_info[i][3]) # label of the node or spot is consists of: spot id, component number
if args.annotation_file_path != '':
label_str = label_str +'_'+ barcode_type[barcode_info[i][0]] # also add the type of the spot to the label if annotation is available
g.add_node(int(ids[i]), x=int(x_index[i]), y=int(y_index[i]), label = label_str, pos = str(x_index[i])+","+str(-y_index[i])+" !", physics=False, shape = marker_size, color=matplotlib.colors.rgb2hex(colors_point[i]))
# scale the edge scores [0 to 1] to make plot work
score_list = []
for k in range (1, len(csv_record_final)-1):
score_list.append(csv_record_final[k][8])
min_score = np.min(score_list)
max_score = np.max(score_list)
count_edges = 0
for k in range (1, len(csv_record_final)-1):
i = csv_record_final[k][6]
j = csv_record_final[k][7]
ligand = csv_record_final[k][2]
receptor = csv_record_final[k][3]
#if ligand=='CCL19' and receptor=='CCR7':
# print('CCL19-CCR7')
edge_score = csv_record_final[k][8]
edge_score = (edge_score-min_score)/(max_score-min_score)
title_str = "L:" + ligand + ", R:" + receptor+ ", "+ str(edge_score) #+
g.add_edge(int(i), int(j), label = title_str, color=colors_point[i], value=np.float64(edge_score)) #
count_edges = count_edges + 1
print("total edges plotted: %d"%count_edges)
nt = Network( directed=True, height='1000px', width='100%') #"500px", "500px",, filter_menu=True
nt.from_nx(g)
nt.save_graph(output_name +'_mygraph.html')
print('Edge graph plot generation done')
########################################################################
# convert it to dot file to be able to convert it to pdf or svg format for inserting into the paper
write_dot(g, output_name + "_test_interactive.dot")
print('dot file generation done')
print('All done')