forked from MOZI-AI/knowledge-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reactome_pathway.py
executable file
·70 lines (55 loc) · 2.71 KB
/
reactome_pathway.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
__author__ = "Hedra"
__email__ = "[email protected]"
# The following script imports Reactome pathways and their relationship from https://reactome.org
# Requires:
# https://reactome.org/download/current/ReactomePathwaysRelation.txt
# https://reactome.org/download/current/ReactomePathways.txt
import pandas as pd
from urllib.request import urlopen
import os
import metadata
from datetime import date
# Helper functions
def eva(name, reid):
if 'R-HSA' in reid:
return ""+'(EvaluationLink \n \t(PredicateNode "has_name")\n \t(ListLink\n \t\t(ConceptNode "'+ reid.strip() + '")\n' + '\t\t(ConceptNode "'+ name.strip() + '")))\n'
else:
return ""
def inherit(parent, child):
if 'R-HSA' in parent and 'R-HSA' in child:
return ""+'(InheritanceLink \n \t(ConceptNode "'+ child.strip() + '")\n' + '\t(ConceptNode "'+ parent.strip() + '"))\n'
else:
return ""
# URL
pathway_rln = "https://reactome.org/download/current/ReactomePathwaysRelation.txt"
pathway = "https://reactome.org/download/current/ReactomePathways.txt"
if not os.path.isfile('ReactomePathwaysRelation.txt'):
print("Downloading ReactomePathwaysRelation.txt")
pathway_relation = pd.read_csv(urlopen(pathway_rln), low_memory=False, delimiter='\t', names=["parent", "child"])
print("Done")
else:
pathway_relation = pd.read_csv('ReactomePathwaysRelation.txt', low_memory=False, delimiter='\t', names=["parent", "child"])
if not os.path.isfile('ReactomePathways.txt'):
print("Downloading ReactomePathways.txt")
pathway_list = pd.read_csv(urlopen(pathway), low_memory=False, delimiter='\t', names=["ID", "name", "Species"])
print("Done")
else:
pathway_list = pd.read_csv('ReactomePathways.txt', low_memory=False, delimiter='\t', names=["ID", "name", "Species"])
pathway_list = pathway_list[pathway_list['Species']=='Homo sapiens']
max_len = max(len(pathway_list), len(pathway_relation))
print("Started importing")
script = "https://github.com/MOZI-AI/knowledge-import/reactome_pathway.py"
pathways = pathway_relation['parent'].values + pathway_relation['child'].values
if not os.path.exists(os.path.join(os.getcwd(), 'dataset')):
os.makedirs('dataset')
with open("dataset/reactome_{}.scm".format(str(date.today())), 'w') as f:
for i in range(max_len):
try:
f.write(eva(pathway_list.iloc[i]['name'],pathway_list.iloc[i]['ID']))
f.write(inherit(pathway_relation.iloc[i]['parent'], pathway_relation.iloc[i]['child']))
except IndexError:
f.write(inherit(pathway_relation.iloc[i]['parent'], pathway_relation.iloc[i]['child']))
num_pathways = {"Reactome Pathway": len(set(pathways))}
metadata.update_meta("Reactome Pathways relationship:latest",
pathway_rln+" "+pathway,script,pathways=num_pathways)
print("Done")