Skip to content

Commit e122957

Browse files
authored
Check signs of interactions in force-tuning (#34)
Aimed at preventing silly user errors.
1 parent 778ca7c commit e122957

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/forces.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,25 @@ forcecomponents(seq, interactions::InteractionList, residueindexes=eachindex(seq
7373
forcecomponents(collectresidues(seq), interactions, residueindexes; kwargs...)
7474

7575
function forcecomponents(mgmms::AbstractVector{<:IsotropicMultiGMM{N}}, interactions::InteractionList, residueindexes=eachindex(mgmms)) where N
76-
GaussianMixtureAlignment.validate_interactions(Dict(interactions)) || throw(ArgumentError("interactions must appear in only one order, got $interactions"))
76+
interactionsdict = Dict(interactions)
77+
GaussianMixtureAlignment.validate_interactions(interactionsdict) || throw(ArgumentError("interactions must appear in only one order, got $interactions"))
7778
# We don't support (:Ionic, x) where x != :Ionic
7879
for interaction in interactions
7980
key1, key2 = getfeatures(interaction)
8081
if (key1 === :Ionic && key2 !== :Ionic) || (key1 !== :Ionic && key2 === :Ionic)
8182
throw(ArgumentError("(:Ionic, x) is not supported unless x=:Ionic, got $interaction"))
8283
end
8384
end
85+
cionic = get(interactionsdict, (:Ionic, :Ionic), nothing)
86+
if cionic !== nothing
87+
# If (:Ionic, :Ionic) is present, the code below will handle the signs. But we need to ensure consistency with other interactions
88+
for (key, sprod) in ((:Steric, :Steric) => 1, (:Hydrophobe, :Hydrophobe) => -1, (:Donor, :Acceptor) => -1)
89+
c = get(interactionsdict, key, nothing)
90+
if c !== nothing
91+
sign(cionic) * sign(c) != sprod && throw(ArgumentError("(:Ionic, :Ionic) is not sign-compatible with $key"))
92+
end
93+
end
94+
end
8495
forces = [zeros(N, eachindex(interactions)) for _ in residueindexes]
8596
for (k, interaction) in pairs(interactions)
8697
key1, key2 = getfeatures(interaction)

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ using Test
237237
w = optimize_weights(forces)
238238
@test all(>=(0), w)
239239
@test sum(w) 1 atol=1e-6
240+
badinteractions = [(:Steric, :Steric) => 1, (:Hydrophobe, :Hydrophobe) => -1, (:Ionic, :Ionic) => -1]
241+
@test_throws "is not sign-compatible" forcecomponents([mgmmx, mgmmy], badinteractions)
240242
weighted_interactions = [key => w0*wi for ((key, w0), wi) in zip(interactions, w)]
241243
wforces = forcecomponents([mgmmx, mgmmy], weighted_interactions)
242244
@test all(f .* w' wf for (f, wf) in zip(forces, wforces))

0 commit comments

Comments
 (0)