{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Input and Output in PyBigDFT\n", "There are a number of standard file types that PyBigDFT can work with using its IO module. Here we will demonstrate some of that capability. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## XYZ Files\n", "The XYZReader class has access to some built in molecules, so let's begin there. You can access any of the molecules in the database just by opening an XYZReader with that name. Or you can specify a filename path to get something you've already made yourself." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from BigDFT.IO import XYZReader\n", "from BigDFT.Systems import System\n", "from BigDFT.Fragments import Fragment\n", "\n", "sys = System()\n", "sys[\"SI4:0\"] = Fragment()\n", "with XYZReader(\"CH4\") as ifile:\n", " for atom in ifile:\n", " sys[\"SI4:0\"].append(atom)\n", " \n", "sys[\"CH2F:1\"] = Fragment()\n", "with XYZReader(\"CH2F\") as ifile:\n", " for atom in ifile:\n", " sys[\"CH2F:1\"].append(atom)\n", " \n", "sys[\"CH2F:1\"].translate([-5, 0, 0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's look at what we built." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", "text/html": [ "
\n", "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", " jupyter labextension install jupyterlab_3dmol

\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from BigDFT.Visualization import InlineVisualizer\n", "viz = InlineVisualizer(500,400)\n", "viz.display_system(sys)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The XYZWriter works in a similar way." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from BigDFT.IO import XYZWriter\n", "natoms = sum([len(x) for x in sys.values()])\n", "with XYZWriter(\"test.xyz\", units=\"angstroem\", natoms=natoms) as ofile:\n", " for frag in sys.values():\n", " for at in frag:\n", " ofile.write(at)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes you just want to read and write systems quickly. In this case, there is the `read_xyz` and `write_xyz` functions." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from BigDFT.IO import write_xyz\n", "with open(\"test2.xyz\", \"w\") as ofile:\n", " write_xyz(sys, ofile)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from BigDFT.IO import read_xyz\n", "with open(\"test2.xyz\") as ifile:\n", " sys2 = read_xyz(ifile, fragmentation=\"atomic\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When reading in an xyz file, there is no fragment information available. By default, it creates a system where each atom will be its own fragment. You can also choose `single` to read into one big fragment." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", "text/html": [ "
\n", "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", " jupyter labextension install jupyterlab_3dmol

\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "viz = InlineVisualizer(500,400)\n", "viz.display_system(sys2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Merging the system into one fragment yourself is also a straightforward operation." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "sys3 = System()\n", "sys3[\"FRA:0\"] = sum(sys2.values())" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "application/3dmoljs_load.v0": "
\n

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol

\n
\n", "text/html": [ "
\n", "

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n", " jupyter labextension install jupyterlab_3dmol

\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "viz = InlineVisualizer(500,400)\n", "viz.display_system(sys3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other Formats\n", "The other formats work much like the `read_xyz` and `write_xyz` approach. For example, let's say we want to write a PDB file." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from BigDFT.IO import write_pdb\n", "with open(\"test.pdb\", \"w\") as ofile:\n", " write_pdb(sys, ofile)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's test if that worked by loading the written file into a string." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HETATM 1 C SI4 A 0 0.000 0.000 0.000 1.00 0.00 B C \n", "HETATM 2 H SI4 A 0 0.628 0.628 0.628 1.00 0.00 B H \n", "HETATM 3 H SI4 A 0 0.628 -0.628 -0.628 1.00 0.00 B H \n", "HETATM 4 H SI4 A 0 -0.628 0.628 -0.628 1.00 0.00 B H \n", "HETATM 5 H SI4 A 0 -0.628 -0.628 0.628 1.00 0.00 B H \n", "HETATM 6 C CH2 A 1 -2.675 0.655 0.000 1.00 0.00 B C \n", "HETATM 7 F CH2 A 1 -2.675 -0.682 0.000 1.00 0.00 B F \n", "HETATM 8 H CH2 A 1 -2.431 1.104 0.947 1.00 0.00 B H \n", "HETATM 9 H CH2 A 1 -2.431 1.104 -0.947 1.00 0.00 B H \n" ] } ], "source": [ "with open(\"test.pdb\") as ifile:\n", " for line in ifile:\n", " print(line, end=\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And of course we can read in a system like this." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "from BigDFT.IO import read_pdb\n", "with open(\"test.pdb\") as ifile:\n", " sys2 = read_pdb(ifile)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SI4:0\n", "{'sym': 'C', 'r': [0.0, 0.0, 0.0], 'name': 'C', 'units': 'angstroem'}\n", "{'sym': 'H', 'r': [0.628, 0.628, 0.628], 'name': 'H', 'units': 'angstroem'}\n", "{'sym': 'H', 'r': [0.628, -0.628, -0.628], 'name': 'H', 'units': 'angstroem'}\n", "{'sym': 'H', 'r': [-0.628, 0.628, -0.628], 'name': 'H', 'units': 'angstroem'}\n", "{'sym': 'H', 'r': [-0.628, -0.628, 0.628], 'name': 'H', 'units': 'angstroem'}\n", "CH2:1\n", "{'sym': 'C', 'r': [-2.675, 0.655, 0.0], 'name': 'C', 'units': 'angstroem'}\n", "{'sym': 'F', 'r': [-2.675, -0.682, 0.0], 'name': 'F', 'units': 'angstroem'}\n", "{'sym': 'H', 'r': [-2.431, 1.104, 0.947], 'name': 'H', 'units': 'angstroem'}\n", "{'sym': 'H', 'r': [-2.431, 1.104, -0.947], 'name': 'H', 'units': 'angstroem'}\n" ] } ], "source": [ "for fragid, frag in sys2.items():\n", " print(fragid)\n", " for at in frag:\n", " print(dict(at))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see that in this case, the read function was able to break the system into fragments. This is because PDB files have fragment information in them, making them a very convenient format for using PyBigDFT." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.10" } }, "nbformat": 4, "nbformat_minor": 4 }