Skip to content

Commit 71ff84e

Browse files
committed
valence check
1 parent 24d099c commit 71ff84e

4 files changed

Lines changed: 50 additions & 10 deletions

File tree

assets/test/valence_err.mol

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
MJ250300
3+
4+
10 10 0 0 0 0 0 0 0 0999 V2000
5+
-0.1074 -1.0478 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
6+
0.7175 -1.0478 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
7+
0.9744 -0.2637 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
8+
0.3050 0.2229 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
9+
-0.3600 -0.2637 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
10+
0.3037 1.0479 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
11+
1.7593 -0.0099 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
12+
-1.1447 -0.0091 0.0000 N 0 3 0 0 0 0 0 0 0 0 0 0
13+
-1.3184 0.8028 0.0000 O 0 5 0 0 0 0 0 0 0 0 0 0
14+
-1.7593 -0.5563 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
15+
1 2 1 0 0 0 0
16+
1 5 2 0 0 0 0
17+
2 3 2 0 0 0 0
18+
3 4 1 0 0 0 0
19+
3 7 1 0 0 0 0
20+
4 5 1 0 0 0 0
21+
4 6 1 0 0 0 0
22+
5 8 1 0 0 0 0
23+
8 9 1 0 0 0 0
24+
8 10 2 0 0 0 0
25+
M END

src/property/valence.jl

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ apparent_valence!(mol::SimpleMolGraph) = set_descriptor!(
122122
mol, :apparent_valence, apparent_valence(mol.graph, bond_order(mol)))
123123

124124

125+
function formal_valence(symbol_arr::Vector{Symbol}, charge_arr::Vector{Int})
126+
arr = fill(zero(Int), length(symbol_arr))
127+
for i in 1:length(symbol_arr)
128+
if haskey(LONEPAIR_COUNT, symbol_arr[i])
129+
arr[i] = 4 - abs(LONEPAIR_COUNT[symbol_arr[i]] - charge_arr[i])
130+
end
131+
end
132+
return arr
133+
end
134+
135+
125136
"""
126137
valence(mol::SimpleMolGraph) -> Vector{Int}
127138
@@ -142,22 +153,24 @@ end
142153
function valence(
143154
symbol_arr::Vector{Symbol}, charge_arr::Vector{Int},
144155
apparent_valence_arr::Vector{Int})
145-
arr = fill(zero(Int), length(symbol_arr))
146-
for i in 1:length(symbol_arr)
147-
if haskey(LONEPAIR_COUNT, symbol_arr[i])
148-
val = 4 - abs(LONEPAIR_COUNT[symbol_arr[i]] - charge_arr[i])
149-
arr[i] = max(val, apparent_valence_arr[i])
150-
else
151-
arr[i] = apparent_valence_arr[i]
152-
end
153-
end
154-
return arr
156+
return max.(formal_valence(symbol_arr, charge_arr), apparent_valence_arr)
155157
end
156158

157159
valence!(mol::SimpleMolGraph) = set_descriptor!(
158160
mol, :valence, valence(atom_symbol(mol), atom_charge(mol), apparent_valence(mol)))
159161

160162

163+
function check_valence!(mol::SimpleMolGraph)
164+
av = apparent_valence(mol)
165+
fv = formal_valence(atom_symbol(mol), atom_charge(mol))
166+
comments = String[]
167+
for i in vertices(mol)[av .> fv]
168+
push!(comments, "unusual valence at #$(i) (actual $(av[i]), expected $(fv[i]))")
169+
end
170+
mol[:logs]["warning_valence"] = join(comments, "; ")
171+
end
172+
173+
161174
"""
162175
explicit_hydrogens(mol::SimpleMolGraph) -> Vector{Int}
163176

src/sdfilereader.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ end
9999

100100

101101
function sdf_on_init!(mol::SimpleMolGraph)
102+
check_valence!(mol)
102103
coords_from_sdf!(mol)
103104
stereocenter_from_sdf2d!(mol)
104105
stereobond_from_sdf2d!(mol)

src/smarts/base.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ SMARTSParser{T,V,E}(smarts) where {T,V,E} = SMARTSParser{T,V,E}(
5959

6060

6161
function smiles_on_init!(mol::SimpleMolGraph)
62+
check_valence!(mol)
6263
stereocenter_from_smiles!(mol)
6364
stereobond_from_smiles!(mol)
6465
end

0 commit comments

Comments
 (0)