Skip to content

Commit

Permalink
Merge pull request #55 from sebapersson/dev
Browse files Browse the repository at this point in the history
Improve SBML support
  • Loading branch information
sebapersson authored Jul 28, 2023
2 parents 36fe82e + b298199 commit be27e4d
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 190 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PEtab"
uuid = "48d54b35-e43e-4a66-a5a1-dde6b987cf69"
authors = ["Viktor Hasselgren", "Sebastian Persson", "Rafael Arutjunjan", "Damiano Ognissanti"]
version = "1.4.1"
version = "1.4.2"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand All @@ -27,6 +27,7 @@ RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47"
SBML = "e5567a89-2604-4b09-9718-f5f78e97c3bb"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
SteadyStateDiffEq = "9672c7b4-1e72-59bd-8a11-6ac3964bc41f"
Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4"
Expand Down
15 changes: 13 additions & 2 deletions src/Create_PEtab_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,23 @@ function readPEtabModel(pathYAML::String;

modelStr = addParameterForConditionSpecificInitialValues(modelStr, pathConditions, pathParameters, pathModelJlFile, writeToFile)

# Load model ODE-system
# For down the line processing model dict is required
if !@isdefined(modelDict)
modelDict, _ = XmlToModellingToolkit(pathSBML, pathModelJlFile, modelName, writeToFile=false,
onlyGetSBMLDict=true, ifElseToEvent=ifElseToEvent)
end

verbose == true && printstyled("[ Info:", color=123, bold=true)
verbose == true && print(" Symbolically processes ODE-system ...")
timeTake = @elapsed begin
_getODESystem = @RuntimeGeneratedFunction(Meta.parse(modelStr))
_odeSystem, stateMap, parameterMap = _getODESystem("https://xkcd.com/303/") # Argument needed by @RuntimeGeneratedFunction
odeSystem = structural_simplify(_odeSystem)
if "algebraicRules" keys(modelDict) || isempty(modelDict["algebraicRules"])
odeSystem = structural_simplify(_odeSystem)
# DAE requires special processing
else
odeSystem = structural_simplify(dae_index_lowering(_odeSystem))
end
parameterNames = parameters(odeSystem)
stateNames = states(odeSystem)
end
Expand Down Expand Up @@ -234,6 +244,7 @@ function addParameterForConditionSpecificInitialValues(modelStr::String,
pathParameters::String,
pathJuliaFile::String,
writeToFile::Bool)

# Load necessary data
experimentalConditionsFile = CSV.File(pathConditions)
parametersFile = CSV.File(pathParameters)
Expand Down
1 change: 1 addition & 0 deletions src/PEtab.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ using PreallocationTools
using NonlinearSolve
using PrecompileTools
using SBML
using SpecialFunctions

RuntimeGeneratedFunctions.init(@__MODULE__)

Expand Down
3 changes: 2 additions & 1 deletion src/Process_PEtab_files/Observables/Common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ function wordToJuliaSyntax(wordTranslate::String,
end

if wordTranslate in listOperations
wordJuliaSyntax *= listOperations[wordTranslate .== listOperations]
wordJuliaSyntax *= listOperations[wordTranslate .== listOperations][1]
return wordJuliaSyntax # Not allowed to follow with a space
end

if length(wordTranslate) >= 19 && wordTranslate[1:19] == "observableParameter"
Expand Down
2 changes: 2 additions & 0 deletions src/Process_PEtab_files/Observables/Create_u0_h_sigma.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ function create_u0_Function(modelName::String,
pODEProblemStr *= "\n\n"
write(io, pODEProblemStr)

write(io, "\tt = 0.0 # u at time zero\n\n")

# Write the formula for each initial condition to file
modelStateNames = [replace.(string.(stateMap[i].first), "(t)" => "") for i in eachindex(stateMap)]
modelStateStr = ""
Expand Down
4 changes: 4 additions & 0 deletions src/Process_PEtab_files/Process_callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function createCallbacksForTimeDepedentPiecewise(odeSystem::ODESystem,

# In case of no-callbacks the function for getting callbacks will be empty, likewise for the function
# which compute tstops (callback-times)
modelName = replace(modelName, "-" => "_")
stringWriteCallbacks = "function getCallbacks_" * modelName * "(foo)\n"
stringWriteTstops = "\nfunction computeTstops(u::AbstractVector, p::AbstractVector)\n"

Expand Down Expand Up @@ -149,6 +150,9 @@ function createCallbackForEvent(eventName::String,
if hasModelStates == false
_conditionFormula = replace(_conditionFormula, "" => "==")
_conditionFormula = replace(_conditionFormula, "" => "==")
else
_conditionFormula = replace(_conditionFormula, "" => "-")
_conditionFormula = replace(_conditionFormula, "" => "-")
end

# TODO : Refactor and merge functionality with above
Expand Down
1 change: 1 addition & 0 deletions src/SBML/Process_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function getArguments(functionAsString, dictionary::Dict, baseFunctions::Vector{
return [argumentString, includesFunction]
end


# Replaces a word, "replaceFrom" in functions with another word, "replaceTo".
# Often used to change "time" to "t"
# Makes sure not to change for example "time1" or "shift_time"
Expand Down
52 changes: 22 additions & 30 deletions src/SBML/Process_rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,30 @@ function processAssignmentRule!(modelDict::Dict, ruleFormula::String, ruleVariab
# event into the model to ensure proper evaluation of the gradient.
if occursin("piecewise(", ruleFormula)
rewritePiecewiseToIfElse(ruleFormula, ruleVariable, modelDict, baseFunctions)
if ruleVariable keys(modelDict["derivatives"])
delete!(modelDict["derivatives"], ruleVariable)
end
return
end

#=
If the rule does not involve a piecewise expression simply encode it as a function which downsteram
is integrated into the equations when inserting "functions" into the reactions
=#

# Extract the parameters and states which make out the rule, if the rule nests another rule
# additional processing is needed
arguments, includesRule = getArguments(ruleFormula, modelDict["modelRuleFunctions"], baseFunctions)
if isempty(arguments)
modelDict["parameters"][ruleVariable] = ruleFormula
return
# Extract the parameters and states which make out the rule, if the rule nests another function
# function is written to math
arguments, includesFunction = getArguments(ruleFormula, modelDict["modelFunctions"], baseFunctions)
if includesFunction == true
ruleFormula = replaceFunctionWithFormula(ruleFormula, modelDict["modelFunctions"])
end
if includesRule == true
ruleFormula = replaceWholeWordDict(ruleFormula, modelDict["modelRuleFunctions"])
end
modelDict["modelRuleFunctions"][ruleVariable] = [arguments, ruleFormula]
# As we hard-code the rule variable into the equation remove it as state or model parameter.
# TODO : Add this as expression into the model eq. and allow structurally simplify to act on it.

if ruleVariable in keys(modelDict["states"])
modelDict["states"] = delete!(modelDict["states"], ruleVariable)
end
if ruleVariable in keys(modelDict["parameters"])
modelDict["parameters"] = delete!(modelDict["parameters"], ruleVariable)
modelDict["assignmentRulesStates"][ruleVariable] = ruleFormula
return
end

modelDict["parameters"][ruleVariable] = ruleFormula
return
end


Expand All @@ -56,27 +53,22 @@ function processRateRule!(modelDict::Dict, ruleFormula::String, ruleVariable::St
if occursin("piecewise(", ruleFormula)
ruleFormula = rewritePiecewiseToIfElse(ruleFormula, ruleVariable, modelDict, baseFunctions, retFormula=true)
else
arguments, includesRule = getArguments(ruleFormula, modelDict["modelRuleFunctions"], baseFunctions)
if !isempty(arguments) && includesRule == true
ruleFormula = replaceWholeWordDict(ruleFormula, modelDict["modelRuleFunctions"])
arguments, includesFunction = getArguments(ruleFormula, modelDict["modelFunctions"], baseFunctions)
if includesFunction == true
ruleFormula = replaceFunctionWithFormula(ruleFormula, modelDict["modelFunctions"])
end
end

# Add rate rule as part of model derivatives and remove from parameters dict if rate rule variable
# is a parameter
if ruleVariable in keys(modelDict["states"])
modelDict["derivatives"][ruleVariable] = "D(" * ruleVariable * ") ~ " * ruleFormula

elseif ruleVariable in keys(modelDict["nonConstantParameters"])
modelDict["states"][ruleVariable] = modelDict["nonConstantParameters"][ruleVariable]
delete!(modelDict["nonConstantParameters"], ruleVariable)
modelDict["derivatives"][ruleVariable] = "D(" * ruleVariable * ") ~ " * ruleFormula

elseif ruleVariable in keys(modelDict["parameters"])
modelDict["derivatives"][ruleVariable] = "D(" * ruleVariable * ") ~ " * ruleFormula
if ruleVariable in keys(modelDict["parameters"])
modelDict["states"][ruleVariable] = modelDict["parameters"][ruleVariable]
modelDict["stateGivenInAmounts"][ruleVariable] = (false, "")
modelDict["hasOnlySubstanceUnits"][ruleVariable] = false
delete!(modelDict["parameters"], ruleVariable)
modelDict["derivatives"][ruleVariable] = "D(" * ruleVariable * ") ~ " * ruleFormula
else
elseif ruleVariable keys(modelDict["states"])
@error "Warning : Cannot find rate rule variable in either model states or parameters"
end
end
Expand Down
Loading

2 comments on commit be27e4d

@sebapersson
Copy link
Owner 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/88517

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 v1.4.2 -m "<description of version>" be27e4d0a073b7498cce82d22bc801e07a096aae
git push origin v1.4.2

Please sign in to comment.