converters.jl 2.49 KB
Newer Older
1
2

"""
3
    set_level_and_version(level, version)
4
5
6
7

A converter to pass into [`readSBML`](@ref) that enforces certain SBML level
and version.
"""
8
set_level_and_version(level, version) =
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
9
10
11
12
13
14
15
16
17
18
19
20
    doc -> check_errors(
        ccall(
            sbml(:SBMLDocument_setLevelAndVersion),
            Cint,
            (VPtr, Cint, Cint),
            doc,
            level,
            version,
        ),
        doc,
        ErrorException("Setting of level and version did not succeed"),
    )
21
22
23
24
25
26
27
28

"""
    libsbml_convert(conversion_options::Vector{Pair{String, Dict{String, String}}})

A converter that runs the SBML conversion routine, with specified conversion
options. The argument is a vector of pairs to allow specifying the order of
conversions.
"""
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
29
30
31
libsbml_convert(
    conversion_options::AbstractVector{<:Pair{String,<:AbstractDict{String,String}}},
) =
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    doc -> begin
        for (converter, options) in conversion_options
            props = ccall(sbml(:ConversionProperties_create), VPtr, ())
            opt = ccall(sbml(:ConversionOption_create), VPtr, (Cstring,), converter)
            ccall(sbml(:ConversionProperties_addOption), Cvoid, (VPtr, VPtr), props, opt)
            for (k, v) in options
                opt = ccall(sbml(:ConversionOption_create), VPtr, (Cstring,), k)
                ccall(sbml(:ConversionOption_setValue), Cvoid, (VPtr, Cstring), opt, v)
                ccall(
                    sbml(:ConversionProperties_addOption),
                    Cvoid,
                    (VPtr, VPtr),
                    props,
                    opt,
                )
            end
Miroslav Kratochvil's avatar
Miroslav Kratochvil committed
48
49
50
51
52
            check_errors(
                ccall(sbml(:SBMLDocument_convert), Cint, (VPtr, VPtr), doc, props),
                doc,
                ErrorException("Conversion returned errors"),
            )
53
54
55
        end
    end

56
57
58
59
60
61
62
63
64
65
"""
    libsbml_convert(converter::String; kwargs...)

Quickly construct a single run of a `libsbml` converter from keyword arguments.

# Example
```
readSBML("example.xml", libsbml_convert("stripPackage", package="layout"))
```
"""
66
67
68
libsbml_convert(converter::String; kwargs...) = libsbml_convert([
    converter => Dict{String,String}(string(k) => string(v) for (k, v) in kwargs),
])
69

70
71
72
73
74
75
"""
    convert_simplify_math

Shortcut for [`libsbml_convert`](@ref) that expands functions, local
parameters, and initial assignments in the SBML document.
"""
76
const convert_simplify_math = libsbml_convert(
77
    ["promoteLocalParameters", "expandFunctionDefinitions", "expandInitialAssignments"] .=> Ref(Dict{String,String}()),
78
)