{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook demonstrates pymatgen's functionality in terms of creating and editing molecules, as well as its integration with OpenBabel. For the latter, please note that you will need to have openbabel with python bindings installed. Please refer to pymatgen's documentation for installation details." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Uncomment the subsequent lines in this cell to install dependencies for Google Colab.\n", "# !pip install pymatgen==2022.7.19" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Molecules" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Full Formula (H4 C1)\n", "Reduced Formula: H4C\n", "Charge = 0.0, Spin Mult = 1\n", "Sites (5)\n", "0 C 0.000000 0.000000 0.000000\n", "1 H 0.000000 0.000000 1.089000\n", "2 H 1.026719 0.000000 -0.363000\n", "3 H -0.513360 -0.889165 -0.363000\n", "4 H -0.513360 0.889165 -0.363000\n" ] } ], "source": [ "from pymatgen.core import Molecule\n", "\n", "# Create a methane molecule.\n", "coords = [\n", " [0.000000, 0.000000, 0.000000],\n", " [0.000000, 0.000000, 1.089000],\n", " [1.026719, 0.000000, -0.363000],\n", " [-0.513360, -0.889165, -0.363000],\n", " [-0.513360, 0.889165, -0.363000],\n", "]\n", "mol = Molecule([\"C\", \"H\", \"H\", \"H\", \"H\"], coords)\n", "print(mol)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0. 0.] C\n", "[0. 0. 1.089] H\n" ] } ], "source": [ "# A Molecule is simply a list of Sites.\n", "print(mol[0])\n", "print(mol[1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Full Formula (H3 C1)\n", "Reduced Formula: H3C\n", "Charge = 0, Spin Mult = 2\n", "Sites (4)\n", "0 C 0.000000 0.000000 0.000000\n", "1 H 1.026719 0.000000 -0.363000\n", "2 H -0.513360 -0.889165 -0.363000\n", "3 H -0.513360 0.889165 -0.363000\n", "Full Formula (H1)\n", "Reduced Formula: H2\n", "Charge = 0, Spin Mult = 2\n", "Sites (1)\n", "0 H 0.000000 0.000000 1.089000\n" ] } ], "source": [ "# Break a Molecule into two by breaking a bond.\n", "for frag in mol.break_bond(0, 1):\n", " print(frag)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[Site: H (0.0000, 0.0000, 1.0890), Site: H (1.0267, 0.0000, -0.3630), Site: H (-0.5134, -0.8892, -0.3630), Site: H (-0.5134, 0.8892, -0.3630)]\n" ] } ], "source": [ "# Getting neighbors that are within 3 angstroms from C atom.\n", "print(mol.get_neighbors(mol[0], 3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[Covalent bond between [0. 0. 0.] C and [0. 0. 1.089] H, Covalent bond between [0. 0. 0.] C and [ 1.026719 0. -0.363 ] H, Covalent bond between [0. 0. 0.] C and [-0.51336 -0.889165 -0.363 ] H, Covalent bond between [0. 0. 0.] C and [-0.51336 0.889165 -0.363 ] H]\n" ] } ], "source": [ "# Detecting bonds\n", "print(mol.get_covalent_bonds())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Full Formula (H4 C1)\n", "Reduced Formula: H4C\n", "abc : 10.000000 10.000000 10.000000\n", "angles: 90.000000 90.000000 90.000000\n", "Sites (5)\n", " # SP a b c\n", "--- ---- -------- -------- ------\n", " 0 H 0.5 0.5 0.6089\n", " 1 H 0.602672 0.5 0.4637\n", " 2 H 0.448664 0.411083 0.4637\n", " 3 H 0.448664 0.588917 0.4637\n", " 4 C 0.5 0.5 0.5\n" ] } ], "source": [ "# If you need to run the molecule in a box with a periodic boundary condition\n", "# code, you can generate the boxed structure as follows (in a 10Ax10Ax10A box)\n", "structure = mol.get_boxed_structure(10, 10, 10)\n", "print(structure)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Writing to XYZ files (easy to open with many molecule file viewers)\n", "from pymatgen.io.xyz import XYZ\n", "\n", "xyz = XYZ(mol)\n", "xyz.write_file(\"methane.xyz\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Openbabel interface" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This section demonstrates pymatgen's integration with openbabel." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'pybel'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mpymatgen\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbabel\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mBabelMolAdaptor\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mpybel\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mpb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mBabelMolAdaptor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# Create a pybel.Molecule, which simplifies a lot of access\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mpm\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mMolecule\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopenbabel_mol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'pybel'" ] } ], "source": [ "import pybel as pb\n", "from pymatgen.io.babel import BabelMolAdaptor\n", "\n", "a = BabelMolAdaptor(mol)\n", "# Create a pybel.Molecule, which simplifies a lot of access\n", "pm = pb.Molecule(a.openbabel_mol)\n", "# Print canonical SMILES representation (unique and comparable).\n", "print(\"Canonical SMILES = {}\".format(pm.write(\"can\")))\n", "# Print Inchi representation\n", "print(\"Inchi= {}\".format(pm.write(\"inchi\")))\n", "# pb.outformats provides a listing of available formats.\n", "# Let's do a write to the commonly used PDB file.\n", "pm.write(\"pdb\", filename=\"methane.pdb\", overwrite=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Generating ethylene carbonate (SMILES obtained from Wikipedia)\n", "# And displaying the svg.\n", "ec = pb.readstring(\"smi\", \"C1COC(=O)O1\")\n", "ec.make3D()\n", "from IPython.core.display import SVG, display_svg\n", "\n", "svg = SVG(ec.write(\"svg\"))\n", "display_svg(svg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Input/Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pymatgen has built-in support for the XYZ and Gaussian, NWchem file formats. It also has support for most other file formats if you have openbabel with Python bindings installed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(mol.to(fmt=\"xyz\"))\n", "print(mol.to(fmt=\"g09\"))\n", "print(mol.to(fmt=\"pdb\")) # Needs Openbabel.\n", "\n", "mol.to(filename=\"methane.xyz\")\n", "mol.to(filename=\"methane.pdb\") # Needs Openbabel.\n", "\n", "print(Molecule.from_file(\"methane.pdb\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more fine-grained control over output, you can use the underlying IO classes Gaussian and Nwchem, two commonly used computational chemistry programs." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pymatgen.io.gaussian import GaussianInput\n", "\n", "gau = GaussianInput(\n", " mol,\n", " charge=0,\n", " spin_multiplicity=1,\n", " title=\"methane\",\n", " functional=\"B3LYP\",\n", " basis_set=\"6-31G(d)\",\n", " route_parameters={\"Opt\": \"\", \"SCF\": \"Tight\"},\n", " link0_parameters={\"%mem\": \"1000MW\"},\n", ")\n", "print(gau)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A standard relaxation + SCF energy nwchem calculation input file for methane.\n", "from pymatgen.io.nwchem import NwInput, NwTask\n", "\n", "tasks = [\n", " NwTask.dft_task(mol, operation=\"optimize\", xc=\"b3lyp\", basis_set=\"6-31G\"),\n", " NwTask.dft_task(mol, operation=\"freq\", xc=\"b3lyp\", basis_set=\"6-31G\"),\n", " NwTask.dft_task(mol, operation=\"energy\", xc=\"b3lyp\", basis_set=\"6-311G\"),\n", "]\n", "nwi = NwInput(mol, tasks, geometry_options=[\"units\", \"angstroms\"])\n", "print(nwi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## This concludes the demo on pymatgen's basic capabilities for molecules." ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 1 }