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

Minor bug fixes in SBML importer #58

Merged
merged 1 commit into from
Aug 14, 2023
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
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
Loading