forked from ClawBio/ClawBio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclawbio.py
More file actions
1082 lines (991 loc) · 40 KB
/
Copy pathclawbio.py
File metadata and controls
1082 lines (991 loc) · 40 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
"""
clawbio.py — ClawBio Bioinformatics Skills Runner
Standalone CLI and importable module for running ClawBio skills.
Usage:
python clawbio.py list
python clawbio.py run pharmgx --demo
python clawbio.py run equity --input data.vcf
python clawbio.py run pharmgx --input patient.txt --output ./results
python clawbio.py upload --input patient.txt --patient-id PT001
python clawbio.py run pharmgx --profile profiles/PT001.json --output ./results
python clawbio.py run full-profile --profile profiles/PT001.json --output ./results
Importable:
# With the repository checkout on sys.path:
from clawbio import run_skill, list_skills, upload_profile
result = run_skill("pharmgx", demo=True)
"""
import argparse
import json
import os
import re
import subprocess
import sys
import time
from datetime import datetime
from pathlib import Path
# --------------------------------------------------------------------------- #
# Paths
# --------------------------------------------------------------------------- #
CLAWBIO_DIR = Path(__file__).resolve().parent
SKILLS_DIR = CLAWBIO_DIR / "skills"
EXAMPLES_DIR = CLAWBIO_DIR / "examples"
DEFAULT_OUTPUT_ROOT = CLAWBIO_DIR / "output"
PROFILES_DIR = CLAWBIO_DIR / "profiles"
# Python binary — use the same interpreter that launched clawbio.py
PYTHON = sys.executable
# --------------------------------------------------------------------------- #
# ANSI color support
# --------------------------------------------------------------------------- #
def _use_color() -> bool:
return sys.stdout.isatty() and os.environ.get("NO_COLOR") is None
_COLOR = _use_color()
BOLD = "\033[1m" if _COLOR else ""
DIM = "\033[2m" if _COLOR else ""
RED = "\033[31m" if _COLOR else ""
GREEN = "\033[32m" if _COLOR else ""
YELLOW = "\033[33m" if _COLOR else ""
CYAN = "\033[36m" if _COLOR else ""
WHITE = "\033[37m" if _COLOR else ""
BG_RED = "\033[41m" if _COLOR else ""
RESET = "\033[0m" if _COLOR else ""
def colorize_report_line(line: str) -> str:
"""Apply ANSI color to a report line based on clinical significance."""
stripped = line.strip()
if not stripped:
return line
if stripped.startswith("#"):
return f"{CYAN}{BOLD}{line}{RESET}"
upper = stripped.upper()
# Special: warfarin + avoid → red background
if "WARFARIN" in upper and "AVOID" in upper:
return f"{BG_RED}{WHITE}{BOLD}{line}{RESET}"
if "AVOID" in upper:
return f"{RED}{BOLD}{line}{RESET}"
if "CAUTION" in upper:
return f"{YELLOW}{line}{RESET}"
if "STANDARD" in upper or "| OK" in upper or "NORMAL" in upper:
return f"{GREEN}{line}{RESET}"
if stripped.startswith("---") or stripped.startswith("===") or stripped.startswith("| ---"):
return f"{DIM}{line}{RESET}"
return line
def print_boxed_header(title: str):
"""Print a Unicode rounded-box header."""
w = len(title) + 4
print(f"{CYAN}╭{'─' * w}╮{RESET}")
print(f"{CYAN}│ {BOLD}{title}{RESET}{CYAN} │{RESET}")
print(f"{CYAN}╰{'─' * w}╯{RESET}")
def _parse_md_table(text: str, header_start: str) -> list[list[str]]:
"""Extract data rows from a markdown table identified by its header."""
rows = []
found = False
for line in text.splitlines():
if header_start in line:
found = True
continue
if found:
if line.strip().startswith("| ---") or line.strip().startswith("|---"):
continue
if line.strip().startswith("|") and line.count("|") >= 3:
rows.append([c.strip() for c in line.split("|")[1:-1]])
elif rows:
break
return rows
def format_pharmgx_preview(report_text: str, report_path: str):
"""Render a rich, biologically insightful pharmgx report for the terminal."""
lines = report_text.splitlines()
# --- Extract metadata ---
meta = {}
for line in lines:
for key in ("Pharmacogenomic SNPs found", "Genes profiled",
"Drugs assessed", "Input", "Format detected"):
if f"**{key}**" in line:
meta[key] = line.split(":", 1)[-1].strip().strip("`* ")
# --- Extract gene profile rows ---
gene_rows = _parse_md_table(report_text, "| Gene | Full Name |")
# --- Extract drug summary rows ---
summary = {}
for row in _parse_md_table(report_text, "| Category | Count |"):
if len(row) >= 2:
summary[row[0]] = row[1]
# --- Extract actionable alerts ---
avoid_drugs, caution_drugs = [], []
section = None
for line in lines:
if "AVOID / USE ALTERNATIVE:" in line:
section = "avoid"
elif "USE WITH CAUTION:" in line:
section = "caution"
elif line.startswith("---") or (line.startswith("##") and "Actionable" not in line):
section = None
elif section and line.strip().startswith("- **"):
m = re.match(r'- \*\*(.+?)\*\* \((.+?)\) \[(.+?)]: (.+)', line.strip())
if m:
entry = {"drug": m[1], "brand": m[2], "genes": m[3], "rec": m[4]}
(avoid_drugs if section == "avoid" else caution_drugs).append(entry)
# === RENDER ===
W = 60
snps = meta.get("Pharmacogenomic SNPs found", "?")
n_genes = meta.get("Genes profiled", "?")
n_drugs = meta.get("Drugs assessed", "?")
fmt = meta.get("Format detected", "unknown")
# ── Header ──
print(f"\n{CYAN}╭{'─' * W}╮{RESET}")
print(f"{CYAN}│{RESET} {BOLD}{CYAN}ClawBio PharmGx Report{RESET}"
f"{' ' * (W - 24)}{CYAN}│{RESET}")
print(f"{CYAN}│{RESET} {DIM}Corpasome (CC0) · doi:10.6084/m9.figshare.693052{RESET}"
f"{' ' * (W - 51)}{CYAN}│{RESET}")
print(f"{CYAN}╰{'─' * W}╯{RESET}")
print()
print(f" {BOLD}{n_genes}{RESET} genes {DIM}·{RESET} "
f"{BOLD}{snps}{RESET} SNPs {DIM}·{RESET} "
f"{BOLD}{n_drugs}{RESET} drugs {DIM}·{RESET} "
f"{DIM}{fmt} format{RESET}")
# ── Critical findings ──
if avoid_drugs:
print(f"\n {BG_RED}{WHITE}{BOLD} {'▲ CRITICAL FINDING':^{W - 4}} {RESET}")
print(f" {RED}{'─' * W}{RESET}")
for d in avoid_drugs:
print(f" {RED}{BOLD}{d['drug']}{RESET} ({d['brand']}) "
f"{DIM}[{d['genes']}]{RESET}")
if d["drug"].lower() == "warfarin":
print()
print(f" {YELLOW}{BOLD}VKORC1{RESET}{YELLOW} rs9923231 {BOLD}TT{RESET}"
f" {DIM}→{RESET} Both copies carry the sensitivity allele.")
print(f" {DIM}This patient produces less vitamin K epoxide reductase,{RESET}")
print(f" {DIM}making them hyper-responsive to warfarin's mechanism.{RESET}")
print()
print(f" {YELLOW}{BOLD}CYP2C9{RESET}{YELLOW} *1/*2 {DIM}(rs1799853 CT){RESET}"
f" {DIM}→{RESET} Intermediate Metabolizer.")
print(f" {DIM}Warfarin is cleared ~40% more slowly than in *1/*1 carriers,{RESET}")
print(f" {DIM}causing the drug to accumulate at standard doses.{RESET}")
print()
print(f" {RED}{BOLD}Combined effect:{RESET} "
f"Standard doses risk {RED}{BOLD}life-threatening bleeding{RESET}.")
print(f" CPIC guidelines recommend {BOLD}50–80% dose reduction{RESET} or")
print(f" switching to a DOAC (apixaban, rivaroxaban).")
else:
print(f" {d['rec']}")
print(f" {RED}{'─' * W}{RESET}")
# ── Gene profile ──
print(f"\n {CYAN}{BOLD}Gene Profile{RESET}")
print(f" {DIM}{'─' * (W - 2)}{RESET}")
for row in gene_rows:
if len(row) < 4:
continue
gene, _, diplotype, phenotype = row[:4]
# Split off "(X/Y SNPs tested)" qualifier from diplotype for cleaner display
dip_match = re.match(r'^(.+?)\s*(\(\d/\d SNPs tested\))?$', diplotype)
dip_core = dip_match[1] if dip_match else diplotype
dip_note = f" {DIM}{dip_match[2]}{RESET}" if dip_match and dip_match[2] else ""
# Choose color by phenotype category
if "Unknown" in phenotype or "unmapped" in phenotype:
pc = YELLOW
phenotype_short = "Unknown"
extra = f" {DIM}(needs clinical testing){RESET}"
elif "High" in phenotype:
pc, phenotype_short, extra = RED, phenotype, ""
elif "Poor" in phenotype:
pc, phenotype_short, extra = RED, phenotype, ""
elif "Intermediate" in phenotype:
pc, phenotype_short, extra = YELLOW, "Intermediate", ""
elif "Non-expressor" in phenotype:
pc, phenotype_short, extra = DIM, "Non-expressor", ""
else:
pc, phenotype_short, extra = GREEN, "Normal", ""
wmark = f" {RED}← warfarin{RESET}" if gene in ("CYP2C9", "VKORC1") else ""
print(f" {BOLD}{gene:<10}{RESET} {DIM}{dip_core:<12}{RESET}"
f" {pc}{phenotype_short}{RESET}{extra}{dip_note}{wmark}")
# ── Drug summary ──
print(f"\n {CYAN}{BOLD}Drug Summary{RESET}")
print(f" {DIM}{'─' * (W - 2)}{RESET}")
buckets = [
("Avoid / use alternative", RED, BOLD),
("Use with caution", YELLOW, ""),
("Standard dosing", GREEN, ""),
("Insufficient data", DIM, ""),
]
for cat, color, bld in buckets:
count = summary.get(cat, "0")
b = BOLD if bld else ""
print(f" {color}{b}■{RESET} {color}{count:>2} {cat}{RESET}")
# ── Caution list ──
if caution_drugs:
print()
names = [f"{YELLOW}{BOLD}{d['drug']}{RESET}" for d in caution_drugs]
print(f" {YELLOW}Caution:{RESET} {f'{DIM}, {RESET}'.join(names)}")
# ── Footer ──
print(f"\n {DIM}Full report → {report_path}{RESET}")
print(f" {DIM}Disclaimer: research/educational use only — not a medical device{RESET}")
print(f"{BOLD}{'━' * W}{RESET}")
# --------------------------------------------------------------------------- #
# Skills registry
# --------------------------------------------------------------------------- #
SKILLS = {
"pharmgx": {
"script": SKILLS_DIR / "pharmgx-reporter" / "pharmgx_reporter.py",
"demo_args": [
"--input",
str(SKILLS_DIR / "pharmgx-reporter" / "demo_patient.txt"),
],
"description": "Pharmacogenomics reporter (12 genes, 31 SNPs, 51 drugs)",
"allowed_extra_flags": {"--weights"},
"api_module": "skills.pharmgx-reporter.api",
"accepts_genotypes": True,
},
"equity": {
"script": SKILLS_DIR / "equity-scorer" / "equity_scorer.py",
"demo_args": [
"--input",
str(EXAMPLES_DIR / "demo_populations.vcf"),
"--pop-map",
str(EXAMPLES_DIR / "demo_population_map.csv"),
],
"description": "HEIM equity scorer (FST, heterozygosity, population representation)",
"allowed_extra_flags": {"--weights", "--pop-map"},
"accepts_genotypes": False, # needs VCF/CSV file, not genotype dict
},
"nutrigx": {
"script": SKILLS_DIR / "nutrigx_advisor" / "nutrigx_advisor.py",
"demo_args": [
"--input",
str(SKILLS_DIR / "nutrigx_advisor" / "tests" / "synthetic_patient.csv"),
],
"description": "Nutrigenomics advisor (diet, vitamins, caffeine, lactose)",
"allowed_extra_flags": set(),
"accepts_genotypes": True,
},
"metagenomics": {
"script": SKILLS_DIR / "claw-metagenomics" / "metagenomics_profiler.py",
"demo_args": ["--demo"],
"description": "Metagenomics profiler (Kraken2, RGI/CARD, HUMAnN3)",
"allowed_extra_flags": set(),
"accepts_genotypes": False,
},
"scrna": {
"script": SKILLS_DIR / "scrna-orchestrator" / "scrna_orchestrator.py",
"demo_args": ["--demo"],
"description": "scRNA Orchestrator (Scanpy QC, doublet detection, clustering, annotation, optional latent downstream mode, dataset-level + within-cluster contrastive markers)",
"allowed_extra_flags": {
"--min-genes",
"--min-cells",
"--max-mt-pct",
"--n-top-hvg",
"--n-pcs",
"--n-neighbors",
"--use-rep",
"--leiden-resolution",
"--random-state",
"--top-markers",
"--contrast-groupby",
"--contrast-scope",
"--contrast-clusterby",
"--contrast-top-genes",
"--doublet-method",
"--annotate",
"--annotation-model",
},
"accepts_genotypes": False,
},
"scrna-embedding": {
"script": SKILLS_DIR / "scrna-embedding" / "scrna_embedding.py",
"demo_args": ["--demo"],
"description": "scRNA Embedding (scVI/scANVI latent embedding, optional batch integration, stable integrated h5ad export)",
"allowed_extra_flags": {
"--method",
"--layer",
"--batch-key",
"--labels-key",
"--unlabeled-category",
"--min-genes",
"--min-cells",
"--max-mt-pct",
"--n-top-hvg",
"--latent-dim",
"--max-epochs",
"--n-neighbors",
"--random-state",
"--accelerator",
},
"accepts_genotypes": False,
},
"compare": {
"script": SKILLS_DIR / "genome-compare" / "genome_compare.py",
"demo_args": ["--demo"],
"description": "Genome comparator (IBS vs George Church + ancestry estimation)",
"allowed_extra_flags": {"--no-figures", "--aims-panel", "--reference"},
"summary_default": True,
"accepts_genotypes": True,
},
"drugphoto": {
"script": SKILLS_DIR / "pharmgx-reporter" / "pharmgx_reporter.py",
"demo_args": [
"--input",
str(SKILLS_DIR / "genome-compare" / "data" / "manuel_corpas_23andme.txt.gz"),
],
"description": "Drug photo analysis (single-drug PGx lookup from photo identification)",
"allowed_extra_flags": {"--drug", "--dose"},
"summary_default": True,
"accepts_genotypes": True,
},
"prs": {
"script": SKILLS_DIR / "gwas-prs" / "gwas_prs.py",
"demo_args": ["--demo"],
"description": "GWAS Polygenic Risk Score calculator (PGS Catalog, 3000+ scores)",
"allowed_extra_flags": {"--trait", "--pgs-id", "--min-overlap", "--max-variants", "--build"},
"accepts_genotypes": True,
},
"clinpgx": {
"script": SKILLS_DIR / "clinpgx" / "clinpgx.py",
"demo_args": ["--demo"],
"description": "ClinPGx API query (gene-drug interactions, CPIC guidelines, drug labels)",
"allowed_extra_flags": {"--gene", "--genes", "--drug", "--drugs", "--no-cache"},
"no_input_required": True,
"accepts_genotypes": False,
},
"gwas": {
"script": SKILLS_DIR / "gwas-lookup" / "gwas_lookup.py",
"demo_args": ["--demo"],
"description": "GWAS Lookup — federated variant query across 9 genomic databases",
"allowed_extra_flags": {"--rsid", "--skip", "--no-figures", "--no-cache", "--max-hits"},
"no_input_required": True,
"accepts_genotypes": False,
},
"bigquery": {
"script": SKILLS_DIR / "bigquery-public" / "bigquery_public.py",
"demo_args": ["--demo"],
"description": "BigQuery Public — read-only SQL bridge for public datasets with local outputs",
"allowed_extra_flags": {
"--query",
"--location",
"--max-rows",
"--max-bytes-billed",
"--param",
"--dry-run",
"--list-datasets",
"--list-tables",
"--describe",
"--preview",
"--count-only",
"--paper",
"--note",
},
"allowed_extra_flags_without_values": {"--dry-run", "--count-only"},
"no_input_required": True,
"accepts_genotypes": False,
},
"profile": {
"script": SKILLS_DIR / "profile-report" / "profile_report.py",
"demo_args": ["--demo"],
"description": "Unified personal genomic profile report",
"allowed_extra_flags": {"--profile"},
"no_input_required": True,
"accepts_genotypes": False,
},
"galaxy": {
"script": SKILLS_DIR / "galaxy-bridge" / "galaxy_bridge.py",
"demo_args": ["--demo"],
"description": "Galaxy tool discovery and execution (8,000+ bioinformatics tools)",
"allowed_extra_flags": {"--search", "--list-categories", "--tool-details", "--run", "--max-results"},
"no_input_required": True,
"accepts_genotypes": False,
},
"bioc": {
"script": SKILLS_DIR / "bioconductor-bridge" / "bioconductor_bridge.py",
"demo_args": ["--demo"],
"description": "Bioconductor package discovery, workflow recommendation, setup, and starter code generation",
"allowed_extra_flags": {
"--search",
"--recommend",
"--workflow",
"--package-details",
"--docs-search",
"--package-docs",
"--list-domains",
"--setup",
"--install",
"--format",
"--modality",
"--container",
"--max-results",
},
"no_input_required": True,
"accepts_genotypes": False,
},
"illumina": {
"script": SKILLS_DIR / "illumina-bridge" / "illumina_bridge.py",
"demo_args": ["--demo"],
"description": "Illumina / DRAGEN bundle import and metadata normalization",
"allowed_extra_flags": {
"--vcf",
"--qc",
"--sample-sheet",
"--metadata-provider",
"--ica-project-id",
"--ica-run-id",
},
"accepts_genotypes": False,
},
"data-extract": {
"script": SKILLS_DIR / "data-extractor" / "data_extractor.py",
"demo_args": ["--demo"],
"description": "Extract numerical data from scientific figure images (Claude vision + OpenCV)",
"allowed_extra_flags": {"--web", "--port", "--plot-type"},
"api_module": "skills.data-extractor.data_extractor_api",
"accepts_genotypes": False,
},
"rnaseq": {
"script": SKILLS_DIR / "rnaseq-de" / "rnaseq_de.py",
"demo_args": ["--demo"],
"description": "Bulk/pseudo-bulk RNA-seq differential expression (QC + PCA + DE)",
"allowed_extra_flags": {
"--counts",
"--metadata",
"--formula",
"--contrast",
"--backend",
"--min-count",
"--min-samples",
},
},
"methylation": {
"script": SKILLS_DIR / "methylation-clock" / "methylation_clock.py",
"demo_args": [
"--input",
str(SKILLS_DIR / "methylation-clock" / "data" / "GSE139307_small.csv.gz"),
],
"description": "Epigenetic age from methylation clocks (PyAging)",
"no_input_required": True,
"allowed_extra_flags": {
"--geo-id",
"--clocks",
"--metadata-cols",
"--imputer-strategy",
"--skip-epicv2-aggregation",
"--verbose",
},
},
"diffviz": {
"script": SKILLS_DIR / "diff-visualizer" / "diff_visualizer.py",
"demo_args": ["--demo"],
"description": "Differential expression visualizer (bulk RNA-seq + scRNA downstream figure/report pack)",
"allowed_extra_flags": {
"--mode",
"--counts",
"--metadata",
"--adata",
"--top-genes",
"--label-top",
"--padj-threshold",
"--lfc-threshold",
"--min-basemean",
},
"accepts_genotypes": False,
},
"protocols-io": {
"script": SKILLS_DIR / "protocols-io" / "protocols_io.py",
"demo_args": ["--demo"],
"description": "protocols.io bridge — search, browse, and retrieve scientific protocols via REST API",
"allowed_extra_flags": {
"--login",
"--search",
"--protocol",
"--steps",
"--dump",
"--page-size",
"--page",
"--filter",
},
"no_input_required": True,
"accepts_genotypes": False,
},
"acmg": {
"script": SKILLS_DIR / "clinical-variant-reporter" / "clinical_variant_reporter.py",
"demo_args": ["--demo"],
"description": "ACMG/AMP clinical variant classifier (28-criteria, SF v3.2 screening)",
"allowed_extra_flags": {"--genes", "--assembly"},
"accepts_genotypes": False,
},
"llm-bench": {
"script": SKILLS_DIR / "llm-biobank-bench" / "llm_biobank_bench.py",
"demo_args": ["--demo"],
"description": "Benchmark LLMs on UK Biobank knowledge retrieval (4 tasks, 6 models)",
"allowed_extra_flags": {
"--task",
"--models",
"--schema19",
"--schema27",
},
"no_input_required": True,
"accepts_genotypes": False,
},
"mr": {
"script": SKILLS_DIR / "mendelian-randomisation" / "mendelian_randomisation.py",
"demo_args": ["--demo"],
"description": "Mendelian Randomisation — two-sample MR with IVW, Egger, weighted median/mode + full sensitivity",
"allowed_extra_flags": {"--instruments"},
"no_input_required": True,
"accepts_genotypes": False,
},
"affprot": {
"script": SKILLS_DIR / "affinity-proteomics" / "affinity_proteomics.py",
"demo_args": ["--demo", "--platform", "olink"],
"description": "Affinity proteomics — Olink NPX + SomaLogic SomaScan differential abundance",
"allowed_extra_flags": {
"--platform", "--meta", "--group-col", "--contrast",
"--fdr", "--fc", "--top-n", "--test",
},
"no_input_required": True,
"accepts_genotypes": False,
},
"gwas-pipe": {
"script": SKILLS_DIR / "gwas-pipeline" / "gwas_pipeline.py",
"demo_args": ["--demo"],
"description": "GWAS pipeline — PLINK2 QC + REGENIE two-step association (Manhattan, QQ, lead variants)",
"allowed_extra_flags": {
"--bed", "--bgen", "--pheno", "--covar",
"--trait-type", "--trait",
"--geno", "--mind", "--maf", "--hwe",
},
"no_input_required": True,
"accepts_genotypes": False,
},
"flow": {
"script": SKILLS_DIR / "flow-bio" / "flow_bio.py",
"demo_args": ["--demo"],
"description": "Flow.bio API bridge (pipelines, samples, projects, executions)",
"allowed_extra_flags": {
"--login", "--username", "--password", "--token", "--url",
"--pipelines", "--samples", "--projects", "--executions",
"--organisms", "--sample-types", "--data",
"--pipeline", "--sample", "--execution",
"--metadata-attributes",
"--pipeline-detail", "--sample-detail", "--execution-detail",
"--search", "--search-samples", "--upload-sample", "--name", "--sample-type",
"--reads1", "--reads2", "--organism", "--project",
"--run-pipeline", "--run-samples", "--run-data", "--run-params",
"--genome", "--json",
},
"no_input_required": True,
"accepts_genotypes": False,
},
}
# Skills that run in the full-profile pipeline (order matters)
FULL_PROFILE_PIPELINE = ["pharmgx", "nutrigx", "prs", "compare"]
# --------------------------------------------------------------------------- #
# list_skills
# --------------------------------------------------------------------------- #
def list_skills() -> dict:
"""Print available skills and return the registry dict."""
print(f"{BOLD}ClawBio Skills{RESET}")
print(f"{'═' * 55}")
for name, info in SKILLS.items():
script_exists = info["script"].exists()
status = f"{GREEN}OK{RESET}" if script_exists else f"{RED}MISSING{RESET}"
print(f" {BOLD}{name:<15}{RESET} {info['description']}")
print(f" {'':15} {DIM}script: {info['script'].name}{RESET} [{status}]")
print()
print(f"{DIM}Run a skill: python clawbio.py run <skill> --demo{RESET}")
print(f"{DIM}With input: python clawbio.py run <skill> --input <file>{RESET}")
print(f"{DIM}Upload once: python clawbio.py upload --input <file> --patient-id PT001{RESET}")
print(f"{DIM}Full profile: python clawbio.py run full-profile --profile profiles/PT001.json{RESET}")
return SKILLS
# --------------------------------------------------------------------------- #
# upload_profile
# --------------------------------------------------------------------------- #
def upload_profile(
input_path: str,
patient_id: str = "",
fmt: str = "auto",
) -> dict:
"""Parse a genetic file and save a PatientProfile.
Returns a dict with profile path and metadata.
"""
# Lazy import to avoid requiring clawbio package for basic subprocess usage
if str(CLAWBIO_DIR) not in sys.path:
sys.path.insert(0, str(CLAWBIO_DIR))
from clawbio.common.profile import PatientProfile
profile = PatientProfile.from_genetic_file(input_path, patient_id=patient_id, fmt=fmt)
PROFILES_DIR.mkdir(parents=True, exist_ok=True)
pid = profile.metadata["patient_id"]
profile_path = PROFILES_DIR / f"{pid}.json"
profile.save(profile_path)
return {
"success": True,
"profile_path": str(profile_path),
"patient_id": pid,
"genotype_count": profile.genotype_count,
"checksum": profile.metadata["checksum"],
}
# --------------------------------------------------------------------------- #
# run_skill
# --------------------------------------------------------------------------- #
def run_skill(
skill_name: str,
input_path: str | None = None,
output_dir: str | None = None,
demo: bool = False,
extra_args: list[str] | None = None,
timeout: int = 300,
profile_path: str | None = None,
) -> dict:
"""
Run a ClawBio skill as a subprocess.
Returns a structured dict with success status, output paths, and logs.
Importable by any agent (RoboTerri, RoboIsaac, Claude Code).
"""
# Handle full-profile virtual skill
if skill_name == "full-profile":
return _run_full_profile(
profile_path=profile_path,
input_path=input_path,
output_dir=output_dir,
timeout=timeout,
)
# Validate skill
skill_info = SKILLS.get(skill_name)
if not skill_info:
return {
"skill": skill_name,
"success": False,
"exit_code": -1,
"output_dir": None,
"files": [],
"stdout": "",
"stderr": f"Unknown skill '{skill_name}'. Available: {list(SKILLS.keys())}",
"duration_seconds": 0,
}
script_path = skill_info["script"]
if not script_path.exists():
return {
"skill": skill_name,
"success": False,
"exit_code": -1,
"output_dir": None,
"files": [],
"stdout": "",
"stderr": f"Script not found: {script_path}",
"duration_seconds": 0,
}
# If --profile is given, resolve the input file from the profile
resolved_input = input_path
if profile_path and not input_path and not demo:
if str(CLAWBIO_DIR) not in sys.path:
sys.path.insert(0, str(CLAWBIO_DIR))
from clawbio.common.profile import PatientProfile
profile = PatientProfile.load(profile_path)
stored_input = profile.metadata.get("input_file", "")
if stored_input:
# Resolve relative paths against CLAWBIO_DIR
p = Path(stored_input)
if not p.is_absolute():
p = CLAWBIO_DIR / p
if p.exists():
resolved_input = str(p.resolve())
# Build output directory
summary_mode = skill_info.get("summary_default", False) and not output_dir
if summary_mode:
out_dir = None
elif output_dir:
out_dir = Path(output_dir)
else:
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
out_dir = DEFAULT_OUTPUT_ROOT / f"{skill_name}_{ts}"
if out_dir:
out_dir.mkdir(parents=True, exist_ok=True)
# Build command
cmd = [PYTHON, str(script_path)]
if demo:
cmd.extend(skill_info["demo_args"])
elif resolved_input:
cmd.extend(["--input", str(resolved_input)])
elif not skill_info.get("no_input_required"):
return {
"skill": skill_name,
"success": False,
"exit_code": -1,
"output_dir": str(out_dir) if out_dir else None,
"files": [],
"stdout": "",
"stderr": "No input provided. Use --demo, --input <file>, or --profile <path>.",
"duration_seconds": 0,
}
if out_dir:
cmd.extend(["--output", str(out_dir)])
# SEC INT-001: filter extra_args against per-skill allowlist
if extra_args:
allowed = skill_info.get("allowed_extra_flags", set())
flags_without_values = skill_info.get("allowed_extra_flags_without_values", set())
blocked = {"--input", "--output", "--demo"}
filtered = []
i = 0
while i < len(extra_args):
flag = extra_args[i].split("=")[0]
if flag in blocked:
i += 2 if "=" not in extra_args[i] and i + 1 < len(extra_args) else i + 1
continue
if flag in allowed:
filtered.append(extra_args[i])
if (
"=" not in extra_args[i]
and flag not in flags_without_values
and i + 1 < len(extra_args)
and extra_args[i + 1].split("=")[0] not in allowed
and extra_args[i + 1].split("=")[0] not in blocked
):
filtered.append(extra_args[i + 1])
i += 1
i += 1
cmd.extend(filtered)
# Run subprocess
t0 = time.time()
try:
proc = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=timeout,
cwd=str(script_path.parent),
)
duration = round(time.time() - t0, 2)
except subprocess.TimeoutExpired:
duration = round(time.time() - t0, 2)
return {
"skill": skill_name,
"success": False,
"exit_code": -1,
"output_dir": str(out_dir) if out_dir else None,
"files": [],
"stdout": "",
"stderr": f"Timed out after {timeout} seconds.",
"duration_seconds": duration,
}
except Exception as e:
duration = round(time.time() - t0, 2)
return {
"skill": skill_name,
"success": False,
"exit_code": -1,
"output_dir": str(out_dir) if out_dir else None,
"files": [],
"stdout": "",
"stderr": str(e),
"duration_seconds": duration,
}
# Collect output files
if out_dir and out_dir.exists():
output_files = sorted(
[f.name for f in out_dir.rglob("*") if f.is_file()],
)
else:
output_files = []
result = {
"skill": skill_name,
"success": proc.returncode == 0,
"exit_code": proc.returncode,
"output_dir": str(out_dir) if out_dir else None,
"files": output_files,
"stdout": proc.stdout,
"stderr": proc.stderr,
"duration_seconds": duration,
}
# If profile was used, store the result back into it
if profile_path and result["success"] and out_dir:
_store_result_in_profile(profile_path, skill_name, out_dir)
return result
# --------------------------------------------------------------------------- #
# Full-profile pipeline
# --------------------------------------------------------------------------- #
def _run_full_profile(
profile_path: str | None,
input_path: str | None,
output_dir: str | None,
timeout: int = 300,
) -> dict:
"""Run all genotype-consuming skills sequentially, accumulating results."""
if not profile_path and not input_path:
return {
"skill": "full-profile",
"success": False,
"exit_code": -1,
"output_dir": None,
"files": [],
"stdout": "",
"stderr": "full-profile requires --profile or --input.",
"duration_seconds": 0,
}
# Create profile if only input was given
if not profile_path and input_path:
upload_result = upload_profile(input_path)
if not upload_result["success"]:
return {
"skill": "full-profile",
"success": False,
"exit_code": -1,
"output_dir": None,
"files": [],
"stdout": "",
"stderr": "Failed to create profile from input file.",
"duration_seconds": 0,
}
profile_path = upload_result["profile_path"]
# Setup output
if output_dir:
out_dir = Path(output_dir)
else:
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
out_dir = DEFAULT_OUTPUT_ROOT / f"full_profile_{ts}"
out_dir.mkdir(parents=True, exist_ok=True)
t0 = time.time()
all_results = {}
all_files = []
combined_stdout = []
combined_stderr = []
any_failure = False
for skill_name in FULL_PROFILE_PIPELINE:
skill_out = out_dir / skill_name
print(f" Running {skill_name}...")
result = run_skill(
skill_name=skill_name,
profile_path=profile_path,
output_dir=str(skill_out),
timeout=timeout,
)
all_results[skill_name] = {
"success": result["success"],
"exit_code": result["exit_code"],
"files": result["files"],
}
if result["stdout"]:
combined_stdout.append(f"=== {skill_name} ===\n{result['stdout']}")
if result["stderr"]:
combined_stderr.append(f"=== {skill_name} ===\n{result['stderr']}")
all_files.extend(result["files"])
if not result["success"]:
any_failure = True
print(f" WARNING: {skill_name} failed (exit {result['exit_code']})")
duration = round(time.time() - t0, 2)
# Write aggregate summary
summary = {
"pipeline": FULL_PROFILE_PIPELINE,
"profile": profile_path,
"results": all_results,
"completed_at": datetime.now().isoformat(),
}
summary_path = out_dir / "pipeline_summary.json"
summary_path.write_text(json.dumps(summary, indent=2, default=str))
return {
"skill": "full-profile",
"success": not any_failure,
"exit_code": 0 if not any_failure else 1,
"output_dir": str(out_dir),
"files": all_files + ["pipeline_summary.json"],
"stdout": "\n\n".join(combined_stdout),
"stderr": "\n\n".join(combined_stderr),
"duration_seconds": duration,
}
def _store_result_in_profile(profile_path: str, skill_name: str, out_dir: Path) -> None:
"""Load result.json from a skill's output and store it in the profile."""
try:
if str(CLAWBIO_DIR) not in sys.path:
sys.path.insert(0, str(CLAWBIO_DIR))
from clawbio.common.profile import PatientProfile
result_json = out_dir / "result.json"
if not result_json.exists():
return
profile = PatientProfile.load(profile_path)
result_data = json.loads(result_json.read_text())
profile.add_skill_result(skill_name, result_data)
profile.save(profile_path)
except Exception:
pass # Don't fail the main pipeline for profile storage issues
# --------------------------------------------------------------------------- #
# CLI entry point
# --------------------------------------------------------------------------- #
def main():
parser = argparse.ArgumentParser(
description="ClawBio — Bioinformatics Skills Runner",
)
sub = parser.add_subparsers(dest="command")
# list
sub.add_parser("list", help="List available skills")
# upload
upload_parser = sub.add_parser("upload", help="Upload genetic data and create a patient profile")
upload_parser.add_argument("--input", required=True, dest="input_path", help="Path to genetic data file")
upload_parser.add_argument("--patient-id", default="", help="Patient identifier (default: derived from filename)")
upload_parser.add_argument("--format", default="auto", help="File format: auto, 23andme, ancestry, vcf")
# run