-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_source_index.py
More file actions
210 lines (180 loc) · 7.74 KB
/
Copy pathbuild_source_index.py
File metadata and controls
210 lines (180 loc) · 7.74 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
#!/usr/bin/env python3
"""
Scan docs and audit data for IGVFF*/ENCFF* accessions; resolve portal metadata
including file_format_specification; write generated/source-index.json and
data-sources/index.mdx.
"""
from __future__ import annotations
import argparse
import json
import re
import sys
import urllib.error
import urllib.parse
import urllib.request
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
from typing import Any
IGVF_FILE = re.compile(r"\bIGVFFI[A-Z0-9]+\b")
ENCODE_FILE = re.compile(r"\bENCFF[A-Z0-9]+\b")
IGVF_FILESET = re.compile(r"\bIGVFDS[A-Z0-9]+\b")
ENCODE_FILESET = re.compile(r"\bENCSR[A-Z0-9]+\b")
def collect_accessions(root: Path) -> set[str]:
found: set[str] = set()
patterns = (IGVF_FILE, ENCODE_FILE, IGVF_FILESET, ENCODE_FILESET)
for path in root.rglob("*"):
if path.suffix not in (".mdx", ".md", ".tsv", ".json", ".py"):
continue
if "audit_snapshots" in path.parts or "node_modules" in path.parts:
continue
try:
text = path.read_text(encoding="utf-8", errors="replace")
except OSError:
continue
for pat in patterns:
found.update(pat.findall(text))
return found
def portal_kind(accession: str) -> str:
if accession.startswith("IGVF"):
return "igvf"
return "encode"
def fetch_portal_json(accession: str, timeout: float = 30) -> dict[str, Any] | None:
kind = portal_kind(accession)
if kind == "igvf":
url = f"https://data.igvf.org/{accession}?format=json"
else:
url = f"https://www.encodeproject.org/{accession}/?format=json"
req = urllib.request.Request(
url,
headers={
"Accept": "application/json",
"User-Agent": "igvf-catalog-docs/1.0 (documentation build)",
},
)
try:
with urllib.request.urlopen(req, timeout=timeout) as resp:
return json.loads(resp.read().decode("utf-8"))
except (urllib.error.HTTPError, urllib.error.URLError, json.JSONDecodeError, TimeoutError):
return None
def link_for(accession: str) -> str:
if accession.startswith("IGVF"):
return f"https://data.igvf.org/{accession}"
return f"https://www.encodeproject.org/{accession}/"
def extract_format_spec(obj: dict[str, Any]) -> dict[str, str]:
spec = obj.get("file_format_specification")
if not spec:
return {}
if isinstance(spec, str):
return {"@id": spec, "accession": spec.rstrip("/").split("/")[-1]}
if isinstance(spec, dict):
acc = spec.get("accession") or (spec.get("@id") or "").rstrip("/").split("/")[-1]
return {"@id": spec.get("@id", ""), "accession": acc, "title": spec.get("title", "")}
return {}
def summarize(obj: dict[str, Any], accession: str) -> dict[str, Any]:
fmt_spec = extract_format_spec(obj)
fmt_spec_link = ""
if fmt_spec.get("accession"):
base = "https://data.igvf.org" if accession.startswith("IGVF") else "https://www.encodeproject.org"
fmt_spec_link = f"{base}/{fmt_spec['accession']}/"
lab = obj.get("lab") or {}
lab_title = lab.get("title", "") if isinstance(lab, dict) else str(lab)
assay = obj.get("assay_title") or obj.get("preferred_assay_title") or ""
if isinstance(assay, list):
assay = ", ".join(str(x) for x in assay)
return {
"accession": accession,
"portal": portal_kind(accession),
"portal_link": link_for(accession),
"title": obj.get("title") or obj.get("accession") or accession,
"file_format": obj.get("file_format", ""),
"output_type": obj.get("output_type", ""),
"assay_title": assay,
"status": obj.get("status", ""),
"lab": lab_title,
"file_format_specification": fmt_spec.get("accession", ""),
"file_format_specification_link": fmt_spec_link,
"schema_version": obj.get("schema_version", ""),
}
def resolve_one(accession: str, timeout: float) -> dict[str, Any]:
obj = fetch_portal_json(accession, timeout)
if obj is None:
return {
"accession": accession,
"portal": portal_kind(accession),
"portal_link": link_for(accession),
"error": "fetch_failed",
}
return summarize(obj, accession)
def write_index_mdx(entries: list[dict[str, Any]], out_path: Path, cfg: dict[str, Any]) -> None:
files = [e for e in entries if e["accession"].startswith(("IGVFF", "ENCFF"))]
files.sort(key=lambda x: x["accession"])
lines = [
"---",
"title: 'Data Sources Index'",
"description: 'IGVF and ENCODE portal files referenced by the Catalog'",
"icon: 'database'",
"---",
"",
"<Warning>",
cfg.get("banner", ""),
"</Warning>",
"",
"# Data Sources Index",
"",
"Catalog edges often reference source files via **`files_fileset`**. "
"Each file below links to its portal record and **file format specification** "
"(column definitions for the raw file).",
"",
"See also [Files & Filesets](/data-sources/files-filesets) and [Field lineage](/data-sources/field-lineage/index).",
"",
"| File | Portal | Format | Format spec | Assay | Lab | Status |",
"|------|--------|--------|-------------|-------|-----|--------|",
]
for e in files:
acc = e["accession"]
portal = f"[{acc}]({e.get('portal_link', '#')})"
fmt = e.get("file_format", "") or "—"
spec_acc = e.get("file_format_specification", "")
spec_link = e.get("file_format_specification_link", "")
spec_cell = f"[{spec_acc}]({spec_link})" if spec_acc and spec_link else (spec_acc or "—")
assay = (e.get("assay_title") or "—").replace("|", "\\|")[:40]
lab = (e.get("lab") or "—").replace("|", "\\|")[:30]
status = e.get("status") or "—"
lines.append(f"| {portal} | {e.get('portal', '')} | {fmt} | {spec_cell} | {assay} | {lab} | {status} |")
if not files:
lines.append("| _No file accessions resolved yet — run `python3 scripts/build_source_index.py`_ | | | | | | |")
lines.append("")
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text("\n".join(lines) + "\n", encoding="utf-8")
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--root", default=".")
ap.add_argument("--json-out", default="generated/source-index.json")
ap.add_argument("--mdx-out", default="data-sources/index.mdx")
ap.add_argument("--config", default="openapi/config.json")
ap.add_argument("--timeout", type=float, default=30.0)
ap.add_argument("--workers", type=int, default=6)
ap.add_argument("--max", type=int, default=0, help="Max accessions to resolve (0=all)")
args = ap.parse_args()
root = Path(args.root)
cfg = json.loads(Path(args.config).read_text(encoding="utf-8"))
accessions = sorted(collect_accessions(root))
if args.max:
accessions = accessions[: args.max]
print(f"Found {len(accessions)} accessions", file=sys.stderr)
entries: list[dict[str, Any]] = []
with ThreadPoolExecutor(max_workers=args.workers) as ex:
futs = {ex.submit(resolve_one, a, args.timeout): a for a in accessions}
for fut in as_completed(futs):
entries.append(fut.result())
entries.sort(key=lambda x: x["accession"])
json_out = Path(args.json_out)
json_out.parent.mkdir(parents=True, exist_ok=True)
with json_out.open("w", encoding="utf-8") as f:
json.dump({"accessions": entries, "count": len(entries)}, f, indent=2)
f.write("\n")
write_index_mdx(entries, Path(args.mdx_out), cfg)
print(f"Wrote {json_out} and {args.mdx_out}", file=sys.stderr)
return 0
if __name__ == "__main__":
raise SystemExit(main())