Skip to content

Commit

Permalink
Write documents with L3V2
Browse files Browse the repository at this point in the history
Suggested by Sarah Keating at
<sbmlteam/libsbml#235 (comment)>.
  • Loading branch information
giordano committed Jun 22, 2022
1 parent f385a0d commit 68d5f0d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
79 changes: 41 additions & 38 deletions src/writesbml.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Ideally we'd like to use level 3, version 2, but because of
# https://github.com/sbmlteam/libsbml/pull/235#issuecomment-1152491848 we have to match
# level/version of the fbc plugin.
# Level/Version for the document
const WRITESBML_DEFAULT_LEVEL = 3
const WRITESBML_DEFAULT_VERSION = 1
const WRITESBML_DEFAULT_PKGVERSION = 2
const WRITESBML_DEFAULT_VERSION = 2
# Level/Version/Package version for the package
const WRITESBML_PKG_DEFAULT_LEVEL = 3
const WRITESBML_PKG_DEFAULT_VERSION = 1
const WRITESBML_PKG_DEFAULT_PKGVERSION = 2

function model_to_sbml!(doc::VPtr, mdl::Model)::VPtr
# Create the model pointer
Expand Down Expand Up @@ -151,9 +152,9 @@ function model_to_sbml!(doc::VPtr, mdl::Model)::VPtr
sbml(:GeneProduct_create),
VPtr,
(Cuint, Cuint, Cuint),
WRITESBML_DEFAULT_LEVEL,
WRITESBML_DEFAULT_VERSION,
WRITESBML_DEFAULT_PKGVERSION,
WRITESBML_PKG_DEFAULT_LEVEL,
WRITESBML_PKG_DEFAULT_VERSION,
WRITESBML_PKG_DEFAULT_PKGVERSION,
)
ccall(sbml(:GeneProduct_setId), Cint, (VPtr, Cstring), geneproduct_ptr, id)
ccall(
Expand Down Expand Up @@ -271,9 +272,6 @@ function model_to_sbml!(doc::VPtr, mdl::Model)::VPtr
reaction_ptr,
reaction.reversible,
)
# The fast attribute is mandatory in Level 3 Version 1, but it was removed in Level
# 3 Version 2. When missing, it is assumed to be false.
ccall(sbml(:Reaction_setFast), Cint, (VPtr, Cint), reaction_ptr, false)
isnothing(reaction.name) || ccall(
sbml(:Reaction_setName),
Cint,
Expand Down Expand Up @@ -308,8 +306,7 @@ function model_to_sbml!(doc::VPtr, mdl::Model)::VPtr
)
end
for (species, stoichiometry) in reaction.products
product_ptr =
ccall(sbml(:Reaction_createProduct), VPtr, (VPtr,), reaction_ptr)
product_ptr = ccall(sbml(:Reaction_createProduct), VPtr, (VPtr,), reaction_ptr)
ccall(
sbml(:SpeciesReference_setSpecies),
Cint,
Expand Down Expand Up @@ -363,9 +360,9 @@ function model_to_sbml!(doc::VPtr, mdl::Model)::VPtr
sbml(:Objective_create),
VPtr,
(Cuint, Cuint, Cuint),
WRITESBML_DEFAULT_LEVEL,
WRITESBML_DEFAULT_VERSION,
WRITESBML_DEFAULT_PKGVERSION,
WRITESBML_PKG_DEFAULT_LEVEL,
WRITESBML_PKG_DEFAULT_VERSION,
WRITESBML_PKG_DEFAULT_PKGVERSION,
)
ccall(sbml(:Objective_setId), Cint, (VPtr, Cstring), objective_ptr, id)
ccall(
Expand All @@ -380,9 +377,9 @@ function model_to_sbml!(doc::VPtr, mdl::Model)::VPtr
sbml(:FluxObjective_create),
VPtr,
(Cuint, Cuint, Cuint),
WRITESBML_DEFAULT_LEVEL,
WRITESBML_DEFAULT_VERSION,
WRITESBML_DEFAULT_PKGVERSION,
WRITESBML_PKG_DEFAULT_LEVEL,
WRITESBML_PKG_DEFAULT_VERSION,
WRITESBML_PKG_DEFAULT_PKGVERSION,
)
ccall(
sbml(:FluxObjective_setReaction),
Expand Down Expand Up @@ -418,14 +415,13 @@ function model_to_sbml!(doc::VPtr, mdl::Model)::VPtr
!iszero(res) &&
@warn "Failed to add objective \"$(id)\": $(OPERATION_RETURN_VALUES[res])"
end
fbc_plugin == C_NULL ||
ccall(
sbml(:FbcModelPlugin_setActiveObjectiveId),
Cint,
(VPtr, Cstring),
fbc_plugin,
mdl.active_objective,
)
fbc_plugin == C_NULL || ccall(
sbml(:FbcModelPlugin_setActiveObjectiveId),
Cint,
(VPtr, Cstring),
fbc_plugin,
mdl.active_objective,
)

# Add species
fbc_plugin == C_NULL ||
Expand Down Expand Up @@ -723,25 +719,32 @@ function _create_doc(mdl::Model)::VPtr
WRITESBML_DEFAULT_VERSION,
)
else
# Get fbc registry entry
sbmlext = ccall(sbml(:SBMLExtensionRegistry_getExtension), VPtr, (Cstring,), "fbc")
# create the sbml namespaces object with fbc
fbc = ccall(sbml(:XMLNamespaces_create), VPtr, ())
# create the sbml namespaces object with fbc
uri = ccall(
sbml(:SBMLExtension_getURI),
Cstring,
(VPtr, Cuint, Cuint, Cuint),
sbmlext,
WRITESBML_PKG_DEFAULT_LEVEL,
WRITESBML_PKG_DEFAULT_VERSION,
WRITESBML_PKG_DEFAULT_PKGVERSION,
)
ccall(sbml(:XMLNamespaces_add), Cint, (VPtr, Cstring, Cstring), fbc, uri, "fbc")
# Create SBML namespace with fbc package
ns = ccall(
sbmlns = ccall(
sbml(:SBMLNamespaces_create),
VPtr,
(Cuint, Cuint),
WRITESBML_DEFAULT_LEVEL,
WRITESBML_DEFAULT_VERSION,
)
ccall(
sbml(:SBMLNamespaces_addPackageNamespace),
Cint,
(VPtr, Cstring, Cuint, Cstring),
ns,
"fbc",
WRITESBML_DEFAULT_PKGVERSION,
"",
)
ccall(sbml(:SBMLNamespaces_addPackageNamespaces), Cint, (VPtr, VPtr), sbmlns, fbc)
# Create document from SBML namespace
d = ccall(sbml(:SBMLDocument_createWithSBMLNamespaces), VPtr, (VPtr,), ns)
d = ccall(sbml(:SBMLDocument_createWithSBMLNamespaces), VPtr, (VPtr,), sbmlns)
# Do not require fbc package
ccall(
sbml(:SBMLDocument_setPackageRequired),
Expand Down
28 changes: 13 additions & 15 deletions test/data/Dasgupta2020-written.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version1/core" xmlns:fbc="http://www.sbml.org/sbml/level3/version1/fbc/version2" level="3" version="1" fbc:required="false">
<sbml xmlns="http://www.sbml.org/sbml/level3/version2/core" xmlns:fbc="http://www.sbml.org/sbml/level3/version1/fbc/version2" level="3" version="2" fbc:required="false">
<model metaid="COPASI0" id="Dasgupta2020___Reduced_model_of_receptor_clusturing_and_aggregation" name="Dasgupta2020 - Reduced model of receptor clusturing and aggregation" fbc:strict="true">
<notes>
<body xmlns="http://www.w3.org/1999/xhtml">
Expand Down Expand Up @@ -50,14 +50,12 @@ response to activation both in 2D and in 3D</p>
<dcterms:creator>
<rdf:Bag>
<rdf:li rdf:parseType="Resource">
<vCard:N rdf:parseType="Resource">
<vCard:Family>Tiwari</vCard:Family>
<vCard:Given>Krishna</vCard:Given>
</vCard:N>
<vCard:EMAIL>tiwarik@babraham.ac.uk</vCard:EMAIL>
<vCard:ORG rdf:parseType="Resource">
<vCard:Orgname>Babraham Institute</vCard:Orgname>
</vCard:ORG>
<vCard4:hasName rdf:parseType="Resource">
<vCard4:family-name>Tiwari</vCard4:family-name>
<vCard4:given-name>Krishna</vCard4:given-name>
</vCard4:hasName>
<vCard4:hasEmail>tiwarik@babraham.ac.uk</vCard4:hasEmail>
<vCard4:organization-name>Babraham Institute</vCard4:organization-name>
</rdf:li>
</rdf:Bag>
</dcterms:creator>
Expand Down Expand Up @@ -412,32 +410,32 @@ response to activation both in 2D and in 3D</p>
</assignmentRule>
</listOfRules>
<listOfReactions>
<reaction id="R4" name="R4" reversible="false" fast="false">
<reaction id="R4" name="R4" reversible="false">
<listOfProducts>
<speciesReference species="N" stoichiometry="1" constant="true"/>
</listOfProducts>
</reaction>
<reaction id="R3" name="R3" reversible="false" fast="false">
<reaction id="R3" name="R3" reversible="false">
<listOfProducts>
<speciesReference species="P" stoichiometry="1" constant="true"/>
</listOfProducts>
</reaction>
<reaction id="R6" name="R6" reversible="false" fast="false">
<reaction id="R6" name="R6" reversible="false">
<listOfReactants>
<speciesReference species="N" stoichiometry="1" constant="true"/>
</listOfReactants>
</reaction>
<reaction id="R1" name="R1" reversible="false" fast="false">
<reaction id="R1" name="R1" reversible="false">
<listOfReactants>
<speciesReference species="P" stoichiometry="1" constant="true"/>
</listOfReactants>
</reaction>
<reaction id="R5" name="R5" reversible="false" fast="false">
<reaction id="R5" name="R5" reversible="false">
<listOfReactants>
<speciesReference species="N" stoichiometry="1" constant="true"/>
</listOfReactants>
</reaction>
<reaction id="R2" name="R2" reversible="false" fast="false">
<reaction id="R2" name="R2" reversible="false">
<listOfReactants>
<speciesReference species="P" stoichiometry="1" constant="true"/>
</listOfReactants>
Expand Down

0 comments on commit 68d5f0d

Please sign in to comment.