File size: 3,003 Bytes
1bc9257 | 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 | """Filter the small catalog first; downloads require --download and a byte budget."""
from __future__ import annotations
import argparse
import json
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from hesrt_io import RELEASE_TAG, download_sample, load_catalog
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
for field in ["organism", "technology", "gse", "gsm", "tissue", "disease"]:
parser.add_argument("--" + field)
parser.add_argument("--limit", type=int)
parser.add_argument("--download", action="store_true")
parser.add_argument("--max-gb", type=float, default=2.0)
parser.add_argument("--workers", type=int, default=4)
parser.add_argument("--revision", default=RELEASE_TAG)
parser.add_argument("--output", type=Path, default=Path("hesrt_subset"))
parser.add_argument("--cache-dir")
args = parser.parse_args()
if not 1 <= args.workers <= 8 or args.max_gb <= 0 or (args.limit is not None and args.limit < 1):
parser.error("Use 1-8 workers, a positive byte budget and a positive limit")
frame, revision = load_catalog(args.revision, args.cache_dir)
for argument, field in [("organism", "organism"), ("technology", "technology"), ("gse", "gse_id"), ("gsm", "gsm_id")]:
value = getattr(args, argument)
if value:
frame = frame.loc[frame[field].eq(value)]
for field in ["tissue", "disease"]:
value = getattr(args, field)
if value:
frame = frame.loc[frame[field].str.contains(value, case=False, regex=False, na=False)]
frame = frame.sort_values(["archive_bytes", "gsm_id"])
if args.limit is not None:
frame = frame.head(args.limit)
args.output.mkdir(parents=True, exist_ok=True)
frame.to_csv(args.output / "selection.csv", index=False)
summary = {"revision": revision, "samples": len(frame), "studies": int(frame.gse_id.nunique()),
"compressed_gb": float(frame.archive_bytes.sum()) / 1e9,
"uncompressed_gb": float(frame.uncompressed_bytes.sum()) / 1e9,
"mode": "download" if args.download else "dry_run"}
(args.output / "selection.json").write_text(json.dumps(summary, indent=2) + "\n")
print(json.dumps(summary, indent=2), flush=True)
if not len(frame):
parser.error("No matching samples; inspect available labels in the catalog")
if not args.download:
print("No sample packages downloaded. Add --download after reviewing selection.csv.")
return
if summary["compressed_gb"] > args.max_gb:
parser.error("Selection exceeds --max-gb; narrow it or explicitly raise the budget")
def fetch(row: dict) -> str:
return str(download_sample(row, args.output, revision, args.cache_dir))
with ThreadPoolExecutor(max_workers=args.workers) as pool:
for folder in pool.map(fetch, frame.to_dict("records")):
print(folder, flush=True)
if __name__ == "__main__":
main()
|