Skip to content

Commit

Permalink
avoid reserved variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
jgreener64 committed Aug 21, 2024
1 parent f1c2cca commit 6c549f9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ext/BioStructuresDSSPExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function BioStructures.rundssp!(struc::MolecularStructure)
return struc
end

BioStructures.rundssp(el::Union{MolecularStructure, Model}) = rundssp!(deepcopy(el))
BioStructures.rundssp(el::Union{MolecularStructure, Model}) = rundssp!(copy(el))

function BioStructures.rundssp(filepath_in, dssp_filepath_out)
run(`$dssp_executable $filepath_in $dssp_filepath_out`)
Expand Down
2 changes: 1 addition & 1 deletion ext/BioStructuresSTRIDEExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function BioStructures.runstride!(struc::MolecularStructure)
return struc
end

BioStructures.runstride(el::Union{MolecularStructure, Model}) = runstride!(deepcopy(el))
BioStructures.runstride(el::Union{MolecularStructure, Model}) = runstride!(copy(el))

function BioStructures.runstride(filepath_in, stride_filepath_out)
run(`$stride_executable $filepath_in -f$stride_filepath_out`)
Expand Down
54 changes: 35 additions & 19 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,21 @@ struct Atom <: AbstractAtom
charge::String
residue::StructuralElement
end
Atom(a::Atom, r::StructuralElement) = Atom(a.serial, a.name, a.alt_loc_id, copy(a.coords), a.occupancy, a.temp_factor, a.element, a.charge, r)

function Atom(a::Atom, r::StructuralElement)
return Atom(a.serial, a.name, a.alt_loc_id, copy(a.coords), a.occupancy,
a.temp_factor, a.element, a.charge, r)
end

"A container to hold different locations of the same atom."
struct DisorderedAtom <: AbstractAtom
alt_loc_ids::Dict{Char, Atom}
default::Char
end
DisorderedAtom(da::DisorderedAtom, r::StructuralElement) = DisorderedAtom(Dict(k => Atom(a, r) for (k, a) in da.alt_loc_ids), da.default)

function DisorderedAtom(da::DisorderedAtom, r::StructuralElement)
return DisorderedAtom(Dict(k => Atom(a, r) for (k, a) in da.alt_loc_ids), da.default)
end

"""
A residue (amino acid) or other molecule - either a `Residue` or a
Expand All @@ -139,11 +146,13 @@ mutable struct Residue <: AbstractResidue
chain::StructuralElement
ss_code::Char
end
function Residue(r::Residue, chain::StructuralElement)
atoms = Dict{String, AbstractAtom}()
rnew = Residue(r.name, r.number, r.ins_code, r.het_res, [name for name in r.atom_list], atoms, chain, r.ss_code)

function Residue(r::Residue, ch::StructuralElement)
atom_dict = Dict{String, AbstractAtom}()
rnew = Residue(r.name, r.number, r.ins_code, r.het_res, [name for name in r.atom_list],
atom_dict, ch, r.ss_code)
for (name, atom) in r.atoms
atoms[name] = isa(atom, Atom) ? Atom(atom, rnew) : DisorderedAtom(atom, rnew)
atom_dict[name] = isa(atom, Atom) ? Atom(atom, rnew) : DisorderedAtom(atom, rnew)
end
return rnew
end
Expand All @@ -156,7 +165,10 @@ struct DisorderedResidue <: AbstractResidue
names::Dict{String, Residue}
default::String
end
DisorderedResidue(dr::DisorderedResidue, chain::StructuralElement) = DisorderedResidue(Dict(k => Residue(r, chain) for (k, r) in dr.names), dr.default)

function DisorderedResidue(dr::DisorderedResidue, ch::StructuralElement)
return DisorderedResidue(Dict(k => Residue(r, ch) for (k, r) in dr.names), dr.default)
end

"A chain (molecule) from a macromolecular structure."
mutable struct Chain <: StructuralElement
Expand All @@ -165,11 +177,12 @@ mutable struct Chain <: StructuralElement
residues::Dict{String, AbstractResidue}
model::StructuralElement
end
function Chain(c::Chain, model::StructuralElement)
residues = Dict{String, AbstractResidue}()
cnew = Chain(c.id, [id for id in c.res_list], residues, model)

function Chain(c::Chain, mo::StructuralElement)
res_dict = Dict{String, AbstractResidue}()
cnew = Chain(c.id, [id for id in c.res_list], res_dict, mo)
for (id, res) in c.residues
residues[id] = isa(res, Residue) ? Residue(res, cnew) : DisorderedResidue(res, cnew)
res_dict[id] = isa(res, Residue) ? Residue(res, cnew) : DisorderedResidue(res, cnew)
end
return cnew
end
Expand All @@ -180,11 +193,12 @@ struct Model <: StructuralElement
chains::Dict{String, Chain}
structure::StructuralElement
end
function Model(m::Model, structure::StructuralElement)
chains = Dict{String, Chain}()
mnew = Model(m.number, chains, structure)

function Model(m::Model, struc::StructuralElement)
chain_dict = Dict{String, Chain}()
mnew = Model(m.number, chain_dict, struc)
for (id, ch) in m.chains
chains[id] = Chain(ch, mnew)
chain_dict[id] = Chain(ch, mnew)
end
return mnew
end
Expand All @@ -197,11 +211,12 @@ struct MolecularStructure <: StructuralElement
name::String
models::Dict{Int, Model}
end

function MolecularStructure(s::MolecularStructure)
models = Dict{Int, Model}()
snew = MolecularStructure(s.name, models)
model_dict = Dict{Int, Model}()
snew = MolecularStructure(s.name, model_dict)
for (number, mo) in s.models
models[number] = Model(mo, snew)
model_dict[number] = Model(mo, snew)
end
return snew
end
Expand Down Expand Up @@ -373,7 +388,8 @@ end
Base.firstindex(struc::MolecularStructure) = first(modelnumbers(struc))
Base.lastindex(struc::MolecularStructure) = last(modelnumbers(struc))

# recursive copy methods. If we copy a subelement (anything below MolecularStructure), it shares the parent element
# Recursive copy methods
# If we copy a sub-element (anything below MolecularStructure), it shares the parent element
Base.copy(a::Atom) = Atom(a, a.residue)
Base.copy(da::DisorderedAtom) = DisorderedAtom(da, only(unique(a -> a.residue, values(da.alt_loc_ids))).residue)
Base.copy(r::Residue) = Residue(r, r.chain)
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ getchildren(r::Residue) = values(r.atoms)
getchildren(c::Chain) = values(c.residues)
getchildren(m::Model) = values(m.chains)
getchildren(s::MolecularStructure) = values(s.models)

function testparent(children, parent)
for child in children
if isa(child, DisorderedAtom)
Expand Down Expand Up @@ -249,7 +250,7 @@ end
@test struc_copy['A'][10]["CA"].coords[2] == 100
@test a.coords[2] == 2
@test struc['A'][10]["CA"].coords[2] == 2
# intermediate copies preserve parenting up to the node of the copy
# Intermediate copies preserve parenting up to the node of the copy
testparent(getchildren(mo), mo)
mo_copy = copy(mo)
testparent(getchildren(mo_copy), mo_copy)
Expand Down

0 comments on commit 6c549f9

Please sign in to comment.