|
| 1 | +from dataclasses import dataclass |
| 2 | +from typing import Union |
| 3 | +from os import PathLike |
| 4 | +from tempfile import NamedTemporaryFile |
| 5 | +import urllib.request |
| 6 | +import filecmp |
| 7 | +import urllib.parse |
| 8 | +import os |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import gdown |
| 12 | + |
| 13 | +dir_path = Path(os.path.dirname(os.path.realpath(__file__))) |
| 14 | + |
| 15 | +def fetch_biomart_url(xml: str) -> str: |
| 16 | + """ |
| 17 | + Access BioMart data through the BioMart REST API: |
| 18 | + https://useast.ensembl.org/info/data/biomart/biomart_restful.html#biomartxml |
| 19 | + """ |
| 20 | + ROOT = "http://www.ensembl.org/biomart/martservice?query=" |
| 21 | + return ROOT + urllib.parse.quote_plus(xml) |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class CacheItem: |
| 25 | + """Class for differentriating between offline and online items in a cache.""" |
| 26 | + |
| 27 | + cached: str |
| 28 | + online: str |
| 29 | + |
| 30 | + def download(self, output: str | PathLike): |
| 31 | + print(f"Downloading {self.online}...") |
| 32 | + |
| 33 | + urllib.request.urlretrieve(self.online, output) |
| 34 | + |
| 35 | + with NamedTemporaryFile() as cached_file: |
| 36 | + print(f"Downloading cache {self.cached}...") |
| 37 | + gdown.download(self.cached, cached_file) |
| 38 | + print("Checking that downloaded artifact matches with cached artifact...") |
| 39 | + filecmp.cmp(output, cached_file.name) |
| 40 | + |
| 41 | + |
| 42 | +CacheDirectory = dict[str, Union[CacheItem, "CacheDirectory"]] |
| 43 | + |
| 44 | +# An *unversioned* directory list. |
| 45 | +directory: CacheDirectory = { |
| 46 | + "STRING": { |
| 47 | + "9606": { |
| 48 | + "links": CacheItem( |
| 49 | + cached="https://drive.google.com/uc?id=1fvjdIbgzbgJrdJxWRRRwwS1zuegf6DOj", |
| 50 | + online="http://stringdb-downloads.org/download/protein.links.v12.0/9606.protein.links.v12.0.txt.gz", |
| 51 | + ), |
| 52 | + "aliases": CacheItem( |
| 53 | + cached="https://drive.google.com/uc?id=1IWrQeTVCcw1A-jDk-4YiReWLnwP0S9bY", |
| 54 | + online="https://stringdb-downloads.org/download/protein.aliases.v12.0/9606.protein.aliases.v12.0.txt.gz", |
| 55 | + ) |
| 56 | + } |
| 57 | + }, |
| 58 | + "UniProt": { |
| 59 | + # We use FTP when possible, but we delegate to the UniProt REST API in cases that would save significant bandwidth. |
| 60 | + "9606": { |
| 61 | + # We prefer manually curated genes. |
| 62 | + "SwissProt_9606.tsv": CacheItem( |
| 63 | + cached="https://drive.google.com/uc?id=1h2Cl-60qcKse-djcsqlRXm_n60mVY7lk", |
| 64 | + online="https://rest.uniprot.org/uniprotkb/stream?fields=accession%2Cid%2Cprotein_name%2Cgene_names&format=tsv&query=%28*%29+AND+%28reviewed%3Atrue%29+AND+%28model_organism%3A9606%29" |
| 65 | + ), |
| 66 | + "HUMAN_9606_idmapping_selected.tab.gz": CacheItem( |
| 67 | + cached="https://drive.google.com/uc?id=1Oysa5COq31H771rVeyrs-6KFhE3VJqoX", |
| 68 | + online="https://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/idmapping/by_organism/HUMAN_9606_idmapping_selected.tab.gz" |
| 69 | + ), |
| 70 | + "HUMAN_9606_idmapping.dat.gz": CacheItem( |
| 71 | + cached="https://drive.google.com/uc?id=1lGxrx_kGyNdupwIOUXzfIZScc7rQKP-O", |
| 72 | + online="https://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/idmapping/by_organism/HUMAN_9606_idmapping.dat.gz" |
| 73 | + ) |
| 74 | + } |
| 75 | + }, |
| 76 | + "DISEASES": { |
| 77 | + # Instead of going through https://unmtid-shinyapps.net/shiny/tiga/, we use their |
| 78 | + # archived files directory instead. |
| 79 | + "tiga_gene-trait_stats.tsv": CacheItem( |
| 80 | + cached="https://drive.google.com/uc?id=114qyuNDy4qdmYDHHJAW-yBeTxcGTDUnK", |
| 81 | + online="https://unmtid-dbs.net/download/TIGA/20250916/tiga_gene-trait_stats.tsv", |
| 82 | + ), |
| 83 | + "HumanDO.tsv": CacheItem( |
| 84 | + cached="https://drive.google.com/uc?id=1lfB1DGJgrXTxP_50L6gGu_Nq6OyDjiIi", |
| 85 | + online="https://raw.githubusercontent.com/DiseaseOntology/HumanDiseaseOntology/016a4ec33d1a1508d669650086cd92ccebe138e6/DOreports/HumanDO.tsv", |
| 86 | + ), |
| 87 | + "human_disease_textmining_filtered.tsv": CacheItem( |
| 88 | + cached="https://drive.google.com/uc?id=1vD8KbT9sk04VEJx9r3_LglCTGYJdhN0D", |
| 89 | + online="https://download.jensenlab.org/human_disease_textmining_filtered.tsv", |
| 90 | + ), |
| 91 | + "human_disease_knowledge_filtered.tsv": CacheItem( |
| 92 | + cached="https://drive.google.com/uc?id=1qGUnjVwF9-8p5xvp8_6CfVsbMSM_wkld", |
| 93 | + online="https://download.jensenlab.org/human_disease_knowledge_filtered.tsv", |
| 94 | + ), |
| 95 | + }, |
| 96 | + "BioMart": { |
| 97 | + "ensg-ensp.tsv": CacheItem( |
| 98 | + cached="https://drive.google.com/uc?id=1-gPrDoluXIGydzWKjWEnW-nWhYu3YkHL", |
| 99 | + online=fetch_biomart_url((dir_path / "biomart" / "ensg-ensp.xml").read_text()) |
| 100 | + ) |
| 101 | + }, |
| 102 | + "DepMap": { |
| 103 | + "OmicsProfiles.csv": CacheItem( |
| 104 | + cached="https://drive.google.com/uc?id=1i54aKfO0Ci2QKLTNJnuQ_jgGhH4c9rTL", |
| 105 | + online="https://depmap.org/portal/download/api/download?file_name=downloads-by-canonical-id%2F2025-05-01-master-mapping-table-28c2.12%2Fpublic_release_date.2025-05-01.master_mapping_table.csv&dl_name=OmicsProfiles.csv&bucket=depmap-external-downloads" |
| 106 | + ), |
| 107 | + "CRISPRGeneDependency.csv": CacheItem( |
| 108 | + cached="https://drive.google.com/uc?id=122rWNqT_u3M7B_11WYZMtOLiPbBykkaz", |
| 109 | + online="https://depmap.org/portal/download/api/download?file_name=downloads-by-canonical-id%2F25q2-public-557c.3%2FCRISPRGeneDependency.csv&dl_name=CRISPRGeneDependency.csv&bucket=depmap-external-downloads" |
| 110 | + ), |
| 111 | + "OmicsSomaticMutationsMatrixDamaging.csv": CacheItem( |
| 112 | + cached="https://drive.google.com/uc?id=1W7N2H0Qi7NwmTmNChcwa2ZZ4WxAuz-Xh", |
| 113 | + online="https://depmap.org/portal/download/api/download?file_name=downloads-by-canonical-id%2Fpublic-25q2-c5ef.87%2FOmicsSomaticMutationsMatrixDamaging.csv&dl_name=OmicsSomaticMutationsMatrixDamaging.csv&bucket=depmap-external-downloads" |
| 114 | + ), |
| 115 | + "OmicsExpressionProteinCodingGenesTPMLogp1.csv": CacheItem( |
| 116 | + cached="https://drive.google.com/uc?id=1P0m88eXJ8GPdru8h9oOcHPeXKU7ljIrP", |
| 117 | + online="https://depmap.org/portal/download/api/download?file_name=downloads-by-canonical-id%2Fpublic-25q2-c5ef.73%2FOmicsExpressionProteinCodingGenesTPMLogp1.csv&dl_name=OmicsExpressionProteinCodingGenesTPMLogp1.csv&bucket=depmap-external-downloads" |
| 118 | + ), |
| 119 | + "OmicsCNGeneWGS.csv": CacheItem( |
| 120 | + cached="https://drive.google.com/uc?id=1TPp3cfK7OZUrftucr3fLO-krXSQAA6Ub", |
| 121 | + online="https://depmap.org/portal/download/api/download?file_name=downloads-by-canonical-id%2Fpublic-25q2-c5ef.104%2FOmicsCNGeneWGS.csv&dl_name=OmicsCNGeneWGS.csv&bucket=depmap-external-downloads" |
| 122 | + ) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +def get_cache_item(path: list[str]) -> CacheItem: |
| 128 | + """Takes a path and gets the underlying cache item.""" |
| 129 | + assert len(path) != 0 |
| 130 | + |
| 131 | + current_item = directory |
| 132 | + for entry in path: |
| 133 | + if isinstance(current_item, CacheItem): |
| 134 | + raise ValueError(f"Path {path} leads to a cache item too early!") |
| 135 | + current_item = current_item[entry] |
| 136 | + |
| 137 | + if not isinstance(current_item, CacheItem): |
| 138 | + raise ValueError(f"Path {path} doesn't lead to a cache item") |
| 139 | + |
| 140 | + return current_item |
0 commit comments