Skip to content

Commit

Permalink
Merge pull request #6 from flowersteam/remove-tempfile
Browse files Browse the repository at this point in the history
Remove tempfile
  • Loading branch information
mayalenE authored Dec 3, 2024
2 parents c709183 + 161ab45 commit 43fad4d
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions sbmltoodejax/parse.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import libsbml
import os
import sbmltoodepy
from tempfile import NamedTemporaryFile
from sbmltoodepy.parse import *

def ParseSBMLFile(file: str):
"""
Expand Down Expand Up @@ -45,16 +44,10 @@ def ParseSBMLFile(file: str):
"""

if os.path.exists(file):
filePath = file
libsbml.readSBML(filePath)
doc = libsbml.readSBML(file)

else:
tmp_sbml_file = NamedTemporaryFile(suffix=".xml")
with open(tmp_sbml_file.name, 'w') as f:
f.write(file)
doc = libsbml.readSBMLFromString(file)
filePath = tmp_sbml_file.name

# Raise an Error if SBML error
if doc.getNumErrors() > 0:
Expand All @@ -65,7 +58,31 @@ def ParseSBMLFile(file: str):
if model.getNumEvents() > 0:
raise NotImplementedError("Events are not handled")

modelData = sbmltoodepy.parse.ParseSBMLFile(filePath)
modelData = sbmltoodepy.dataclasses.ModelData()
for i in range(model.getNumParameters()):
newParameter = ParseParameterAssignment(i, model.getParameter(i))
modelData.parameters[newParameter.Id] = newParameter
for i in range(model.getNumCompartments()):
newCompartment = ParseCompartment(i, model.getCompartment(i))
modelData.compartments[newCompartment.Id] = newCompartment
for i in range(model.getNumSpecies()):
newSpecies = ParseSpecies(i, model.getSpecies(i))
modelData.species[newSpecies.Id] = newSpecies
for i in range(model.getNumFunctionDefinitions()):
newFunction = ParseFunction(i, model.getFunctionDefinition(i))
modelData.functions[newFunction.Id] = newFunction
for i in range(model.getNumRules()):
newRule = ParseRule(i,model.getRule(i))
if type(newRule) == sbmltoodepy.dataclasses.AssignmentRuleData:
modelData.assignmentRules[newRule.Id] = newRule
elif type(newRule) == sbmltoodepy.dataclasses.RateRuleData:
modelData.rateRules[newRule.Id] = newRule
for i in range(model.getNumReactions()):
newReaction = ParseReaction(i, model.getReaction(i))
modelData.reactions[newReaction.Id] = newReaction
for i in range(model.getNumInitialAssignments()):
newAssignment = ParseInitialAssignment(i, model.getInitialAssignment(i))
modelData.initialAssignments[newAssignment.Id] = newAssignment

return modelData

Expand Down

0 comments on commit 43fad4d

Please sign in to comment.