| 1 |
import base64 |
| 2 |
import glob |
| 3 |
import os |
| 4 |
import os.path as op |
| 5 |
import posixpath as pp |
| 6 |
from urllib.parse import urlencode, urljoin |
| 7 |
|
| 8 |
import pandas as pd |
| 9 |
import requests |
| 10 |
|
| 11 |
|
| 12 |
class EncodeClient: |
| 13 |
BASE_URL = "http://www.encodeproject.org/" |
| 14 |
|
| 15 |
# 2020-05-15 compatible with ENCODE Metadata at: |
| 16 |
METADATA_URL = "https://www.encodeproject.org/metadata/type=Experiment&status=released/metadata.tsv" |
| 17 |
|
| 18 |
KNOWN_ASSEMBLIES = ( |
| 19 |
"GRCh38", |
| 20 |
"GRCh38-minimal", |
| 21 |
"ce10", |
| 22 |
"ce11", |
| 23 |
"dm3", |
| 24 |
"dm6", |
| 25 |
"hg19", |
| 26 |
"mm10", |
| 27 |
"mm10-minimal", |
| 28 |
"mm9", |
| 29 |
) |
| 30 |
|
| 31 |
def __init__(self, cachedir, assembly, metadata=None): |
| 32 |
if assembly not in self.KNOWN_ASSEMBLIES: |
| 33 |
raise ValueError("assembly must be in:", self.KNOWN_ASSEMBLIES) |
| 34 |
|
| 35 |
self.cachedir = op.join(cachedir, assembly) |
| 36 |
if not op.isdir(self.cachedir): |
| 37 |
os.makedirs(self.cachedir, exist_ok=True) |
| 38 |
|
| 39 |
if metadata is None: |
| 40 |
metadata_path = op.join(cachedir, "metadata.tsv") |
| 41 |
|
| 42 |
if not op.exists(metadata_path): |
| 43 |
print( |
| 44 |
"getting metadata from ENCODE, please wait while " |
| 45 |
"(~240Mb) file downloads" |
| 46 |
) |
| 47 |
with requests.get(self.METADATA_URL, stream=True) as r: |
| 48 |
r.raise_for_status() |
| 49 |
with open(metadata_path, "wb") as f: |
| 50 |
for chunk in r.iter_content(chunk_size=8192): |
| 51 |
f.write(chunk) |
| 52 |
|
| 53 |
self._meta = pd.read_table(metadata_path, low_memory=False) |
| 54 |
table_assemblies = sorted( |
| 55 |
self._meta["File assembly"].dropna().unique().tolist() |
| 56 |
) |
| 57 |
|
| 58 |
if not set(table_assemblies).issubset(set(self.KNOWN_ASSEMBLIES)): |
| 59 |
raise ValueError( |
| 60 |
"Table assemblies do not match known assemblies, " |
| 61 |
"check ENCODE metadata version" |
| 62 |
) |
| 63 |
self._meta = self._meta[self._meta["File assembly"] == assembly].copy() |
| 64 |
self._meta = self._meta.set_index("File accession") |
| 65 |
|
| 66 |
else: |
| 67 |
self._meta = metadata |
| 68 |
|
| 69 |
def _batch_download(self, args): |
| 70 |
params = urlencode(args) |
| 71 |
url = pp.join("batch_download", params) |
| 72 |
url = urljoin(self.BASE_URL, url) |
| 73 |
r = requests.get(url) |
| 74 |
r.raise_for_status() |
| 75 |
return r |
| 76 |
|
| 77 |
def _metadata(self, args): |
| 78 |
params = urlencode(args) |
| 79 |
url = pp.join("metadata", params, "metadata.tsv") |
| 80 |
url = urljoin(self.BASE_URL, url) |
| 81 |
r = requests.get(url) |
| 82 |
r.raise_for_status() |
| 83 |
return r |
| 84 |
|
| 85 |
@property |
| 86 |
def meta(self): |
| 87 |
return self._meta.copy() |
| 88 |
|
| 89 |
def info(self, accession, width=850, height=450): |
| 90 |
from IPython.display import HTML |
| 91 |
|
| 92 |
url = urljoin(self.BASE_URL, pp.join("experiments", accession)) |
| 93 |
return HTML( |
| 94 |
f'<iframe width="{width}px" height="{height}px" src={url}></iframe>' |
| 95 |
) |
| 96 |
|
| 97 |
def fetch(self, accession): |
| 98 |
url = self.meta.loc[accession, "File download URL"] |
| 99 |
# sig = self.meta.loc[accession, 'md5sum'] |
| 100 |
filename = op.split(url)[1] |
| 101 |
path = op.join(self.cachedir, filename) |
| 102 |
if op.exists(path): |
| 103 |
pass |
| 104 |
# print('File "{}" available'.format(filename)) |
| 105 |
else: |
| 106 |
print(f'Downloading "{filename}"') |
| 107 |
r = requests.get(url) |
| 108 |
r.raise_for_status() |
| 109 |
with open(path, "wb") as f: |
| 110 |
f.write(r.content) |
| 111 |
return path |
| 112 |
|
| 113 |
def fetch_all(self, accessions): |
| 114 |
return list(map(self.fetch, accessions)) |
| 115 |
|
| 116 |
|
| 117 |
class FDNClient: |
| 118 |
BASE_URL = "https://data.4dnucleome.org/" |
| 119 |
|
| 120 |
def __init__(self, cachedir, assembly, metadata=None, key_id=None, key_secret=None): |
| 121 |
self.cachedir = op.join(cachedir, assembly) |
| 122 |
if not op.isdir(self.cachedir): |
| 123 |
raise OSError(f"Directory doesn't exist: '{cachedir}'") |
| 124 |
if metadata is None: |
| 125 |
metadata_paths = sorted(glob.glob(op.join(cachedir, "metadata*.tsv"))) |
| 126 |
metadata_path = metadata_paths[-1] |
| 127 |
self._meta = pd.read_table(metadata_path, low_memory=False, comment="#") |
| 128 |
if assembly == "GRCh38": |
| 129 |
self._meta = self._meta[self._meta["Organism"] == "human"].copy() |
| 130 |
self._meta = self._meta.set_index("File Accession") |
| 131 |
else: |
| 132 |
self._meta = metadata |
| 133 |
if key_id is not None: |
| 134 |
credential = (key_id + ":" + key_secret).encode("utf-8") |
| 135 |
self._token = base64.b64encode(credential) |
| 136 |
else: |
| 137 |
self._token = None |
| 138 |
|
| 139 |
@property |
| 140 |
def meta(self): |
| 141 |
return self._meta.copy() |
| 142 |
|
| 143 |
def info(self, accession, width=850, height=450): |
| 144 |
from IPython.display import HTML |
| 145 |
|
| 146 |
url = urljoin(self.BASE_URL, pp.join("experiments", accession)) |
| 147 |
return HTML( |
| 148 |
f'<iframe width="{width}px" height="{height}px" src={url}></iframe>' |
| 149 |
) |
| 150 |
|
| 151 |
def fetch(self, accession): |
| 152 |
url = self.meta.loc[accession, "File Download URL"] |
| 153 |
# sig = self.meta.loc[accession, 'md5sum'] |
| 154 |
filename = op.split(url)[1] |
| 155 |
path = op.join(self.cachedir, filename) |
| 156 |
if op.exists(path): |
| 157 |
pass |
| 158 |
# print('File "{}" available'.format(filename)) |
| 159 |
else: |
| 160 |
print(f'Downloading "{filename}"') |
| 161 |
if self._token: |
| 162 |
headers = {"Authorization": b"Basic " + self._token} |
| 163 |
else: |
| 164 |
headers = None |
| 165 |
r = requests.get(url, headers=headers) |
| 166 |
r.raise_for_status() |
| 167 |
with open(path, "wb") as f: |
| 168 |
f.write(r.content) |
| 169 |
return path |
| 170 |
|
| 171 |
def fetch_all(self, accessions): |
| 172 |
return list(map(self.fetch, accessions)) |