Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: add a hydrogel builder #103

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/testsuite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
uses: actions/checkout@main
- name: Validate CITATION.cff
uses: dieghernan/cff-validator@v3
with:
install-r: true # Ensure R is explicitly installed
- name: Setup EESSI
uses: eessi/github-action-eessi@v3
with:
Expand Down
60 changes: 16 additions & 44 deletions lib/lattice.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#
# Copyright (C) 2024 pyMBE-dev team
#
# This file is part of pyMBE.
Expand All @@ -20,17 +19,16 @@
import itertools
import numpy as np

#pmb = pyMBE.pymbe_library(seed=42)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remember to clean-up these comments before merging it!

class LatticeBuilder:
"""
Generic lattice builder.

Args:
lattice(`object`): Data class that represents a lattice, for example a :class:`DiamondLattice`.
strict(`bool`): If set to `True`, the lattice connectivity cannot be altered.
For example, a chain cannot be created between two nodes that are
not explicitly connected according to the definition in `lattice`.

Attributes:
kwargs_node_labels(`dict`): Keyword arguments passed to the matplotlib
`Axes3D.text() <https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.text.html>`_
Expand All @@ -46,6 +44,8 @@ class LatticeBuilder:
function to draw the simulation box.
"""

# Remove pmb when integrated to main pyMBE.py file

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remember to clean-up these comments before merging it!

def __init__(self, lattice, strict=True):
self.lattice = lattice
self.strict = strict
Expand All @@ -58,6 +58,9 @@ def __init__(self, lattice, strict=True):
self.kwargs_monomers = {}
self.kwargs_bonds = {}
self.kwargs_box = {}
self.MPC = lattice.MPC
self.BOXL = lattice.BOXL


def _get_node_by_label(self, node):
assert node in self.node_labels, f"node '{node}' doesn't exist in a {self.lattice.name} lattice"
Expand Down Expand Up @@ -93,7 +96,6 @@ def _make_sphere(cls, radius, resolution):
def add_default_chains(self, mpc):
"""
Build a default lattice network. Skip node pairs that already have a chain defined.

Args:
mpc(`int`): Number of monomers per chain.
"""
Expand All @@ -104,7 +106,6 @@ def add_default_chains(self, mpc):
def draw_lattice(self, ax):
"""
Draw the lattice in an `Axes3D <https://matplotlib.org/stable/api/toolkits/mplot3d/axes3d.html>`_ canvas.

Args:
ax: Axes.
"""
Expand Down Expand Up @@ -150,8 +151,10 @@ def draw_lattice(self, ax):
if self.colormap:
color = self.colormap[node_type]
kwargs_monomers["c"] = color

node_positions = scatter_data[node_type]
pos = np.array(node_positions)
# plotting nodes and monomers
ax_data = ax.scatter(pos[:,0], pos[:,1], pos[:,2], edgecolor="none",
zorder=2, label=node_type, s=12**2, **kwargs_monomers)
color = ax_data.get_facecolors()[0]
Expand All @@ -163,7 +166,6 @@ def draw_lattice(self, ax):
def draw_simulation_box(self, ax):
"""
Draw the simulation box in an `Axes3D <https://matplotlib.org/stable/api/toolkits/mplot3d/axes3d.html>`_ canvas.

Args:
ax: Axes.
"""
Expand All @@ -182,11 +184,9 @@ def draw_simulation_box(self, ax):
def get_chain(self, node_start, node_end):
"""
Get a chain between two nodes.

Args:
node_start(`str`): Start node label, e.g. ``[0 0 0]`` for the node at the origin.
node_end(`str`): End node label, e.g. ``[1 1 1]`` for the first node along the diagonal.

Returns:
`list`: Sequence of residue labels.
"""
Expand All @@ -202,10 +202,8 @@ def get_monomer_color(self, label):
"""
Get the color corresponding to a monomer label.
Only works if a custom color map was set via :meth:`set_colormap`!

Args:
label(`str`): Monomer label.

Returns:
The color.
"""
Expand All @@ -216,10 +214,8 @@ def get_monomer_color_index(self, label):
"""
Get the color wheel index corresponding to a monomer label.
Only works if a custom color map was set via :meth:`set_colormap`!

Args:
label(`str`): Monomer label.

Returns:
`int`: The color index.
"""
Expand All @@ -230,56 +226,24 @@ def get_monomer_color_index(self, label):
def get_node(self, node):
"""
Get a node residue label.

Args:
node(`str`): Node label, e.g. ``[0 0 0]`` for the node at the origin.

Returns:
`str`: Residue label.
"""
key = self._get_node_by_label(node)
return self.nodes[key]

def set_chain(self, node_start, node_end, sequence):
"""
Set a chain between two nodes.

Args:
node_start(`str`): Start node label, e.g. ``[0 0 0]`` for the node at the origin.
node_end(`str`): End node label, e.g. ``[1 1 1]`` for the first node along the diagonal.
sequence(`list`): Sequence of residue labels.
"""
assert len(sequence) != 0 and not isinstance(sequence, str)
key, reverse = self._get_node_vector_pair(node_start, node_end)
assert node_start != node_end or sequence == sequence[::-1], \
(f"chain cannot be defined between '{node_start}' and '{node_end}' since it "
"would form a loop with a non-symmetric sequence (under-defined stereocenter)")
if reverse:
sequence = sequence[::-1]
self.chains[key] = sequence

def set_colormap(self, colormap):
"""
Set a discrete color map. By default, the standard matplotlib color wheel will be used.

Args:
colormap(`dict`): Discrete color wheel that maps monomer labels to colors.
"""
assert isinstance(colormap, dict)
self.colormap = colormap.copy()
self.colormap_sorted_names = list(self.colormap.keys())

def set_node(self, node, residue):
"""
Set a node residue type.

Args:
node(`str`): Node label, e.g. ``[0 0 0]`` for the node at the origin.
residue(`str`): Node residue label.
"""
key = self._get_node_by_label(node)
self.nodes[key] = residue


class DiamondLattice:
"""
Expand All @@ -294,3 +258,11 @@ class DiamondLattice:
(2, 5), (3, 6), (4, 7), (5, 0),
(5, 3), (5, 4), (6, 0), (6, 2),
(6, 4), (7, 0), (7, 2), (7, 3)}

def __init__(self,MPC,BOND_LENGTH):
if not isinstance(MPC, int) or MPC <= 0:
raise ValueError("MPC must be a non-zero positive integer.")
self.MPC = MPC
self.BOND_LENGTH = BOND_LENGTH
self.BOXL = (self.MPC+1)*self.BOND_LENGTH.magnitude / (np.sqrt(3)*0.25)

Loading
Loading