Skip to content

Commit

Permalink
Minor bug fixes in SBML importer
Browse files Browse the repository at this point in the history
  • Loading branch information
sebapersson committed Aug 14, 2023
1 parent e7b44e6 commit 59b7fa4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/Create_PEtab_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ function addParameterForConditionSpecificInitialValues(modelStr::String,
for rowValue in experimentalConditionsFile[Symbol(state)]
if typeof(rowValue) <: Real
continue
elseif ismissing(rowValue)
continue
elseif isNumber(rowValue) == true || string(rowValue) parameterNames
continue
elseif rowValue parametersFile[:parameterId]
Expand Down
12 changes: 9 additions & 3 deletions src/Process_PEtab_files/Observables/Common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,16 @@ end
Replace the explicit rule variable with the explicit rule
"""
function replaceExplicitVariableWithRule(formula::String, SBMLDict::Dict)::String
for (key,value) in SBMLDict["modelRuleFunctions"]
formula = replaceWholeWord(formula, key, "(" * value[2] * ")")
_formula = deepcopy(formula)
while true
for (key, value) in SBMLDict["assignmentRulesStates"]
_formula = replaceWholeWord(_formula, key, "(" * value * ")")
end
_formula == formula && break
formula = deepcopy(_formula)
end
return formula

return _formula
end


Expand Down
15 changes: 9 additions & 6 deletions src/Process_PEtab_files/Observables/Create_u0_h_sigma.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ function create_σ_h_u0_File(modelName::String,
hStr = create_h_Function(modelName, dirJulia, modelStateNames, parameterInfo, pODEProblemNames,
string.(θ_indices.θ_nonDynamicNames), observablesData, SBMLDict, writeToFile)

u0!Str = create_u0_Function(modelName, dirJulia, parameterInfo, pODEProblemNames, stateMap, writeToFile, inPlace=true)
u0!Str = create_u0_Function(modelName, dirJulia, parameterInfo, pODEProblemNames, stateMap, writeToFile, SBMLDict, inPlace=true)

u0Str = create_u0_Function(modelName, dirJulia, parameterInfo, pODEProblemNames, stateMap, writeToFile, inPlace=false)
u0Str = create_u0_Function(modelName, dirJulia, parameterInfo, pODEProblemNames, stateMap, writeToFile, SBMLDict, inPlace=false)

σStr = create_σ_Function(modelName, dirJulia, parameterInfo, modelStateNames, pODEProblemNames, string.(θ_indices.θ_nonDynamicNames), observablesData, SBMLDict, writeToFile)

Expand Down Expand Up @@ -178,7 +178,8 @@ end
dirModel::String,
parameterInfo::ParametersInfo,
pODEProblemNames::Vector{String},
stateMap;
stateMap,
SBMLDict;
inPlace::Bool=true)
For modelName create a function for computing initial value by translating the stateMap
Expand All @@ -192,7 +193,8 @@ function create_u0_Function(modelName::String,
parameterInfo::ParametersInfo,
pODEProblemNames::Vector{String},
stateMap,
writeToFile::Bool;
writeToFile::Bool,
SBMLDict;
inPlace::Bool=true)

pathSave = joinpath(dirModel, modelName * "_h_sd_u0.jl")
Expand All @@ -216,9 +218,10 @@ function create_u0_Function(modelName::String,
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)]
_modelStateNames = [replace.(string.(stateMap[i].first), "(t)" => "") for i in eachindex(stateMap)]
modelStateNames = filter(x -> x string.(keys(SBMLDict["assignmentRulesStates"])), _modelStateNames)
modelStateStr = ""
for i in eachindex(stateMap)
for i in eachindex(modelStateNames)
stateName = modelStateNames[i]
_stateExpression = replace(string(stateMap[i].second), " " => "")
stateFormula = petabFormulaToJulia(_stateExpression, modelStateNames, parameterInfo, pODEProblemNames, String[])
Expand Down
4 changes: 2 additions & 2 deletions src/SBML/Process_rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ function processAssignmentRule!(modelDict::Dict, ruleFormula::String, ruleVariab

if ruleVariable in keys(modelDict["states"])
modelDict["assignmentRulesStates"][ruleVariable] = ruleFormula
return
# Delete from state dictionary (as we no longer should assign an initial value to the state)
delete!(modelDict["states"], ruleVariable)
end

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

Expand Down
26 changes: 19 additions & 7 deletions src/SBML/SBML_to_ModellingToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ function buildODEModelDictionary(modelSBML, ifElseToEvent::Bool)
for (state, formula) in modelDict["assignmentRulesStates"]
modelDict["derivatives"][state] = state * " ~ " * formula
if state nonConstantParameterNames
modelDict["states"][state] = formula
delete!(modelDict["states"], state)
delete!(modelDict["parameters"], state)
nonConstantParameterNames = filter(x -> x != state, nonConstantParameterNames)
end
end

Expand Down Expand Up @@ -581,14 +583,17 @@ function createODEModelFunction(modelDict, pathJlFile, modelName, juliaFile, wri
for key in keys(modelDict["states"])
stringDict["variables"] *= key * "(t) "
end
for (key, value) in modelDict["assignmentRulesStates"]
stringDict["variables"] *= key * "(t) "
end

for (index, key) in enumerate(keys(modelDict["states"]))
if index < length(modelDict["states"])
stringDict["stateArray"] *= key * ", "
else
stringDict["stateArray"] *= key * "]"
end
for (key, value) in modelDict["states"]
stringDict["stateArray"] *= key * ", "
end
for (key, value) in modelDict["assignmentRulesStates"]
stringDict["stateArray"] *= key * ", "
end
stringDict["stateArray"] = stringDict["stateArray"][1:end-2] * "]"

if length(modelDict["nonConstantParameters"]) > 0
stringDict["variableParameters"] = " ModelingToolkit.@variables"
Expand Down Expand Up @@ -661,6 +666,9 @@ function createODEModelFunction(modelDict, pathJlFile, modelName, juliaFile, wri
for key in keys(modelDict["algebraicRules"])
stringDict["derivatives"] *= ",\n " * modelDict["algebraicRules"][key]
end
for key in keys(modelDict["assignmentRulesStates"])
stringDict["derivatives"] *= ",\n " * key * " ~ " * modelDict["assignmentRulesStates"][key]
end
stringDict["derivatives"] *= "\n"
stringDict["derivatives"] *= " ]"

Expand Down Expand Up @@ -696,6 +704,10 @@ function createODEModelFunction(modelDict, pathJlFile, modelName, juliaFile, wri
assignString = ",\n " * key * " => " * value
stringDict["initialSpeciesValues"] *= assignString
end
for (key, value) in modelDict["assignmentRulesStates"]
assignString = ",\n " * key * " => " * value
stringDict["initialSpeciesValues"] *= assignString
end
stringDict["initialSpeciesValues"] *= "\n"
stringDict["initialSpeciesValues"] *= " ]"

Expand Down

0 comments on commit 59b7fa4

Please sign in to comment.