55import copy
66import io
77import re
8- import warnings
98import xml .etree .ElementTree as ET
109from pathlib import Path
1110from xml .etree .ElementTree import tostring
2019from openmm .app .pdbfile import PDBFile
2120from openmm .unit import kelvin , picoseconds
2221from pymatgen .core import Element
23- from pymatgen .io .openff import get_atom_map
22+ from pymatgen .io .openff import coerce_formal_charges , get_atom_map
2423
2524from atomate2 .openff .utils import create_mol_spec , merge_specs_by_name_and_smiles
2625from atomate2 .openmm .jobs .base import openmm_job
@@ -85,12 +84,8 @@ def increment_types(self, increment: str) -> None:
8584 if type_stub in key :
8685 element .attrib [key ] += increment
8786
88- def to_openff_molecule (self ) -> tk .Molecule :
87+ def to_openff_molecule (self , template_molecule : tk . Molecule = None ) -> tk .Molecule :
8988 """Convert the XMLMoleculeFF to an openff_toolkit Molecule."""
90- if sum (self .partial_charges ) > 1e-3 :
91- # TODO: update message
92- warnings .warn ("Formal charges not considered." , stacklevel = 1 )
93-
9489 p_table = {e .symbol : e .number for e in Element }
9590 openff_mol = tk .Molecule ()
9691 for atom in self .tree .getroot ().findall (".//Residues/Residue/Atom" ):
@@ -108,6 +103,9 @@ def to_openff_molecule(self) -> tk.Molecule:
108103
109104 openff_mol .partial_charges = self .partial_charges * unit .elementary_charge
110105
106+ if template_molecule :
107+ openff_mol = coerce_formal_charges (openff_mol , template_molecule )
108+
111109 return openff_mol
112110
113111 @property
@@ -253,7 +251,7 @@ def generate_openmm_interchange(
253251
254252 for mol_spec , xml_mol in zip (mol_specs , xml_mols , strict = True ):
255253 openff_mol = tk .Molecule .from_json (mol_spec .openff_mol )
256- xml_openff_mol = xml_mol .to_openff_molecule ()
254+ xml_openff_mol = xml_mol .to_openff_molecule (template_molecule = openff_mol )
257255 is_isomorphic , _atom_map = get_atom_map (openff_mol , xml_openff_mol )
258256 if not is_isomorphic :
259257 raise ValueError (
0 commit comments