-
Notifications
You must be signed in to change notification settings - Fork 6
/
reactome_pathway.py
executable file
·69 lines (56 loc) · 2.73 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
from atomwrappers import *
# URL's
pathway_rln = "https://reactome.org/download/current/ReactomePathwaysRelation.txt"
pathway = "https://reactome.org/download/current/ReactomePathways.txt"
def homosapien(pathway_id):
if pathway_id.startswith("R-HSA"):
return True
else:
return False
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')
output = "dataset/reactome_{}.scm".format(str(date.today()))
with open(output, 'w') as f:
for i in range(len(pathway_list)):
pw_name = pathway_list.iloc[i]['name']
pw_id = pathway_list.iloc[i]['ID']
eva_name = CEvaluationLink(CPredicateNode("has_name"), CListLink(ReactomeNode(pw_id), CConceptNode(pw_name)))
f.write(eva_name.recursive_print() + "\n")
for i in range(len(pathway_relation)):
pw_parent = pathway_relation.iloc[i]['parent']
pw_child = pathway_relation.iloc[i]['child']
if homosapien(pw_child) and homosapien(pw_parent):
inherit = CInheritanceLink(ReactomeNode(pw_child), ReactomeNode(pw_parent))
f.write(inherit.recursive_print() + "\n")
num_pathways = {"Reactome Pathway": len(set(pathways))}
metadata.update_meta("Reactome Pathways relationship:latest",
pathway_rln+" "+pathway,script,pathways=num_pathways)
print("Done, check {}".format(output))