Skip to content

Commit

Permalink
Finish enm
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy Click committed Sep 20, 2017
2 parents c6f15f4 + 8eec2f5 commit 79e4d96
Show file tree
Hide file tree
Showing 30 changed files with 78,732 additions and 86 deletions.
30 changes: 0 additions & 30 deletions src/fluctmatch/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,3 @@
print_function,
unicode_literals,
)

from future.utils import (
native_str,
raise_from,
with_metaclass,
)
from future.builtins import (
ascii,
bytes,
chr,
dict,
filter,
hex,
input,
map,
next,
oct,
open,
pow,
range,
round,
str,
super,
zip,
)

import numpy as np
import pandas as pd


86 changes: 86 additions & 0 deletions src/fluctmatch/models/enm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import,
division,
print_function,
unicode_literals,
)

from future.builtins import (
super,
zip,
)

from future.utils import (
raise_with_traceback,
)

import numpy as np

import MDAnalysis as mda
from MDAnalysis.core import topologyattrs
from MDAnalysis.lib.distances import distance_array

from . import universe


class Enm(universe._Universe):
_rmin = 0.
_rmax = 10.

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._initialize(*args, **kwargs)

def __repr__(self):
message = "<CG Universe with {} beads".format(self.atoms.n_atoms)
try:
message += " and {:d} bonds".format(len(self._topology.bonds.values))
except AttributeError as exc:
pass
finally:
message += ">"
return message

def _initialize(self, *args, **kwargs):
# Atomistic Universe
try:
self.atu = mda.Universe(*args, **kwargs)
except (IOError, OSError, ValueError) as exc:
raise_with_traceback(RuntimeError("Failed to create a universe."))
self.__dict__.update(self.atu.__dict__)

universe.rename_universe(self)
self._generate_from_topology()

self._add_bonds()
if kwargs.get("guess_bonds", False):
self._add_angles()
self._add_dihedrals()
self._add_impropers()

def _add_bonds(self):
dm = np.asarray([distance_array(self.atoms.positions, self.atoms.positions, backend="OpenMP") for _ in self.trajectory])
if self._rmin > 0.:
_, a0, a1 = np.where((dm >= self._rmin) & (dm <= self._rmax))
else:
_, a0, a1 = np.where((dm > self._rmin) & (dm <= self._rmax))
bonds = topologyattrs.Bonds(set([(x, y) for x, y in zip(a0, a1) if y > x]))
self._topology.add_TopologyAttr(bonds)
self._generate_from_topology()

@property
def rmin(self):
return self._rmin

@rmin.setter
def rmin(self, distance):
self._rmin = distance

@property
def rmax(self):
return self._rmax

@rmax.setter
def rmax(self, distance):
self._rmax = distance
94 changes: 94 additions & 0 deletions src/fluctmatch/models/ions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import,
division,
print_function,
unicode_literals,
)

from future.builtins import (
super,
zip,
)

from collections import OrderedDict

from . import universe
from .selection import *


class SolventIons(universe._Universe):
"""Include ions within the solvent.
"""
_mapping = OrderedDict()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._mapping["ION"] = "name LI LIT K NA F CL BR I"

kwargs["guess_bonds"] = False
kwargs["mapping"] = self._mapping
self._initialize(*args, **kwargs)
resnames = np.unique(self.residues.resnames)
restypes = {
k: v
for k, v in zip(resnames, np.arange(resnames.size)+10)
}
self.atoms.types = np.asarray([
restypes[atom.resname]
for atom in self.atoms
])

def _add_bonds(self):
pass


class BioIons(universe._Universe):
"""Select ions normally found within biological systems.
"""
_mapping = OrderedDict()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._mapping["ions"] = "bioion"

