Datasets:
Formats:
csv
Languages:
English
Size:
< 1K
Tags:
chemistry
medicinal-chemistry
computational-chemistry
structure-based-design
ampa-receptor
molecular-design
Download registry/register_structures.py from PureOne/Xi-Spirane-4: direct link, hf CLI and curl.
- Browser
- Download file 6.65 kB
-
https://huggingface.co/datasets/PureOne/Xi-Spirane-4/resolve/main/registry/register_structures.py
- Command line
-
hf download hf://datasets/PureOne/Xi-Spirane-4/registry/register_structures.py
-
curl -L -o register_structures.py https://huggingface.co/datasets/PureOne/Xi-Spirane-4/resolve/main/registry/register_structures.py
6.65 kB
| """Generate and validate the neutral-parent structure register and the matched-isomer drawing.""" | |
| import argparse | |
| import csv | |
| from io import BytesIO | |
| from pathlib import Path | |
| from PIL import Image, ImageDraw, ImageFont | |
| from rdkit import Chem, rdBase | |
| from rdkit.Chem import Crippen, Descriptors, rdDepictor, rdMolDescriptors | |
| from rdkit.Chem.Draw import rdMolDraw2D | |
| HEAD = 'O=C(c1ccc2nccnc2c1)' | |
| DATA = [ | |
| ('Q-Ox', 'first synthesis target', 'N1CCC2(C1)COC2', 5, 4, 'CPBQSUFBANFXIW-UHFFFAOYSA-N'), | |
| ('Q-Az', 'same-formula orientation comparator', 'N1CC2(C1)CCOC2', 4, 5, 'NLHYZTOKDPADFN-UHFFFAOYSA-N'), | |
| ('Q-Ox-C', 'secondary carbon control for Q-Ox', 'N1CCC2(C1)CCC2', 5, 4, 'WAQOTSQSPYLNEQ-UHFFFAOYSA-N'), | |
| ('Q-Az-C', 'secondary carbon control for Q-Az', 'N1CC2(C1)CCCC2', 4, 5, 'BEPWJZWTOBTGHU-UHFFFAOYSA-N'), | |
| ('Q-C3', 'compact cyclopropane contour hypothesis', 'N1CCC2(C1)CC2', 5, 3, 'PBDSSTHUKFYOKD-UHFFFAOYSA-N'), | |
| ('Q-Pyr', 'known nonspiro reference, PubChem CID 16734346', 'N1CCCC1', 5, 0, 'ALJNIHFPBNQNRK-UHFFFAOYSA-N'), | |
| ('CX516', 'published reference, PubChem CID 148184', 'N1CCCCC1', 6, 0, 'ANDGGVOPIJEHOF-UHFFFAOYSA-N'), | |
| ] | |
| PROTON = 1.007276466621 | |
| SODIUM_ION = 22.989218 | |
| CORE = Chem.MolFromSmarts('c-[C](=O)-[N;r]') | |
| def register(row): | |
| name, role, tail, nsize, partner, key = row | |
| input_smiles = HEAD + tail | |
| mol = Chem.MolFromSmiles(input_smiles) | |
| assert mol is not None and Chem.GetFormalCharge(mol) == 0 | |
| assert Chem.MolToInchiKey(mol) == key and not list(Chem.FindPotentialStereo(mol)) | |
| assert len(mol.GetSubstructMatches(CORE)) == 1 | |
| ar_c, carbonyl, carbonyl_o, amide_n = mol.GetSubstructMatch(CORE) | |
| rings = mol.GetRingInfo().AtomRings() | |
| nring, = [r for r in rings if amide_n in r] | |
| assert len(nring) == nsize | |
| neighbors = [r for r in rings if r != nring and (set(r) & set(nring))] | |
| spiro_idx = '' | |
| if partner: | |
| pring, = neighbors | |
| shared, = set(pring) & set(nring) | |
| spiro_idx = shared + 1 | |
| assert len(pring) == partner | |
| if name.endswith('-C') or name == 'Q-C3': | |
| assert all(mol.GetAtomWithIdx(i).GetAtomicNum() == 6 for i in pring) | |
| else: | |
| assert sum(mol.GetAtomWithIdx(i).GetAtomicNum() == 8 for i in pring) == 1 | |
| else: | |
| assert not neighbors | |
| rdDepictor.Compute2DCoords(mol) | |
| mass = rdMolDescriptors.CalcExactMolWt(mol) | |
| info = { | |
| 'id': name, 'role': role, 'state': 'neutral parent; calculated', | |
| 'input_smiles': input_smiles, 'canonical_smiles': Chem.MolToSmiles(mol), | |
| 'standard_inchi': Chem.MolToInchi(mol), 'standard_inchikey': key, | |
| 'formula': rdMolDescriptors.CalcMolFormula(mol), | |
| 'mw_g_mol': f'{Descriptors.MolWt(mol):.3f}', | |
| 'neutral_exact_mass_Da': f'{mass:.8f}', | |
| 'calculated_M_plus_H_mz': f'{mass + PROTON:.8f}', | |
| 'calculated_M_plus_Na_mz': f'{mass + SODIUM_ION:.8f}', | |
| 'cLogP_Crippen': f'{Crippen.MolLogP(mol):.4f}', | |
| 'TPSA_A2': f'{rdMolDescriptors.CalcTPSA(mol):.2f}', | |
| 'HBA': rdMolDescriptors.CalcNumHBA(mol), 'HBD': rdMolDescriptors.CalcNumHBD(mol), | |
| 'formal_charge': Chem.GetFormalCharge(mol), | |
| 'potential_stereo_elements': len(list(Chem.FindPotentialStereo(mol))), | |
| 'amide_N_ring_size': nsize, 'partner_ring_size': partner, | |
| 'SDF_amide_N_atom_1based': amide_n + 1, 'SDF_spiro_C_atom_1based': spiro_idx, | |
| 'RDKit_version': rdBase.rdkitVersion, | |
| } | |
| mol.SetProp('_Name', name) | |
| for field, value in info.items(): | |
| mol.SetProp(field, str(value)) | |
| return mol, info | |
| def write_sdf(path, molecules): | |
| with Chem.SDWriter(str(path)) as writer: | |
| writer.SetForceV3000(True) | |
| for mol in molecules: | |
| writer.write(mol) | |
| restored = list(Chem.SDMolSupplier(str(path), removeHs=False)) | |
| assert len(restored) == len(molecules) | |
| for original, copy in zip(molecules, restored): | |
| assert copy and copy.GetProp('_Name') == original.GetProp('_Name') | |
| assert copy.GetProp('standard_inchikey') == Chem.MolToInchiKey(copy) | |
| assert Chem.MolToSmiles(copy) == Chem.MolToSmiles(original) | |
| assert not copy.GetConformer().Is3D() | |
| def font(size, bold=False): | |
| return ImageFont.truetype('DejaVuSans-Bold.ttf' if bold else 'DejaVuSans.ttf', size=size) | |
| def draw_pair(molecules, out): | |
| size = 1600, 545 | |
| image = Image.new('RGB', size, 'white') | |
| d = ImageDraw.Draw(image) | |
| ink, muted = '#132d30', '#365659' | |
| d.line([(800, 42), (800, 505)], fill='#d9e3e1', width=2) | |
| headings = [('Q-Ox', 'Pyrrolidine amide · Oxetane ether'), ('Q-Az', 'Azetidine amide · Tetrahydrofuran ether')] | |
| # Identical orientation of the common quinoxaline/carbonyl/amide-N framework. | |
| anchor = Chem.MolFromSmiles('O=C(c1ccc2nccnc2c1)N') | |
| rdDepictor.Compute2DCoords(anchor) | |
| for i, orig in enumerate(molecules[:2]): | |
| mol = Chem.Mol(orig) | |
| rdDepictor.GenerateDepictionMatching2DStructure(mol, anchor) | |
| drawer = rdMolDraw2D.MolDraw2DCairo(755, 395) | |
| opt = drawer.drawOptions() | |
| opt.padding = 0.13 | |
| opt.bondLineWidth = 3.0 | |
| opt.fixedBondLength = 47. | |
| drawer.DrawMolecule(mol) | |
| drawer.FinishDrawing() | |
| drawn = Image.open(BytesIO(drawer.GetDrawingText())).convert('RGB') | |
| x = i * 800 + 40 | |
| d.text((x, 21), headings[i][0], fill=ink, font=font(44, True)) | |
| d.text((x, 82), headings[i][1], fill=muted, font=font(25)) | |
| image.paste(drawn, (i * 800 + 21, 123)) | |
| image.save(out) | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--out', type=Path, default=Path(__file__).resolve().parent) | |
| args = parser.parse_args() | |
| args.out.mkdir(parents=True, exist_ok=True) | |
| records = [register(spec) for spec in DATA] | |
| mols = [x[0] for x in records] | |
| infos = [x[1] for x in records] | |
| assert len({x['standard_inchikey'] for x in infos}) == len(infos) | |
| for i, j in ((0, 1), (2, 3)): | |
| for field in ('formula', 'neutral_exact_mass_Da', 'cLogP_Crippen', 'TPSA_A2'): | |
| assert infos[i][field] == infos[j][field] | |
| write_sdf(args.out/'Xi_Spirane_4_Register.sdf', mols) | |
| write_sdf(args.out/'Q_Ox_Lead.sdf', mols[:1]) | |
| write_sdf(args.out/'Q_C3.sdf', [mols[4]]) | |
| with (args.out/'Identity_Descriptors.csv').open('w', newline='', encoding='utf-8') as fp: | |
| wr = csv.DictWriter(fp, fieldnames=infos[0]) | |
| wr.writeheader() | |
| wr.writerows(infos) | |
| draw_pair(mols, args.out/'Q_Ox_Q_Az.png') | |
| print(f'{len(mols)} neutral graphs, 2D V3000 roundtrips passed, RDKit {rdBase.rdkitVersion}') | |
| if __name__ == '__main__': | |
| main() | |