Repository navigation
Expand file tree
/
Copy pathopentargets.py
More file actions
1014 lines (839 loc) · 29.8 KB
/
Copy pathopentargets.py
File metadata and controls
1014 lines (839 loc) · 29.8 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Open Targets Platform 26.03 clinical indications -> merge-compatible CSV.
Read existing Parquet files, check all partitions against the official release
listing (unless --offline), and overwrite original processed output only after
successful validation. No downloads unless --download-missing is passed.
Clinical indications aggregate clinical reports. Their evidence is not independent
of underlying AACT, ChEMBL, EMA, PMDA, etc. APPROVAL != PHASE_4.
"""
from __future__ import annotations
import argparse
import csv
import hashlib
import json
import re
from collections import Counter, defaultdict
from datetime import datetime, timezone
from pathlib import Path
from urllib.parse import urljoin, urlparse
import pyarrow.parquet as pq
import requests
ROOT = Path(__file__).resolve().parent
BASE = "https://ftp.ebi.ac.uk/pub/databases/opentargets/platform"
RELEASE = "26.03"
DATASETS = ("clinical_indication", "drug_molecule", "disease")
CORE = [
"drug_name", "drug_identifier", "drug_identifier_type",
"disease_or_condition_name", "disease_or_condition_identifier",
"disease_or_condition_identifier_type", "relationship_type", "evidence_type",
"source", "internal_source",
]
META = [
"drug_id", "disease_id", "disease_name",
"max_clinical_stage_for_indication", "clinical_phase_raw",
"clinical_stage_validation", "clinical_phase_numeric",
"clinical_stage_is_explicit_approval", "clinical_report_ids",
"clinical_report_ids_original", "clinical_report_count",
"opentargets_indication_id", "drug_name_lookup_status",
"disease_name_lookup_status", "dataset", "release",
"opentargets_source_parquet", "opentargets_source_row_number",
"evidence_granularity",
]
for entity in ("drug", "disease"):
META += [f"{entity}_identifier_{suffix}" for suffix in
("original", "namespace", "canonical", "validation")]
COLUMNS = CORE + META
REJECT_COLUMNS = ["source_parquet", "source_row_number", "indication_id",
"drug_id", "disease_id", "rejection_reason"]
STAGES = {
"UNKNOWN", "PRECLINICAL", "IND", "EARLY_PHASE_1", "PHASE_1",
"PHASE_1_2", "PHASE_2", "PHASE_2_3", "PHASE_3", "PREAPPROVAL",
"APPROVAL", "PHASE_4", "WITHDRAWAL",
}
PHASES = {f"PHASE_{n}": str(n) for n in range(1, 5)}
def now():
return datetime.now(timezone.utc).isoformat()
def section(s):
print("\n" + "=" * 90 + "\n" + s + "\n" + "=" * 90, flush=True)
def sha256(path):
h = hashlib.sha256()
with Path(path).open("rb") as stream:
for b in iter(lambda: stream.read(4 * 1024 * 1024), b""):
h.update(b)
return h.hexdigest()
def value(x):
return "" if x is None else str(x).strip()
def list_items(x):
if x is None:
return []
if hasattr(x, "tolist"):
x = x.tolist()
if isinstance(x, (list, tuple)):
return [value(v) for v in x if value(v)]
if value(x):
return [value(x)]
return []
def normalize_stage(raw):
"""Do not conflate marketing authorisation with Phase IV trials."""
original = value(raw)
key = re.sub(r"[\s/-]+", "_", original.upper()).strip("_")
variants = {"APPROVED": "APPROVAL", "LAUNCHED": "APPROVAL",
"PHASE_IV": "PHASE_4", "PHASE_III": "PHASE_3",
"PHASE_II": "PHASE_2", "PHASE_I": "PHASE_1",
"EARLY_PHASE_I": "EARLY_PHASE_1", "PHASE_I_II": "PHASE_1_2",
"PHASE_II_III": "PHASE_2_3", "PRE_APPROVAL": "PREAPPROVAL"}
key = variants.get(key, key)
if not original:
return "", "missing", "", "False"
if key not in STAGES:
return original, "unrecognised", "", "False"
return key, "recognised", PHASES.get(key, ""), str(key == "APPROVAL")
def identifier(x, entity):
"""Syntax-only validation; canonical syntax does not imply ontology mapping."""
original = value(x)
token = original.upper().replace("_", ":")
if not original:
return ("", "", "", "missing")
if entity == "drug":
if re.fullmatch(r"CHEMBL\d+", token):
return (original, "ChEMBL", f"CHEMBL:{token}", "syntax_valid")
return (original, "unresolved", "", "unrecognised_namespace")
for pattern, prefix, label, padding in (
(r"EFO:(\d+)", "EFO", "EFO", 7),
(r"MONDO:(\d+)", "MONDO", "MONDO", 7),
(r"HP:(\d+)", "HP", "HPO", 7),
(r"DOID:(\d+)", "DOID", "DOID", 0),
(r"(?:ORPHA|ORPHANET):(\d+)", "ORPHA", "Orphanet", 0),
(r"OMIM:(\d+)", "OMIM", "OMIM", 0),
(r"MESH:([CD]\d{6,9})", "MESH", "MeSH", 0),
):
m = re.fullmatch(pattern, token)
if m:
suffix = m.group(1).zfill(padding) if padding else m.group(1)
return (original, label, f"{prefix}:{suffix}", "syntax_valid")
return (original, "unresolved", "", "unrecognised_namespace")
def source_listing(release, dataset):
"""Discover official Parquet partition names, not source data values."""
base = f"{BASE}/{release}/output/{dataset}/"
response = requests.get(
base,
timeout=(25, 120),
headers={"User-Agent": "OT-Research-Exporter/3.0"},
)
response.raise_for_status()
hrefs = re.findall(r'href=["\']([^"\']+\.parquet)["\']', response.text, re.I)
result = {}
for href in hrefs:
full = urljoin(base, href)
parsed = urlparse(full)
if (
parsed.netloc != "ftp.ebi.ac.uk"
or parsed.path.rsplit("/", 1)[0]
!= urlparse(base).path.rstrip("/")
):
continue
filename = Path(parsed.path).name
if filename in result and result[filename] != full:
raise ValueError("Duplicate partition filename with different URLs")
result[filename] = full
if not result:
raise RuntimeError(
f"No parquet partitions found in official directory: {base}"
)
return result
def obtain(url, dest):
dest.parent.mkdir(parents=True, exist_ok=True)
tmp = dest.with_name(dest.name + ".download")
try:
with requests.get(url, stream=True, timeout=(30, 1800)) as r:
r.raise_for_status()
with tmp.open("wb") as f:
for piece in r.iter_content(4 * 1024 * 1024):
if piece:
f.write(piece)
if not tmp.is_file() or not tmp.stat().st_size:
raise RuntimeError(f"Empty downloaded parquet: {url}")
pq.ParquetFile(tmp)
tmp.replace(dest)
finally:
tmp.unlink(missing_ok=True)
def choose_files(workdir, release, dataset, offline, download_missing):
directory = workdir / release / dataset
local = sorted(
p for p in directory.glob("*.parquet") if p.is_file()
)
if offline:
if not local:
raise FileNotFoundError(
f"No offline parquet for {dataset} in {directory}"
)
print(
f"[OFFLINE/NOT INVENTORY VERIFIED] "
f"{dataset}: {len(local)} local files"
)
return local, False, []
listing = source_listing(release, dataset)
expected = set(listing)
existing = {p.name for p in local}
extras = sorted(existing - expected)
if extras:
raise RuntimeError(
f"Unexpected {dataset} parquet files: {extras[:15]}"
)
missing = sorted(expected - existing)
if missing and not download_missing:
raise FileNotFoundError(
f"{dataset}: {len(missing)} missing partitions: {missing[:15]}. "
"Pass --download-missing to fetch them, or --offline to inspect "
"existing files (not lockable)."
)
for name in missing:
print(f"[DOWNLOAD MISSING] {dataset}/{name}")
obtain(listing[name], directory / name)
found = sorted(directory / name for name in expected)
for p in found:
if p.stat().st_size == 0:
raise RuntimeError(f"Empty parquet {p}")
pq.ParquetFile(p)
print(
f"[VERIFIED INVENTORY] {dataset}: "
f"{len(found)} official parquet files, missing=0"
)
return found, True, sorted(expected)
def inspect_source(dataset, paths):
info = []
required = (
{"id", "name"}
if dataset != "clinical_indication"
else {
"id", "drugId", "diseaseId",
"maxClinicalStage", "clinicalReportIds",
}
)
total = 0
for path in paths:
p = pq.ParquetFile(path)
columns = p.schema_arrow.names
missing = required - set(columns)
if missing:
raise RuntimeError(
f"{path.name}: {dataset} missing source columns "
f"{sorted(missing)}"
)
n = p.metadata.num_rows
total += n
info.append({
"file": str(path),
"rows": n,
"columns": columns,
"bytes": path.stat().st_size,
"sha256": sha256(path),
})
print(
f" {path.name}: rows={n:,}; "
f"fields={len(columns)}; bytes={path.stat().st_size:,}"
)
print(" HEADER: " + " | ".join(columns))
print(
f"[{dataset}] {len(paths)} partitions; "
f"{total:,} records"
)
return info, total
def name_lookup(paths, kind):
"""Map unique names only; use identifier placeholder if conflicting."""
names = defaultdict(set)
for path in paths:
reader = pq.ParquetFile(path)
for batch in reader.iter_batches(
batch_size=40000,
columns=["id", "name"],
):
for row in batch.to_pylist():
id_ = value(row["id"])
name = value(row["name"])
if id_ and name:
names[id_].add(name)
mapping = {
k: next(iter(v))
for k, v in names.items()
if len(v) == 1
}
conflicts = {
k: sorted(v)
for k, v in names.items()
if len(v) > 1
}
print(
f"[{kind} LOOKUP] "
f"unambiguous={len(mapping):,}, "
f"conflicting IDs={len(conflicts):,}"
)
return mapping, conflicts
def optional_chembl_lookup(path):
if not path:
return {}, {}
names = defaultdict(set)
with Path(path).open(
encoding="utf-8-sig",
newline="",
) as f:
reader = csv.DictReader(f)
idcol = (
"drug_id"
if "drug_id" in reader.fieldnames
else "drug_identifier"
)
if (
idcol not in reader.fieldnames
or "drug_name" not in reader.fieldnames
):
raise RuntimeError(
"ChEMBL fallback CSV needs "
"drug_id/drug_identifier and drug_name"
)
for row in reader:
drug_id = value(row[idcol])
name = value(row["drug_name"])
if drug_id and name and name != drug_id:
names[drug_id].add(name)
clean = {
k: next(iter(v))
for k, v in names.items()
if len(v) == 1
}
conflicts = {
k: sorted(v)
for k, v in names.items()
if len(v) > 1
}
print(
f"[OPTIONAL ChEMBL MAP] "
f"unambiguous={len(clean):,} "
f"conflicts={len(conflicts):,}"
)
return clean, conflicts
def form_record(
raw,
drug_names,
disease_names,
drug_conflicts,
disease_conflicts,
chembl_names,
chembl_conflicts,
release,
partition,
row_index,
):
drug = value(raw.get("drugId"))
disease = value(raw.get("diseaseId"))
indication = value(raw.get("id"))
errors = []
if not drug:
errors.append("missing_drug_id")
if not disease:
errors.append("missing_disease_id")
if errors:
return None, {
"source_parquet": partition,
"source_row_number": row_index,
"indication_id": indication,
"drug_id": drug,
"disease_id": disease,
"rejection_reason": "|".join(errors),
}
dorig, dns, dcanonical, dvalid = identifier(drug, "drug")
sorig, sns, scanonical, svalid = identifier(disease, "disease")
if drug in drug_conflicts:
drug_name, drug_lookup = (
drug,
"conflicting_names_identifier_placeholder",
)
elif drug in drug_names:
drug_name, drug_lookup = drug_names[drug], "drug_molecule"
elif drug in chembl_conflicts:
drug_name, drug_lookup = (
drug,
"conflicting_chembl_names_identifier_placeholder",
)
elif drug in chembl_names:
drug_name, drug_lookup = (
chembl_names[drug],
"optional_chembl_csv",
)
else:
drug_name, drug_lookup = (
drug,
"unmatched_identifier_placeholder",
)
if disease in disease_conflicts:
disease_name, disease_lookup = (
disease,
"conflicting_names_identifier_placeholder",
)
elif disease in disease_names:
disease_name, disease_lookup = (
disease_names[disease],
"disease",
)
else:
disease_name, disease_lookup = (
disease,
"unmatched_identifier_placeholder",
)
original_stage = value(raw.get("maxClinicalStage"))
stage, stage_validation, phase_number, approval = normalize_stage(
original_stage
)
refs = list_items(raw.get("clinicalReportIds"))
normalized_refs = sorted(set(refs))
out = {
"drug_name": drug_name,
"drug_identifier": drug,
"drug_identifier_type": dns if dns else "unresolved",
"disease_or_condition_name": disease_name,
"disease_or_condition_identifier": disease,
"disease_or_condition_identifier_type": sns if sns else "unresolved",
"relationship_type": "clinical_indication",
"evidence_type": "opentargets_clinical_indication",
"source": "OpenTargets",
"internal_source": (
f"Open Targets Platform {release} clinical_indication"
),
"drug_id": drug,
"disease_id": disease,
"disease_name": disease_name,
"max_clinical_stage_for_indication": stage,
"clinical_phase_raw": original_stage,
"clinical_stage_validation": stage_validation,
"clinical_phase_numeric": phase_number,
"clinical_stage_is_explicit_approval": approval,
"clinical_report_ids": ";".join(normalized_refs),
"clinical_report_ids_original": json.dumps(
refs,
ensure_ascii=False,
),
"clinical_report_count": len(normalized_refs),
"opentargets_indication_id": indication,
"drug_name_lookup_status": drug_lookup,
"disease_name_lookup_status": disease_lookup,
"dataset": "clinical_indication",
"release": release,
"opentargets_source_parquet": partition,
"opentargets_source_row_number": row_index,
"evidence_granularity": "aggregated_clinical_indication",
"drug_identifier_original": dorig,
"drug_identifier_namespace": dns,
"drug_identifier_canonical": dcanonical,
"drug_identifier_validation": dvalid,
"disease_identifier_original": sorig,
"disease_identifier_namespace": sns,
"disease_identifier_canonical": scanonical,
"disease_identifier_validation": svalid,
}
return out, None
def run(args):
release = args.release
if release != RELEASE:
raise ValueError(
f"This script pins release {RELEASE}; got {release!r}"
)
workdir = args.workdir.resolve()
outfile = args.out.resolve()
metapath = args.metadata.resolve()
outfile.parent.mkdir(parents=True, exist_ok=True)
metapath.parent.mkdir(parents=True, exist_ok=True)
tmp = outfile.with_name(
outfile.name + ".processing.tmp"
)
rejected_path = outfile.with_name(
"opentargets_rejected_rows.csv"
)
reject_tmp = rejected_path.with_name(
rejected_path.name + ".processing.tmp"
)
report = {
"source": "OpenTargets",
"release": release,
"started_at_utc": now(),
"status": "started",
"complete_partition_inventory_verified": False,
"interpretation": (
"Aggregate drug-disease clinical indication; reports "
"not independent of constituent sources"
),
"core_columns": CORE,
"output_columns": COLUMNS,
}
try:
section("1. ORIGINAL RELEASE FILE INVENTORY")
all_paths = {}
inventories = {}
input_counts = {}
verified = True
for dataset in DATASETS:
paths, flag, names = choose_files(
workdir,
release,
dataset,
args.offline,
args.download_missing,
)
all_paths[dataset] = paths
info, count = inspect_source(dataset, paths)
inventories[dataset] = {
"partition_listing_verified": flag,
"official_parquet_filenames": names,
"files": info,
}
input_counts[dataset] = count
verified &= flag
report["datasets"] = inventories
report["input_rows"] = input_counts
report["complete_partition_inventory_verified"] = verified
section("2. EXACT SOURCE LOOKUPS / NAMING")
drug_names, drug_conflicts = name_lookup(
all_paths["drug_molecule"], "drug"
)
disease_names, disease_conflicts = name_lookup(
all_paths["disease"], "disease"
)
chembl_names, chembl_conflicts = optional_chembl_lookup(
args.chembl
)
report["name_lookup"] = {
"drug_unambiguous": len(drug_names),
"disease_unambiguous": len(disease_names),
"drug_conflicts": len(drug_conflicts),
"disease_conflicts": len(disease_conflicts),
"drug_conflict_examples": dict(
list(drug_conflicts.items())[:15]
),
"disease_conflict_examples": dict(
list(disease_conflicts.items())[:15]
),
"optional_chembl_unambiguous": len(chembl_names),
"optional_chembl_conflicts": len(chembl_conflicts),
}
print(
"JOIN: clinical_indication.drugId -> "
"drug_molecule.id (unique lookup)"
)
print(
"JOIN: clinical_indication.diseaseId -> "
"disease.id (unique lookup)"
)
print(
"AMBIGUOUS/MISSING NAMES: use identifier placeholder, "
"never arbitrary last name"
)
print(
"REPORT IDs: exact source list JSON plus "
"sorted unique list; not independent evidence"
)
print(
"STAGES: APPROVAL separate from PHASE_4 and WITHDRAWAL"
)
section(
"3. STREAM INDICATION ROWS AND KEEP REPORT PROVENANCE"
)
counters = Counter()
stages = Counter()
name_status = Counter()
disease_status = Counter()
ns_counts = Counter()
validation_counts = Counter()
pairs = set()
indication_ids = Counter()
sample = []
with (
tmp.open(
"w",
encoding="utf-8",
newline="",
) as full_f,
reject_tmp.open(
"w",
encoding="utf-8",
newline="",
) as rej_f,
):
writer = csv.DictWriter(
full_f,
fieldnames=COLUMNS,
)
writer.writeheader()
rejects = csv.DictWriter(
rej_f,
fieldnames=REJECT_COLUMNS,
)
rejects.writeheader()
for path in all_paths["clinical_indication"]:
parquet = pq.ParquetFile(path)
source_row = 0
columns = [
"id",
"drugId",
"diseaseId",
"maxClinicalStage",
"clinicalReportIds",
]
for batch in parquet.iter_batches(
batch_size=args.batch_size,
columns=columns,
):
for raw in batch.to_pylist():
source_row += 1
counters["input_rows"] += 1
record, reject = form_record(
raw,
drug_names,
disease_names,
drug_conflicts,
disease_conflicts,
chembl_names,
chembl_conflicts,
release,
path.name,
source_row,
)
if reject:
rejects.writerow(reject)
counters["rejected_rows"] += 1
continue
writer.writerow(record)
counters["output_rows"] += 1
counters[
"clinical_report_links_unique_per_aggregate_sum"
] += int(record["clinical_report_count"])
if record["clinical_report_count"] == 0:
counters[
"indications_without_report_ids"
] += 1
stages[
record["max_clinical_stage_for_indication"]
or "[missing]"
] += 1
name_status[
record["drug_name_lookup_status"]
] += 1
disease_status[
record["disease_name_lookup_status"]
] += 1
ns_counts[
record["disease_identifier_namespace"]
] += 1
validation_counts[
record["disease_identifier_validation"]
] += 1
pairs.add((
record["drug_identifier"],
record["disease_or_condition_identifier"],
))
if record["opentargets_indication_id"]:
indication_ids[
record["opentargets_indication_id"]
] += 1
else:
counters[
"missing_indication_ids"
] += 1
if len(sample) < max(1, args.head_n):
sample.append(record)
print(
f" {path.name}: source rows={source_row:,}"
)
if source_row != parquet.metadata.num_rows:
raise RuntimeError(
f"Parquet row count mismatch in {path}"
)
if (
counters["input_rows"]
!= input_counts["clinical_indication"]
):
raise RuntimeError(
"Clinical-indication partition rows do not reconcile"
)
if (
counters["input_rows"]
!= counters["output_rows"] + counters["rejected_rows"]
):
raise RuntimeError(
"Rejected + retained != input count"
)
if counters["output_rows"] == 0:
raise RuntimeError(
"No eligible Open Targets records; refusing overwrite"
)
duplicates = sum(
n - 1
for n in indication_ids.values()
if n > 1
)
report.update({
"row_counts": dict(counters),
"distinct_drug_disease_pairs": len(pairs),
"duplicate_nonempty_indication_ids": duplicates,
"clinical_stage_counts": dict(stages),
"drug_name_lookup_status_counts": dict(name_status),
"disease_name_lookup_status_counts": dict(disease_status),
"disease_namespace_counts": dict(ns_counts),
"disease_validation_counts": dict(validation_counts),
"sample_records": sample,
"merge_caveat": (
"The clinical_indication evidence is derived from "
"clinical reports and overlaps with AACT/ChEMBL; "
"retain clinical_report_ids and never assume independence."
),
})
section("4. OUTPUT VALIDATION AND COUNTS")
for k, v in counters.items():
print(f" {k}: {v:,}")
print(
" distinct drug-disease pairs:",
len(pairs),
)
print(
" repeated indication IDs:",
duplicates,
)
print(" stages:", dict(stages))
print(" drug names:", dict(name_status))
print(" disease names:", dict(disease_status))
print(" disease namespace:", dict(ns_counts))
print(
" output columns:",
" | ".join(COLUMNS),
)
print(
" first record:",
json.dumps(sample[0], indent=2)[:4500],
)
with tmp.open(
encoding="utf-8",
newline="",
) as f:
if next(csv.reader(f)) != COLUMNS:
raise RuntimeError("Output schema mismatch")
# Commit only after all source reads, count reconciliation
# and schema checks.
tmp.replace(outfile)
reject_tmp.replace(rejected_path)
report["output_file"] = str(outfile)
report["rejected_file"] = str(rejected_path)
report["output_sha256"] = sha256(outfile)
report["rejected_sha256"] = sha256(rejected_path)
report["finished_at_utc"] = now()
report["status"] = (
"success"
if verified
else "offline_unverified_inventory"
)
report_tmp = metapath.with_name(
metapath.name + ".processing.tmp"
)
report_tmp.write_text(
json.dumps(
report,
indent=2,
ensure_ascii=False,
default=str,
) + "\n",
encoding="utf-8",
)
report_tmp.replace(metapath)
print("[OUTPUT OVERWRITTEN]", outfile)
print("[AUDIT OVERWRITTEN]", rejected_path)
print("[METADATA OVERWRITTEN]", metapath)
if not verified:
print(
"[WARNING] --offline used: release partition "
"completeness NOT independently verified. "
"Do not lock yet."
)
except Exception as exc:
failure = metapath.with_name(
"opentargets_failed_run.json"
)
report.update({
"status": "failed",
"error": repr(exc),
"finished_at_utc": now(),
})
failure.parent.mkdir(
parents=True,
exist_ok=True,
)
failure.write_text(
json.dumps(
report,
indent=2,
default=str,
),
encoding="utf-8",
)
raise
finally:
tmp.unlink(missing_ok=True)
reject_tmp.unlink(missing_ok=True)
def main():
p = argparse.ArgumentParser(
description=__doc__,
)
p.add_argument(
"--release",
default=RELEASE,
)
p.add_argument(
"--workdir",
type=Path,
default=ROOT / "data/raw/opentargets",
)
p.add_argument(
"--out",
type=Path,
default=ROOT / "data/processed/opentargets_drug_disease.csv",
)
p.add_argument(
"--metadata",
type=Path,
default=ROOT / "data/metadata/opentargets_processing_metadata.json",
)
p.add_argument(
"--chembl",
type=Path,
default=None,
help="Optional unambiguous drug name fallback from processed ChEMBL",
)
p.add_argument(
"--download-missing",
action="store_true",
help="Download only missing official partitions",
)
p.add_argument(
"--offline",
action="store_true",
help=(
"Use cached parquet without online inventory check; "
"result NOT lockable until verified"
),
)
p.add_argument(
"--batch-size",
type=int,
default=30000,
)
p.add_argument(
"--head-n",
type=int,
default=3,
)
args = p.parse_args()