Download code/formula_structure.py from DBbun/oligotox-phase2-dataset: direct link, hf CLI and curl.
- Browser
- Download file 7.53 kB
-
https://huggingface.co/datasets/DBbun/oligotox-phase2-dataset/resolve/main/code/formula_structure.py
- Command line
-
hf download hf://datasets/DBbun/oligotox-phase2-dataset/code/formula_structure.py
-
curl -L -o formula_structure.py https://huggingface.co/datasets/DBbun/oligotox-phase2-dataset/resolve/main/code/formula_structure.py
7.53 kB
| """ | |
| Derive verified structural chemistry from published molecular formulas. | |
| The FDA label states a molecular formula for nearly every approved | |
| oligonucleotide. That formula is not just an identifier - it pins down real | |
| structure, independently of whether the label prints the sequence: | |
| P internucleotide linkages (+ any terminal phosphate) | |
| S phosphorothioate linkages | |
| F 2'-fluoro residues | |
| Na salt counterions, which equal the number of acidic phosphate groups | |
| N constrains base composition, since A and G carry 5 N, C carries 3, | |
| and T/U carry 2 | |
| So for molecules whose sequence appears only as a structural-formula image, the | |
| formula still yields retrieved - not recalled - chemistry. Where length and | |
| sugar chemistry are also known, base composition can be solved outright and | |
| cross-checked against the carbon and oxygen counts. | |
| Everything here derives from strings already retrieved from DailyMed, so each | |
| value inherits that provenance. | |
| """ | |
| import itertools | |
| import json | |
| import re | |
| from pathlib import Path | |
| import pandas as pd | |
| from formula_checksum import RIBO, DEOXY, MOE_ADDON, parse_formula, render | |
| HERE = Path(__file__).parent | |
| # Conjugates that are not GalNAc: avacincaptad pegol carries a branched PEG, | |
| # imetelstat a 3'-palmitoyl lipid, and defibrotide is a polydisperse mixture | |
| # rather than a single defined strand. | |
| OTHER_CONJUGATES = {"avacincaptad pegol", "imetelstat", "defibrotide"} | |
| # N atoms per nucleobase - the diagnostic that separates purines from pyrimidines | |
| N_PER_BASE = {"A": 5, "G": 5, "C": 3, "T": 2, "U": 2} | |
| # The derived counts below assume a phosphate backbone in which every P is one | |
| # internucleotide linkage and S marks a phosphorothioate. That holds only for | |
| # these two backbones. A phosphorodiamidate morpholino carries an extra | |
| # terminal P and no phosphodiester at all, so eteplirsen's P30 is neither 29 | |
| # linkages nor 30 phosphodiesters. | |
| PHOSPHATE_BACKBONES = {"full_phosphorothioate", "mixed_PS_PO_backbone"} | |
| def structural_facts(formula: str, conjugated: bool = False, | |
| phosphate_backbone: bool = True) -> dict: | |
| f = parse_formula(formula) | |
| out = { | |
| "n_P": f.get("P", 0), | |
| "n_S_phosphorothioate": f.get("S", 0), | |
| "n_F_2prime_fluoro": f.get("F", 0), | |
| "n_Na_counterions": f.get("Na", 0), | |
| "n_N": f.get("N", 0), | |
| "n_C": f.get("C", 0), | |
| "n_O": f.get("O", 0), | |
| } | |
| # Elemental counts are not backbone counts. A conjugate or a terminal | |
| # phosphate carries its own P and sometimes S, so for eplontersen P20 | |
| # accompanies 19 internucleotide linkages, and olpasiran and plozasiran show | |
| # S7 against 6 backbone phosphorothioates. Derived structural counts are | |
| # therefore emitted only for unconjugated linear molecules; elsewhere the | |
| # elemental totals stand alone. | |
| p, s = out["n_P"], out["n_S_phosphorothioate"] | |
| if p and not conjugated and phosphate_backbone: | |
| out["phosphorothioate_fraction_from_formula"] = round(s / p, 4) | |
| out["n_phosphodiester_from_formula"] = p - s | |
| out["derived_counts_basis"] = "unconjugated linear strand" | |
| else: | |
| out["derived_counts_basis"] = ( | |
| "elemental totals only; a conjugate, a terminal group, or a " | |
| "non-phosphate backbone places P or S outside the internucleotide " | |
| "linkages, so backbone counts do not follow from the formula") | |
| # Na normally titrates every acidic phosphate, so Na == P is the expected | |
| # relation for a fully ionised salt; a mismatch is worth flagging. | |
| if out["n_Na_counterions"]: | |
| out["salt_consistent_Na_eq_P"] = int(out["n_Na_counterions"] == p) | |
| return out | |
| def solve_base_composition(formula: str, n_res: int, sugars: list, | |
| all_pyrimidines_methylated: bool = True): | |
| """Find base compositions consistent with the stated formula. | |
| Brute force over (nA, nG, nC, nT) summing to n_res, rebuilding the formula | |
| for each and keeping exact matches on C, H, N and O. | |
| """ | |
| from formula_checksum import oligo_formula | |
| target = parse_formula(formula) | |
| n_link = n_res - 1 | |
| n_ps = target.get("S", 0) | |
| linkages = ["PS"] * n_ps + ["PO"] * (n_link - n_ps) | |
| sodium = target.get("Na", 0) > 0 | |
| hits = [] | |
| for nA in range(n_res + 1): | |
| for nG in range(n_res - nA + 1): | |
| for nC in range(n_res - nA - nG + 1): | |
| nT = n_res - nA - nG - nC | |
| # N count is cheap to test and eliminates almost everything | |
| if nA * 5 + nG * 5 + nC * 3 + nT * 2 != target.get("N", 0): | |
| continue | |
| bases = "A" * nA + "G" * nG + "C" * nC + "T" * nT | |
| residues = [ | |
| (b, s, all_pyrimidines_methylated and b in "CTU") | |
| for b, s in zip(bases, sugars) | |
| ] | |
| f = oligo_formula(residues, linkages, sodium_salt=sodium) | |
| if all(f.get(k, 0) == target.get(k, 0) for k in ("C", "H", "N", "O")): | |
| hits.append({"A": nA, "G": nG, "C": nC, "T_or_U": nT}) | |
| return hits | |
| def main() -> None: | |
| chem = pd.read_csv(HERE / "fda_oligo_chemistry.csv") | |
| rows = [] | |
| for _, r in chem.iterrows(): | |
| rec = {"drug": r["drug"], "molecular_formula": r.get("molecular_formula"), | |
| "source_url": r.get("source_url")} | |
| # Anything carrying chemistry beyond the bare strand - a GalNAc | |
| # cluster, a PEG, a lipid - puts P or S outside the backbone, so the | |
| # derived counts are withheld for these. | |
| # the column is 1.0 or NaN, and NaN is truthy as a float | |
| conj = (r.get("galnac_conjugated") == 1) or r["drug"] in OTHER_CONJUGATES | |
| rec["conjugated"] = int(conj) | |
| phos = r.get("backbone_chemistry") in PHOSPHATE_BACKBONES | |
| rec["phosphate_backbone"] = int(phos) | |
| if isinstance(r.get("molecular_formula"), str) and r["molecular_formula"]: | |
| rec.update(structural_facts(r["molecular_formula"], conjugated=conj, | |
| phosphate_backbone=phos)) | |
| rows.append(rec) | |
| df = pd.DataFrame(rows) | |
| cols = ["drug", "conjugated", "phosphate_backbone", "n_P", "n_S_phosphorothioate", "n_phosphodiester_from_formula", | |
| "phosphorothioate_fraction_from_formula", "n_F_2prime_fluoro", | |
| "n_Na_counterions", "salt_consistent_Na_eq_P", "molecular_formula"] | |
| print(df[[c for c in cols if c in df.columns]].to_string(index=False)) | |
| df.to_csv(HERE / "fda_oligo_formula_structure.csv", index=False) | |
| print(f"\nwrote fda_oligo_formula_structure.csv ({len(df)} drugs)") | |
| # Demonstrate composition solving on the uniform 2'-MOE gapmer class, where | |
| # length and sugar layout are known from the label. | |
| print("\n=== base composition solved from formula (2'-MOE gapmers) ===") | |
| known = { | |
| "inotersen": (20, ["2prime_MOE"] * 5 + ["DNA"] * 10 + ["2prime_MOE"] * 5, | |
| "C230H299N69Na19O121P19S19"), | |
| "tofersen": (20, ["2prime_MOE"] * 5 + ["DNA"] * 10 + ["2prime_MOE"] * 5, | |
| "C230H317N72O123P19S15"), | |
| } | |
| for drug, (n, sug, form) in known.items(): | |
| hits = solve_base_composition(form, n, sug) | |
| print(f" {drug:12s} {len(hits)} composition(s) consistent: {hits}") | |
| if __name__ == "__main__": | |
| main() | |