|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Kurmangaliyev 2019: cell_type_annotations.json from the authors' per-stage clusters. |
| 3 | +
|
| 4 | +The authors analysed the 24 h and 48 h APF libraries separately and assigned each |
| 5 | +stage's eight clusters to the subtypes T4a-d / T5a-d. GEO (GSE126139) has one per-cell |
| 6 | +file per library, GSM*_clust24.txt / GSM*_clust48.txt (barcode<TAB>cluster: the subtype, |
| 7 | +or "filtered" for cells removed by the authors' quality control). A cell's stage (its |
| 8 | +library) and subtype give its author cluster: one entry per stage x subtype. |
| 9 | +
|
| 10 | + label <stage>_<subtype> (e.g. 24h_T4a) |
| 11 | + type "<subtype> neuron" (the type name in Hörmann 2020's type mappings) |
| 12 | + clustering the stage (24h, 48h) |
| 13 | + source_cluster_id the subtype label (the authors give the per-stage clusters no numbers) |
| 14 | + cells_by_library cells per GEO library (GSM title) |
| 15 | +
|
| 16 | +Also writes .cache/kurmangaliyev_2019/memberships.tsv.gz (cell<TAB>cluster, cells named |
| 17 | +<stage>_<10x barcode>, as in the combined matrix of combine_matrices.py) for the |
| 18 | +expression build. |
| 19 | +
|
| 20 | +Usage: |
| 21 | + uv run python projects/kurmangaliyev_2019/build_clusters.py |
| 22 | +""" |
| 23 | + |
| 24 | +from __future__ import annotations |
| 25 | + |
| 26 | +import gzip |
| 27 | +import json |
| 28 | +import re |
| 29 | +from pathlib import Path |
| 30 | + |
| 31 | +import yaml |
| 32 | + |
| 33 | +PROJ = Path(__file__).resolve().parent |
| 34 | +REPO_ROOT = PROJ.parents[1] |
| 35 | +CACHE = REPO_ROOT / ".cache" / "kurmangaliyev_2019" |
| 36 | +GEO = CACHE / "geo" |
| 37 | +GEO_URL = "https://ftp.ncbi.nlm.nih.gov/geo/samples/GSM3592nnn/{gsm}/suppl/{file}" |
| 38 | +# stage -> (GSM, GEO library title, per-cell cluster file) |
| 39 | +LIBRARIES = { |
| 40 | + "24h": ("GSM3592259", "T4/T5 24h APF", "GSM3592259_clust24.txt.gz"), |
| 41 | + "48h": ("GSM3592260", "T4/T5 48h APF", "GSM3592260_clust48.txt.gz"), |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +def main() -> None: |
| 46 | + cfg = yaml.safe_load((PROJ / "config.yaml").read_text()) |
| 47 | + names = {c["id"]: c["name"] for c in cfg["sample_build"]["clusterings"]} |
| 48 | + anns, members = [], [] |
| 49 | + for stage, (gsm, title, fname) in LIBRARIES.items(): |
| 50 | + path = GEO / fname |
| 51 | + if not path.exists(): |
| 52 | + raise SystemExit(f"missing {path}: download {GEO_URL.format(gsm=gsm, file=fname)}") |
| 53 | + cells: dict[str, list[str]] = {} |
| 54 | + with gzip.open(path, "rt") as fh: |
| 55 | + assert fh.readline().split() == ["barcode", "cluster"] |
| 56 | + for line in fh: |
| 57 | + barcode, cluster = line.split() |
| 58 | + if cluster == "filtered": |
| 59 | + continue |
| 60 | + assert re.fullmatch(r"T[45][a-d]", cluster), cluster |
| 61 | + cells.setdefault(cluster, []).append(f"{stage}_{barcode}-1") |
| 62 | + assert len(cells) == 8, (stage, sorted(cells)) |
| 63 | + for subtype in sorted(cells): |
| 64 | + label = f"{stage}_{subtype}" |
| 65 | + anns.append({ |
| 66 | + "label": label, |
| 67 | + "type": f"{subtype} neuron", |
| 68 | + "clustering": stage, |
| 69 | + "clustering_name": names[stage], |
| 70 | + "source_cluster_id": subtype, |
| 71 | + "cell_number": len(cells[subtype]), |
| 72 | + "cells_by_library": {title: len(cells[subtype])}, |
| 73 | + }) |
| 74 | + members += [(c, label) for c in cells[subtype]] |
| 75 | + with gzip.open(CACHE / "memberships.tsv.gz", "wt") as fh: |
| 76 | + fh.write("barcode\tcluster\n") |
| 77 | + for c, label in members: |
| 78 | + fh.write(f"{c}\t{label}\n") |
| 79 | + out = { |
| 80 | + "source": {"doi": cfg["source"]["doi"], "title": cfg["source"]["title"]}, |
| 81 | + "dataset": { |
| 82 | + "accession": cfg["dataset"]["accession"], |
| 83 | + "label_source": "the authors' per-cell cluster files on GEO (GSM3592259_clust24.txt, " |
| 84 | + "GSM3592260_clust48.txt: each cell's subtype, per stage)", |
| 85 | + "label_level": "author cluster (one per stage x subtype)", |
| 86 | + "validate_against": "paper (per-stage clusters assigned to the eight subtypes)", |
| 87 | + }, |
| 88 | + "annotations": anns, |
| 89 | + } |
| 90 | + (PROJ / "cell_type_annotations.json").write_text(json.dumps(out, indent=2, ensure_ascii=False) + "\n") |
| 91 | + print(f"wrote {len(anns)} clusters, {len(members)} cells") |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
0 commit comments