{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":67356,"databundleVersionId":8006601,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Install Required Libraries","metadata":{}},{"cell_type":"code","source":"!pip install rdkit","metadata":{"execution":{"iopub.status.busy":"2024-06-16T17:54:32.277109Z","iopub.execute_input":"2024-06-16T17:54:32.277918Z","iopub.status.idle":"2024-06-16T17:54:51.369724Z","shell.execute_reply.started":"2024-06-16T17:54:32.277886Z","shell.execute_reply":"2024-06-16T17:54:51.368439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import required libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nfrom rdkit import Chem\nfrom rdkit.Chem import AllChem","metadata":{"execution":{"iopub.status.busy":"2024-06-16T17:54:51.372273Z","iopub.execute_input":"2024-06-16T17:54:51.372643Z","iopub.status.idle":"2024-06-16T17:54:51.585671Z","shell.execute_reply.started":"2024-06-16T17:54:51.372610Z","shell.execute_reply":"2024-06-16T17:54:51.584398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convert to Atomic No. and its matrix","metadata":{}},{"cell_type":"code","source":"def xyz_parse(xyz):\n    nAtoms = int(xyz.split(\"\\n\")[0])\n    xyzmatrix = np.ndarray((nAtoms, 4), dtype=\"float\")\n    ANs = {'H': 1, 'He': 2, 'Li': 3, 'Be': 4, 'B': 5, 'C': 6, 'N': 7, 'O': 8, 'F': 9, 'Ne': 10, 'Na': 11, 'Mg': 12, 'Al': 13, 'Si': 14, 'P': 15, 'S': 16, 'Cl': 17, 'Ar': 18, 'K': 19, 'Ca': 20, 'Sc': 21, 'Ti': 22, 'V': 23, 'Cr': 24, 'Mn': 25, 'Fe': 26, 'Co': 27, 'Ni': 28, 'Cu': 29, 'Zn': 30, 'Ga': 31, 'Ge': 32, 'As': 33, 'Se': 34, 'Br': 35, 'Kr': 36, 'Rb': 37, 'Sr': 38, 'Y': 39, 'Zr': 40, 'Nb': 41, 'Mo': 42, 'Tc': 43, 'Ru': 44, 'Rh': 45, 'Pd': 46, 'Ag': 47, 'Cd': 48, 'In': 49, 'Sn': 50, 'Sb': 51, 'Te': 52, 'I': 53, 'Xe': 54, 'Cs': 55, 'Ba': 56, 'La': 57, 'Ce': 58, 'Pr': 59, 'Nd': 60, 'Pm': 61, 'Sm': 62, 'Eu': 63, 'Gd': 64, 'Tb': 65, 'Dy': 66, 'Ho': 67, 'Er': 68, 'Tm': 69, 'Yb': 70, 'Lu': 71, 'Hf': 72, 'Ta': 73, 'W': 74, 'Re': 75, 'Os': 76, 'Ir': 77, 'Pt': 78, 'Au': 79, 'Hg': 80, 'Tl': 81, 'Pb': 82, 'Bi': 83, 'Po': 84, 'At': 85, 'Rn': 86, 'Fr': 87, 'Ra': 88, 'Ac': 89, 'Th': 90, 'Pa': 91, 'U': 92, 'Np': 93, 'Pu': 94, 'Am': 95, 'Cm': 96, 'Bk': 97, 'Cf': 98, 'Es': 99, 'Fm': 100, 'Md': 101, 'No': 102, 'Lr': 103, 'Rf': 104, 'Db': 105, 'Sg': 106, 'Bh': 107, 'Hs': 108, 'Mt': 109}\n    i = 0\n    for line in xyz.split(\"\\n\"):\n        line = line.split()\n        if len(line) == 4:\n            xyzmatrix[i][0] = float(ANs[line[0]])\n            xyzmatrix[i][1] = float(line[1])\n            xyzmatrix[i][2] = float(line[2])\n            xyzmatrix[i][3] = float(line[3])\n            i+=1\n    return xyzmatrix","metadata":{"execution":{"iopub.status.busy":"2024-06-16T17:54:51.587021Z","iopub.execute_input":"2024-06-16T17:54:51.587368Z","iopub.status.idle":"2024-06-16T17:54:51.605611Z","shell.execute_reply.started":"2024-06-16T17:54:51.587338Z","shell.execute_reply":"2024-06-16T17:54:51.604379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generate Coulomb matrix from Spatial Matrix","metadata":{}},{"cell_type":"code","source":"def gen_coulombmatrix(xyzmatrix):\n    nAtoms = int(xyzmatrix.shape[0])\n    cij = np.zeros((nAtoms, nAtoms))\n    for i in range(nAtoms):\n        for j in range(nAtoms):\n            if i == j:\n                cij[i][j] = 0.5 * xyzmatrix[i][0] ** 2.4  # Diagonal term described by Potential energy of isolated atom\n            else:\n                dist = np.linalg.norm(np.array(xyzmatrix[i][1:]) - np.array(xyzmatrix[j][1:]))\n\n                cij[i][j] = xyzmatrix[i][0] * xyzmatrix[j][0] / dist  # Pair-wise repulsion\n    return cij","metadata":{"execution":{"iopub.status.busy":"2024-06-16T17:54:51.608774Z","iopub.execute_input":"2024-06-16T17:54:51.609244Z","iopub.status.idle":"2024-06-16T17:54:51.618973Z","shell.execute_reply.started":"2024-06-16T17:54:51.609205Z","shell.execute_reply":"2024-06-16T17:54:51.617757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Final","metadata":{}},{"cell_type":"code","source":"def smi2cm(smi, dimensions = 2, Hs = True, return_xyz = False):\n    mol = Chem.MolFromSmiles(smi)\n    if Hs:\n        mol = Chem.AddHs(mol)\n    else:\n        mol = Chem.RemoveHs(mol)\n    if int(dimensions) == 2:\n        AllChem.Compute2DCoords(mol)\n    elif int(dimensions) == 3:\n        AllChem.EmbedMolecule(mol)\n        AllChem.UFFOptimizeMolecule(mol)\n    else:\n        print(\"Invalid input for parameter: dimensions\\nPlease only use either 2 or 3\")\n    xyz = Chem.MolToXYZBlock(mol)\n    xyzmatrix = xyz_parse(xyz)\n    if return_xyz:\n        return xyzmatrix\n    else:\n        return gen_coulombmatrix(xyzmatrix)","metadata":{"execution":{"iopub.status.busy":"2024-06-16T17:54:51.620452Z","iopub.execute_input":"2024-06-16T17:54:51.620901Z","iopub.status.idle":"2024-06-16T17:54:51.634732Z","shell.execute_reply.started":"2024-06-16T17:54:51.620862Z","shell.execute_reply":"2024-06-16T17:54:51.633262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Usage","metadata":{}},{"cell_type":"code","source":"smi = \"CS(=O)(=O)C1=CC=C(C=C1)C(CCNC(=O)C2=CC=C(C=C2)C#N)C3=CC=C(C=C3)F\"\ncij = smi2cm(smi, 3, True)\ncij","metadata":{"execution":{"iopub.status.busy":"2024-06-16T17:54:51.636138Z","iopub.execute_input":"2024-06-16T17:54:51.636523Z","iopub.status.idle":"2024-06-16T17:54:51.828941Z","shell.execute_reply.started":"2024-06-16T17:54:51.636495Z","shell.execute_reply":"2024-06-16T17:54:51.827744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### References\n[1] Alexander van Teijlingen\n[2] Rupp, M.; Tkatchenko, A.; Müller, K. R.; Von Lilienfeld, O. A. Fast and Accurate Modeling of Molecular Atomization Energies with Machine Learning. Phys. Rev. Lett. 2012, 108 (5), 1–5. https://doi.org/10.1103/PhysRevLett.108.058301.","metadata":{}}]}