-
Notifications
You must be signed in to change notification settings - Fork 6
/
PE_Identifier_mapping.py
executable file
·201 lines (177 loc) · 8.14 KB
/
PE_Identifier_mapping.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
__author__ = "Hedra"
__email__ = "[email protected]"
# The following script imports the Physical Entity (PE) Identifier mapping files from https://reactome.org/download-data
# Requires: NCBI2Reactome_PE_Pathway.txt
# UniProt2Reactome_PE_Pathway.txt
# ChEBI2Reactome_PE_Pathway.txt
# from https://reactome.org/download/current/
import pandas as pd
import wget
import os
import sys
import metadata
from datetime import date
from atomwrappers import *
import argparse
# Get each of the files first
# URL's
ncbi = "https://reactome.org/download/current/NCBI2Reactome_PE_Pathway.txt"
uniprot = "https://reactome.org/download/current/UniProt2Reactome_PE_Pathway.txt"
chebi = "https://reactome.org/download/current/ChEBI2Reactome_PE_Pathway.txt"
script = "https://github.com/MOZI-AI/knowledge-import/PE_Identifier_mapping.py"
# If you have the files downloaded, make sure the file names are the same
# Or modify the file names in this code to match yours.
def get_data(name):
for data in name:
if(not os.path.isfile('raw_data/{}'.format(data.split('/')[-1]))):
print("Downloading the datasets, It might take a while")
wget.download(data, "raw_data/")
print("Done")
# The column 'R_PE_name' contains the Gene Symbol and its location information, so we need to split it
# Example: A1BG [extracellular region]
# A1BG is the Gene symbol and 'extracellular region' is the gene location
# some has extra symbols which needs preprocessing e.g. CCL5(24-91) [extracellular region], p-S472-AKT3 [plasma membrane]
def find_location(PEname, filter=False):
if "[" in PEname and "]" in PEname:
loc = PEname[PEname.find("[")+1:PEname.find("]")]
gene = PEname.split("[" +loc +"]")[0]
else:
loc = ""
gene = PEname
gene = gene.replace(gene[gene.find("("):PEname.find(")")+1], "").replace(")", "").replace("(","")
if "-" in gene:
gene = [i for i in gene.split("-") if not i.strip().isdigit()][-1]
gene = gene.strip()
if filter:
return gene
return gene,loc
def import_dataset(dataset, delim, without_location=False):
print("Started importing " + dataset)
if "UniProt" in dataset or "ChEBI" in dataset:
data = pd.read_csv(dataset, low_memory=False, delimiter=delim, names=["db_id", "R_PE_id", "R_PE_name","pathway","url","event_name", "evidence_code", "species","un1","un2","un3","un4","un5","un6"])
else:
data = pd.read_csv(dataset, low_memory=False, delimiter=delim, names=["db_id", "R_PE_id", "R_PE_name","pathway","url","event_name", "evidence_code", "species"])
mapping_entrez = pd.read_csv("raw_data/entrez.txt", low_memory=False, sep="\t")
# Take only symbols of Human species
data_human = data[data['species'] == 'Homo sapiens'][['db_id','R_PE_name','pathway']]
if without_location:
if not os.path.exists(os.path.join(os.getcwd(), 'gene-level-without-location')):
os.makedirs('gene-level-without-location')
file_name = open("gene-level-without-location/"+dataset.split("/")[-1]+"_without_location_{}.scm".format(str(date.today())), "w")
if not os.path.exists(os.path.join(os.getcwd(), 'dataset')):
os.makedirs('dataset')
with open("dataset/"+dataset.split("/")[-1]+"_{}.scm".format(str(date.today())), 'w') as f:
if "NCBI" in dataset:
genes = []
pathways = []
infered = {}
gene_symbols = mapping_entrez["Approved symbol"].values
for i in range(len(data_human)):
gene_sym, location = find_location(data_human.iloc[i]['R_PE_name'])
pathway = data_human.iloc[i]['pathway']
db_id = data_human.iloc[i]['db_id']
try:
gene = mapping_entrez[mapping_entrez["NCBI Gene ID"] == int(db_id)]["Approved symbol"].values[0]
except:
if len(gene_sym.split(" ")) > 1:
if str(db_id) in infered.keys():
gene = infered[str(db_id)]
else:
# non_exist.append(gene_sym + '\t' +str(db_id))
continue
else:
if gene_sym in gene_symbols:
gene = gene_sym
infered[str(db_id)] = gene
else:
continue
if not gene.isdigit() and not len(gene) == 1 and not gene in ["", " "]:
gene = gene.strip()
member = CMemberLink(CGeneNode(gene),ReactomeNode(pathway))
eva = CEvaluationLink(CPredicateNode("has_location"), CListLink(CGeneNode(gene), CConceptNode(location)))
cont = CContextLink(member, eva)
f.write(cont.recursive_print() + "\n")
if without_location:
file_name.write(member.recursive_print() + "\n")
if not gene in genes:
genes.append(gene)
if not pathway in pathways:
pathways.append(pathway)
version = "NCBI2reactome_pathway_mapping:latest"
num_pathways = {"Reactome Pathway": len(pathways)}
metadata.update_meta(version,ncbi,script,genes=len(genes),pathways=num_pathways)
elif "UniProt" in dataset:
molecules = []
pathways = []
for i in range(len(data_human)):
prot = str(data_human.iloc[i]['R_PE_name'])
loc = prot[prot.find("[")+1:prot.find("]")]
prot_name = prot.split("[" +loc +"]")[0]
pathway = data_human.iloc[i]['pathway']
protein = [i for i in str(data_human.iloc[i]['db_id']).split("-") if not i.strip().isdigit()][-1]
protein = protein.strip()
member = CMemberLink(ProteinNode(protein), ReactomeNode(pathway))
eva_loc = CEvaluationLink(CPredicateNode("has_location"), CListLink(ProteinNode(protein), CConceptNode(loc)))
eva_name = CEvaluationLink(CPredicateNode("has_name"), CListLink(ProteinNode(protein), CConceptNode(prot_name)))
cont = CContextLink(member, eva_loc)
f.write(cont.recursive_print() + "\n")
if without_location:
file_name.write(member.recursive_print() + "\n")
if not protein in molecules:
molecules.append(protein)
f.write(eva_name.recursive_print() + "\n")
if not pathway in pathways:
pathways.append(pathway)
version = "Uniprot2reactome_pathway_mapping:latest"
num_pathways = {"Reactome Pathway": len(pathways)}
metadata.update_meta(version,ncbi,script,prot=len(molecules),pathways=num_pathways)
elif "ChEBI" in dataset:
molecules = []
pathways = []
for i in range(len(data_human)):
chebi = str(data_human.iloc[i]['R_PE_name'])
loc = chebi[chebi.find("[")+1:chebi.find("]")]
chebi_name = chebi.split("[" +loc +"]")[0].replace('"',"")
chebi_id = str(data_human.iloc[i]['db_id'])
if not chebi_id is "nan":
chebi_id = "ChEBI:"+ str(chebi_id.strip())
pathway = data_human.iloc[i]['pathway']
member = CMemberLink(ChebiNode(chebi_id), ReactomeNode(pathway))
eva_loc = CEvaluationLink(CPredicateNode("has_location"), CListLink(ChebiNode(chebi_id), CConceptNode(loc)))
eva_name = CEvaluationLink(CPredicateNode("has_name"), CListLink(ChebiNode(chebi_id), CConceptNode(chebi_name)))
cont = CContextLink(member, eva_loc)
f.write(cont.recursive_print() + "\n")
if without_location:
file_name.write(member.recursive_print() + "\n")
if not chebi_id in molecules:
molecules.append(chebi_id)
f.write(eva_name.recursive_print() + "\n")
if not pathway in pathways:
pathways.append(pathway)
version = "Chebi2reactome_pathway_mapping:latest"
num_pathways = {"Reactome Pathway": len(pathways)}
metadata.update_meta(version,ncbi,script,chebi=len(molecules),pathways=num_pathways)
print("Done")
def parse_arg():
parser = argparse.ArgumentParser(description='Import Physical entities to pathway mapping from https://reactome.org')
parser.add_argument('--option', type=str, default='all',
help='which dataset to import: prot, chebi, ncbi')
return parser.parse_args()
if __name__ == "__main__":
option = parse_arg().option
if option == "ncbi":
get_data([ncbi])
import_dataset('raw_data/NCBI2Reactome_PE_Pathway.txt', '\t', without_location=True)
elif option == "prot":
get_data([uniprot])
import_dataset('raw_data/UniProt2Reactome_PE_Pathway.txt', '\t')
elif option == "chebi":
get_data([chebi])
import_dataset('raw_data/ChEBI2Reactome_PE_Pathway.txt', '\t', without_location=True)
elif option == "all":
get_data([chebi, uniprot, ncbi])
import_dataset('raw_data/NCBI2Reactome_PE_Pathway.txt', '\t', without_location=True)
import_dataset('raw_data/UniProt2Reactome_PE_Pathway.txt', '\t')
import_dataset('raw_data/ChEBI2Reactome_PE_Pathway.txt', '\t', without_location=True)
else:
print("Specify the correct option, Try again")