Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement do-able IO functions #266

Merged
merged 2 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/readsbml.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,21 @@ end
"""
$(TYPEDSIGNATURES)

Shortcut for running a function `f` on the contents of the SBML file, suitable
for the `do` block syntax. Returns whatever `f` returns.

# Example
```
readSBML("my_model.xml") do m
@info "model stats" length(m.species) length(m.reactions)
end
```
"""
readSBML(f::Function, args...; kwargs...) = return f(readSBML(args...; kwargs...))

"""
$(TYPEDSIGNATURES)

Read the SBML from the string `str` and return the contained `SBML.Model`.

For the other arguments see the docstring of [`readSBML`](@ref), which can be
Expand Down
17 changes: 17 additions & 0 deletions src/writesbml.jl
Original file line number Diff line number Diff line change
Expand Up @@ -692,3 +692,20 @@ function writeSBML(mdl::Model)::String
end
return str
end

"""
$(TYPEDSIGNATURES)

Shortcut for writing a SBML [`Model`](@ref) created by function `f`, suitable
for the `do` block syntax.

# Example
```
writeSBML("my_model.xml") do
compartments = ...
species = ...
Model(; name = "my model", compartments, species)
end
```
"""
writeSBML(f::Function, args...; kwargs...) = writeSBML(f(), args...; kwargs...)
22 changes: 13 additions & 9 deletions test/writemodels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,20 @@ end
# Make sure that the model we read from the written out file is consistent
# with the original model.
@testset "Round-trip: $(basename(file))" for file in first.(sbmlfiles)
model = readSBML(file)
fix_constant!(model)
remove_some_annotation_strings!(model)
# This is useful for debugging:
# writeSBML(model, file*"-debug.xml")
round_trip_model = readSBMLFromString(@test_logs(writeSBML(model)))
round_trip_model_str = @test_logs writeSBML() do
readSBML(file) do m
fix_constant!(m)
remove_some_annotation_strings!(m)
# This is useful for debugging:
# writeSBML(model, file*"-debug.xml")
return m
end
end

# re-read the unmodified model, fix constantness and compare again
model = readSBML(file)
fix_constant!(model)
@test model == round_trip_model
readSBML(file) do m
fix_constant!(m)
@test m == readSBMLFromString(round_trip_model_str)
end
end
end
Loading