Skip to content

Commit

Permalink
Change all molecule changing logic in bin/martinize2 to Processors
Browse files Browse the repository at this point in the history
  • Loading branch information
pckroon committed May 8, 2024
1 parent ff636ed commit 69437d2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
17 changes: 5 additions & 12 deletions bin/martinize2
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ import argparse
import functools
import logging
import itertools
import textwrap
from pathlib import Path
import sys
import networkx as nx
import vermouth
import vermouth.forcefield
from vermouth.file_writer import deferred_open, DeferredFileWriter
from vermouth.file_writer import DeferredFileWriter
from vermouth import DATA_PATH
from vermouth.processors.processor import ProcessorPipeline
from vermouth.dssp import dssp
Expand All @@ -52,12 +51,8 @@ from vermouth.map_input import (
combine_mappings,
)
from vermouth.rcsu.contact_map import read_go_map
from vermouth.citation_parser import citation_formatter
from vermouth.gmx.topology import write_gmx_topology

# TODO Since vermouth's __init__.py does some logging (KDTree), this may or may
# not work as intended. Investigation required.

LOGGER = TypeAdapter(logging.getLogger("vermouth"))

PRETTY_FORMATTER = logging.Formatter(
Expand Down Expand Up @@ -1045,20 +1040,18 @@ 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.VirtualSiteCreator().run_system(system)
postprocessing.add(VirtualSiteCreator())
itp_paths = {"atomtypes": "virtual_sites_atomtypes.itp",
"nonbond_params": "virtual_sites_nonbond_params.itp"}
# now we add a bias by defining specific virtual-site water interactions
vermouth.processors.ComputeWaterBias(args.water_bias,
postprocessing.add(vermouth.ComputeWaterBias(args.water_bias,
{ s:float(eps) for s, eps in args.water_bias_eps},
[(int(start), int(stop)) for start, stop in args.water_idrs],
).run_system(system)
))

# Here we need to add the resids from the PDB back if that is needed
if args.resid_handling == "input":
for molecule in system.molecules:
old_resids = nx.get_node_attributes(molecule, "_old_resid")
nx.set_node_attributes(molecule, old_resids, "resid")
postprocessing.add(vermouth.CopyNodeAttrs("_old_resid", "resid"))

# The Martini Go model assumes that we do not mess with the order of
# particles in any way especially the virtual sites needed for the Go
Expand Down
1 change: 1 addition & 0 deletions vermouth/processors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@
from .merge_all_molecules import MergeAllMolecules
from .annotate_mut_mod import AnnotateMutMod
from .water_bias import ComputeWaterBias
from .copy_node_attrs import CopyNodeAttrs
29 changes: 29 additions & 0 deletions vermouth/processors/copy_node_attrs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2024 University of Groningen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import networkx as nx
from .processor import Processor


class CopyNodeAttrs(Processor):
def __init__(self, from_attr, to_attr):
self.from_attr = from_attr
self.to_attr = to_attr

Check warning on line 24 in vermouth/processors/copy_node_attrs.py

View check run for this annotation

Codecov / codecov/patch

vermouth/processors/copy_node_attrs.py#L23-L24

Added lines #L23 - L24 were not covered by tests

def run_molecule(self, molecule):
values = nx.get_node_attributes(molecule, self.from_attr)
nx.set_node_attributes(molecule, values, self.to_attr)
return molecule

Check warning on line 29 in vermouth/processors/copy_node_attrs.py

View check run for this annotation

Codecov / codecov/patch

vermouth/processors/copy_node_attrs.py#L27-L29

Added lines #L27 - L29 were not covered by tests

0 comments on commit 69437d2

Please sign in to comment.