Skip to content

Commit

Permalink
Merge pull request #555 from Lp0lp/Go
Browse files Browse the repository at this point in the history
Cleanup to Go implementation
  • Loading branch information
fgrunewald authored Oct 19, 2023
2 parents 293b73e + 0056f7f commit 6ee5107
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 147 deletions.
2 changes: 1 addition & 1 deletion bin/martinize2
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ def entry():
# if the go model hasn't been used we need to create virtual
# sites for the biasing
if not args.go:
vermouth.rcsu.go_vs_includes.VirtualSideCreator().run_system(system)
vermouth.rcsu.go_vs_includes.VirtualSiteCreator().run_system(system)
itp_paths = ["virtual_sites_atomtypes.itp", "virtual_sites_nonbond_params.itp"]
# now we add a bias by defining specific virtual-site water interactions
vermouth.processors.ComputeWaterBias(args.water_bias,
Expand Down
6 changes: 2 additions & 4 deletions vermouth/rcsu/go_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import inspect
import vermouth
from ..processors.processor import Processor
from .go_vs_includes import VirtualSideCreator
from .go_vs_includes import VirtualSiteCreator
from .go_structure_bias import ComputeStructuralGoBias
from ..processors import SetMoleculeMeta

Expand All @@ -38,8 +38,6 @@ def prepare_run(self, system, moltype):
# with the proper Go-model for multimers
vermouth.MergeAllMolecules().run_system(system)
molecule = system.molecules[0]
# res_graph = vermouth.graph_utils.make_residue_graph(molecule)
# molecule.res_graph = res_graph
molecule.meta['moltype'] = moltype

def run_system(self, system, **kwargs):
Expand All @@ -52,5 +50,5 @@ def run_system(self, system, **kwargs):
return system

GoPipeline = GoProcessorPipline([SetMoleculeMeta,
VirtualSideCreator,
VirtualSiteCreator,
ComputeStructuralGoBias])
2 changes: 1 addition & 1 deletion vermouth/rcsu/go_structure_bias.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def __init__(self,
self.res_graph = None
self.system = None
self.__chain_id_to_resnode = {}
self.magic_number = 1.12246204830
self.magic_number = 2**(1/6)

# do not overwrite when subclassing
def _chain_id_to_resnode(self, chain, resid):
Expand Down
2 changes: 1 addition & 1 deletion vermouth/rcsu/go_vs_includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
LOGGER = StyleAdapter(get_logger(__name__))


class VirtualSideCreator(Processor):
class VirtualSiteCreator(Processor):
"""
Create virtual-sites for the Martini Go model implementation.
This processor also updates the gmx
Expand Down
6 changes: 3 additions & 3 deletions vermouth/tests/gmx/test_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_atomtypes(tmp_path, dummy_molecule, atomtypes, expected, C6C12):
DeferredFileWriter().write()

with open(str(outpath)) as infile:
for line, ref_line in zip(infile.readlines(), expected):
for line, ref_line in zip(infile, expected):
assert line == ref_line


Expand Down Expand Up @@ -147,7 +147,7 @@ def test_nonbond_params(tmp_path, nbparams, expected, C6C12):
DeferredFileWriter().write()

with open(str(outpath)) as infile:
for line, ref_line in zip(infile.readlines(), expected):
for line, ref_line in zip(infile, expected):
assert line == ref_line

def test_toplevel_topology(tmp_path, dummy_molecule):
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_toplevel_topology(tmp_path, dummy_molecule):
"""
ref_lines = textwrap.dedent(reference).splitlines()
with open(str(outpath)) as infile:
for line, ref_line in zip(infile.readlines(), ref_lines):
for line, ref_line in zip(infile, ref_lines):
print('l', line.strip())
print('lr', ref_line.strip())
assert line.strip() == ref_line
128 changes: 126 additions & 2 deletions vermouth/tests/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
"""
import operator
import os
import pytest
import numpy as np
import networkx as nx
import networkx.algorithms.isomorphism as iso

import vermouth
from vermouth.system import System
from vermouth.forcefield import ForceField

def make_into_set(iter_of_dict):
"""
Expand Down Expand Up @@ -78,4 +83,123 @@ def find_in_path(names=('martinize2', 'martinize2.py')):
for name in names:
fullpath = os.path.join(folder, name)
if os.path.isfile(fullpath):
return fullpath
return fullpath

def create_sys_all_attrs(molecule, moltype, secstruc, defaults, attrs):
"""
Generate a test system from a molecule
with all attributes set and blocks in
force-field.
Parameters
----------
molecule: :class:vermouth.Molecule
moltype: str
sets meta['moltype']
secstruc: dict[int, str]
secondary structure attributes
as resid str pairs
defaults: dict
dict of attribute default value
attrs: dict
dict of attribute name and dict
of node value pairs
Returns
-------
:class:vermouth.System
"""
# set mol meta
molecule.meta['moltype'] = moltype
# assign default node attributes
for attr, value in defaults.items():
nx.set_node_attributes(molecule, value, attr)

# assign node attributes
for attr, values in attrs.items():
nx.set_node_attributes(molecule, values, attr)

# assign resids
resids = nx.get_node_attributes(molecule, "resid")
nx.set_node_attributes(molecule, resids, "_old_resid")

# make the proper force-field
ff = ForceField("test")
ff.variables['water_type'] = "W"
ff.variables['regular'] = 0.47
ff.variables['small'] = 0.41
ff.variables['tiny'] = 0.38

res_graph = vermouth.graph_utils.make_residue_graph(molecule)
for node in res_graph.nodes:
mol_nodes = res_graph.nodes[node]['graph'].nodes
block = vermouth.molecule.Block()
resname = res_graph.nodes[node]['resname']
resid = res_graph.nodes[node]['resid']
# assign secondary structure
for mol_node in mol_nodes:
molecule.nodes[mol_node]['cgsecstruct'] = secstruc[resid]
block.add_node(molecule.nodes[mol_node]['atomname'],
atype=molecule.nodes[mol_node]['atype'])

ff.blocks[resname] = block


# create the system
molecule._force_field = ff
system = System()
system.molecules.append(molecule)
return system

@pytest.fixture
def test_molecule(scope='function'):
"""
Molecule with the following connectivity and atom-naming:
SC2: 2 8
| |
SC1: 1 4 7
| | |
BB: 0 - 3 - 5 - 6
-------------
resid: 1 2 3 4 column wise
"""

force_field = vermouth.forcefield.ForceField("test")
molecule = vermouth.molecule.Molecule(force_field=force_field)
molecule.meta['test'] = True
# The node keys should not be in a sorted order as it would mask any issue
# due to the keys being accidentally sorted.
molecule.add_node(2, atomname='SC2',
position=np.array([0., 1.0, 0.0]), resid=1)
molecule.add_node(0, atomname='BB',
position=np.array([0., 0., 0.]), resid=1)
molecule.add_node(1, atomname='SC1',
position=np.array([0., 0.5, 0.0]), resid=1)

molecule.add_node(3, atomname='BB', position=np.array(
[0.5, 0.0, 0.0]), resid=2)
molecule.add_node(4, atomname='SC1', position=np.array(
[0.5, 0.5, 0.0]), resid=2)

molecule.add_node(5, atomname='BB', position=np.array(
[1.0, 0.0, 0.0]), resid=3)

molecule.add_node(6, atomname='BB', position=np.array(
[1.5, 0.0, 0.0]), resid=4)
molecule.add_node(7, atomname='SC1', position=np.array(
[1.5, 0.5, 0.0]), resid=4)
molecule.add_node(8, atomname='SC2', position=np.array(
[1.5, 1.0, 0.0]), resid=4)

molecule.add_edge(0, 1)
molecule.add_edge(0, 2)
molecule.add_edge(0, 3)
molecule.add_edge(3, 4)
molecule.add_edge(3, 5)
molecule.add_edge(5, 6)
molecule.add_edge(6, 7)
molecule.add_edge(7, 8)

return molecule

4 changes: 2 additions & 2 deletions vermouth/tests/rcsu/test_go_structure_bias.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import networkx as nx
import vermouth
from vermouth.rcsu.go_vs_includes import VirtualSideCreator
from vermouth.rcsu.go_vs_includes import VirtualSiteCreator
from vermouth.tests.test_water_bias import create_sys_all_attrs
from vermouth.tests.test_apply_rubber_band import test_molecule
from vermouth.rcsu.go_structure_bias import ComputeStructuralGoBias
Expand Down Expand Up @@ -124,7 +124,7 @@ def test_contact_selector(test_molecule,
"atype": atypes})

# generate the virtual sites
VirtualSideCreator().run_system(system)
VirtualSiteCreator().run_system(system)
# initialize the Go processor
go_processor = ComputeStructuralGoBias(contact_map=cmap,
cutoff_short=cshort,
Expand Down
2 changes: 1 addition & 1 deletion vermouth/tests/rcsu/test_go_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_get_bead_size(atype, size):
# two regions false
([(101, 120), (180, 210)], 15, False),
))
def test_get_bead_size(regions, resid, result):
def test_in_region(regions, resid, result):
assert result == _in_resid_region(resid, regions)


