Skip to content

Commit c168705

Browse files
authored
Merge pull request #4 from reactome/develop
Develop to master
2 parents 131965a + 441dc22 commit c168705

File tree

4 files changed

+122
-3
lines changed

4 files changed

+122
-3
lines changed

reactome2py/content.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1887,4 +1887,4 @@ def search_suggest(query='platele'):
18871887
if response.status_code == 200:
18881888
return response.json()
18891889
else:
1890-
print('Status code returned a value of %s' % response.status_code)
1890+
print('Status code returned a value of %s' % response.status_code)

reactome2py/utils.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
utility functions for Reactome data-fetch, mappings, and overlay networks
3+
"""
4+
from requests.exceptions import ConnectionError
5+
import requests
6+
import io
7+
import tarfile
8+
import zipfile
9+
10+
11+
def ehld_stids():
12+
"""
13+
retrieves a list of high-level hierarchy pathway with Enhanced High Level Diagrams (EHLD)
14+
https://reactome.org/icon-info/ehld-specs-guideline
15+
:return: list of pathway stIds
16+
"""
17+
18+
url = "https://reactome.org/download/current/ehld/svgsummary.txt"
19+
20+
try:
21+
response = requests.get(url=url)
22+
except ConnectionError as e:
23+
print(e)
24+
25+
if response.status_code == 200:
26+
content_list = response.text.splitlines()
27+
st_ids = [stId for stId in content_list if 'R-' in stId]
28+
return st_ids
29+
else:
30+
print('Status code returned a value of %s' % response.status_code)
31+
32+
33+
def sbgn_stids():
34+
"""
35+
retieves a list of lower-level (with hierarchy) pathways that have SBGNs
36+
https://reactome.org/about/news/110-sbgn-files-revamp
37+
:return: list of pathway stIds
38+
"""
39+
40+
url = "https://reactome.org/download/current/homo_sapiens.sbgn.tar.gz"
41+
42+
try:
43+
response = requests.get(url=url)
44+
except ConnectionError as e:
45+
print(e)
46+
47+
if response.status_code == 200:
48+
tar_file = tarfile.open(fileobj=io.BytesIO(response.content))
49+
file_names = tar_file.getnames()
50+
ehlds = ehld_stids()
51+
sbgns = [f.replace('.sbgn', '') for f in file_names]
52+
sbgn_only = list(set(sbgns) - set(ehlds))
53+
return sbgn_only
54+
else:
55+
print('Status code returned a value of %s' % response.status_code)
56+
57+
58+
def _yield_zip(response):
59+
"""
60+
read zipfile in memory
61+
https://docs.python.org/3/library/zipfile.html
62+
:param response:
63+
:return: content of zip file
64+
"""
65+
66+
with zipfile.ZipFile(io.BytesIO(response.content)) as the_zip:
67+
for zip_info in the_zip.infolist():
68+
with the_zip.open(zip_info) as the_file:
69+
yield the_file.readlines()
70+
71+
72+
def _read_ziplines(response):
73+
"""
74+
helper function to clean zipline content parsing
75+
:param response:
76+
:return: list
77+
"""
78+
79+
return [c.split('\t') for c in [c.decode('utf8') for c in list(_yield_zip(response))[0]]]
80+
81+
82+
def gene_mappings():
83+
"""
84+
Maps reactome pathway stId and name to it's associated gene list (HGNC)
85+
:return: dictionary of reactome pathways and HGNC gene mappings to the pathway.
86+
"""
87+
88+
url = "https://reactome.org/download/current/ReactomePathways.gmt.zip"
89+
90+
try:
91+
response = requests.get(url=url)
92+
except ConnectionError as e:
93+
print(e)
94+
95+
if response.status_code == 200:
96+
gm = _read_ziplines(response)
97+
relations = []
98+
99+
for i, e in enumerate(gm):
100+
gm[i] = [s.strip() for s in gm[i]]
101+
d = dict(name=gm[i][0], stId=gm[i][1], genes=gm[i][2:len(gm[i])])
102+
relations.append(d)
103+
104+
return relations
105+
else:
106+
print('Status code returned a value of %s' % response.status_code)

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup, find_packages
22

3-
__version__ = '0.0.1'
3+
__version__ = '0.0.2'
44

55
setup(
66
name='reactome2py',
@@ -15,7 +15,7 @@
1515
'console_scripts': ['reactome2py = reactome2py.__main__:main']
1616
},
1717
install_requires=[
18-
'requests'
18+
'requests',
1919
],
2020
extras_require={
2121
'pandas': ["pandas==0.24.2"],

tests/unit/test_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from reactome2py import utils
2+
3+
4+
def test_ehld_stids():
5+
assert isinstance(utils.ehld_stids(), list)
6+
7+
8+
def test_sbgn_stids():
9+
assert isinstance(utils.sbgn_stids(), list)
10+
11+
12+
def test_gene_mappings():
13+
assert isinstance(utils.gene_mappings()[0], dict)

0 commit comments

Comments
 (0)