scripts/chembl_target.py
scripts/chembl_target.pyBrowse 4 files
620 tokens
2,203 bytes
Token encoding: o200k_base
Snapshot 24fd22b
← Back to SKILL.md
1#!/usr/bin/env python32"""3chembl_target.py — Search ChEMBL for a target and retrieve top active compounds.4Usage: python3 chembl_target.py "EGFR" --min-pchembl 7 --limit 205No external dependencies.6"""7import sys, json, time, argparse8import urllib.request, urllib.parse9 10BASE = "https://www.ebi.ac.uk/chembl/api/data"11 12def get(endpoint):13 try:14 req = urllib.request.Request(f"{BASE}{endpoint}", headers={"Accept":"application/json"})15 with urllib.request.urlopen(req, timeout=15) as r:16 return json.loads(r.read())17 except Exception as e:18 print(f"API error: {e}", file=sys.stderr); return None19 20def main():21 parser = argparse.ArgumentParser(description="ChEMBL target → active compounds")22 parser.add_argument("target")23 parser.add_argument("--min-pchembl", type=float, default=6.0)24 parser.add_argument("--limit", type=int, default=10)25 args = parser.parse_args()26 27 enc = urllib.parse.quote(args.target)28 data = get(f"/target/search?q={enc}&limit=5&format=json")29 if not data or not data.get("targets"):30 print("No targets found."); sys.exit(1)31 32 t = data["targets"][0]33 tid = t.get("target_chembl_id","")34 print(f"\nTarget: {t.get('pref_name')} ({tid})")35 print(f"Type: {t.get('target_type')} | Organism: {t.get('organism','N/A')}")36 print(f"\nFetching compounds with pChEMBL ≥ {args.min_pchembl}...\n")37 38 acts = get(f"/activity?target_chembl_id={tid}&pchembl_value__gte={args.min_pchembl}&assay_type=B&limit={args.limit}&order_by=-pchembl_value&format=json")39 if not acts or not acts.get("activities"):40 print("No activities found."); sys.exit(0)41 42 print(f"{'Molecule':<18} {'pChEMBL':>8} {'Type':<12} {'Value':<10} {'Units'}")43 print("-"*65)44 seen = set()45 for a in acts["activities"]:46 mid = a.get("molecule_chembl_id","N/A")47 if mid in seen: continue48 seen.add(mid)49 print(f"{mid:<18} {str(a.get('pchembl_value','N/A')):>8} {str(a.get('standard_type','N/A')):<12} {str(a.get('standard_value','N/A')):<10} {a.get('standard_units','N/A')}")50 time.sleep(0.1)51 print(f"\nTotal: {len(seen)} unique molecules")52 53if __name__ == "__main__": main()54