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

Read Partial Charges from mol2 and SDF #258

Merged
merged 20 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
726ae0b
add args to read charges from atom prop
rwxayheee Nov 30, 2024
63b0fff
edit init_atom to support read charges from atom prop
rwxayheee Nov 30, 2024
bedbdb7
finish draft charges from mol2
rwxayheee Nov 30, 2024
2d9492e
change default read_charge_from_propname to _TriposPartialCharge
rwxayheee Nov 30, 2024
31c0d4b
fixup! change default read_charge_from_propname to _TriposPartialCharge
rwxayheee Nov 30, 2024
f6699e7
fixup! change default read_charge_from_propname to _TriposPartialCharge
rwxayheee Dec 1, 2024
e0f7c53
set default atom property name to PartialCharge; revise error handlin…
rwxayheee Dec 5, 2024
6741616
rename charge_propname to charge_atom_prop
rwxayheee Dec 5, 2024
2bfa6a2
add option charge_from_prop to mk_prepare_ligand.py
rwxayheee Dec 5, 2024
c7f0fb4
use arguments from cli to override config, before
rwxayheee Dec 5, 2024
7f1380f
fixup! use arguments from cli to override config, before preparator =…
rwxayheee Dec 5, 2024
0568941
fix mistakes in handling args charge_from_prop and config;
rwxayheee Dec 5, 2024
772c8d9
Only explicitly provided command line arguments override config
rwxayheee Dec 6, 2024
3c4422c
remove special & redundant handling of explicitly prvided args;
rwxayheee Dec 6, 2024
5995773
clean up unused backend options; redirect error msg to file=sys.stderr
rwxayheee Dec 6, 2024
d0fd018
clearify error msg; make them all begin with Error:
rwxayheee Dec 6, 2024
86b0eb2
clean up lines added in testing
rwxayheee Dec 6, 2024
36c3af4
revert unecessary changes to make diffs prettier
rwxayheee Dec 6, 2024
81151af
clean up changes; move --charge_atom_prop to config_group
rwxayheee Dec 6, 2024
a2a7984
clarify argparse overriding config file
diogomart Dec 6, 2024
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
20 changes: 19 additions & 1 deletion meeko/cli/mk_prepare_ligand.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def cmd_lineparser():
"--name_from_prop",
help="set molecule name from RDKit/SDF property",
)
io_group.add_argument(
"--charge_from_prop",
nargs="*",
Copy link
Contributor

Choose a reason for hiding this comment

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

no need for nargs

help="set atom partial charges from the input file. The default is PartialCharge for SDF and _TriposPartialCharge for MOL2 unless overriden by a user defined property name. ",
)
io_group.add_argument(
"-o",
"--out",
Expand Down Expand Up @@ -214,7 +219,7 @@ def cmd_lineparser():
)
config_group.add_argument(
"--charge_model",
choices=("gasteiger", "espaloma", "zero"),
choices=("gasteiger", "espaloma", "zero", "read"),
help="default is 'gasteiger', 'zero' sets all zeros",
default="gasteiger",
)
Expand Down Expand Up @@ -551,6 +556,19 @@ def main():
nr_failures = 0
is_after_first = False
preparator = MoleculePreparation.from_config(config)

if args.charge_from_prop is not None:
if args.charge_model != "read":
print('--charge_from_prop must be used with --charge_model "read"')
sys.exit(1)