Expand Down
53 changes: 1 addition & 52 deletions vermouth/tests/test_apply_rubber_band.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
make_same_region_criterion,
are_connected,
build_connectivity_matrix)
from vermouth.tests.helper_functions import test_molecule

# pylint: disable=redefined-outer-name

Expand Down Expand Up @@ -260,58 +261,6 @@ def test_make_same_region_criterion(regions, left, right, nodes, edges, chain, r
same_region = make_same_region_criterion(regions)
assert same_region(graph=graph, left=left, right=right) == outcome

@pytest.fixture
def test_molecule(scope='function'):
"""
Molecule with the following connectivity and atom-naming:
SC2: 2 8
| |
SC1: 1 4 7
| | |
BB: 0 - 3 - 5 - 6
-------------
resid: 1 2 3 4 column wise
"""

force_field = vermouth.forcefield.ForceField("test")
molecule = vermouth.molecule.Molecule(force_field=force_field)
molecule.meta['test'] = True
# The node keys should not be in a sorted order as it would mask any issue
# due to the keys being accidentally sorted.
molecule.add_node(2, atomname='SC2',
position=np.array([0., 1.0, 0.0]), resid=1)
molecule.add_node(0, atomname='BB',
position=np.array([0., 0., 0.]), resid=1)
molecule.add_node(1, atomname='SC1',
position=np.array([0., 0.5, 0.0]), resid=1)

molecule.add_node(3, atomname='BB', position=np.array(
[0.5, 0.0, 0.0]), resid=2)
molecule.add_node(4, atomname='SC1', position=np.array(
[0.5, 0.5, 0.0]), resid=2)

molecule.add_node(5, atomname='BB', position=np.array(
[1.0, 0.0, 0.0]), resid=3)

molecule.add_node(6, atomname='BB', position=np.array(
[1.5, 0.0, 0.0]), resid=4)
molecule.add_node(7, atomname='SC1', position=np.array(
[1.5, 0.5, 0.0]), resid=4)
molecule.add_node(8, atomname='SC2', position=np.array(
[1.5, 1.0, 0.0]), resid=4)

molecule.add_edge(0, 1)
molecule.add_edge(0, 2)
molecule.add_edge(0, 3)
molecule.add_edge(3, 4)
molecule.add_edge(3, 5)
molecule.add_edge(5, 6)
molecule.add_edge(6, 7)
molecule.add_edge(7, 8)

return molecule


@pytest.mark.parametrize('chain_attribute, atom_names, res_min_dist, outcome',
(({0: 'A', 1: 'A', 2: 'A',
Expand Down
Loading

0 comments on commit 6ee5107

Please sign in to comment.