Skip to content

Commit

Permalink
Add Deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOresten committed Oct 7, 2024
1 parent bc36e46 commit 40fb3ec
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/ProteinChains.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module ProteinChains

using StaticArrays: SVector

using Backboner

include("ideal.jl")
Expand Down Expand Up @@ -38,4 +36,6 @@ export writecif, writepdb
export PDBFormat, MMCIFFormat
export pdbentry, @pdb_str

include("serialization/serialization.jl")

end
5 changes: 3 additions & 2 deletions src/atom.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PeriodicTable: elements
using StaticArrays: SVector

const ELEMENT_SYMBOL_TO_NUMBER = Dict(uppercase(elements[number].symbol) => number for number in 1:118)
const number_TO_ELEMENT_SYMBOL = Dict(n => s for (s, n) in ELEMENT_SYMBOL_TO_NUMBER)
Expand All @@ -22,10 +23,10 @@ end
Atom{T}(atom::Atom) where T = Atom{T}(atom.name, atom.number, atom.x, atom.y, atom.z)

@inline Atom(name::UInt32, number::Integer, x::T, y::T, z::T) where T = Atom{T}(name, number, x, y, z)
@inline Atom(name::AbstractString, element_symbol::AbstractString, x::T, y::T, z::T) where T =
@inline Atom(name::AbstractString, element_symbol::AbstractString, x, y, z) =
Atom(encode_atom_name(name, element_symbol), element_symbol_to_number(element_symbol), x, y, z)

@inline Atom(name, number, coords::AbstractVector{T}) where T = Atom(name, number, coords...)
@inline Atom(name, number, coords::AbstractVector) = Atom(name, number, coords...)

coords(atom::Atom) = SVector(atom.x, atom.y, atom.z)

Expand Down
2 changes: 0 additions & 2 deletions src/io/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ include("renumber.jl")
include("read.jl")
include("write.jl")
include("download.jl")

include("serialization.jl")
27 changes: 27 additions & 0 deletions src/serialization/lazy.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
mutable struct Deserializer <: AbstractDict{String,ProteinStructure}
filename::String
file::HDF5.File
keys::Set{String}

function Deserializer(filename::AbstractString)
file = HDF5.h5open(filename, "r")
deserializer = new(filename, file, Set(keys(file)))
finalizer(close, deserializer)
return deserializer
end
end

Base.close(deserializer::Deserializer) = close(deserializer.file)

Base.keys(deserializer::Deserializer) = deserializer.keys
Base.length(deserializer::Deserializer) = length(keys(deserializer))
Base.getindex(deserializer::Deserializer, key::AbstractString) = read(deserializer.file[key], ProteinStructure)

function Base.iterate(deserializer::Deserializer, state...)
handler(::Nothing) = nothing
handler((key,state)::Tuple{String,Int}) = (key => deserializer[key], state)
return handler(iterate(deserializer.keys, state...))
end

Base.show(io::IO, deserializer::Deserializer) = print(io, "$(Deserializer)(\"$(deserializer.filename)\")")
Base.show(io::IO, ::MIME"text/plain", deserializer::Deserializer) = print(io, summary(deserializer))
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ end

function serialize(path::AbstractString, dataset::ProteinDataset; mode="w")
HDF5.h5open(path, mode) do f
foreach(((key, structure),) -> write(f, structure, key), dataset)
for (key, structure) in dataset
write(f, structure, key)
end
end
return path
end
Expand All @@ -96,3 +98,5 @@ function deserialize(path::AbstractString)
end
return ProteinDataset(structures)
end

include("lazy.jl")

0 comments on commit 40fb3ec

Please sign in to comment.