preparator.charge_model = "read"
if args.charge_from_prop:
preparator.charge_atom_prop = args.charge_from_prop[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

no need for [0] after nargs above is removed

else:
if ext=="mol2":
preparator.charge_atom_prop = "_TriposPartialCharge"
Copy link
Contributor

Choose a reason for hiding this comment

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

this is inside the if args.charge_from_prop is not None so i think this never gets executed


for mol in mol_supplier:
if is_after_first and output.is_multimol == False:
print("Processed only the first molecule of multiple molecule input.")
Expand Down
23 changes: 14 additions & 9 deletions meeko/molsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1724,17 +1724,22 @@ def init_atom(self, compute_gasteiger_charges: bool, read_charges_from_prop: str
ok_charges[i] += chrg_by_heavy_atom[newidx]
charges = ok_charges
elif read_charges_from_prop is not None:
if not isinstance(read_charges_from_prop, str):
if not isinstance(read_charges_from_prop, str) or not read_charges_from_prop:
raise ValueError(
f"Invalid value for read_charges_from_prop: expected a string (str), but got {type(read_charges_from_prop).__name__} instead. "
f"Invalid atom property name for read_charges_from_prop: expected a nonempty string (str), but got {type(read_charges_from_prop).__name__} instead. "
)
if not read_charges_from_prop:
read_charges_from_prop = "_TriposPartialCharge"
raise Warning(
"The charge_model of MoleculePreparation is set to be 'read', but a valid charge_propname is not given. " + eol +
"The default property name ('_TriposPartialCharge') will be used. "
)
charges = [float(atom.GetProp(read_charges_from_prop)) for atom in self.mol.GetAtoms()]
charges = [
float(atom.GetProp(read_charges_from_prop))
if atom.HasProp(read_charges_from_prop) else None
for atom in self.mol.GetAtoms()
]
if None in charges:
for idx, charge in enumerate(charges):
if charge is None:
print(f"Charge at index {idx} is None.")
raise ValueError(
f"The list of charges based on atom property name {read_charges_from_prop} contains None. "
)
else:
charges = [0.0] * self.mol.GetNumAtoms()
# register atom
Expand Down
28 changes: 14 additions & 14 deletions meeko/preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(
input_offatom_params=None,
load_offatom_params=None,
charge_model="gasteiger",
charge_propname=None,
charge_atom_prop=None,
dihedral_model=None,
reactive_smarts=None,
reactive_smarts_idx=None,
Expand Down Expand Up @@ -156,24 +156,24 @@ def __init__(
)

self.charge_model = charge_model
self.charge_propname = charge_propname
self.charge_atom_prop = charge_atom_prop

if self.charge_model!="read" and self.charge_propname:
if self.charge_model!="read" and self.charge_atom_prop:
raise ValueError(
"A charge_propname (%s) is given to MoleculePreparation but its current charge_model is %s. " + eol +
"To read charges from atom properties in the input mol, set charge_model to 'read' and name the property in 'charge_propname'. "
% (charge_propname, charge_model)
"A charge_atom_prop (%s) is given to MoleculePreparation but its current charge_model is %s. " + eol +
"To read charges from atom properties in the input mol, set charge_model to 'read' and name the property as 'charge_atom_prop'. "
% (charge_atom_prop, charge_model)
)
if self.charge_model=="read":
if not self.charge_propname:
self.charge_propname = "_TriposPartialCharge"
raise Warning(
"The charge_model of MoleculePreparation is set to be 'read', but a valid charge_propname is not given. " + eol +
"The default property name ('_TriposPartialCharge') will be used. "
if not self.charge_atom_prop:
self.charge_atom_prop = "PartialCharge"
warnings.warn(
"The charge_model of MoleculePreparation is set to be 'read', but a valid charge_atom_prop is not given. " + eol +
"The default atom property ('PartialCharge') will be used. "
)
elif not isinstance(self.charge_propname, str):
elif not isinstance(self.charge_atom_prop, str):
raise ValueError(
f"Invalid value for charge_propname: expected a string (str), but got {type(self.charge_propname).__name__} instead. "
f"Invalid value for charge_atom_prop: expected a string (str), but got {type(self.charge_atom_prop).__name__} instead. "
)

allowed_dihedral_models = [None, "openff", "espaloma"]
Expand Down Expand Up @@ -511,7 +511,7 @@ def prepare(
keep_chorded_rings=self.keep_chorded_rings,
keep_equivalent_rings=self.keep_equivalent_rings,
compute_gasteiger_charges=self.charge_model == "gasteiger",
read_charges_from_prop=self.charge_propname,
read_charges_from_prop=self.charge_atom_prop,
conformer_id=conformer_id,
)

Expand Down
Loading