kwargs["guess_bonds"] = False
kwargs["mapping"] = self._mapping
self._initialize(*args, **kwargs)
resnames = np.unique(self.residues.resnames)
restypes = {
k: v
for k, v in zip(resnames, np.arange(resnames.size)+20)
}
self.atoms.types = np.asarray([
restypes[atom.resname]
for atom in self.atoms
])

def _add_bonds(self):
pass


class NobleAtoms(universe._Universe):
"""Select atoms column VIII of the periodic table.
"""
_mapping = OrderedDict()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

kwargs["guess_bonds"] = False
kwargs["mapping"] = self._mapping
self._initialize(*args, **kwargs)
resnames = np.unique(self.residues.resnames)
restypes = {
k: v
for k, v in zip(resnames, np.arange(resnames.size)+40)
}
self.atoms.types = np.asarray([
restypes[atom.resname]
for atom in self.atoms
])

def _add_bonds(self):
pass
110 changes: 110 additions & 0 deletions src/fluctmatch/models/nucleic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import,
division,
print_function,
unicode_literals,
)

from future.builtins import (
super,
zip,
)
from collections import OrderedDict

from MDAnalysis.core import topologyattrs
from . import universe
from .selection import *


class Nucleic3(universe._Universe):
"""Create a universe consisting of the phosphate, sugar, and base of the nucleic acid.
"""
_mapping = OrderedDict()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._mapping["P"] = "nucleicphosphate"
self._mapping["C4'"] = "hnucleicsugar"
self._mapping["C5"] = "hnucleicbase"

kwargs["mapping"] = self._mapping
self._initialize(*args, **kwargs)

def _add_bonds(self):
bonds = []
bonds.extend([
_
for s in self.segments
for _ in zip(s.atoms.select_atoms("name P").ix,
s.atoms.select_atoms("name C4'").ix)
])
bonds.extend([
_
for s in self.segments
for _ in zip(s.atoms.select_atoms("name C4'").ix,
s.atoms.select_atoms("name C5").ix)
])
bonds.extend([
_
for s in self.segments
for _ in zip(s.atoms.select_atoms("name C4'").ix[:-1],
s.atoms.select_atoms("name P").ix[1:])
])
self._topology.add_TopologyAttr(topologyattrs.Bonds(bonds))
self._generate_from_topology()


class Nucleic4(universe._Universe):
"""Create a universe consisting of the phosphate, C4', C3', and base of the nucleic acid.
"""
_mapping = OrderedDict()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._mapping["P"] = "nucleicphosphate"
self._mapping["C4'"] = "sugarC4"
self._mapping["C3'"] = "sugarC3"
self._mapping["C5"] = "nucleiccenter"
_nucl = "hnucleicbase"

kwargs["mapping"] = self._mapping
self._initialize(*args, **kwargs)

# Update the masses and charges
nucl_base = self.atu.select_atoms(_nucl).split("residue")
self.atoms.select_atoms("name C5").masses = np.array([_.total_mass() for _ in nucl_base])

try:
self.atoms.select_atoms("name C5").charges = np.array([_.total_charge() for _ in nucl_base])
except AttributeError:
pass

def _add_bonds(self):
bonds = []
bonds.extend([
_
for s in self.segments
for _ in zip(s.atoms.select_atoms("name P").ix,
s.atoms.select_atoms("name C4'").ix)
])
bonds.extend([
_
for s in self.segments
for _ in zip(s.atoms.select_atoms("name C4'").ix,
s.atoms.select_atoms("name C3'").ix)
])
bonds.extend([
_
for s in self.segments
for _ in zip(s.atoms.select_atoms("name C4'").ix,
s.atoms.select_atoms("name C5").ix)
])
bonds.extend([
_
for s in self.segments
for _ in zip(s.atoms.select_atoms("name C3'").ix[:-1],
s.atoms.select_atoms("name P").ix[1:])
])
self._topology.add_TopologyAttr(topologyattrs.Bonds(bonds))
self._generate_from_topology()
Loading

0 comments on commit 79e4d96

Please sign in to comment.