{"cells":[{"metadata":{},"cell_type":"markdown","source":"A few simple examples to illustrate the algorithm described in the [forum](https://www.kaggle.com/c/bms-molecular-translation/discussion/224375)\n\nEach molecule is displayed in 3 different forms: standard form; with atom numbers and with the number of hydrogen atoms for each atom of the basic structure."},{"metadata":{},"cell_type":"markdown","source":"[C11H20N2](#1)"},{"metadata":{},"cell_type":"markdown","source":"# Install rdkit environment"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"!conda install -y -c rdkit rdkit","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from rdkit.Chem import MolFromInchi","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Convert from InChI to Image"},{"metadata":{"trusted":true},"cell_type":"code","source":"def mol_with_atom_index(mol):\n    for i, atom in enumerate(mol.GetAtoms()):\n        atom.SetAtomMapNum(atom.GetIdx()+1)\n    return mol","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndef mol_with_hybridization(mol):\n    for i, atom in enumerate(mol.GetAtoms()):\n        count_hydrogens = atom.GetTotalNumHs()\n        if count_hydrogens==0:\n            lbl = '%s'%(atom.GetSymbol())\n#         elif count_hydrogens==1:\n#             lbl = '%sH'%(atom.GetSymbol())\n        else:\n            lbl = '%sH%s'%(atom.GetSymbol(),count_hydrogens)\n        atom.SetProp('atomLabel',lbl)\n    return mol","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_img(inchi, d=True):\n    mol = MolFromInchi(inchi)\n    display(mol)\n    if d:\n        display(mol_with_atom_index(mol))\n        display(mol_with_hybridization(mol))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_img('InChI=1S/C4H9Cl/c1-3-4(2)5/h4H,3H2,1-2H3')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_img('InChI=1S/C2H6/c1-2/h1-2H3')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_img('InChI=1S/C2H6O/c1-2-3/h3H,2H2,1H3')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_img('InChI=1S/C2H6O/c1-3-2/h1-2H3')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_img('InChI=1S/C6H6/c1-2-4-6-5-3-1/h1-6H')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_img('InChI=1S/C7H8/c1-7-5-3-2-4-6-7/h2-6H,1H3')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_img('InChI=1S/C2H7N/c1-2-3/h2-3H2,1H3')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_img('InChI=1S/C2H4O2/c1-2(3)4/h1H3,(H,3,4)')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_img('InChI=1S/CH3NO/c2-1-3/h1H,(H2,2,3)')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_img('InChI=1S/C4H8/c1-3-4-2/h3-4H,1-2H3')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_img('InChI=1S/C3H7NO2/c1-2(4)3(5)6/h2H,4H2,1H3,(H,5,6)/t2-/m1/s1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_img('InChI=1S/C11H17ClN2/c1-8(9(6-13)7-14)10-4-2-3-5-11(10)12/h2-5,8-9H,6-7,13-14H2,1H3')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<a id=\"1\"></a>\nC11H20N2"},{"metadata":{"trusted":true},"cell_type":"code","source":"show_img('InChI=1S/C11H20N2/c1-11(4-5-11)13-9-2-3-10(13)7-8(12)6-9/h8-10H,2-7,12H2,1H3', d=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# show_img('')","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}