-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph.py
134 lines (100 loc) · 3.22 KB
/
graph.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
# -*- coding: utf-8 -*-
"""Create BEL graph"""
import os
import pandas as pd
from tqdm import tqdm
from pubchempy import Compound
import pybel
from pybel import BELGraph
from pybel.dsl import Abundance, Protein, Entity
RELATION_MAPPER = {
'binds': BELGraph.add_binds
}
def create_chemical_nodes(node_dict: dict):
"""Method to create chemical nodes"""
df = pd.read_csv('data/normalized_data/chemicals.csv', sep='\t')
for row in tqdm(df.values, desc='Creating chemical nodes'):
(
id,
pubchem_id,
smiles
) = row
if pd.notna(pubchem_id):
pubchem_id = int(pubchem_id) # convert flot to int values
chemical_node = Abundance(
namespace='pubchem.compound',
identifier=str(pubchem_id),
name=Compound.from_cid(pubchem_id).synonyms[0],
xrefs=[Entity(
namespace='enamine',
identifier=id,
), Entity(
namespace='smiles',
identifier=smiles,
)]
)
else:
chemical_node = Abundance(
namespace='enamine',
identifier=id,
)
node_dict[id] = chemical_node
return node_dict
def create_protein_nodes(node_dict: dict):
"""Method to create protein nodes"""
df = pd.read_csv('data/normalized_data/protein.csv', sep='\t')
for row in tqdm(df.values, desc='Creating protein nodes'):
(
id,
name,
uniprot
) = row
if pd.notna(uniprot):
protein_node = Protein(
namespace='uniprot',
identifier=uniprot,
name=name,
xrefs=[Entity(
namespace='prot',
identifier=id,
)]
)
else:
protein_node = Abundance(
namespace='prot',
identifier=id,
)
node_dict[name] = protein_node
return node_dict
def create_chembl_nodes(node_dict: dict):
"""Method to create protein nodes"""
df = pd.read_excel('data/normalized_data/protein.csv', sep='\t')
def save_graph(bel_graph: BELGraph):
"""Save the BEL graph"""
os.makedirs('data/graph', exist_ok=True)
return pybel.dump(bel_graph, path='data/graph/covid_nmr.bel.nodelink.json')
def create_graph():
"""Method to create BEL graph from NMR data"""
nodes = {}
nodes = create_chemical_nodes(node_dict=nodes)
nodes = create_protein_nodes(node_dict=nodes)
graph_df = pd.read_csv('data/normalized_data/graph_data.csv', sep='\t')
graph = BELGraph(name='COVID-NMR')
for row in tqdm(graph_df.values, desc='Exporting data to BEL'):
(
source_id,
rel,
target_id
) = row
source_node = nodes[source_id]
target_node = nodes[target_id]
RELATION_MAPPER[rel](
graph,
source_node, target_node,
evidence='From COVID_NMR data',
citation=('database', 'covid-nmr')
)
graph.summarize()
save_graph(graph)
if __name__ == '__main__':
create_graph()