-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTD_Louvain_partial_rankedgsea_weighted.py
executable file
·374 lines (334 loc) · 20.7 KB
/
CTD_Louvain_partial_rankedgsea_weighted.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import pandas as pd
import networkx as nx
import numpy as np
import pickle as pk
import gseapy as gp
#Let's import the chemical-gene interactions from CTD (downloaded on 5th April 2021)
chem_gene_df = pd.read_csv("input/CTD/CTD_chem_gene_ixns.tsv",delimiter= '\t', skipinitialspace=True)
#Here, we filter for only the interactions that regards the H. Sapiens
chem_homo = chem_gene_df[(chem_gene_df['Organism'] == 'Homo sapiens')]
#Let's import the network
backbone_ss_exposure_network = nx.read_weighted_edgelist("output/backbone_exp_graph_significant_weighted.edgelist")
#This cells create a dictionary where each key is a chemical compound and the correspondent value is a genelist
chem_gene = {}
for i,v in chem_homo.iterrows():
try:
chem_gene[v["ChemicalID"]] |= {v["GeneSymbol"]}
except KeyError as e:
chem_gene[v["ChemicalID"]] = set([v["GeneSymbol"]])
#Here, we keep only the exposures which perturb at least one gene
chem_gene_cleaned = {}
tot_gene_list=[]
for k,v in chem_gene.items():
if len(v)>0:
chem_gene_cleaned[k]=v
for gene in v:
tot_gene_list.append(gene)
else:
pass
with open('output/Communities/Louvain/weighted_ji_fine_grained_communities_dict.pickle', 'rb') as handle:
ee_first_louvain_iteration_exposures = pk.load(handle)
with open('output/Communities/Louvain/weighted_ji_middle_communities_dict.pickle', 'rb') as handle:
ee_second_louvain_iteration_exposures = pk.load(handle)
with open('output/Communities/Louvain/weighted_ji_broad_communities_dict.pickle', 'rb') as handle:
ee_third_louvain_iteration_exposures = pk.load(handle)
def enr_df_dict(gene_list): #This function returns the enrichment df for GOBP,GOCC,GOMF,KeGG
libraries=['GO_Biological_Process_2023']
c = 0
while c<10:
try:
geneset_to_enrich_GOBP_df = gp.enrichr(gene_list=gene_list,
gene_sets=libraries,
organism='human',
outdir=None,
)
break
except (NameError, AttributeError):
c += 1
libraries=['GO_Molecular_Function_2023']
c = 0
while c<10:
try:
geneset_to_enrich_GOMF_df = gp.enrichr(gene_list=gene_list,
gene_sets=libraries,
organism='human',
outdir=None,
)
break
except (NameError, AttributeError):
c += 1
libraries=['GO_Cellular_Component_2023']
c = 0
while c<10:
try:
geneset_to_enrich_GOCC_df = gp.enrichr(gene_list=gene_list,
gene_sets=libraries,
organism='human',
outdir=None,
)
break
except (NameError, AttributeError):
c += 1
libraries=['KEGG_2021_Human']
c = 0
while c<10:
try:
geneset_to_enrich_KEGG_df = gp.enrichr(gene_list=gene_list,
gene_sets=libraries,
organism='human',
outdir=None,
)
break
except (NameError, AttributeError):
c += 1
enrdf_dict={}
enrdf_dict['KEGG']=geneset_to_enrich_KEGG_df
enrdf_dict['GOBP']=geneset_to_enrich_GOBP_df
enrdf_dict['GOMF']=geneset_to_enrich_GOMF_df
enrdf_dict['GOCC']=geneset_to_enrich_GOCC_df
return enrdf_dict
def enr_fdr_list(enrdf_dict):
sig_dict={}
for lib, enr_df in enrdf_dict.items():
lib_significant_terms=[]
for i,v in enr_df.res2d.iterrows():
if v['Adjusted P-value']<0.05:
lib_significant_terms.append(v['Term'])
sig_dict[lib]=lib_significant_terms
return sig_dict
def enr_ranking_leading_genes(pre_rank_df,library_list): #This function returns the leading genes
leading_genes_tot=[]
for lib in library_list:
c=0
while c<10:
try:
lib_pre_res = gp.prerank(rnk=pre_rank_df, gene_sets=lib,
processes=4,min_size=3,max_size=5000,outdir=None, #we impose at least 5 enriched common genes
permutation_num=500) # reduce number to speed up testing
leading_gene_lib_list=[]
for gene_str in lib_pre_res.res2d['Lead_genes'].tolist(): #lib_pre_res.res2d['Lead_genes'] if in the cluster
leading_gene_lib_list.extend(gene_str.split(";"))
leading_genes_tot.extend(leading_gene_lib_list)
if 'Lead_genes' in lib_pre_res.res2d:
break
except (NameError, AttributeError):
c += 1
return set(leading_genes_tot)
#Here, we define the list of gene libraries for which we perform the biological enrichment
lib_list=['GO_Biological_Process_2023','GO_Molecular_Function_2023','GO_Cellular_Component_2023','KEGG_2021_Human']
#I consider a pre-rank for making a choice on the gene cutoff
#defining for each comunity a set of genes that is well-representative of each cluster
from collections import Counter
pre_rank_first_louvain_cluster_genelist={}
for com,explist in ee_first_louvain_iteration_exposures.items():
gene_list=[]
rank_genelist=[]
if len(explist)>1:
G_sub = nx.subgraph(backbone_ss_exposure_network,explist) #let's create the subgraph corresponding to the cluster
for edge in G_sub.edges():
overlap_geneset=chem_gene_cleaned[edge[0]] & chem_gene_cleaned[edge[1]]
for gene in overlap_geneset:
gene_list.append(gene)
count_dict=Counter(gene_list)
for gene in set(gene_list):
rank_genelist.append([gene,count_dict[gene]])
else:
for gene in chem_gene_cleaned[explist[0]]:
rank_genelist.append([gene,1])
pre_rank_first_louvain_cluster_genelist[com]=rank_genelist
pre_rank_first_louvain_cluster_genelist_df={}
for com,genelist in pre_rank_first_louvain_cluster_genelist.items():
rank_df=pd.DataFrame(columns=('gene', 'rank'))
for n in range(len(genelist)):
rank_df.loc[n] = [genelist[n][0], float(genelist[n][1])]
rank_df=rank_df.sort_values(by=['rank'],ascending=False)
pre_rank_first_louvain_cluster_genelist_df[com]=rank_df
pre_rank_second_louvain_cluster_genelist={}
for com,explist in ee_second_louvain_iteration_exposures.items():
gene_list=[]
rank_genelist=[]
if len(explist)>1:
G_sub = nx.subgraph(backbone_ss_exposure_network,explist) #let's create the subgraph corresponding to the cluster
for edge in G_sub.edges():
overlap_geneset=chem_gene_cleaned[edge[0]] & chem_gene_cleaned[edge[1]]
for gene in overlap_geneset:
gene_list.append(gene)
count_dict=Counter(gene_list)
for gene in set(gene_list):
rank_genelist.append([gene,count_dict[gene]])
else:
for gene in chem_gene_cleaned[explist[0]]:
rank_genelist.append([gene,1])
pre_rank_second_louvain_cluster_genelist[com]=rank_genelist
pre_rank_second_louvain_cluster_genelist_df={}
for com,genelist in pre_rank_second_louvain_cluster_genelist.items():
rank_df=pd.DataFrame(columns=('gene', 'rank'))
for n in range(len(genelist)):
rank_df.loc[n] = [genelist[n][0], float(genelist[n][1])]
rank_df=rank_df.sort_values(by=['rank'],ascending=False)
pre_rank_second_louvain_cluster_genelist_df[com]=rank_df
pre_rank_third_louvain_cluster_genelist={}
for com,explist in ee_third_louvain_iteration_exposures.items():
gene_list=[]
rank_genelist=[]
if len(explist)>1:
G_sub = nx.subgraph(backbone_ss_exposure_network,explist) #let's create the subgraph corresponding to the cluster
for edge in G_sub.edges():
overlap_geneset=chem_gene_cleaned[edge[0]] & chem_gene_cleaned[edge[1]]
for gene in overlap_geneset:
gene_list.append(gene)
count_dict=Counter(gene_list)
for gene in set(gene_list):
rank_genelist.append([gene,count_dict[gene]])
else:
for gene in chem_gene_cleaned[explist[0]]:
rank_genelist.append([gene,1])
pre_rank_third_louvain_cluster_genelist[com]=rank_genelist
pre_rank_third_louvain_cluster_genelist_df={}
for com,genelist in pre_rank_third_louvain_cluster_genelist.items():
rank_df=pd.DataFrame(columns=('gene', 'rank'))
for n in range(len(genelist)):
rank_df.loc[n] = [genelist[n][0], float(genelist[n][1])]
rank_df=rank_df.sort_values(by=['rank'],ascending=False)
pre_rank_third_louvain_cluster_genelist_df[com]=rank_df
#First louvain interation
#Now we will run the traditional enrichment analysis for (Fisher's exact test + BH correction for those communties
#that perturb less than 20 genes, otherwise we will collect the leading genes from the GSEA with ranking
#and then performing again a traditional enrichment analysis
lead_genes_kegg_first_louvain_cluster_significant_fdr={}
lead_genes_gobp_first_louvain_cluster_significant_fdr={}
lead_genes_gocc_first_louvain_cluster_significant_fdr={}
lead_genes_gomf_first_louvain_cluster_significant_fdr={}
for com in list(pre_rank_first_louvain_cluster_genelist_df.keys()):
num_of_genes=len(pre_rank_first_louvain_cluster_genelist_df[com])
if num_of_genes>20:
try:
lead_genelist =list(enr_ranking_leading_genes(pre_rank_first_louvain_cluster_genelist_df[com],lib_list))
come_enr_df_dict=enr_df_dict(lead_genelist)
come_enr_fdr_df_dict=enr_fdr_list(come_enr_df_dict)
lead_genes_kegg_first_louvain_cluster_significant_fdr[com]=[len(lead_genelist),come_enr_fdr_df_dict['KEGG']]
lead_genes_gobp_first_louvain_cluster_significant_fdr[com]=[len(lead_genelist),come_enr_fdr_df_dict['GOBP']]
lead_genes_gomf_first_louvain_cluster_significant_fdr[com]=[len(lead_genelist),come_enr_fdr_df_dict['GOMF']]
lead_genes_gocc_first_louvain_cluster_significant_fdr[com]=[len(lead_genelist),come_enr_fdr_df_dict['GOCC']]
except:
lead_genes_kegg_first_louvain_cluster_significant_fdr[com]=[len(lead_genelist),'no enrichment with leading genes']
lead_genes_gobp_first_louvain_cluster_significant_fdr[com]=[len(lead_genelist),'no enrichment with leading genes']
lead_genes_gomf_first_louvain_cluster_significant_fdr[com]=[len(lead_genelist),'no enrichment with leading genes']
lead_genes_gocc_first_louvain_cluster_significant_fdr[com]=[len(lead_genelist),'no enrichment with leading genes']
else:
com_genelist=pre_rank_first_louvain_cluster_genelist_df[com]['gene'].tolist()
try:
come_enr_df_dict=enr_df_dict(com_genelist)
come_enr_fdr_df_dict=enr_fdr_list(come_enr_df_dict)
lead_genes_kegg_first_louvain_cluster_significant_fdr[com]=[num_of_genes,come_enr_fdr_df_dict['KEGG']]
lead_genes_gobp_first_louvain_cluster_significant_fdr[com]=[num_of_genes,come_enr_fdr_df_dict['GOBP']]
lead_genes_gomf_first_louvain_cluster_significant_fdr[com]=[num_of_genes,come_enr_fdr_df_dict['GOMF']]
lead_genes_gocc_first_louvain_cluster_significant_fdr[com]=[num_of_genes,come_enr_fdr_df_dict['GOCC']]
except:
lead_genes_kegg_first_louvain_cluster_significant_fdr[com]=[num_of_genes,'not found']
lead_genes_gobp_first_louvain_cluster_significant_fdr[com]=[num_of_genes,'not found']
lead_genes_gomf_first_louvain_cluster_significant_fdr[com]=[num_of_genes,'not found']
lead_genes_gocc_first_louvain_cluster_significant_fdr[com]=[num_of_genes,'not found']
print(com,len(pre_rank_first_louvain_cluster_genelist_df[com]),'not ranked')
#second louvain interation
#Now we will run the traditional enrichment analysis for (Fisher's exact test + BH correction for those communties
#that perturb less than 20 genes, otherwise we will collect the leading genes from the GSEA with ranking
#and then performing again a traditional enrichment analysis
lead_genes_kegg_second_louvain_cluster_significant_fdr={}
lead_genes_gobp_second_louvain_cluster_significant_fdr={}
lead_genes_gocc_second_louvain_cluster_significant_fdr={}
lead_genes_gomf_second_louvain_cluster_significant_fdr={}
for com in list(pre_rank_second_louvain_cluster_genelist_df.keys()):
num_of_genes=len(pre_rank_second_louvain_cluster_genelist_df[com])
if num_of_genes>20:
try:
lead_genelist =list(enr_ranking_leading_genes(pre_rank_second_louvain_cluster_genelist_df[com],lib_list))
come_enr_df_dict=enr_df_dict(lead_genelist)
come_enr_fdr_df_dict=enr_fdr_list(come_enr_df_dict)
lead_genes_kegg_second_louvain_cluster_significant_fdr[com]=[len(lead_genelist),come_enr_fdr_df_dict['KEGG']]
lead_genes_gobp_second_louvain_cluster_significant_fdr[com]=[len(lead_genelist),come_enr_fdr_df_dict['GOBP']]
lead_genes_gomf_second_louvain_cluster_significant_fdr[com]=[len(lead_genelist),come_enr_fdr_df_dict['GOMF']]
lead_genes_gocc_second_louvain_cluster_significant_fdr[com]=[len(lead_genelist),come_enr_fdr_df_dict['GOCC']]
except:
lead_genes_kegg_second_louvain_cluster_significant_fdr[com]=[len(lead_genelist),'no enrichment with leading genes']
lead_genes_gobp_second_louvain_cluster_significant_fdr[com]=[len(lead_genelist),'no enrichment with leading genes']
lead_genes_gomf_second_louvain_cluster_significant_fdr[com]=[len(lead_genelist),'no enrichment with leading genes']
lead_genes_gocc_second_louvain_cluster_significant_fdr[com]=[len(lead_genelist),'no enrichment with leading genes']
else:
com_genelist=pre_rank_second_louvain_cluster_genelist_df[com]['gene'].tolist()
try:
come_enr_df_dict=enr_df_dict(com_genelist)
come_enr_fdr_df_dict=enr_fdr_list(come_enr_df_dict)
lead_genes_kegg_second_louvain_cluster_significant_fdr[com]=[num_of_genes,come_enr_fdr_df_dict['KEGG']]
lead_genes_gobp_second_louvain_cluster_significant_fdr[com]=[num_of_genes,come_enr_fdr_df_dict['GOBP']]
lead_genes_gomf_second_louvain_cluster_significant_fdr[com]=[num_of_genes,come_enr_fdr_df_dict['GOMF']]
lead_genes_gocc_second_louvain_cluster_significant_fdr[com]=[num_of_genes,come_enr_fdr_df_dict['GOCC']]
except:
lead_genes_kegg_second_louvain_cluster_significant_fdr[com]=[num_of_genes,'not found']
lead_genes_gobp_second_louvain_cluster_significant_fdr[com]=[num_of_genes,'not found']
lead_genes_gomf_second_louvain_cluster_significant_fdr[com]=[num_of_genes,'not found']
lead_genes_gocc_second_louvain_cluster_significant_fdr[com]=[num_of_genes,'not found']
print(com,len(pre_rank_second_louvain_cluster_genelist_df[com]),'not ranked')
#third louvain interation
#Now we will run the traditional enrichment analysis for (Fisher's exact test + BH correction for those communties
#that perturb less than 20 genes, otherwise we will collect the leading genes from the GSEA with ranking
#and then performing again a traditional enrichment analysis
lead_genes_kegg_third_louvain_cluster_significant_fdr={}
lead_genes_gobp_third_louvain_cluster_significant_fdr={}
lead_genes_gocc_third_louvain_cluster_significant_fdr={}
lead_genes_gomf_third_louvain_cluster_significant_fdr={}
for com in list(pre_rank_third_louvain_cluster_genelist_df.keys()):
num_of_genes=len(pre_rank_third_louvain_cluster_genelist_df[com])
if num_of_genes>20:
try:
lead_genelist =list(enr_ranking_leading_genes(pre_rank_third_louvain_cluster_genelist_df[com],lib_list))
come_enr_df_dict=enr_df_dict(lead_genelist)
come_enr_fdr_df_dict=enr_fdr_list(come_enr_df_dict)
lead_genes_kegg_third_louvain_cluster_significant_fdr[com]=[len(lead_genelist),come_enr_fdr_df_dict['KEGG']]
lead_genes_gobp_third_louvain_cluster_significant_fdr[com]=[len(lead_genelist),come_enr_fdr_df_dict['GOBP']]
lead_genes_gomf_third_louvain_cluster_significant_fdr[com]=[len(lead_genelist),come_enr_fdr_df_dict['GOMF']]
lead_genes_gocc_third_louvain_cluster_significant_fdr[com]=[len(lead_genelist),come_enr_fdr_df_dict['GOCC']]
except:
lead_genes_kegg_third_louvain_cluster_significant_fdr[com]=[len(lead_genelist),'no enrichment with leading genes']
lead_genes_gobp_third_louvain_cluster_significant_fdr[com]=[len(lead_genelist),'no enrichment with leading genes']
lead_genes_gomf_third_louvain_cluster_significant_fdr[com]=[len(lead_genelist),'no enrichment with leading genes']
lead_genes_gocc_third_louvain_cluster_significant_fdr[com]=[len(lead_genelist),'no enrichment with leading genes']
else:
com_genelist=pre_rank_third_louvain_cluster_genelist_df[com]['gene'].tolist()
try:
come_enr_df_dict=enr_df_dict(com_genelist)
come_enr_fdr_df_dict=enr_fdr_list(come_enr_df_dict)
lead_genes_kegg_third_louvain_cluster_significant_fdr[com]=[num_of_genes,come_enr_fdr_df_dict['KEGG']]
lead_genes_gobp_third_louvain_cluster_significant_fdr[com]=[num_of_genes,come_enr_fdr_df_dict['GOBP']]
lead_genes_gomf_third_louvain_cluster_significant_fdr[com]=[num_of_genes,come_enr_fdr_df_dict['GOMF']]
lead_genes_gocc_third_louvain_cluster_significant_fdr[com]=[num_of_genes,come_enr_fdr_df_dict['GOCC']]
except:
lead_genes_kegg_third_louvain_cluster_significant_fdr[com]=[num_of_genes,'not found']
lead_genes_gobp_third_louvain_cluster_significant_fdr[com]=[num_of_genes,'not found']
lead_genes_gomf_third_louvain_cluster_significant_fdr[com]=[num_of_genes,'not found']
lead_genes_gocc_third_louvain_cluster_significant_fdr[com]=[num_of_genes,'not found']
print(com,len(pre_rank_third_louvain_cluster_genelist_df[com]),'not ranked')
with open('output/Communities/Louvain/weighted_ji_lead_genes_kegg_broad_louvain_cluster_significant_fdr.pickle', 'wb') as handle:
pk.dump(lead_genes_kegg_third_louvain_cluster_significant_fdr, handle, protocol=pk.HIGHEST_PROTOCOL)
with open('output/Communities/Louvain/weighted_ji_lead_genes_gobp_broad_louvain_cluster_significant_fdr.pickle', 'wb') as handle:
pk.dump(lead_genes_gobp_third_louvain_cluster_significant_fdr, handle, protocol=pk.HIGHEST_PROTOCOL)
with open('output/Communities/Louvain/weighted_ji_lead_genes_gomf_broad_louvain_cluster_significant_fdr.pickle', 'wb') as handle:
pk.dump(lead_genes_gomf_third_louvain_cluster_significant_fdr, handle, protocol=pk.HIGHEST_PROTOCOL)
with open('output/Communities/Louvain/weighted_ji_lead_genes_gocc_broad_louvain_cluster_significant_fdr.pickle', 'wb') as handle:
pk.dump(lead_genes_gocc_third_louvain_cluster_significant_fdr, handle, protocol=pk.HIGHEST_PROTOCOL)
with open('output/Communities/Louvain/weighted_ji_lead_genes_kegg_middle_louvain_cluster_significant_fdr.pickle', 'wb') as handle:
pk.dump(lead_genes_kegg_second_louvain_cluster_significant_fdr, handle, protocol=pk.HIGHEST_PROTOCOL)
with open('output/Communities/Louvain/weighted_ji_lead_genes_gobp_middle_louvain_cluster_significant_fdr.pickle', 'wb') as handle:
pk.dump(lead_genes_gobp_second_louvain_cluster_significant_fdr, handle, protocol=pk.HIGHEST_PROTOCOL)
with open('output/Communities/Louvain/weighted_ji_lead_genes_gomf_middle_louvain_cluster_significant_fdr.pickle', 'wb') as handle:
pk.dump(lead_genes_gomf_second_louvain_cluster_significant_fdr, handle, protocol=pk.HIGHEST_PROTOCOL)
with open('output/Communities/Louvain/weighted_ji_lead_genes_gocc_middle_louvain_cluster_significant_fdr.pickle', 'wb') as handle:
pk.dump(lead_genes_gocc_second_louvain_cluster_significant_fdr, handle, protocol=pk.HIGHEST_PROTOCOL)
with open('output/Communities/Louvain/weighted_ji_lead_genes_kegg_fine_grained_louvain_cluster_significant_fdr.pickle', 'wb') as handle:
pk.dump(lead_genes_kegg_first_louvain_cluster_significant_fdr, handle, protocol=pk.HIGHEST_PROTOCOL)
with open('output/Communities/Louvain/weighted_ji_lead_genes_gobp_fine_grained_louvain_cluster_significant_fdr.pickle', 'wb') as handle:
pk.dump(lead_genes_gobp_first_louvain_cluster_significant_fdr, handle, protocol=pk.HIGHEST_PROTOCOL)
with open('output/Communities/Louvain/weighted_ji_lead_genes_gomf_fine_grained_louvain_cluster_significant_fdr.pickle', 'wb') as handle:
pk.dump(lead_genes_gomf_first_louvain_cluster_significant_fdr, handle, protocol=pk.HIGHEST_PROTOCOL)
with open('output/Communities/Louvain/weighted_ji_lead_genes_gocc_fine_grained_louvain_cluster_significant_fdr.pickle', 'wb') as handle:
pk.dump(lead_genes_gocc_first_louvain_cluster_significant_fdr, handle, protocol=pk.HIGHEST_PROTOCOL)