%pylab inline
from cno.io import sbmlqual

"""
Explanation: SBMLqual
cellnopt provide some tools to read/write SBMLqual format. IT does not rely on libsbml but should
be able to read/write logic models.
End of explanation
"""


from bioservices import BioModels

b = BioModels()

models = b.getModelsIdByName('macnamara')
models

model = b.getModelSBMLById(models[0])

with open('test.xml','w') as fh:
    fh.write(model)

# the reader creates a SIF file out of the SBML
reader = sbmlqual.SBMLQual()
sif = reader.read_sbmlqual("test.xml")

# data is not stored in BioModel, but in cellnopt
from cno import CNOGraph, cnodata, XMIDAS
data = XMIDAS(cnodata("MD-ToyPB.csv"))

c = CNOGraph(sif, data)

c.png

"""
Explanation: Reading a known logic model from BioModel
End of explanation
"""


from cno import CNOGraph, cnodata, XMIDAS
data = XMIDAS(cnodata("MD-ToyPB.csv"))
sif = cnodata("PKN-ToyPB.sif")

c1 = CNOGraph(sif, data)

c1.to_sbmlqual("PKN-ToyPB.xml")

"""
Explanation: Creating the SBMLqual
End of explanation
"""


c2 =  CNOGraph("PKN-ToyPB.xml", data)

# check that the initial model is identical to the one 
# saved with to_sbmlqual
c1 == c2

c2.png

"""
Explanation: Reading back
End of explanation
"""


