{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:90%;letter-spacing:0.5px;margin:0\"><b> </b>Import Libraries</p></div>","metadata":{}},{"cell_type":"code","source":"%pip install rdkit","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:23:14.749619Z","iopub.execute_input":"2023-09-26T06:23:14.750033Z","iopub.status.idle":"2023-09-26T06:23:30.248072Z","shell.execute_reply.started":"2023-09-26T06:23:14.750000Z","shell.execute_reply":"2023-09-26T06:23:30.246801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Reference:[RDKit](https://www.rdkit.org/docs/Cookbook.html)","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom rdkit import Chem\nfrom rdkit.Chem.Draw import IPythonConsole\nfrom rdkit.Chem import Draw\nfrom rdkit.Chem import AllChem\nfrom rdkit.Chem.Draw import rdMolDraw2D\nimport io\nfrom PIL import Image\nfrom collections import defaultdict\n\nIPythonConsole.ipython_useSVG=True  #< set this to False if you want PNGs instead of SVGs","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:23:36.256485Z","iopub.execute_input":"2023-09-26T06:23:36.256954Z","iopub.status.idle":"2023-09-26T06:23:36.769913Z","shell.execute_reply.started":"2023-09-26T06:23:36.256890Z","shell.execute_reply":"2023-09-26T06:23:36.768910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:90%;letter-spacing:0.5px;margin:0\"><b> </b>Load data💾</p></div>","metadata":{}},{"cell_type":"code","source":"d_train = pd.read_parquet('/kaggle/input/open-problems-single-cell-perturbations/de_train.parquet')\nd_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:23:42.078934Z","iopub.execute_input":"2023-09-26T06:23:42.079512Z","iopub.status.idle":"2023-09-26T06:23:45.591776Z","shell.execute_reply.started":"2023-09-26T06:23:42.079478Z","shell.execute_reply":"2023-09-26T06:23:45.590446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d_train.tail()","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:23:49.774209Z","iopub.execute_input":"2023-09-26T06:23:49.774623Z","iopub.status.idle":"2023-09-26T06:23:49.801684Z","shell.execute_reply.started":"2023-09-26T06:23:49.774594Z","shell.execute_reply":"2023-09-26T06:23:49.800336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:90%;letter-spacing:0.5px;margin:0\"><b> </b>Smiles List</p></div>","metadata":{}},{"cell_type":"code","source":"d_train['SMILES'].to_csv('smiles_list.txt', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:25:04.850649Z","iopub.execute_input":"2023-09-26T06:25:04.851088Z","iopub.status.idle":"2023-09-26T06:25:04.863812Z","shell.execute_reply.started":"2023-09-26T06:25:04.851053Z","shell.execute_reply":"2023-09-26T06:25:04.862854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('smiles_list.txt', 'r') as file:\n    content = file.read()\n\nprint(content)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:25:08.663995Z","iopub.execute_input":"2023-09-26T06:25:08.664864Z","iopub.status.idle":"2023-09-26T06:25:08.670334Z","shell.execute_reply.started":"2023-09-26T06:25:08.664832Z","shell.execute_reply":"2023-09-26T06:25:08.669325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:90%;letter-spacing:0.5px;margin:0\"><b> </b>Drawing Molecules</p></div>\n\n<p style=\"color: darkblue; font-size: 16px;\">\nA molecule is a group of atoms bonded together, representing the smallest fundamental unit of a chemical compound that retains the chemical properties of that compound. These atoms can be of the same or different elements, and they are held together by chemical bonds, which are interactions between the electrons of the atoms.</p>\n\n<p style=\"color: darkblue; font-size: 16px;\">Molecules can range in complexity from simple diatomic molecules like oxygen (O2), which consists of two oxygen atoms bonded together, to highly complex and large molecules like DNA or proteins, which are made up of thousands or even millions of atoms bonded together in specific arrangements.</p>\n\n<p style=\"color: darkblue; font-size: 16px;\">Molecules are the building blocks of matter and are central to the understanding of chemistry and biochemistry, as they play a crucial role in chemical reactions, biological processes, and the structure and function of materials in the world around us.</p>\n \n#### 🟠 Draw a molecule with atom index numbers.","metadata":{}},{"cell_type":"code","source":"def mol_with_atom_index(mol):\n    for atom in mol.GetAtoms():\n        atom.SetAtomMapNum(atom.GetIdx())\n    return mol\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:25:23.237527Z","iopub.execute_input":"2023-09-26T06:25:23.237942Z","iopub.status.idle":"2023-09-26T06:25:23.243858Z","shell.execute_reply.started":"2023-09-26T06:25:23.237909Z","shell.execute_reply":"2023-09-26T06:25:23.242506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mol = Chem.MolFromSmiles(\"COc1cc2c(Nc3ccc(Br)cc3F)ncnc2cc1OCC1CCN(C)CC1\")\n# Default\nmol","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:25:27.528212Z","iopub.execute_input":"2023-09-26T06:25:27.528680Z","iopub.status.idle":"2023-09-26T06:25:27.555887Z","shell.execute_reply.started":"2023-09-26T06:25:27.528647Z","shell.execute_reply":"2023-09-26T06:25:27.554852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With atom index\nmol_with_atom_index(mol)","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:25:31.726355Z","iopub.execute_input":"2023-09-26T06:25:31.726785Z","iopub.status.idle":"2023-09-26T06:25:31.781127Z","shell.execute_reply.started":"2023-09-26T06:25:31.726754Z","shell.execute_reply":"2023-09-26T06:25:31.780019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"color: darkgreen; font-size: 16px;\">An alternative method for incorporating atom indices is by modifying the properties of the IPythonConsole. This results in an image similar to the one shown above, with the key distinction being that the atom indices are positioned in close proximity to the respective atoms rather than directly on top of them.</p>","metadata":{}},{"cell_type":"code","source":"IPythonConsole.drawOptions.addAtomIndices = True\nIPythonConsole.molSize = 300,300","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:25:36.073097Z","iopub.execute_input":"2023-09-26T06:25:36.074013Z","iopub.status.idle":"2023-09-26T06:25:36.079518Z","shell.execute_reply.started":"2023-09-26T06:25:36.073970Z","shell.execute_reply":"2023-09-26T06:25:36.078380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mol = Chem.MolFromSmiles(\"COc1cc2c(Nc3ccc(Br)cc3F)ncnc2cc1OCC1CCN(C)CC1\")\nmol","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:25:39.415738Z","iopub.execute_input":"2023-09-26T06:25:39.416150Z","iopub.status.idle":"2023-09-26T06:25:39.454104Z","shell.execute_reply.started":"2023-09-26T06:25:39.416121Z","shell.execute_reply":"2023-09-26T06:25:39.452936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:90%;letter-spacing:0.5px;margin:0\"><b> </b>Calculation</p></div>\n","metadata":{}},{"cell_type":"markdown","source":"#### 🟠 Draw a molecule with a calculation value displayed (e.g., Gasteiger Charge)\n\n<p style=\"color: darkmagenta; font-size: 16px;\">The Gasteiger charge, also known as Gasteiger-Marsili charges, is a method for estimating partial atomic charges in a molecule. It was developed by Professor Hans Georg Gasteiger and his colleague Thomas A. Marsili.</p>\n\n<p style=\"color: darkmagenta; font-size: 16px;\">In computational chemistry, understanding the distribution of electric charge within a molecule is crucial for simulating molecular behavior. The Gasteiger charge assigns partial charges to each atom in a molecule based on its structure and the distribution of electrons.</p>\n\n<p style=\"color: darkmagenta; font-size: 16px;\">The calculation of Gasteiger charges involves a process where electrons are distributed among the atoms in a molecule according to certain rules. These rules take into account factors such as electronegativity, hybridization state, and neighboring atoms. The resulting partial charges are used in various computational methods, including molecular modeling, simulations, and drug design.</p>\n\n<p style=\"color: darkmagenta; font-size: 16px;\">Gasteiger charges provide a simplified yet useful representation of electronic charge distribution in a molecule, making them valuable in a wide range of applications within computational chemistry and related fields.</p>","metadata":{}},{"cell_type":"code","source":"from rdkit.Chem.Draw import IPythonConsole\nIPythonConsole.molSize = 250,250","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:25:46.121045Z","iopub.execute_input":"2023-09-26T06:25:46.121510Z","iopub.status.idle":"2023-09-26T06:25:46.127102Z","shell.execute_reply.started":"2023-09-26T06:25:46.121462Z","shell.execute_reply":"2023-09-26T06:25:46.125912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = Chem.MolFromSmiles('Clc1ccccc1C(c1ccccc1)(c1ccccc1)n1ccnc1')\nAllChem.ComputeGasteigerCharges(m)\nm","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:25:50.094003Z","iopub.execute_input":"2023-09-26T06:25:50.094424Z","iopub.status.idle":"2023-09-26T06:25:50.127314Z","shell.execute_reply.started":"2023-09-26T06:25:50.094395Z","shell.execute_reply":"2023-09-26T06:25:50.126474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m2 = Chem.Mol(m)\nfor at in m2.GetAtoms():\n    lbl = '%.2f'%(at.GetDoubleProp(\"_GasteigerCharge\"))\n    at.SetProp('atomNote',lbl)\nm2","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:25:54.275327Z","iopub.execute_input":"2023-09-26T06:25:54.276454Z","iopub.status.idle":"2023-09-26T06:25:54.350764Z","shell.execute_reply.started":"2023-09-26T06:25:54.276409Z","shell.execute_reply":"2023-09-26T06:25:54.349454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:90%;letter-spacing:0.5px;margin:0\"><b> </b>Stereo Annotations</p></div>\n\n\n","metadata":{}},{"cell_type":"markdown","source":"#### 🟠 Draw a molecule with stereochemistry annotations displayed.\n\n<p style=\"color: brown; font-size: 16px;\">Stereochemistry annotations are symbols or notations used in chemistry to represent the three-dimensional arrangement of atoms or groups in a molecule. They convey information about the spatial relationships between different parts of a molecule, which is crucial for understanding its behavior in chemical reactions. Common forms of stereochemistry annotations include wedge-dash notation, E/Z notation for double bonds, R/S notation for chiral centers, and cis/trans notation for geometric isomers.</p>\n","metadata":{}},{"cell_type":"code","source":"IPythonConsole.drawOptions.addAtomIndices = False\nIPythonConsole.drawOptions.addStereoAnnotation = True","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:26:00.132363Z","iopub.execute_input":"2023-09-26T06:26:00.132777Z","iopub.status.idle":"2023-09-26T06:26:00.138461Z","shell.execute_reply.started":"2023-09-26T06:26:00.132747Z","shell.execute_reply":"2023-09-26T06:26:00.137312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Default Representation uses legacy FindMolChiralCenters() code\nm1 = Chem.MolFromSmiles('Nc1ccn([C@@H]2CS[C@H](CO)O2)c(=O)n1')\nm2 = Chem.MolFromSmiles('Nc1ncn([C@@H]2O[C@H](CO)[C@@H](O)[C@H]2O)c(=O)n1')\nDraw.MolsToGridImage((m1,m2), subImgSize=(250,250))","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:26:04.180401Z","iopub.execute_input":"2023-09-26T06:26:04.180857Z","iopub.status.idle":"2023-09-26T06:26:04.207220Z","shell.execute_reply.started":"2023-09-26T06:26:04.180828Z","shell.execute_reply":"2023-09-26T06:26:04.205995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new stereochemistry code with more accurate CIP labels, 2020.09 release\nfrom rdkit.Chem import rdCIPLabeler\nrdCIPLabeler.AssignCIPLabels(m1)\nrdCIPLabeler.AssignCIPLabels(m2)\nDraw.MolsToGridImage((m1,m2), subImgSize=(250,250))","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:26:08.293739Z","iopub.execute_input":"2023-09-26T06:26:08.294154Z","iopub.status.idle":"2023-09-26T06:26:08.316516Z","shell.execute_reply.started":"2023-09-26T06:26:08.294126Z","shell.execute_reply":"2023-09-26T06:26:08.315442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:80%;letter-spacing:0.5px;margin:0\"><b> </b>Black and White Molecules</p></div>\n\n#### 🟠 Draw a molecule in black and white.\n","metadata":{}},{"cell_type":"code","source":"ms = [Chem.MolFromSmiles(x) for x in ('Nc1ccn([C@@H]2CS[C@H](CO)O2)c(=O)n1','Nc1ncn([C@@H]2O[C@H](CO)[C@@H](O)[C@H]2O)c(=O)n1')]\nDraw.MolsToGridImage(ms)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:26:12.841627Z","iopub.execute_input":"2023-09-26T06:26:12.842084Z","iopub.status.idle":"2023-09-26T06:26:12.867801Z","shell.execute_reply.started":"2023-09-26T06:26:12.842048Z","shell.execute_reply":"2023-09-26T06:26:12.866734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IPythonConsole.drawOptions.useBWAtomPalette()\nDraw.MolsToGridImage(ms)","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:26:16.964988Z","iopub.execute_input":"2023-09-26T06:26:16.965427Z","iopub.status.idle":"2023-09-26T06:26:16.984340Z","shell.execute_reply.started":"2023-09-26T06:26:16.965393Z","shell.execute_reply":"2023-09-26T06:26:16.983049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Alternatively, use the rdMolDraw2D package\nfrom rdkit.Chem.Draw import rdMolDraw2D\nimport io\nfrom PIL import Image\n\ndrawer = rdMolDraw2D.MolDraw2DCairo(500,180,200,180)\ndrawer.drawOptions().useBWAtomPalette()\ndrawer.DrawMolecules(ms)\ndrawer.FinishDrawing()\nbio = io.BytesIO(drawer.GetDrawingText())\nImage.open(bio)","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:26:22.016420Z","iopub.execute_input":"2023-09-26T06:26:22.017412Z","iopub.status.idle":"2023-09-26T06:26:22.052050Z","shell.execute_reply.started":"2023-09-26T06:26:22.017369Z","shell.execute_reply":"2023-09-26T06:26:22.051019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:80%;letter-spacing:0.5px;margin:0\"><b> </b>Highlight a Substructure in a Molecule</p></div>\n\n#### 🟠 Draw a molecule with a substructure highlight ","metadata":{}},{"cell_type":"code","source":"from rdkit import Chem\nfrom rdkit.Chem.Draw import IPythonConsole","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:28:02.777363Z","iopub.execute_input":"2023-09-26T06:28:02.777804Z","iopub.status.idle":"2023-09-26T06:28:02.783814Z","shell.execute_reply.started":"2023-09-26T06:28:02.777771Z","shell.execute_reply":"2023-09-26T06:28:02.782395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = Chem.MolFromSmiles('Cc1cc(C(C)Nc2ccccc2)c2nc(N3CCOCC3)cc(=O)n2c1')\nsubstructure = Chem.MolFromSmarts('Nc1ccccc1')\nprint(m.GetSubstructMatches(substructure))","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:28:14.841486Z","iopub.execute_input":"2023-09-26T06:28:14.841894Z","iopub.status.idle":"2023-09-26T06:28:14.849170Z","shell.execute_reply.started":"2023-09-26T06:28:14.841866Z","shell.execute_reply":"2023-09-26T06:28:14.848133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:28:18.398320Z","iopub.execute_input":"2023-09-26T06:28:18.399158Z","iopub.status.idle":"2023-09-26T06:28:18.423703Z","shell.execute_reply.started":"2023-09-26T06:28:18.399119Z","shell.execute_reply":"2023-09-26T06:28:18.422359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# you can also manually set the atoms that should be highlighted:\nm.__sssAtoms = [0,1,2,6,11,12]\nm","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:28:22.495053Z","iopub.execute_input":"2023-09-26T06:28:22.495532Z","iopub.status.idle":"2023-09-26T06:28:22.519447Z","shell.execute_reply.started":"2023-09-26T06:28:22.495488Z","shell.execute_reply":"2023-09-26T06:28:22.518558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:80%;letter-spacing:0.5px;margin:0\"><b> </b>Highlight Molecule Differences</p></div>\n\n\n#### 🟠 Highlight molecule differences based on maximum common substructure\n","metadata":{}},{"cell_type":"code","source":"from rdkit import Chem\nfrom rdkit.Chem import Draw\nfrom rdkit.Chem.Draw import IPythonConsole\nfrom rdkit.Chem import rdFMCS\nfrom rdkit.Chem.Draw import rdDepictor\nrdDepictor.SetPreferCoordGen(True)\nIPythonConsole.drawOptions.minFontSize=20","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:28:27.732388Z","iopub.execute_input":"2023-09-26T06:28:27.732794Z","iopub.status.idle":"2023-09-26T06:28:27.743171Z","shell.execute_reply.started":"2023-09-26T06:28:27.732765Z","shell.execute_reply":"2023-09-26T06:28:27.741691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mol1 = Chem.MolFromSmiles('CN1CCC[C@@H]1CCO[C@](C)(c1ccccc1)c1ccc(Cl)cc1')\nmol2 = Chem.MolFromSmiles('Cn1cncc1[C@@](N)(c1ccc(Cl)cc1)c1ccc2c(c1)c(-c1cccc(Cl)c1)cc(=O)n2C')","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:28:32.071191Z","iopub.execute_input":"2023-09-26T06:28:32.072701Z","iopub.status.idle":"2023-09-26T06:28:32.080105Z","shell.execute_reply.started":"2023-09-26T06:28:32.072649Z","shell.execute_reply":"2023-09-26T06:28:32.079147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Draw.MolsToGridImage([mol1, mol2])","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:28:36.401016Z","iopub.execute_input":"2023-09-26T06:28:36.401448Z","iopub.status.idle":"2023-09-26T06:28:36.427581Z","shell.execute_reply.started":"2023-09-26T06:28:36.401417Z","shell.execute_reply":"2023-09-26T06:28:36.426370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def view_difference(mol1, mol2):\n    mcs = rdFMCS.FindMCS([mol1,mol2])\n    mcs_mol = Chem.MolFromSmarts(mcs.smartsString)\n    match1 = mol1.GetSubstructMatch(mcs_mol)\n    target_atm1 = []\n    for atom in mol1.GetAtoms():\n        if atom.GetIdx() not in match1:\n            target_atm1.append(atom.GetIdx())\n    match2 = mol2.GetSubstructMatch(mcs_mol)\n    target_atm2 = []\n    for atom in mol2.GetAtoms():\n        if atom.GetIdx() not in match2:\n            target_atm2.append(atom.GetIdx())\n    return Draw.MolsToGridImage([mol1, mol2],highlightAtomLists=[target_atm1, target_atm2])","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:29:07.130637Z","iopub.execute_input":"2023-09-26T06:29:07.131142Z","iopub.status.idle":"2023-09-26T06:29:07.139014Z","shell.execute_reply.started":"2023-09-26T06:29:07.131106Z","shell.execute_reply":"2023-09-26T06:29:07.137793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"view_difference(mol1,mol2)","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:29:11.355398Z","iopub.execute_input":"2023-09-26T06:29:11.355837Z","iopub.status.idle":"2023-09-26T06:29:11.467554Z","shell.execute_reply.started":"2023-09-26T06:29:11.355802Z","shell.execute_reply":"2023-09-26T06:29:11.466442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:80%;letter-spacing:0.5px;margin:0\"><b> </b>Highlight Entire Molecule</p></div>\n\n#### 🟠 Highlight all atoms and bonds\n\n<p style=\"color: darkorange; font-size: 16px;\">Highlighting all atoms and bonds involves visually emphasizing every atom and chemical bond in a molecular structure. This can be useful in molecular visualization software or tools to provide a clear and comprehensive view of the entire molecule. It aids in analyzing the structural features and relationships within the molecule, which is valuable for various applications in chemistry, biology, and related fields.</p>","metadata":{}},{"cell_type":"code","source":"mol = Chem.MolFromSmiles('CN(C)CC[C@H](CSc1ccccc1)Nc1ccc(S(=O)(=O)NC(=O)c2ccc(N3CCN(Cc4ccccc4-c4ccc(Cl)cc4)CC3)cc2)cc1[N+](=O)[O-]')\nrgba_color = (0.0, 0.0, 1.0, 0.1) # transparent blue\n\natoms = []\nfor a in mol.GetAtoms():\n    atoms.append(a.GetIdx())\n\nbonds = []\nfor bond in mol.GetBonds():\n    aid1 = atoms[bond.GetBeginAtomIdx()]\n    aid2 = atoms[bond.GetEndAtomIdx()]\n    bonds.append(mol.GetBondBetweenAtoms(aid1,aid2).GetIdx())\n\ndrawer = rdMolDraw2D.MolDraw2DCairo(350,300)\ndrawer.drawOptions().fillHighlights=True\ndrawer.drawOptions().setHighlightColour((rgba_color))\ndrawer.drawOptions().highlightBondWidthMultiplier=20\ndrawer.drawOptions().clearBackground = False\nrdMolDraw2D.PrepareAndDrawMolecule(drawer, mol, highlightAtoms=atoms, highlightBonds=bonds)\nbio = io.BytesIO(drawer.GetDrawingText())\nImage.open(bio)","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:29:21.388920Z","iopub.execute_input":"2023-09-26T06:29:21.389383Z","iopub.status.idle":"2023-09-26T06:29:21.463689Z","shell.execute_reply.started":"2023-09-26T06:29:21.389348Z","shell.execute_reply":"2023-09-26T06:29:21.462499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:80%;letter-spacing:0.5px;margin:0\"><b> </b>Highlight Molecule with Multiple Colors</p></div>\n\n#### 🟠 Highlight a molecule with different colors based on if the atom/bond is aromatic","metadata":{}},{"cell_type":"code","source":"mol = Chem.MolFromSmiles('Cn1cncc1[C@@](N)(c1ccc(Cl)cc1)c1ccc2c(c1)c(-c1cccc(Cl)c1)cc(=O)n2C')\ncolors = [(0.0, 0.0, 1.0, 0.1), (1.0, 0.0, 0.0, 0.2)]\n\nathighlights = defaultdict(list)\narads = {}\nfor a in mol.GetAtoms():\n    if a.GetIsAromatic():\n        aid = a.GetIdx()\n        athighlights[aid].append(colors[0])\n        arads[aid] = 0.3\n    else:\n        aid = a.GetIdx()\n        athighlights[aid].append(colors[1])\n        arads[aid] = 0.3\n\nbndhighlights = defaultdict(list)\nfor bond in mol.GetBonds():\n    aid1 = bond.GetBeginAtomIdx()\n    aid2 = bond.GetEndAtomIdx()\n\n    if bond.GetIsAromatic():\n        bid = mol.GetBondBetweenAtoms(aid1,aid2).GetIdx()\n        bndhighlights[bid].append(colors[0])\n    else:\n        bid = mol.GetBondBetweenAtoms(aid1,aid2).GetIdx()\n        bndhighlights[bid].append(colors[1])\n\nd2d = rdMolDraw2D.MolDraw2DCairo(350,400)\nd2d.DrawMoleculeWithHighlights(mol,\"\",dict(athighlights),dict(bndhighlights),arads,{})\nd2d.FinishDrawing()\nbio = io.BytesIO(d2d.GetDrawingText())\nImage.open(bio)","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:29:28.276482Z","iopub.execute_input":"2023-09-26T06:29:28.276887Z","iopub.status.idle":"2023-09-26T06:29:28.348584Z","shell.execute_reply.started":"2023-09-26T06:29:28.276858Z","shell.execute_reply":"2023-09-26T06:29:28.347696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:80%;letter-spacing:0.5px;margin:0\"><b> </b>Using CoordGen Library</p></div>\n\n#### 🟠 Draw a molecule using CoordGen Library\n\n<p style=\"color: darkblue; font-size: 16px;\">The CoordGen library is a software tool used in chemoinformatics for generating 2D coordinates from chemical structures. It assists in the process of converting three-dimensional molecular structures into two-dimensional representations that can be easily visualized and analyzed. This tool is particularly valuable in fields like computational chemistry and drug discovery, where accurate molecular depiction is essential for various computational simulations and analyses.</p>\n","metadata":{}},{"cell_type":"code","source":"from rdkit import Chem\nfrom rdkit.Chem.Draw import IPythonConsole\nIPythonConsole.molSize = 350,300\nfrom rdkit.Chem import Draw","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:29:36.162789Z","iopub.execute_input":"2023-09-26T06:29:36.163799Z","iopub.status.idle":"2023-09-26T06:29:36.170621Z","shell.execute_reply.started":"2023-09-26T06:29:36.163738Z","shell.execute_reply":"2023-09-26T06:29:36.169254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# default drawing\nmol = Chem.MolFromSmiles(\"CN(C)CC[C@H](CSc1ccccc1)Nc1ccc(S(=O)(=O)NC(=O)c2ccc(N3CCN(Cc4ccccc4-c4ccc(Cl)cc4)CC3)cc2)cc1[N+](=O)[O-]\")\nmol","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:29:39.465181Z","iopub.execute_input":"2023-09-26T06:29:39.465610Z","iopub.status.idle":"2023-09-26T06:29:39.520641Z","shell.execute_reply.started":"2023-09-26T06:29:39.465581Z","shell.execute_reply":"2023-09-26T06:29:39.519522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with CoordGen\nfrom rdkit.Chem import rdCoordGen\nrdCoordGen.AddCoords(mol)\nmol","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:29:43.949923Z","iopub.execute_input":"2023-09-26T06:29:43.950364Z","iopub.status.idle":"2023-09-26T06:29:43.988109Z","shell.execute_reply.started":"2023-09-26T06:29:43.950328Z","shell.execute_reply":"2023-09-26T06:29:43.987043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:90%;letter-spacing:0.5px;margin:0\"><b> </b>On a Plot</p></div>\n\n#### 🟠 Draw a molecule on a matplotlib plot.","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nfrom rdkit import Chem\nfrom rdkit.Chem.Draw import IPythonConsole","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:29:48.962586Z","iopub.execute_input":"2023-09-26T06:29:48.963002Z","iopub.status.idle":"2023-09-26T06:29:48.968448Z","shell.execute_reply.started":"2023-09-26T06:29:48.962974Z","shell.execute_reply":"2023-09-26T06:29:48.967254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.arange(0, 180, 1)\ny = np.sin(x)","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:29:53.295743Z","iopub.execute_input":"2023-09-26T06:29:53.296263Z","iopub.status.idle":"2023-09-26T06:29:53.302733Z","shell.execute_reply.started":"2023-09-26T06:29:53.296220Z","shell.execute_reply":"2023-09-26T06:29:53.301359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mol = Chem.MolFromSmiles('Nc1ccn([C@@H]2CS[C@H](CO)O2)c(=O)n1')\nim = Chem.Draw.MolToImage(mol)","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:29:56.472293Z","iopub.execute_input":"2023-09-26T06:29:56.473616Z","iopub.status.idle":"2023-09-26T06:29:56.488612Z","shell.execute_reply.started":"2023-09-26T06:29:56.473573Z","shell.execute_reply":"2023-09-26T06:29:56.487211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(10,5))\nplt.plot(x, y)\nplt.ylim(-1, 5)\nax = plt.axes([0.6, 0.47, 0.38, 0.38], frameon=True)\nax.imshow(im)\nax.axis('off')","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:29:59.830941Z","iopub.execute_input":"2023-09-26T06:29:59.831355Z","iopub.status.idle":"2023-09-26T06:30:00.186506Z","shell.execute_reply.started":"2023-09-26T06:29:59.831327Z","shell.execute_reply":"2023-09-26T06:30:00.185215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:80%;letter-spacing:0.5px;margin:0\"><b> </b>Bonds and Bonding</p></div>\n\n\n<p style=\"color: darkgreen; font-size: 16px;\">Bonds and bonding refer to the connections between atoms in a molecule. These connections are formed when atoms share or transfer electrons, leading to the creation of stable chemical compounds. Bonds can be covalent, where electrons are shared, or ionic, where electrons are transferred. Covalent bonds are typically stronger and involve the sharing of electrons, while ionic bonds result from the transfer of electrons between atoms of different electronegativities. The type of bonding significantly influences the physical and chemical properties of a substance.</p>\n\n\n#### 🟠 Hybridization Type and Count\n\n<p style=\"color: brown; font-size: 16px;\">Hybridization type and count describe how atomic orbitals combine to form hybrid orbitals during the formation of chemical bonds. This process allows for efficient overlap between orbitals, resulting in the creation of stronger and more stable bonds. The type of hybridization (e.g., sp, sp2, sp3) is determined by the number of atomic orbitals that are mixed. The count indicates how many hybrid orbitals are formed in the process. These hybrid orbitals play a crucial role in determining the geometry and properties of molecules.</p>\n\n\n","metadata":{}},{"cell_type":"code","source":"m = Chem.MolFromSmiles(\"Cc1nc2c(-c3cnc(N)nc3)nc(N3CCOCC3)nc2n1C(C)C\")\nfor x in m.GetAtoms():\n    print(x.GetIdx(), x.GetHybridization())","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:30:07.207471Z","iopub.execute_input":"2023-09-26T06:30:07.208730Z","iopub.status.idle":"2023-09-26T06:30:07.216279Z","shell.execute_reply.started":"2023-09-26T06:30:07.208688Z","shell.execute_reply":"2023-09-26T06:30:07.215109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if you want to count hybridization type (e.g., SP3):\nfrom rdkit import Chem\nm = Chem.MolFromSmiles(\"Cc1nc2c(-c3cnc(N)nc3)nc(N3CCOCC3)nc2n1C(C)C\")\nprint(sum((x.GetHybridization() == Chem.HybridizationType.SP3) for x in m.GetAtoms()))","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:30:11.396184Z","iopub.execute_input":"2023-09-26T06:30:11.397041Z","iopub.status.idle":"2023-09-26T06:30:11.403856Z","shell.execute_reply.started":"2023-09-26T06:30:11.397000Z","shell.execute_reply":"2023-09-26T06:30:11.402955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:80%;letter-spacing:0.5px;margin:0\"><b> </b>Rings, Aromaticity, and Kekulization</p></div>\n\n\n### Rings, Aromaticity, and Kekulization are key concepts in organic chemistry:\n\n<p style=\"color: darkorange; font-size: 16px;\">Rings: In chemistry, a ring refers to a closed loop of atoms connected by covalent bonds within a molecule. Rings play a fundamental role in the structure and behavior of organic compounds.</p>\n\n<p style=\"color: darkorange; font-size: 16px;\">Aromaticity: Aromaticity is a property of certain planar, cyclic compounds with a specific arrangement of pi electrons. Aromatic molecules, like benzene, exhibit enhanced stability and unique reactivity patterns compared to non-aromatic compounds.</p>\n\n<p style=\"color: darkorange; font-size: 16px;\">Kekulization: Kekulization is a concept used to represent the structure of aromatic compounds, particularly benzene. It involves alternating single and double bonds between carbon atoms to show the delocalized nature of electrons in the ring.</p>\n\n<p style=\"color: darkorange; font-size: 16px;\">These concepts are central to understanding the behavior of many organic molecules.</p>\n","metadata":{}},{"cell_type":"markdown","source":"#### 🟠 Count ring systems in a molecule\n\n<p style=\"color: magenta; font-size: 16px;\">Counting ring systems in a molecule involves identifying and tallying the distinct closed-loop structures formed by covalent bonds between atoms. Ring systems are important in understanding a molecule's structure and reactivity. For example, a molecule with multiple ring systems may exhibit different chemical properties compared to a molecule with only one ring system.</p>\n","metadata":{}},{"cell_type":"code","source":"from rdkit import Chem\nfrom rdkit.Chem.Draw import IPythonConsole","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:30:17.732921Z","iopub.execute_input":"2023-09-26T06:30:17.733366Z","iopub.status.idle":"2023-09-26T06:30:17.738916Z","shell.execute_reply.started":"2023-09-26T06:30:17.733332Z","shell.execute_reply":"2023-09-26T06:30:17.737549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def GetRingSystems(mol, includeSpiro=False):\n    ri = mol.GetRingInfo()\n    systems = []\n    for ring in ri.AtomRings():\n        ringAts = set(ring)\n        nSystems = []\n        for system in systems:\n            nInCommon = len(ringAts.intersection(system))\n            if nInCommon and (includeSpiro or nInCommon>1):\n                ringAts = ringAts.union(system)\n            else:\n                nSystems.append(system)\n        nSystems.append(ringAts)\n        systems = nSystems\n    return systems\nmol = Chem.MolFromSmiles('CC(C)c1cc(-c2n[nH]c(=O)n2-c2ccc3c(ccn3C)c2)c(O)cc1O')\nprint(GetRingSystems(mol))","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:30:21.809612Z","iopub.execute_input":"2023-09-26T06:30:21.810051Z","iopub.status.idle":"2023-09-26T06:30:21.820988Z","shell.execute_reply.started":"2023-09-26T06:30:21.810022Z","shell.execute_reply":"2023-09-26T06:30:21.819661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Draw molecule with atom index (see RDKitCB_0)\ndef mol_with_atom_index(mol):\n    for atom in mol.GetAtoms():\n        atom.SetAtomMapNum(atom.GetIdx())\n    return mol\nmol_with_atom_index(mol)","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:30:25.969756Z","iopub.execute_input":"2023-09-26T06:30:25.970174Z","iopub.status.idle":"2023-09-26T06:30:26.018530Z","shell.execute_reply.started":"2023-09-26T06:30:25.970144Z","shell.execute_reply":"2023-09-26T06:30:26.017673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:80%;letter-spacing:0.5px;margin:0\"><b> </b>Identify Aromatic Rings</p></div>\n\n<p style=\"color: darkblue; font-size: 16px;\">Identifying aromatic rings involves recognizing specific cyclic arrangements of atoms in a molecule, typically composed of carbon atoms, that exhibit special stability and unique reactivity patterns. Aromatic rings are characterized by a continuous π electron cloud distributed across the ring. This concept is central in organic chemistry and helps predict the behavior of aromatic compounds in various chemical reactions.</p>\n\n#### 🟠 Identify which rings are aromatic in a molecule\n","metadata":{}},{"cell_type":"code","source":"m = Chem.MolFromSmiles('CC(C)C[C@H](NC(=O)CNC(=O)c1cc(Cl)ccc1Cl)B(O)O')\nm","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:30:31.519250Z","iopub.execute_input":"2023-09-26T06:30:31.519698Z","iopub.status.idle":"2023-09-26T06:30:31.547689Z","shell.execute_reply.started":"2023-09-26T06:30:31.519666Z","shell.execute_reply":"2023-09-26T06:30:31.546931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ri = m.GetRingInfo()\n# You can interrogate the RingInfo object to tell you the atoms that make up each ring:\nprint(ri.AtomRings())","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:30:36.852544Z","iopub.execute_input":"2023-09-26T06:30:36.852951Z","iopub.status.idle":"2023-09-26T06:30:36.860501Z","shell.execute_reply.started":"2023-09-26T06:30:36.852923Z","shell.execute_reply":"2023-09-26T06:30:36.859311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# or the bonds that make up each ring:\nprint(ri.BondRings())\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:30:39.620708Z","iopub.execute_input":"2023-09-26T06:30:39.621134Z","iopub.status.idle":"2023-09-26T06:30:39.627529Z","shell.execute_reply.started":"2023-09-26T06:30:39.621104Z","shell.execute_reply":"2023-09-26T06:30:39.626503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To detect aromatic rings, I would loop over the bonds in each ring and\n# flag the ring as aromatic if all bonds are aromatic:\ndef isRingAromatic(mol, bondRing):\n        for id in bondRing:\n            if not mol.GetBondWithIdx(id).GetIsAromatic():\n                return False\n        return True\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:30:47.536958Z","iopub.execute_input":"2023-09-26T06:30:47.537378Z","iopub.status.idle":"2023-09-26T06:30:47.542580Z","shell.execute_reply.started":"2023-09-26T06:30:47.537348Z","shell.execute_reply":"2023-09-26T06:30:47.541698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(isRingAromatic(m, ri.BondRings()[0]))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:30:53.665518Z","iopub.execute_input":"2023-09-26T06:30:53.666057Z","iopub.status.idle":"2023-09-26T06:30:53.674209Z","shell.execute_reply.started":"2023-09-26T06:30:53.666014Z","shell.execute_reply":"2023-09-26T06:30:53.672725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(isRingAromatic(m, ri.BondRings()[-1]))","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:30:58.111136Z","iopub.execute_input":"2023-09-26T06:30:58.111613Z","iopub.status.idle":"2023-09-26T06:30:58.120099Z","shell.execute_reply.started":"2023-09-26T06:30:58.111577Z","shell.execute_reply":"2023-09-26T06:30:58.118345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:80%;letter-spacing:0.5px;margin:0\"><b> </b>Identify Aromatic Atoms</p></div>\n\n<p style=\"color: brown; font-size: 16px;\">Identifying aromatic atoms involves recognizing specific atoms within an aromatic ring, typically composed of carbon atoms, that contribute to the special stability and unique reactivity patterns of the ring. These aromatic atoms are part of a cyclic arrangement with a continuous π electron cloud distributed across the ring. Understanding which atoms are aromatic is important in predicting the behavior of aromatic compounds in various chemical reactions.</p>\n\n\n#### 🟠 Differentiate aromatic carbon from olefinic carbon with SMARTS\n\n<p style=\"color: darkgreen; font-size: 16px;\">Aromatic carbon from olefinic carbon with SMARTS involves using the SMARTS notation—a language for describing chemical patterns—to specifically target and distinguish between aromatic and olefinic (carbon-carbon double bond-containing) carbons in a molecule. This distinction is valuable in medicinal chemistry and computational chemistry applications, allowing for precise identification of structural features for further analysis and modeling.</p>","metadata":{}},{"cell_type":"code","source":"mol = Chem.MolFromSmiles(\"O=C(c1ccc(OCCN2CCCCC2)cc1)c1c(-c2ccc(O)cc2)sc2cc(O)ccc12\")\naromatic_carbon = Chem.MolFromSmarts(\"c\")\nprint(mol.GetSubstructMatches(aromatic_carbon))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:31:03.458659Z","iopub.execute_input":"2023-09-26T06:31:03.459057Z","iopub.status.idle":"2023-09-26T06:31:03.466087Z","shell.execute_reply.started":"2023-09-26T06:31:03.459028Z","shell.execute_reply":"2023-09-26T06:31:03.464676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# here we query for SP2 aliphatic carbons:\nolefinic_carbon = Chem.MolFromSmarts(\"[C^2]\")\nprint(mol.GetSubstructMatches(olefinic_carbon))","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:31:07.596141Z","iopub.execute_input":"2023-09-26T06:31:07.596594Z","iopub.status.idle":"2023-09-26T06:31:07.601835Z","shell.execute_reply.started":"2023-09-26T06:31:07.596562Z","shell.execute_reply":"2023-09-26T06:31:07.601051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from rdkit import Chem\nfrom rdkit.Chem import rdqueries","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:31:11.168479Z","iopub.execute_input":"2023-09-26T06:31:11.168915Z","iopub.status.idle":"2023-09-26T06:31:11.173981Z","shell.execute_reply.started":"2023-09-26T06:31:11.168879Z","shell.execute_reply":"2023-09-26T06:31:11.173060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mol = Chem.MolFromSmiles(\"O=C(c1ccc(OCCN2CCCCC2)cc1)c1c(-c2ccc(O)cc2)sc2cc(O)ccc12\")\nq = rdqueries.IsAromaticQueryAtom()\nprint([x.GetIdx() for x in mol.GetAtomsMatchingQuery(q)])\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:31:15.056983Z","iopub.execute_input":"2023-09-26T06:31:15.057425Z","iopub.status.idle":"2023-09-26T06:31:15.064935Z","shell.execute_reply.started":"2023-09-26T06:31:15.057389Z","shell.execute_reply":"2023-09-26T06:31:15.063681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"q = rdqueries.HybridizationEqualsQueryAtom(Chem.HybridizationType.SP2)\nprint([x.GetIdx() for x in mol.GetAtomsMatchingQuery(q)])\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:31:18.665533Z","iopub.execute_input":"2023-09-26T06:31:18.665959Z","iopub.status.idle":"2023-09-26T06:31:18.672319Z","shell.execute_reply.started":"2023-09-26T06:31:18.665926Z","shell.execute_reply":"2023-09-26T06:31:18.671461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"qcombined = rdqueries.IsAliphaticQueryAtom()\nqcombined.ExpandQuery(q)\nprint([x.GetIdx() for x in mol.GetAtomsMatchingQuery(qcombined)])","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:31:22.512237Z","iopub.execute_input":"2023-09-26T06:31:22.512672Z","iopub.status.idle":"2023-09-26T06:31:22.519548Z","shell.execute_reply.started":"2023-09-26T06:31:22.512638Z","shell.execute_reply":"2023-09-26T06:31:22.518446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:80%;letter-spacing:0.5px;margin:0\"><b> </b>Stereochemistry</p></div>\n\n\n#### 🟠 Identifying Stereochemistry\n\n  * Find chiral centers and double bond stereochemistry.","metadata":{}},{"cell_type":"code","source":"from rdkit import Chem\nfrom rdkit.Chem import Draw\nfrom rdkit.Chem.Draw import IPythonConsole\nIPythonConsole.drawOptions.addAtomIndices = True\nIPythonConsole.drawOptions.addStereoAnnotation = False\nIPythonConsole.molSize = 200,200","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:31:27.585296Z","iopub.execute_input":"2023-09-26T06:31:27.585737Z","iopub.status.idle":"2023-09-26T06:31:27.591673Z","shell.execute_reply.started":"2023-09-26T06:31:27.585700Z","shell.execute_reply":"2023-09-26T06:31:27.590364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = Chem.MolFromSmiles(\"Nc1ncnc2c1c(I)cn2[C@@H]1O[C@H](CO)[C@@H](O)[C@H]1O\")\nm","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:31:44.145490Z","iopub.execute_input":"2023-09-26T06:31:44.145894Z","iopub.status.idle":"2023-09-26T06:31:44.177700Z","shell.execute_reply.started":"2023-09-26T06:31:44.145855Z","shell.execute_reply":"2023-09-26T06:31:44.176612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# legacy FindMolChiralCenters()\nprint(Chem.FindMolChiralCenters(m,force=True,includeUnassigned=True,useLegacyImplementation=True))","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:31:51.261356Z","iopub.execute_input":"2023-09-26T06:31:51.261782Z","iopub.status.idle":"2023-09-26T06:31:51.267700Z","shell.execute_reply.started":"2023-09-26T06:31:51.261749Z","shell.execute_reply":"2023-09-26T06:31:51.266955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new stereochemistry code\nprint(Chem.FindMolChiralCenters(m,force=True,includeUnassigned=True,useLegacyImplementation=False))","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:31:55.100486Z","iopub.execute_input":"2023-09-26T06:31:55.100895Z","iopub.status.idle":"2023-09-26T06:31:55.108989Z","shell.execute_reply.started":"2023-09-26T06:31:55.100865Z","shell.execute_reply":"2023-09-26T06:31:55.107575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Identifying Double Bond Stereochemistry\nIPythonConsole.molSize = 250,250\nmol = Chem.MolFromSmiles(\"Nc1ncnc2c1c(I)cn2[C@@H]1O[C@H](CO)[C@@H](O)[C@H]1O\")\nmol","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:31:58.640086Z","iopub.execute_input":"2023-09-26T06:31:58.640521Z","iopub.status.idle":"2023-09-26T06:31:58.675120Z","shell.execute_reply.started":"2023-09-26T06:31:58.640485Z","shell.execute_reply":"2023-09-26T06:31:58.673970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# legacy FindMolChiralCenters()\nprint(Chem.FindMolChiralCenters(m,force=True,includeUnassigned=True,useLegacyImplementation=True))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:32:03.355345Z","iopub.execute_input":"2023-09-26T06:32:03.355774Z","iopub.status.idle":"2023-09-26T06:32:03.362683Z","shell.execute_reply.started":"2023-09-26T06:32:03.355741Z","shell.execute_reply":"2023-09-26T06:32:03.361479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new stereochemistry code\nprint(Chem.FindMolChiralCenters(m,force=True,includeUnassigned=True,useLegacyImplementation=False))","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:32:06.925123Z","iopub.execute_input":"2023-09-26T06:32:06.926303Z","iopub.status.idle":"2023-09-26T06:32:06.932571Z","shell.execute_reply.started":"2023-09-26T06:32:06.926231Z","shell.execute_reply":"2023-09-26T06:32:06.931675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Using GetStereo()\nfor b in mol.GetBonds():\n    print(b.GetBeginAtomIdx(),b.GetEndAtomIdx(),\n          b.GetBondType(),b.GetStereo())","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:32:10.420893Z","iopub.execute_input":"2023-09-26T06:32:10.421486Z","iopub.status.idle":"2023-09-26T06:32:10.429098Z","shell.execute_reply.started":"2023-09-26T06:32:10.421443Z","shell.execute_reply":"2023-09-26T06:32:10.427924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Double bond configuration can also be identified with new\n# stereochemistry code using Chem.FindPotentialStereo()\nsi = Chem.FindPotentialStereo(mol)\nfor element in si:\n    print(f'  Type: {element.type}, Which: {element.centeredOn}, Specified: {element.specified}, Descriptor: {element.descriptor} ')","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:32:14.756430Z","iopub.execute_input":"2023-09-26T06:32:14.757319Z","iopub.status.idle":"2023-09-26T06:32:14.764246Z","shell.execute_reply.started":"2023-09-26T06:32:14.757248Z","shell.execute_reply":"2023-09-26T06:32:14.762609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:#007BA7;font-family:Nexa;overflow:hidden\"><p style=\"padding:15px;color:yellow;overflow:hidden;font-size:80%;letter-spacing:0.5px;margin:0\"><b> </b>Contiguous Rotable Bonds</p></div>\n\n<p style=\"color: magenta; font-size: 16px;\">Contiguous rotatable bonds refer to a series of connected, single bonds in a molecule that allow for free rotation. These bonds provide flexibility to the molecule, enabling it to adopt different conformations. Knowledge of contiguous rotatable bonds is important in drug design and molecular modeling, as it influences the three-dimensional shape and flexibility of a molecule, which in turn affects its biological activity and interactions with other molecules.</p>\n\n\n#### 🟠 Calculate the largest number of contiguous rotable bonds.","metadata":{}},{"cell_type":"code","source":"from rdkit import Chem\nfrom rdkit.Chem.Lipinski import RotatableBondSmarts","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:32:22.985316Z","iopub.execute_input":"2023-09-26T06:32:22.985730Z","iopub.status.idle":"2023-09-26T06:32:22.993363Z","shell.execute_reply.started":"2023-09-26T06:32:22.985701Z","shell.execute_reply":"2023-09-26T06:32:22.991996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mol = Chem.MolFromSmiles('CCc1nn(CCCN2CCN(c3cccc(Cl)c3)CC2)c(=O)n1CCOc1ccccc1')\nmol","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:32:26.986434Z","iopub.execute_input":"2023-09-26T06:32:26.986854Z","iopub.status.idle":"2023-09-26T06:32:27.047519Z","shell.execute_reply.started":"2023-09-26T06:32:26.986824Z","shell.execute_reply":"2023-09-26T06:32:27.046702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def find_bond_groups(mol):\n    \"\"\"Find groups of contiguous rotatable bonds and return them sorted by decreasing size\"\"\"\n    rot_atom_pairs = mol.GetSubstructMatches(RotatableBondSmarts)\n    rot_bond_set = set([mol.GetBondBetweenAtoms(*ap).GetIdx() for ap in rot_atom_pairs])\n    rot_bond_groups = []\n    while (rot_bond_set):\n        i = rot_bond_set.pop()\n        connected_bond_set = set([i])\n        stack = [i]\n        while (stack):\n            i = stack.pop()\n            b = mol.GetBondWithIdx(i)\n            bonds = []\n            for a in (b.GetBeginAtom(), b.GetEndAtom()):\n                bonds.extend([b.GetIdx() for b in a.GetBonds() if (\n                    (b.GetIdx() in rot_bond_set) and (not (b.GetIdx() in connected_bond_set)))])\n            connected_bond_set.update(bonds)\n            stack.extend(bonds)\n        rot_bond_set.difference_update(connected_bond_set)\n        rot_bond_groups.append(tuple(connected_bond_set))\n    return tuple(sorted(rot_bond_groups, reverse = True, key = lambda x: len(x)))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:32:32.148823Z","iopub.execute_input":"2023-09-26T06:32:32.149286Z","iopub.status.idle":"2023-09-26T06:32:32.158621Z","shell.execute_reply.started":"2023-09-26T06:32:32.149239Z","shell.execute_reply":"2023-09-26T06:32:32.157340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find groups of contiguous rotatable bonds in mol\nbond_groups = find_bond_groups(mol)\n# As bond groups are sorted by decreasing size, the size of the first group (if any)\n# is the largest number of contiguous rotatable bonds in mol\nlargest_n_cont_rot_bonds = len(bond_groups[0]) if bond_groups else 0","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:32:36.861457Z","iopub.execute_input":"2023-09-26T06:32:36.861891Z","iopub.status.idle":"2023-09-26T06:32:36.867674Z","shell.execute_reply.started":"2023-09-26T06:32:36.861858Z","shell.execute_reply":"2023-09-26T06:32:36.866534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(largest_n_cont_rot_bonds)","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:32:41.655113Z","iopub.execute_input":"2023-09-26T06:32:41.656361Z","iopub.status.idle":"2023-09-26T06:32:41.661516Z","shell.execute_reply.started":"2023-09-26T06:32:41.656319Z","shell.execute_reply":"2023-09-26T06:32:41.660477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(bond_groups)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:32:44.911525Z","iopub.execute_input":"2023-09-26T06:32:44.911934Z","iopub.status.idle":"2023-09-26T06:32:44.917624Z","shell.execute_reply.started":"2023-09-26T06:32:44.911904Z","shell.execute_reply":"2023-09-26T06:32:44.916578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mol","metadata":{"execution":{"iopub.status.busy":"2023-09-26T06:32:48.169847Z","iopub.execute_input":"2023-09-26T06:32:48.170243Z","iopub.status.idle":"2023-09-26T06:32:48.240079Z","shell.execute_reply.started":"2023-09-26T06:32:48.170215Z","shell.execute_reply":"2023-09-26T06:32:48.239029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-block alert-info\"> \"Your positive feedback and upvotes are incredibly appreciated! They inspire me to create more valuable content and help others in their learning journey. Your support fosters a vibrant community of knowledge-sharing. Thank you for considering an upvote, and best wishes on your learning journey!\" 😊📌</div>","metadata":{}}]}