{"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":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <center>Molecule<center>\nA molecule is the smallest particle of a substance made up of one or more atoms, that has all of the physical and chemical properties of that substance. For instance, an oxygen molecule has two oxygen atoms, a water molecule has two hydrogen atoms and one oxygen atom. Biological molecules, such as proteins and DNA, can be made up of many thousands of atoms. [[1]](https://www.cancer.gov/publications/dictionaries/cancer-terms/def/molecule)  \n<center><img src=\"https://images.fineartamerica.com/images/artworkimages/mediumlarge/3/serotonin-molecule-art-galaxy.jpg\" height=\"400\" width=\"700\"/></center>\n<br>\n<center>Figure - 1: <i><a href=\"https://emag.medicalexpo.com/real-time-artificial-intelligence-system-for-the-operating-room/\">Serotonine Molecule</a></i></center>\n<br>","metadata":{}},{"cell_type":"markdown","source":"**Updates**\n\n1. Added Hydrogen atoms to the molecule. \n2. Removed solvent molecules [Br, Cl in bb_2 and bb_3 structures]","metadata":{}},{"cell_type":"markdown","source":"# <center>Visualizing Molecules using py3Dmol<center>","metadata":{}},{"cell_type":"markdown","source":"> **Install libraries**","metadata":{}},{"cell_type":"code","source":"!pip install dask \n!pip install rdkit\n!pip install py3Dmol","metadata":{"execution":{"iopub.status.busy":"2024-04-27T04:01:01.311292Z","iopub.execute_input":"2024-04-27T04:01:01.311720Z","iopub.status.idle":"2024-04-27T04:01:50.685602Z","shell.execute_reply.started":"2024-04-27T04:01:01.311682Z","shell.execute_reply":"2024-04-27T04:01:50.684173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> **Import Libraries**","metadata":{}},{"cell_type":"code","source":"import dask.dataframe as dd \nfrom rdkit import Chem \nfrom rdkit.Chem import Draw\nimport py3Dmol\nfrom rdkit.Chem import AllChem","metadata":{"execution":{"iopub.status.busy":"2024-04-27T04:01:50.687919Z","iopub.execute_input":"2024-04-27T04:01:50.688274Z","iopub.status.idle":"2024-04-27T04:01:53.952676Z","shell.execute_reply.started":"2024-04-27T04:01:50.688241Z","shell.execute_reply":"2024-04-27T04:01:53.951398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> **Load the dataset**","metadata":{}},{"cell_type":"code","source":"#Read the train_parquet file\nparquet = dd.read_parquet(\"/kaggle/input/leash-BELKA/train.parquet\")\nparquet.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T04:01:53.954466Z","iopub.execute_input":"2024-04-27T04:01:53.955217Z","iopub.status.idle":"2024-04-27T04:01:55.966770Z","shell.execute_reply.started":"2024-04-27T04:01:53.955140Z","shell.execute_reply":"2024-04-27T04:01:55.965923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> **2D Visualization**","metadata":{}},{"cell_type":"code","source":"# Code to visualize the first molecule in molecule_smiles col in 2D.\n\n# Convert Dask Series to pandas Series and extract the first SMILES string\nfirst_smiles = parquet['molecule_smiles'].compute().iloc[0]\n\n# Convert the SMILES string to a molecule object\nfirst_molecule = Chem.MolFromSmiles(first_smiles)\n\n# Draw and display the molecule\nDraw.MolToImage(first_molecule, size=(700, 700))\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-27T04:01:55.969106Z","iopub.execute_input":"2024-04-27T04:01:55.970037Z","iopub.status.idle":"2024-04-27T04:02:31.399145Z","shell.execute_reply.started":"2024-04-27T04:01:55.970001Z","shell.execute_reply":"2024-04-27T04:02:31.397886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> **3D Visualization**","metadata":{}},{"cell_type":"markdown","source":"> **1. Molecule Smiles**","metadata":{}},{"cell_type":"code","source":"\n# Code to visualize the first molecule in molecule_smiles col in 3D.\n\n# Convert the SMILES string to an RDKit molecule object\nfirst_molecule_smiles = parquet['molecule_smiles'].compute().iloc[0]\nfirst_molecule = Chem.MolFromSmiles(first_molecule_smiles)\n\n# 2. Add hydrogen atoms to the molecule\nfirst_molecule = Chem.AddHs(first_molecule)\n\n# Embed the molecule in 3D space\nAllChem.EmbedMolecule(first_molecule)\n\n# Visualize the molecule in 3D using Py3Dmol\nview = py3Dmol.view(width=800, height=600)\npdb_block_molecule = Chem.MolToPDBBlock(first_molecule)\nview.addModel(pdb_block_molecule, 'pdb')\nview.setStyle({'stick': {}})\nview.zoomTo()\nview.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T04:02:31.400673Z","iopub.execute_input":"2024-04-27T04:02:31.401582Z","iopub.status.idle":"2024-04-27T04:03:03.605084Z","shell.execute_reply.started":"2024-04-27T04:02:31.401541Z","shell.execute_reply":"2024-04-27T04:03:03.603913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> **2. Building Block 1 Smiles**","metadata":{}},{"cell_type":"code","source":"\n# Code to visualize the first molecule in building block_1 col in 3D.\n# Convert the SMILES string to an RDKit molecule object\nbb1_smiles = parquet['buildingblock1_smiles'].compute().iloc[0]\nbb1_molecule = Chem.MolFromSmiles(bb1_smiles)\n\n# 2. Add hydrogen atoms to the molecule\nbb1_molecule = Chem.AddHs(bb1_molecule)\n\n# Embed the molecule in 3D space\nAllChem.EmbedMolecule(bb1_molecule)\n\n# Visualize the molecule in 3D using Py3Dmol\nview = py3Dmol.view(width=800, height=600)\npdb_block_bb_1 = Chem.MolToPDBBlock(bb1_molecule)\nview.addModel(pdb_block_bb_1, 'pdb')\nview.setStyle({'stick': {}})\nview.zoomTo()\nview.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T04:03:03.606650Z","iopub.execute_input":"2024-04-27T04:03:03.607026Z","iopub.status.idle":"2024-04-27T04:03:18.802172Z","shell.execute_reply.started":"2024-04-27T04:03:03.606996Z","shell.execute_reply":"2024-04-27T04:03:18.800910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> **3. Building Block 2 Smiles**","metadata":{}},{"cell_type":"code","source":"\n# Code to visualize the first molecule in building block_2 col in 3D.\n# Convert the SMILES string to an RDKit molecule object\nbb2_smiles = parquet['buildingblock2_smiles'].compute().iloc[0]\nbb2_molecule = Chem.MolFromSmiles(bb2_smiles)\n\n# Add hydrogen atoms to the molecule\nbb2_molecule = Chem.AddHs(bb2_molecule)\n\n# Embed the molecule in 3D space\nAllChem.EmbedMolecule(bb2_molecule)\n\n# Check if the molecule contains a solvent molecule (chlorine atom 'Cl')\nif 'Cl' in bb2_smiles:\n    # Remove the solvent molecule\n    bb2_molecule = Chem.DeleteSubstructs(bb2_molecule, Chem.MolFromSmiles('Cl'))\n\n# Visualize the molecule in 3D using Py3Dmol\nview = py3Dmol.view(width=800, height=600)\npdb_block_bb_2 = Chem.MolToPDBBlock(bb2_molecule)\nview.addModel(pdb_block_bb_2, 'pdb')\nview.setStyle({'stick': {}})\nview.zoomTo()\nview.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T04:03:18.804261Z","iopub.execute_input":"2024-04-27T04:03:18.804969Z","iopub.status.idle":"2024-04-27T04:03:27.271293Z","shell.execute_reply.started":"2024-04-27T04:03:18.804920Z","shell.execute_reply":"2024-04-27T04:03:27.270317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> **4. Building Block 3 Smiles**","metadata":{}},{"cell_type":"code","source":"\n# Code to visualize the first molecule in building block_3 col in 3D.\n# Convert the SMILES string to an RDKit molecule object\nbb3_smiles = parquet['buildingblock3_smiles'].compute().iloc[0]\nbb3_molecule = Chem.MolFromSmiles(bb3_smiles)\n\n# Add hydrogen atoms to the molecule\nbb3_molecule = Chem.AddHs(bb3_molecule)\n\n\n# Embed the molecule in 3D space\nAllChem.EmbedMolecule(bb3_molecule)\n\n# Check if the molecule contains a solvent molecule (bromine atom 'Br')\nif 'Br' in bb3_smiles:\n    # Remove the solvent molecule\n    bb3_molecule = Chem.DeleteSubstructs(bb3_molecule, Chem.MolFromSmiles('Br'))\n\n\n# Visualize the molecule in 3D using Py3Dmol\nview = py3Dmol.view(width=800, height=600)\npdb_block_bb_3 = Chem.MolToPDBBlock(bb3_molecule)\nview.addModel(pdb_block_bb_3, 'pdb')\nview.setStyle({'stick': {}})\nview.zoomTo()\nview.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T04:03:27.272604Z","iopub.execute_input":"2024-04-27T04:03:27.273128Z","iopub.status.idle":"2024-04-27T04:03:34.735796Z","shell.execute_reply.started":"2024-04-27T04:03:27.273098Z","shell.execute_reply":"2024-04-27T04:03:34.734533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[](http://https://alphafold.ebi.ac.uk/files/AF-P02768-F1-predicted_aligned_error_v4.png)","metadata":{}},{"cell_type":"markdown","source":"**References**\n\n1. [Molecule - Definition by National Cancer Institute.](https://www.cancer.gov/publications/dictionaries/cancer-terms/def/molecule)","metadata":{}},{"cell_type":"markdown","source":"Note: This work is in progress. This is only the basic 3d visualization to get a better perspective of what we are working with in this competition! :)","metadata":{}}]}