Skip to content

Commit

Permalink
added PDB trajectory writing capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
corinwagen committed Mar 23, 2020
1 parent ab9a1b4 commit 0192e31
Show file tree
Hide file tree
Showing 10 changed files with 286,183 additions and 4 deletions.
1 change: 1 addition & 0 deletions cctk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
from .mol2_file import MOL2File
from .mae_file import MAEFile
from .orca_file import OrcaFile
from .pdb_file import PDBFile

62 changes: 62 additions & 0 deletions cctk/pdb_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import sys
import re
import numpy as np

from abc import abstractmethod

from cctk import File, Molecule, ConformationalEnsemble
from cctk.helper_functions import get_symbol

class PDBFile(File):
"""
Generic class for all ``.pdb`` files.
"""

def __init__(self, molecule, title=None):
pass

@classmethod
def read_file(cls, filename):
pass

@classmethod
def write_molecule_to_file(cls, filename, molecule, num=1, append=False):
"""
Write a ``.pdb`` file, using object attributes.
Args:
filename (str): path to the new file
molecule (Molecule): ``Molecule`` object
num (int): model number
append (Bool): whether to write to file normally or append
"""
text = f"MODEL {num}\n"

for idx, Z in enumerate(molecule.atomic_numbers, start=1):
line = molecule.get_vector(idx)
symb = get_symbol(Z)
text += f"HETATM {idx:>4} {symb:<2} * 0 {line[0]:7.3f} {line[1]:7.3f} {line[2]:7.3f} 1.00 0.00 {symb:<2}\n"

text += f"ENDMDL\n"

if append:
super().append_to_file(filename, text)
else:
super().write_file(filename, text)


@classmethod
def write_ensemble_to_trajectory(cls, filename, ensemble):
"""
Writes a ``ConformationalEnsemble`` to a trajectory file.
Args:
filename (str): where to write the file
ensemble (Ensemble): ``Ensemble`` object to write
"""
for idx, molecule in enumerate(ensemble.molecules):
if idx == 0:
cls.write_molecule_to_file(filename, molecule, num=idx+1, append=False)
else:
cls.write_molecule_to_file(filename, molecule, num=idx+1, append=True)

44 changes: 44 additions & 0 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,50 @@ Now, run ``conda activate cctk`` to enter the ``cctk`` Python environment (and `

(More complete guides to ``conda`` usage can be found elsewhere.)

**Upgrading**

To get the latest version of *cctk*, navigate to the correct ``conda`` environment and run::

$ pip install --upgrade cctk

Your First *cctk* Script
------------------------

Once you have *cctk* properly installed, you can write a simple "hello world" program.

Create a sample ``.xyz`` file (for instance, ``water.xyz``)::

3
hello world
O 0.0 0.0 0.0
H 1.0 0.0 0.0
H 0.0 1.0 0.0

Now create a file called ``hello_world.py``::

import cctk

file = cctk.XYZFile.read_file("water.xyz")

print(f"{file.title} is title")
print(f"{file.molecule.atomic_numbers} is atomic numbers")
angle = file.molecule.get_angle(2,1,3)
print(f"The angle between atoms 2, 1, and 3 is {angle} degrees")

if angle < 105:
print("This is a strained water molecule!")

Running this script produces the following output::

$ ls
hello_world.py water.xyz
$ python hello_world.py
hello world is title
[8 1 1] is atomic numbers
The angle between atoms 2, 1, and 3 is 90.0 degrees
This is a strained water molecule!

Using *cctk*
____________

Expand Down
4 changes: 2 additions & 2 deletions scripts/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@
distances[i]
))

except ValueError as e:
print(f"job has not finished any iterations: {e}")
except:
print(f"job has not finished any iterations")
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
packages=["cctk", "cctk.data", "cctk.groups"],
# include_package_data=True,
package_data={"cctk.data": ["*"], "cctk.groups": ["*"],},
version="v0.1.2",
version="v0.1.3",
license="Apache 2.O",
description="computational chemistry toolkit",
author="Corin Wagen and Eugene Kwan",
author_email="corin.wagen@gmail.com",
url="https://github.com/ekwan/cctk",
download_url="https://github.com/ekwan/cctk/archive/v0.1.2.tar.gz",
download_url="https://github.com/ekwan/cctk/archive/v0.1.3.tar.gz",
install_requires=["numpy", "networkx", "importlib_resources"],
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 0192e31

Please sign in to comment.