Skip to content

Commit

Permalink
Support writing dict-like properties with string keys + bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOresten committed Nov 18, 2024
1 parent 8972934 commit f7bac85
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ProteinChains"
uuid = "b8e8f2a5-48d3-44f1-ba0d-c71cb7726ff8"
authors = ["Anton Oresten <[email protected]> and contributors"]
version = "0.5.0"
version = "0.5.1"

[deps]
Backboner = "9ac9c2a2-1cfe-46d3-b3fd-6fa470ea56a7"
Expand Down
2 changes: 1 addition & 1 deletion src/chain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Base.getproperty(chain::ProteinChain, name::Symbol) =
Base.propertynames(chain::ProteinChain, private::Bool=false) = (setdiff(fieldnames(ProteinChain), private ? () : (:properties,))..., propertynames(chain.properties)...)

function setproperties!(chain::ProteinChain, ps::NamedTuple)
chain.properties = setproperties(chain.properties, ps)
chain.properties = setproperties(chain.properties, sortnames(ps))
chain
end

Expand Down
1 change: 1 addition & 0 deletions src/properties.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function setproperties! end
function addproperties! end
function removeproperties! end

# probably shouldnt deepcopy
setproperties(x, args...) = setproperties!(deepcopy(x), args...)
addproperties(x, args...; kwargs...) = addproperties!(deepcopy(x), args...; kwargs...)
removeproperties(x, args...) = removeproperties!(deepcopy(x), args...)
Expand Down
9 changes: 9 additions & 0 deletions src/store/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ function deleteproperty(group::HDF5.Group, ::Type, ::Val{:properties}, names::Sy
return group
end

function writeproperty(group::HDF5.Group, T::Type, ::Val{name}, dict::AbstractDict{<:AbstractString}) where name
haskey(group, string(name)) && HDF5.delete_object(group[string(name)])
dict_group = HDF5.create_group(group, string(name))
for (key, value) in pairs(dict)
writeproperty(dict_group, T, Val(Symbol(key)), value)
end
return dict_group
end

## chain properties

function writeproperty(group::HDF5.Group, ::Type{ProteinChain{T}}, ::Val{:atoms}, atoms::Vector{Vector{Atom{T}}}) where T
Expand Down
2 changes: 1 addition & 1 deletion src/structure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Base.getproperty(structure::ProteinStructure, name::Symbol) =
Base.propertynames(structure::ProteinStructure, private::Bool=false) = (setdiff(fieldnames(ProteinStructure), private ? () : (:properties,))..., propertynames(structure.properties)...)

function setproperties!(structure::ProteinStructure, properties::NamedTuple)
structure.properties = setproperties(structure.properties, properties)
structure.properties = setproperties(structure.properties, sortnames(properties))
structure
end

Expand Down
7 changes: 4 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ using Test
filename = joinpath(dir, "store.h5")
structures = [pdb"1EYE", pdb"3HFM"]
for (i, chain) in enumerate(structures[1])
structures[1][i] = addproperties(chain, rand3=rand(3))
addproperties!(chain, rand3=rand(3))
end
for (i, chain) in enumerate(structures[2])
structures[2][i] = addproperties(chain, rand3=rand(3), taxid=-1)
addproperties!(chain, rand3=rand(3), taxid=-1, dict=Dict("a"=>1, "b"=>2))
end
ProteinChains.serialize(filename, structures)
structures_copy = ProteinChains.deserialize(filename)
Expand All @@ -103,7 +103,8 @@ using Test
store = ProteinStructureStore(filename)
@test haskey(store, "1EYE.cif")
@test issubset((:rand3,), propertynames(store["1EYE.cif"][1]))
@test all(chain -> issubset((:rand3, :taxid), propertynames(chain)), store["3HFM.cif"])
@test all(chain -> issubset((:rand3, :taxid, :dict), propertynames(chain)), store["3HFM.cif"])
@test store["3HFM.cif"][1].dict == Dict("a"=>1, "b"=>2)
delete!(store, "1EYE.cif")
@test !haskey(store, "1EYE.cif")

Expand Down

2 comments on commit f7bac85

@AntonOresten
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/119703

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.1 -m "<description of version>" f7bac85876c8517ccff42f5890df83f66b4f1c98
git push origin v0.5.1

Please sign in to comment.