This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_fcs.py
75 lines (60 loc) · 2.02 KB
/
get_fcs.py
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
#!/usr/bin/env python
"""
Download all FCS files from Cytobank.
"""
import sys
import requests
from src.conf import *
API_URL = "https://premium.cytobank.org/cytobank/api/v1/"
OUTPUT_DIR = Path("data") / "fcs"
OUTPUT_DIR.mkdir(exist_ok=True, parents=True)
EXPERIMENTS = ["308893"]
def main():
request_type = "authenticate"
creds = json.load(open(Path("~").expanduser() / ".cytobank.auth.json"))
res1 = requests.post(API_URL + request_type, data=creds)
res1.raise_for_status()
headers = {"Authorization": "Bearer " + res1.json()["user"]["authToken"]}
# # List experiments
request_type = "experiments"
# res12 = requests.get(
# API_URL + request_type, headers=headers)
# res12.raise_for_status()
# res12.json()['experiments']
# EXPERIMENTS = ["309657", "309658", "309659", "309660"]
for experiment in EXPERIMENTS:
# # List FCSs
res2 = requests.get(
API_URL + request_type + f"/{experiment}/" + "fcs_files",
headers=headers,
)
res2.raise_for_status()
# # Download single FCS
fcs = {x["id"]: x["filename"] for x in res2.json()["fcsFiles"]}
for fid, fname in fcs.items():
output_file = OUTPUT_DIR / fname.replace(" ", "_")
if output_file.exists():
print(f"{output_file} already exists, skipping...")
continue
print(experiment, fid)
res3 = requests.get(
API_URL
+ request_type
+ f"/{experiment}/"
+ f"fcs_files/{fid}/download",
headers=headers,
)
with open(output_file, "wb") as f:
f.write(res3.content)
open(OUTPUT_DIR / "__done__", "w")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
quit()
# this complication here is a workaround for the SCU cluster
# which ipython processes somehow don't finish correctly
else:
quit()
finally:
quit()