{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"# HOW TO: Easy Visualization of Molecules.\n\nGreetings everyone!\n\nI've seen many people ask for a simple yet elegant visualization tool for the [Predicting Molecular Properties](https://www.kaggle.com/c/champs-scalar-coupling/overview) challenge. Therefore, I'm going to explain in this Kernel how to install and use **ase**, which is a python module that allows one to work with atoms and molecules. \n\nIt is available on gitlab: [ase](https://gitlab.com/ase/ase).\n\nThe first thing we need to do is to install **ase** on our Kernel. To do that, just click on the *Settings* tab on the right panel, then click on *Install...*, right next to *Packages*. In the *pip package name* entry, just write **ase** then hit *Install Package*.\n\nKaggle is going to do its things then restart the Kernel. **ase** should then be installed! Let's check this:"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import ase","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"It worked! \n\nNow let's visualize one of the molecule from the structures.csv file:"},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\n\nstruct_file = pd.read_csv('../input/structures.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we select a random molecule from this file:"},{"metadata":{"trusted":true},"cell_type":"code","source":"import random\n\n# Select a molecule\nrandom_molecule = random.choice(struct_file['molecule_name'].unique())\nmolecule = struct_file[struct_file['molecule_name'] == random_molecule]\ndisplay(molecule)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Next we need to retrieve the atomic coordinates in a numpy array form:"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Get atomic coordinates\natoms = molecule.iloc[:, 3:].values\nprint(atoms)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The last thing we need is the atomic symbols:"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Get atomic symbols\nsymbols = molecule.iloc[:, 2].values\nprint(symbols)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Finally, let's put everything into something that **ase** can process:"},{"metadata":{"trusted":true},"cell_type":"code","source":"from ase import Atoms\nimport ase.visualize\n\nsystem = Atoms(positions=atoms, symbols=symbols)\n\nase.visualize.view(system, viewer=\"x3d\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"TADA!!!\n\nYou can rotate the molecule with a left click, translate it with a middle click, and zoom in or out using right click. \n\nAll this can be summarized in a single function:"},{"metadata":{"trusted":true},"cell_type":"code","source":"def view(molecule):\n    # Select a molecule\n    mol = struct_file[struct_file['molecule_name'] == molecule]\n    \n    # Get atomic coordinates\n    xcart = mol.iloc[:, 3:].values\n    \n    # Get atomic symbols\n    symbols = mol.iloc[:, 2].values\n    \n    # Display molecule\n    system = Atoms(positions=xcart, symbols=symbols)\n    print('Molecule Name: %s.' %molecule)\n    return ase.visualize.view(system, viewer=\"x3d\")\n\nrandom_molecule = random.choice(struct_file['molecule_name'].unique())\nview(random_molecule)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"I hope you enjoyed this little notebook!\n\nCheers\n\nBoris D."}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}