{"cells":[{"metadata":{},"cell_type":"markdown","source":"The aim of this notebook is to get the 3d image of some molecules, which is rendered weirdly in two dimension by rdkit. \n\nYou can find an example below. \n\nSee also this (notebook)[https://www.kaggle.com/nayuts/let-s-understand-dataset-with-molecular-3d-models] by Nayu TS"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom tqdm.auto import tqdm\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport PIL.Image as Image\nimport PIL.ImageOps as ops\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Install the necessary libraries\n-------------------------------"},{"metadata":{"trusted":true},"cell_type":"code","source":"! conda install -y -c rdkit rdkit;\n!pip install py3Dmol","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import rdkit.Chem as Chem\nfrom rdkit.Chem import AllChem\nimport py3Dmol","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Some helper functions\n---------------------"},{"metadata":{"trusted":true},"cell_type":"code","source":"def InChi2Smiles(x):\n    return Chem.MolToSmiles(Chem.inchi.MolFromInchi(x))\ndef InChi2CXSmiles(x):\n    return Chem.MolToCXSmiles(Chem.inchi.MolFromInchi(x))\n\ndef InChi2CX_Smiles(x):\n    mol = Chem.inchi.MolFromInchi(x)\n    return [Chem.MolToSmiles(mol),Chem.MolToCXSmiles(mol)]\ndef Smiles2InChi(x):\n    return Chem.MolToInchi(Chem.MolFromSmiles(x))\ndef show_InChi(x):\n    display(Chem.inchi.MolFromInchi(x))\ndef show_Smiles(x):\n    display(Chem.MolFromSmiles(x))\nroot =\"../input/bms-molecular-translation/train\"\ndef show_image(x):\n    path = f\"{root}/{x[0]}/{x[1]}/{x[2]}/{x}.png\" \n    plt.imshow(plt.imread(path),cmap=\"gray\")\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_3dmol(image_id=None,inchi=None,smiles=None,mol=None):\n    mol = (mol or \n           (smiles and Chem.MolFromSmiles(smiles)) or \n           (inchi and Chem.MolFromInchi(inchi)) or\n           (image_id and (image_id in train.index) \n            and Chem.MolFromInchi(train.InChI[image_id])))\n    if not mol:\n        print('No molecule is provided')\n        return\n           \n    molh = Chem.AddHs(mol)\n    if AllChem.EmbedMolecule(molh,randomSeed=0xf00d)<0:\n        print('Failed to embed in 3d')\n        return\n    pdb_data = Chem.MolToPDBBlock(molh)\n    view = py3Dmol.view(width=680, height=300, query=None, data=pdb_data, linked=False)\n    view.setStyle({'stick': {}})\n    view.setBackgroundColor('#f9f4fb')\n    return view","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"A portion of the training data\n------------------------------"},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nn_samples = 10_000\nn_samples = None\ntrain = pd.read_csv(\"../input/bms-molecular-translation/train_labels.csv\",nrows=n_samples)\ntrain = train.set_index('image_id')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_id = '0000f7467baa'\nshow_image(image_id)\ndisplay(Chem.inchi.MolFromInchi(train.InChI[image_id]))\nshow_3dmol(image_id).show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"A failure\n---------"},{"metadata":{"trusted":true},"cell_type":"code","source":"image_id = '4dbbaa6fa2d2'\nshow_image(image_id)\ndisplay(Chem.inchi.MolFromInchi(train.InChI[image_id]))\nshow_3dmol(image_id)\n","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}