-
Notifications
You must be signed in to change notification settings - Fork 0
/
VANE.py
382 lines (259 loc) · 14.6 KB
/
VANE.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
375
376
377
378
379
380
381
382
from re import S
import pandas as pd
from ld_parser import get_ld_snps, process_api_call
from snp_parser import get_position_snp, create_df_for_locus, merge_and_clean
import sqlite3
from regulome_db_parsing import regulome_parse_snp, score_assign, organ_df, cell_df
import seaborn as sns
import networkx as nx
from pyvis.network import Network
import os
import random as rd
import matplotlib.pyplot as plt
import numpy as np
class VANE:
def __init__(self, list_of_variants = None, mapping_table_dir = None, sql_dir = None):
"""list of variants: List composed of tagging SNPs in rsid format."""
if mapping_table_dir == None:
mapping_table = pd.read_table('base_files/gene_protein_ensembl_map_table.tsv')
self.mapping_table = mapping_table
self.tagging_snps = list_of_variants
self.snp_2_gene = None
self.regulome_db = None
def process_tag_variants(self, ld, population=None, verbose=False):
"""Populations can be extracted from here: https://rest.ensembl.org/documentation/info/variation_populations, if None = "1000GENOMES:phase_3:CEU"
verbose: True/False"""
tagging_snp_list = self.tagging_snps
df_all_positons = []
for rsid in tagging_snp_list:
list_of_snps = get_ld_snps(rsid, ld, population)
list_of_snps.append(rsid)
df_with_position = create_df_for_locus(list_of_snps, rsid, verbose)
df_all_positons.append(df_with_position)
df_all_positons = pd.concat(df_all_positons)
return df_all_positons
def process_list_finemapped(self, list_of_locus_name, list_of_finemapped, verbose=False):
"""In case that you want to use fine-mapped SNPs for any given LOCUS, you can use this function.
list_of_locus_name = List for how you want to call the locus.
list_of_finemapped = This is a list of lists. For each locus you want a list of the finemapped SNPs in rsid format.
Both list_of_locus_name, and list_of_fiemapped SNPs need to be in order.
Example:
You have three locus, locus 1, locus 2, and locus 3. You have three SNPs rs2, rs3, and rs4, finemapped to locus 1,
rs42, rs32, and rs11 finemapped to Locus 2, and rs111, rs321, and rs33, finemapped to locus 3.
This how it should be formatted:
list_of_locus_name = ['locus_1', 'locus_2', 'locus_3']
list_of_finemapped = [['rs2', 'rs3', 'rs4'], ['rs42', 'rs32', 'rs11'], ['rs111', 'rs321', 'rs33']]"""
df_all_positons = []
for locus, fine_mapped_snps in zip(list_of_locus_name, list_of_finemapped):
df_with_position = create_df_for_locus(fine_mapped_snps, locus, verbose=verbose)
df_all_positons.append(df_with_position)
df_all_positons = pd.concat(df_all_positons)
return df_all_positons
def query_snp_opentargets(self, df, sqlt3_dir):
"""df: DF from create_df_for_locus()
returns a dataframe with the SNPs in opentargets and the probability of affecting genes"""
sql_dir = sqlt3_dir
#sql_dir = "/pool0/home/secret/snp2gene_clean/snp2_gene_sql/clean_snp2gene.db"
cnx = sqlite3.connect(sql_dir)
position_list = tuple(df['position'].to_list())
chromosome_list = tuple(df['chromosome'].to_list())
SQL_QUERY = """SELECT * FROM snp2gene WHERE position IN {pos} AND chr_id in {chrom}""".format(pos = position_list, chrom = chromosome_list)
df_results = pd.read_sql_query(SQL_QUERY, cnx)
df_results.rename(columns={'chr_id':'chromosome'}, inplace=True)
return df_results
def merge_and_clean(self, snp_2_gene, df_with_position, threshold = 0):
"""snp2gene: DF returned from query_snp_opentargets()
df_with_position: DF returned from create_df_for_locus()
threshold: Amount of rsIDs that need to map to a gene to be used in the analysis.
returns a dataframe with each row containing the SNP and the highest probability per each gene"""
snp_2_gene_clean = snp_2_gene.copy()
snp_2_gene_clean = snp_2_gene_clean.merge(df_with_position, on=['chromosome', 'position'])
snp_2_gene_clean = snp_2_gene_clean[['rsid', 'gene_id', 'position', 'chromosome', 'ref', 'alt', 'overall_score', 'tagging_snp']]
idx = snp_2_gene_clean.groupby('rsid')['overall_score'].transform(max) == snp_2_gene_clean['overall_score']
snp_2_gene_clean = snp_2_gene_clean[idx]
gene_group = snp_2_gene_clean.groupby('gene_id').count().reset_index()
gene_group = gene_group[['gene_id', 'rsid']]
genes_ = gene_group.loc[gene_group['rsid'] > threshold]['gene_id'].to_list()
snp_2_gene_clean = snp_2_gene_clean.loc[snp_2_gene_clean['gene_id'].isin(genes_)]
self.snp_2_gene_clean = snp_2_gene_clean
return gene_group
def regulomedb_scoring(self, snp_2_gene_all=None, save=True, verbose=False):
"""Take the SNP 2 GENE cleaned and merge dataframe, query regulomeDB, and score the chromatin state."""
if snp_2_gene_all == None:
snp_2_gene_all = self.snp_2_gene_clean
total_number = len(snp_2_gene_all)
list_of_results = []
a = 0
for index, row in snp_2_gene_all.iterrows():
a += 1
if verbose:
print("{number} SNPs processed out of {total}".format(number = a, total = total_number))
chromomosome = row['chromosome']
position = row['position']
gene = row['gene_id']
df_results = regulome_parse_snp(position, chromomosome)
df_results['gene'] = gene
list_of_results.append(df_results)
result_df_all = pd.concat(list_of_results)
result_df_all = score_assign(result_df_all)
if save:
self.regulome_db = result_df_all
return
else:
return result_df_all
def process_cells(self, cells, result_df_all=None, save=True):
"""Takes a cell list, and returns a processed files with the cells selected.
It also saves in to the attribute df_for_network, a dataframe that can be used to generate networks"""
if result_df_all == None:
result_df_all = self.regulome_db
gene_list = result_df_all['gene'].unique()
results_cells_per_gene = []
for gene in gene_list:
gene_df = result_df_all.loc[result_df_all['gene'] == gene]
cells_scored, df_cells = cell_df(cells, gene_df)
cells_scored['gene'] = gene
results_cells_per_gene.append(cells_scored)
cell_results = pd.concat(results_cells_per_gene).reset_index()
if save:
self.cell_results = cell_results
result_to_plot = self.cell_results.merge(self.mapping_table, on='gene')
plot_df = result_to_plot[['normalized score', 'protein_gene', 'cell_specific']]
plot_df.drop_duplicates(inplace=True)
plot_df_pivoted = plot_df.pivot('protein_gene', 'cell_specific')
self.df_for_network = plot_df_pivoted
return cell_results
def process_organs(self, organs, result_df_all=None, save=True):
if result_df_all == None:
result_df_all = self.regulome_db
organ_lists = []
gene_list = result_df_all['gene'].unique()
for gene in gene_list:
gene_df = result_df_all.loc[result_df_all['gene'] == gene]
organ_scored, df_organ = organ_df(organs, gene_df)
organ_scored['gene'] = gene
organ_lists.append(organ_scored)
organs_results = pd.concat(organ_lists).reset_index()
if save:
self.organ_results = organs_results
return organs_results
def plot_cells(self, scale_heatmap=False, standarize_gene = False):
"""Scale_heatmap: If True scale heatmap from -1 to 1."""
result_to_plot = self.cell_results.merge(self.mapping_table, on='gene')
plot_df = result_to_plot[['normalized score', 'protein_gene', 'cell_specific']]
plot_df.drop_duplicates(inplace=True)
plot_df_pivoted = plot_df.pivot('protein_gene', 'cell_specific')
gene_standarized_cell = ((plot_df_pivoted.T - plot_df_pivoted.T.mean()) / plot_df_pivoted.T.std()).T
self.cell_results_full = plot_df_pivoted
if standarize_gene:
standard_image_cell = sns.heatmap(gene_standarized_cell, cmap='vlag')
self.standardized_gene = standard_image_cell
else:
if scale_heatmap:
standard_image_cell = sns.heatmap(plot_df_pivoted, cmap='vlag', vmin= -1, vmax=1)
else:
standard_image_cell = sns.heatmap(plot_df_pivoted, cmap='vlag')
return standard_image_cell
def plot_organs(self, scale_heatmap=False, standarize_gene = False):
"""Scale_heatmap: If True scale heatmap from -1 to 1. If False, auto scale."""
organs_results = self.organ_results.merge(self.mapping_table, on='gene')
organs_results.drop_duplicates(inplace=True)
organ_plot = organs_results[['organ_specific', 'normalized score', 'protein_gene']]
organ_plot.drop_duplicates(subset=['protein_gene', 'organ_specific'], inplace=True)
organ_plot = organ_plot.pivot('protein_gene', 'organ_specific')
self.organ_df = organ_plot
if standarize_gene:
gene_standarized_organs = ((organ_plot.T - organ_plot.T.mean()) / organ_plot.T.std()).T
standard_image_organ = sns.heatmap(gene_standarized_organs, cmap='vlag', vmin= -1, vmax=1)
else:
if scale_heatmap:
standard_image_organ = sns.heatmap(organ_plot, cmap='vlag', vmin= -1, vmax=1)
else:
standard_image_organ = sns.heatmap(organ_plot, cmap='vlag')
return standard_image_organ
class VANE_network():
def __init__(self, network_folder, df_for_network, mapping_table = None, cell_dicts = None):
"""list of variants: List composed of tagging SNPs in rsid format.
df_for_network is df from process_self, can be called with self.df_for_network() """
self.df_for_network = df_for_network.reset_index()
self.network_folder = network_folder
self.cell_dicts =cell_dicts
if cell_dicts == None:
self.cell_dicts = {'B':'B-Cell', 'T':'T-Cell', 'mono':'Monocytes', 'astrocyte':'Astrocytes', 'oligodendrocyte':'Oligodendrocytes'}
self.mapping_table = mapping_table
if mapping_table == None:
self.mapping_table = pd.read_table('base_files/gene_protein_ensembl_map_table.tsv')
def create_cellular_network(self, cell, epigenome_threshold = 0, custom=False):
cell_name = self.cell_dicts[cell]
#load graph
cell_graph = self.load_cell_network(cell_name)
self.original_graph = cell_graph
#Get subset
df_protein_expressed = self.df_for_network
if custom:
list_genes = df_protein_expressed.loc[df_protein_expressed[cell] >= epigenome_threshold]['cell_specific'].to_list()
else:
list_genes = df_protein_expressed[('protein_gene','')].loc[df_protein_expressed['normalized score', cell] > epigenome_threshold].to_list()
print(list_genes)
if len(list_genes) == 0:
print('List of Gene empty, try with a lower Epigenomic threshold')
else:
proteins_to_extract = self.mapping_table.loc[self.mapping_table['protein_gene'].isin(list_genes)]['protein'].to_list()
cell_subgraph = cell_graph.subgraph(proteins_to_extract)
self.graph = cell_subgraph
return cell_subgraph
def load_cell_network(self, cell_name):
"""cell name comes from the form
cell dict is a cell from regulome db to cells from protein atlas
"""
network_folder = self.network_folder
cell_list = os.listdir(self.network_folder)
cell_file_dir = [cell for cell in cell_list if cell_name.lower() in cell.lower()]
cell_graph = nx.read_gml(self.network_folder + cell_file_dir[0])
return cell_graph
def plot_network(self, html_name):
"""once you create a cellular network with create_cellular_network(), you can plot the network using pyvis.
This function returns a HTML file that can be opened any browser
html_name = any name for the html"""
nt = Network('900px', width='100%',
directed = False)
nt.from_nx(self.graph)
nt.show_buttons()
html_name = html_name + '.html'
nt.show(html_name)
return print('Network generate at {html_name}'.format(html_name = html_name))
def calculate_p_value(self, n = 1000):
"""You need to run creat_cellular_network(), before running this function.
It returns a set with the 5 and 95 CI, and one-tailed pval."""
graph_node_sample = self.graph.number_of_nodes()
graph_edge_original = self.graph.number_of_edges()
list_numb_nodes = []
list_numb_edges = []
for x in range(n):
sample = rd.sample(self.original_graph.nodes, graph_node_sample)
graph_sample = self.original_graph.subgraph(sample)
number_of_nodes = graph_sample.number_of_nodes()
number_of_edges = graph_sample.number_of_edges()
list_numb_nodes.append(number_of_nodes)
list_numb_edges.append(number_of_edges)
ci_5 = np.percentile(list_numb_edges, 5)
ci_95 = np.percentile(list_numb_edges, 95)
number_bigger = len([x for x in list_numb_edges if x > graph_edge_original])
pval = (1+number_bigger)/(len(list_numb_edges)+1)
return (ci_5, ci_95), pval
def plot_bootstrap(self):
graph_node_sample = self.graph.number_of_nodes()
graph_edge_original = self.graph.number_of_edges()
list_numb_nodes = []
list_numb_edges = []
for x in range(1000):
sample = rd.sample(self.original_graph.nodes, graph_node_sample)
graph_sample = self.original_graph.subgraph(sample)
number_of_nodes = graph_sample.number_of_nodes()
number_of_edges = graph_sample.number_of_edges()
list_numb_nodes.append(number_of_nodes)
list_numb_edges.append(number_of_edges)
sns.set_theme()
plt.axvline(x=graph_edge_original, ymin=0, ymax=0.95,color='r')
figure = sns.histplot(list_numb_edges, binwidth=0.3)
figure.set(xlabel='Edge Count', ylabel='Frequency in bootstrapping')
return figure