Skip to content

Commit

Permalink
read provided ff file and use these blocks instead of making new ones
Browse files Browse the repository at this point in the history
  • Loading branch information
fgrunewald committed Mar 4, 2024
1 parent 8e0c257 commit 3e4a737
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 2 additions & 0 deletions bin/polyply
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ def main(): # pylint: disable=too-many-locals,too-many-statements
parser_itp_ff.add_argument('-s', dest="smile_str", required=True)
parser_itp_ff.add_argument('-o', dest="outpath", type=Path)
parser_itp_ff.add_argument('-c', dest="res_charges", nargs='+', type=lambda s: s.split(':'),)
parser_itp_ff.add_argument('-f', dest='inpath', type=Path, required=False, default=[],
help='Input file (ITP|FF)', nargs='*')

parser_itp_ff.set_defaults(func=itp_to_ff)

Expand Down
4 changes: 2 additions & 2 deletions polyply/src/big_smile_mol_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class DefBigSmileParser:
which describes a polymer molecule.
"""

def __init__(self):
self.force_field = None
def __init__(self, force_field):
self.force_field = force_field
self.meta_molecule = None
self.molecule = None

Expand Down
10 changes: 6 additions & 4 deletions polyply/src/big_smile_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def fragment_iter(fragment_str):
nx.set_node_attributes(mol_graph, resname, 'resname')
yield resname, mol_graph

def force_field_from_fragments(fragment_str):
def force_field_from_fragments(fragment_str, force_field=None):
"""
Collects the fragments defined in a BigSmile string
as :class:`vermouth.molecule.Blocks` in a force-field
Expand All @@ -266,9 +266,11 @@ def force_field_from_fragments(fragment_str):
-------
:class:`vermouth.forcefield.ForceField`
"""
force_field = ForceField("big_smile_ff")
if force_field is None:
force_field = ForceField("big_smile_ff")
frag_iter = fragment_iter(fragment_str)
for resname, mol_graph in frag_iter:
mol_block = Block(mol_graph)
force_field.blocks[resname] = mol_block
if resname not in force_field.blocks:
mol_block = Block(mol_graph)
force_field.blocks[resname] = mol_block
return force_field
18 changes: 14 additions & 4 deletions polyply/src/itp_to_ff.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from polyply.src.ffoutput import ForceFieldDirectiveWriter
from polyply.src.charges import balance_charges, set_charges
from polyply.src.big_smile_mol_processor import DefBigSmileParser
from .load_library import load_ff_library

def _read_itp_file(itppath):
"""
Expand All @@ -34,10 +35,17 @@ def _read_itp_file(itppath):
mol.make_edges_from_interaction_type(type_="bonds")
return mol

def itp_to_ff(itppath, smile_str, outpath, res_charges=None):
def itp_to_ff(itppath, smile_str, outpath, inpath=[], res_charges=None):
"""
Main executable for itp to ff tool.
"""
# load FF files if given
if inpath:
force_field = load_ff_library("new", None, inpath)
# if none are given we create an empty ff
else:
force_field = ForceField("new")

# what charges belong to which resname
if res_charges:
crg_dict = dict(res_charges)
Expand All @@ -52,14 +60,16 @@ def itp_to_ff(itppath, smile_str, outpath, res_charges=None):
target_mol = _read_itp_file(itppath)

# read the big-smile representation
meta_mol = DefBigSmileParser().parse(smile_str)
meta_mol = DefBigSmileParser(force_field).parse(smile_str)

# identify and extract all unique fragments
unique_fragments, res_graph = FragmentFinder(target_mol).extract_unique_fragments(meta_mol.molecule)

# extract the blocks with parameters
force_field = ForceField("new")
for name, fragment in unique_fragments.items():
# don't overwrite existing blocks
if name in force_field.blocks:
continue
new_block = extract_block(target_mol, list(fragment.nodes), defines={})
nx.set_node_attributes(new_block, 1, "resid")
new_block.nrexcl = target_mol.nrexcl
Expand All @@ -70,7 +80,7 @@ def itp_to_ff(itppath, smile_str, outpath, res_charges=None):
charge=float(crg_dict[name]))

# extract the regular links
force_field.links = extract_links(target_mol)
force_field.links.append(extract_links(target_mol))
# extract links that span the terminii
find_termini_mods(res_graph, target_mol, force_field)

Expand Down

0 comments on commit 3e4a737

Please sign in to comment.