Skip to content

Commit 804bda0

Browse files
committed
updater for custom preprocessing #132
1 parent c712301 commit 804bda0

File tree

10 files changed

+190
-199
lines changed

10 files changed

+190
-199
lines changed

src/coords.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ function coords_from_sdf!(mol::SimpleMolGraph)
9090
zrange = nv(mol) == 0 ? (0, 0) : extrema(mol[i].coords[3] for i in vertices(mol))
9191
# Embed 3D coords to 2D
9292
push!(
93-
mol.gprops.descriptors.coords2d,
93+
mol[:descriptors].coords2d,
9494
[Point2d(mol[i].coords[1:2]...) for i in vertices(mol)])
9595
if zrange[2] - zrange[1] > 0.001 # 3D coords available
9696
push!(
97-
mol.gprops.descriptors.coords3d,
97+
mol[:descriptors].coords3d,
9898
[Point3d(mol[i].coords[1:3]...) for i in vertices(mol)])
9999
end
100100
# Bond style in 2D notation (wedges)
@@ -117,7 +117,7 @@ function coords_from_sdf!(mol::SimpleMolGraph)
117117
arr[i] = :none
118118
end
119119
end
120-
push!(mol.gprops.descriptors.draw2d_bond_style, arr)
120+
push!(mol[:descriptors].draw2d_bond_style, arr)
121121
end
122122

123123

@@ -172,9 +172,9 @@ draw2d_bond_style(mol::SimpleMolGraph) = draw2d_bond_style(mol, 1)
172172

