File size: 7,526 Bytes
f02a121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

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()