|
| 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) |
0 commit comments