-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeature1_DepMap.py
More file actions
398 lines (299 loc) · 10.6 KB
/
Copy pathFeature1_DepMap.py
File metadata and controls
398 lines (299 loc) · 10.6 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Feature1_DepMap.py
Download selected DepMap release files using the Figshare public API.
No merging.
No feature generation.
No processing.
Run:
python Feature1_DepMap.py
Optional:
python Feature1_DepMap.py --release "DepMap Public 26Q1"
python Feature1_DepMap.py --include-optional
"""
import argparse
import time
from pathlib import Path
import requests
# =============================================================================
# SETTINGS
# =============================================================================
DEFAULT_RELEASE = "DepMap Public 26Q1"
OUTDIR = Path("Feature1_DepMap") / "downloads"
REQUIRED_FILES = [
"README.txt",
"Gene.csv",
"Model.csv",
"CRISPRGeneEffect.csv",
"CRISPRGeneDependency.csv",
"OmicsExpressionTPMLogp1HumanProteinCodingGenes.csv",
]
OPTIONAL_FILES = [
"OmicsSomaticMutationsMatrixHotspot.csv",
"OmicsSomaticMutationsMatrixDamaging.csv",
"OmicsCNGeneWGS.csv",
"OmicsFusionFiltered.csv",
"SubtypeMatrix.csv",
"SubtypeTree.csv",
]
DO_NOT_DOWNLOAD = [
"PortalCompounds.csv",
]
FIGSHARE_API = "https://api.figshare.com/v2"
# =============================================================================
# API HELPERS
# =============================================================================
def make_session():
s = requests.Session()
s.headers.update({
"User-Agent": "Mozilla/5.0 DepMapDownloader/1.0",
"Accept": "application/json",
})
return s
def api_get(session, url, params=None, retries=5):
for attempt in range(1, retries + 1):
try:
r = session.get(url, params=params, timeout=120)
if r.status_code == 200:
return r.json()
print(f"[API WARNING] HTTP {r.status_code}: {url}")
print(str(r.text)[:500])
except Exception as e:
print(f"[API WARNING] Attempt {attempt}/{retries} failed: {e}")
if attempt < retries:
time.sleep(3 * attempt)
raise RuntimeError(f"API request failed: {url}")
def search_figshare_articles(session, release_name):
"""
Search Figshare for the DepMap release article.
"""
print("=" * 100)
print("[API] Searching Figshare articles")
print(f"[QUERY] {release_name}")
queries = [
release_name,
release_name.replace("DepMap Public ", "DepMap "),
release_name.replace("Public ", ""),
]
all_hits = []
for q in queries:
url = f"{FIGSHARE_API}/articles"
params = {
"search_for": q,
"page_size": 100,
"order": "published_date",
"order_direction": "desc",
}
hits = api_get(session, url, params=params)
for h in hits:
title = h.get("title", "")
if "depmap" in title.lower():
all_hits.append(h)
# Deduplicate by article id
dedup = {}
for h in all_hits:
dedup[h["id"]] = h
hits = list(dedup.values())
print(f"[API] Candidate articles found: {len(hits)}")
for h in hits[:20]:
print(f" - ID={h.get('id')} | {h.get('title')}")
return hits
def get_article_details(session, article_id):
url = f"{FIGSHARE_API}/articles/{article_id}"
return api_get(session, url)
def get_article_files(session, article_id):
url = f"{FIGSHARE_API}/articles/{article_id}/files"
return api_get(session, url)
def find_release_article_with_files(session, release_name, wanted_files):
"""
Find the Figshare article that contains the requested DepMap files.
"""
candidates = search_figshare_articles(session, release_name)
if not candidates:
raise RuntimeError(
f"No Figshare article candidates found for release: {release_name}"
)
best = None
best_score = -1
best_files = None
wanted_set = set(wanted_files)
print("=" * 100)
print("[API] Checking candidate article files")
for c in candidates:
article_id = c["id"]
title = c.get("title", "")
try:
files = get_article_files(session, article_id)
except Exception as e:
print(f"[SKIP] Could not fetch files for article {article_id}: {e}")
continue
names = {f.get("name") for f in files}
score = len(wanted_set.intersection(names))
print(f"[CHECK] ID={article_id} | matched {score}/{len(wanted_set)} | {title}")
if score > best_score:
best = c
best_score = score
best_files = files
if best is None or best_score == 0:
raise RuntimeError(
"Could not find a Figshare article containing the requested DepMap files. "
"The release name may be different on Figshare."
)
print("=" * 100)
print("[SELECTED ARTICLE]")
print(f"[ID] {best['id']}")
print(f"[TITLE] {best.get('title')}")
print(f"[MATCH] {best_score}/{len(wanted_set)} requested files found")
return best, best_files
# =============================================================================
# DOWNLOAD
# =============================================================================
def download_url(session, url, outpath, expected_size=None, retries=5):
if outpath.exists() and outpath.stat().st_size > 0:
print(f"[SKIP] Already exists: {outpath}")
print(f" Size: {outpath.stat().st_size / 1024 / 1024:.2f} MB")
return True
tmp = outpath.with_suffix(outpath.suffix + ".tmp")
for attempt in range(1, retries + 1):
try:
print("=" * 100)
print(f"[DOWNLOAD] {outpath.name}")
print(f"[SAVE TO] {outpath}")
with session.get(url, stream=True, timeout=300) as r:
if r.status_code != 200:
print(f"[HTTP] {r.status_code}")
print(str(r.text)[:500])
raise RuntimeError(f"HTTP {r.status_code}")
total = int(r.headers.get("content-length", 0))
if total == 0 and expected_size:
total = int(expected_size)
downloaded = 0
with open(tmp, "wb") as f:
for chunk in r.iter_content(chunk_size=1024 * 1024):
if not chunk:
continue
f.write(chunk)
downloaded += len(chunk)
if total > 0:
pct = downloaded / total * 100
print(
f"\r {downloaded / 1024 / 1024:.2f} MB / "
f"{total / 1024 / 1024:.2f} MB ({pct:.1f}%)",
end="",
flush=True,
)
else:
print(
f"\r {downloaded / 1024 / 1024:.2f} MB",
end="",
flush=True,
)
print()
tmp.replace(outpath)
if outpath.stat().st_size == 0:
raise RuntimeError("Downloaded file is empty")
print(f"[DONE] {outpath.name}")
print(f"[SIZE] {outpath.stat().st_size / 1024 / 1024:.2f} MB")
return True
except Exception as e:
print()
print(f"[FAILED] Attempt {attempt}/{retries}: {outpath.name}")
print(f"[ERROR] {e}")
if tmp.exists():
tmp.unlink()
if attempt < retries:
wait = 5 * attempt
print(f"[RETRY] Waiting {wait} seconds...")
time.sleep(wait)
return False
def main():
parser = argparse.ArgumentParser(
description="Download selected DepMap files using Figshare API."
)
parser.add_argument(
"--release",
default=DEFAULT_RELEASE,
help=f'DepMap release name. Default: "{DEFAULT_RELEASE}"',
)
parser.add_argument(
"--include-optional",
action="store_true",
help="Also download optional mutation/CN/fusion/subtype files.",
)
parser.add_argument(
"--outdir",
default=str(OUTDIR),
help=f"Output directory. Default: {OUTDIR}",
)
args = parser.parse_args()
outdir = Path(args.outdir)
outdir.mkdir(parents=True, exist_ok=True)
files_to_download = list(REQUIRED_FILES)
if args.include_optional:
files_to_download.extend(OPTIONAL_FILES)
files_to_download = [f for f in files_to_download if f not in DO_NOT_DOWNLOAD]
print("=" * 100)
print("DEPMAP DOWNLOAD ONLY USING FIGSHARE API")
print("=" * 100)
print(f"[RELEASE] {args.release}")
print(f"[OUTDIR] {outdir.resolve()}")
print("[FILES TO DOWNLOAD]")
for f in files_to_download:
print(f" - {f}")
print()
print("[NOT DOWNLOADING - LEAKAGE RISK]")
for f in DO_NOT_DOWNLOAD:
print(f" - {f}")
session = make_session()
article, article_files = find_release_article_with_files(
session=session,
release_name=args.release,
wanted_files=files_to_download,
)
file_map = {f["name"]: f for f in article_files}
print("=" * 100)
print("[AVAILABLE MATCHED FILES]")
for fname in files_to_download:
if fname in file_map:
f = file_map[fname]
size_mb = f.get("size", 0) / 1024 / 1024
print(f" [FOUND] {fname} ({size_mb:.2f} MB)")
else:
print(f" [MISSING] {fname}")
failed = []
missing = []
for fname in files_to_download:
if fname not in file_map:
missing.append(fname)
continue
f = file_map[fname]
download_link = f.get("download_url")
if not download_link:
print(f"[NO DOWNLOAD URL] {fname}")
failed.append(fname)
continue
ok = download_url(
session=session,
url=download_link,
outpath=outdir / fname,
expected_size=f.get("size"),
)
if not ok:
failed.append(fname)
print("=" * 100)
print("[FINISHED]")
print(f"[LOCATION] {outdir.resolve()}")
if missing:
print("[MISSING FROM FIGSHARE ARTICLE]")
for f in missing:
print(f" - {f}")
if failed:
print("[FAILED DOWNLOADS]")
for f in failed:
print(f" - {f}")
if not missing and not failed:
print("[ALL REQUESTED FILES DOWNLOADED]")
print("=" * 100)
if __name__ == "__main__":
main()