173173
function update_coords!(mol::SimpleMolGraph)
174174
# TODO: just remap nodes if still all existing vertices have coords.
175-
empty!(mol.gprops.descriptors.coords2d)
176-
empty!(mol.gprops.descriptors.coords3d)
177-
empty!(mol.gprops.descriptors.draw2d_bond_style)
175+
empty!(mol[:descriptors].coords2d)
176+
empty!(mol[:descriptors].coords3d)
177+
empty!(mol[:descriptors].draw2d_bond_style)
178178
if hasfield(vproptype(mol), :coords)
179179
if !any(isnothing(mol[i].coords) for i in vertices(mol))
180180
coords_from_sdf!(mol)
@@ -197,22 +197,9 @@ where n is atom count, which stores 2D coordinates (x, y) of each atoms.
197197
"""
198198
coordgen(mol::SimpleMolGraph) = coordgen(
199199
mol.graph, atom_number(mol), bond_order(mol),
200-
mol.gprops.stereocenter, mol.gprops.stereobond
200+
mol[:stereocenter], mol[:stereobond]
201201
)
202202

203-
function coordgen!(mol::ReactiveMolGraph)
204-
# Initialize
205-
empty!(mol.gprops.descriptors.coords2d)
206-
empty!(mol.gprops.descriptors.draw2d_bond_style)
207-
# TODO: unspecified stereochem in SMILES
208-
coords, styles = coordgen(
209-
mol.graph, atom_number(mol), bond_order(mol),
210-
mol[:stereocenter], mol[:stereobond]
211-
)
212-
push!(mol.gprops.descriptors.coords2d, coords)
213-
push!(mol.gprops.descriptors.draw2d_bond_style, styles)
214-
end
215-
216203
function coordgen(
217204
g::SimpleGraph{T}, atomnum::Vector{Int}, bondorder_::Vector{Int},
218205
stereocenters::Dict{T,Tuple{T,T,T,Bool}},
@@ -284,3 +271,16 @@ function coordgen(
284271
# TODO: keep wave bond in SDFile
285272
return coords, styles
286273
end
274+
275+
function coordgen!(mol::SimpleMolGraph)
276+
# Initialize
277+
empty!(mol[:descriptors].coords2d)
278+
empty!(mol[:descriptors].draw2d_bond_style)
279+
# TODO: unspecified stereochem in SMILES
280+
coords, styles = coordgen(
281+
mol.graph, atom_number(mol), bond_order(mol),
282+
mol[:stereocenter], mol[:stereobond]
283+
)
284+
push!(mol[:descriptors].coords2d, coords)
285+
push!(mol[:descriptors].draw2d_bond_style, styles)
286+
end

src/model/molgraph.jl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,22 @@ mutable struct MolState{T,F1,F2} <: AbstractState
3737
initialized::Bool
3838
has_updates::Bool
3939
has_new_edges::Bool # as a SSSR recalculation flag
40+
force_calculate::Bool # ignore cached descriptors (temporary set when running auto-preprocess)
4041
on_init::F1
4142
on_update::F2
4243
end
4344

4445

4546
function MolState{T}(
46-
; initialized=false, has_updates=true, has_new_edges=true,
47+
; initialized=false, has_updates=true, has_new_edges=true, force_calculate=false,
4748
on_init=default_on_init!, on_update=default_on_update!) where T <: Integer
4849
return MolState{T,typeof(on_init),typeof(on_update)}(
49-
initialized, has_updates, has_new_edges, on_init, on_update)
50+
initialized, has_updates, has_new_edges, force_calculate, on_init, on_update)
5051
end
5152

5253

5354
Base.copy(state::T) where T <: MolState = T(
54-
state.initialized, state.has_updates, state.has_new_edges,
55+
state.initialized, state.has_updates, state.has_new_edges, state.force_calculate,
5556
state.on_init, state.on_update
5657
)
5758

@@ -60,7 +61,12 @@ Base.copy(state::T) where T <: MolState = T(
6061

6162
function get_descriptor(mol::ReactiveMolGraph, field::Symbol)
6263
dispatch_update!(mol)
63-
return getproperty(mol.gprops.descriptors, field)
64+
return getproperty(mol[:descriptors], field)
65+
end
66+
67+
function has_descriptor(mol::ReactiveMolGraph, field::Symbol)
68+
mol.state.force_calculate && return false
69+
return hasproperty(mol[:descriptors], field)
6470
end
6571

6672

@@ -103,8 +109,10 @@ to recalculate descriptors. If there are no changes, just return the stored desc
103109
"""
104110
function dispatch_update!(mol::ReactiveMolGraph)
105111
mol.state.has_updates || return
106-
mol.state.has_updates = false # call before update to avoid infinite roop
112+
mol.state.force_calculate = true # to avoid infinite roop
107113
mol.state.on_update(mol)
114+
mol.state.force_calculate = false
115+
mol.state.has_updates = false
108116
mol.state.has_new_edges = false
109117
return
110118
end
@@ -151,7 +159,7 @@ Custom `on_init` function is called inside this method.
151159
function initialize!(mol::ReactiveMolGraph)
152160
mol.state.initialized || mol.state.on_init(mol)
153161
mol.state.initialized = true
154-
dispatch_update!(mol) # just checking on-update callback errors
162+
dispatch_update!(mol) # check on-update callback errors
155163
return
156164
end
157165

@@ -275,7 +283,6 @@ end
275283

276284

277285
function Graphs.rem_vertex!(mol::ReactiveMolGraph, v::Integer)
278-
dispatch_update!(mol)
279286
nv_ = nv(mol)
280287
old_edges = collect(edges(mol))
281288
rem_vertex!(mol.graph, v) || return false
@@ -301,7 +308,6 @@ end
301308

302309
function Graphs.rem_vertices!(mol::ReactiveMolGraph{T,V,E}, vs::Vector{T}) where {T,V,E}
303310
# TODO: if many vertices should be removed, induced_subgraph may be more efficient.
304-
dispatch_update!(mol)
305311
old_edges = collect(edges(mol))
306312
vmap = rem_vertices!(mol.graph, vs)
307313
# remap vertex properties
@@ -330,7 +336,6 @@ end
330336

331337
function _induced_subgraph(mol::T, vlist_or_elist) where {T<:ReactiveMolGraph}
332338
# In many cases, induced_subgraph(mol.graph, vlist_or_elist) is sufficient
333-
dispatch_update!(mol)
334339
subg, vmap = induced_subgraph(mol.graph, vlist_or_elist)
335340
vps = Dict{eltype(T),vproptype(T)}()
336341
for v in vertices(subg)

0 commit comments

Comments
 (0)