Skip to content

Commit 1b38d64

Browse files
authored
Merge pull request #356 from hrani/master
readKkit.cpp : create annotator field for kinetic, readSBML.py: valid…
2 parents a5e0e95 + d3c2c5f commit 1b38d64

File tree

12 files changed

+795
-64
lines changed

12 files changed

+795
-64
lines changed

kinetics/ReadKkit.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ Id makeStandardElements( Id pa, const string& modelname )
131131
mgr = shell->doCreate( "Neutral", pa, modelname, 1, MooseGlobal );
132132
Id kinetics( modelPath + "/kinetics" );
133133
if ( kinetics == Id() ) {
134-
kinetics =
135-
shell->doCreate( "CubeMesh", mgr, "kinetics", 1, MooseGlobal );
134+
kinetics = shell->doCreate( "CubeMesh", mgr, "kinetics", 1, MooseGlobal );
136135
SetGet2< double, unsigned int >::set( kinetics, "buildDefaultMesh", 1e-15, 1 );
136+
Id cInfo = shell->doCreate( "Annotator", kinetics, "info", 1 );
137+
assert( cInfo != Id() );
137138
}
138139
assert( kinetics != Id() );
139140

python/moose/SBML/readSBML.py

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
** copyright (C) 2003-2017 Upinder S. Bhalla. and NCBS
1414
Created : Thu May 13 10:19:00 2016(+0530)
1515
Version
16-
Last-Updated: Tue Dec 3 17:30:00 2018(+0530)
16+
Last-Updated: Sat Jan 19 10:30:00 2019(+0530)
1717
By:HarshaRani
1818
**********************************************************************/
19+
2019:
20+
Jan 19: - validator flag is set 'on' from True
21+
- groupname if missing in the sbml file then groupid is taken,
22+
if both are missing then its not a valide sbml file
1923
2018
2024
Dec 3: - reading motor and diffconstant from pool
2125
Nov 30: - groups and subgroups are read from xml to moose
@@ -82,7 +86,7 @@
8286
except ImportError:
8387
pass
8488

85-
def mooseReadSBML(filepath, loadpath, solver="ee",validate="True"):
89+
def mooseReadSBML(filepath, loadpath, solver="ee",validate="on"):
8690
"""Load SBML model
8791
"""
8892
global foundLibSBML_
@@ -102,13 +106,15 @@ def mooseReadSBML(filepath, loadpath, solver="ee",validate="True"):
102106
with open(filepath, "r") as filep:
103107
loadpath = loadpath[loadpath.find('/')+1:]
104108
loaderror = None
109+
errorFlag = ""
105110
filep = open(filepath, "r")
106111
document = libsbml.readSBML(filepath)
107112
tobecontinue = False
108-
if validate == "True":
109-
tobecontinue = validateModel(document)
113+
if validate == "on":
114+
tobecontinue,errorFlag = validateModel(document)
110115
else:
111116
tobecontinue = True
117+
112118
if tobecontinue:
113119
level = document.getLevel()
114120
version = document.getVersion()
@@ -126,6 +132,7 @@ def mooseReadSBML(filepath, loadpath, solver="ee",validate="True"):
126132
loadpath ='/'+loadpath
127133
baseId = moose.Neutral(loadpath)
128134
basePath = baseId
135+
129136
# All the model will be created under model as
130137
# a thumbrule
131138
basePath = moose.Neutral(baseId.path)
@@ -150,7 +157,7 @@ def mooseReadSBML(filepath, loadpath, solver="ee",validate="True"):
150157
errorFlag,msgCmpt = createCompartment(
151158
basePath, model, comptSbmlidMooseIdMap)
152159

153-
groupInfo = checkGroup(basePath,model)
160+
groupInfo = checkGroup(basePath,model,comptSbmlidMooseIdMap)
154161
funcDef = checkFuncDef(model)
155162
if errorFlag:
156163
specInfoMap = {}
@@ -207,8 +214,11 @@ def mooseReadSBML(filepath, loadpath, solver="ee",validate="True"):
207214
loaderror = loaderror
208215
return moose.element(loadpath), loaderror
209216
else:
210-
print("Validation failed while reading the model.")
211-
return moose.element('/'), "This document is not valid SBML"
217+
print("Validation failed while reading the model."+"\n"+errorFlag)
218+
if errorFlag != "":
219+
return moose.element('/'),errorFlag
220+
else:
221+
return moose.element('/'), "This document is not valid SBML"
212222

213223
def checkFuncDef(model):
214224
funcDef = {}
@@ -229,35 +239,41 @@ def checkFuncDef(model):
229239
funcDef[f.getName()] = {'bvar':bvar, "MathML": fmath.getRightChild()}
230240
return funcDef
231241

232-
def checkGroup(basePath,model):
242+
def checkGroup(basePath,model,comptSbmlidMooseIdMap):
233243
groupInfo = {}
234244
modelAnnotaInfo = {}
235245
if model.getPlugin("groups") != None:
236246
mplugin = model.getPlugin("groups")
237247
modelgn = mplugin.getNumGroups()
238248
for gindex in range(0, mplugin.getNumGroups()):
239249
p = mplugin.getGroup(gindex)
250+
grpName = ""
240251
groupAnnoInfo = {}
241252
groupAnnoInfo = getObjAnnotation(p, modelAnnotaInfo)
253+
242254
if groupAnnoInfo != {}:
243-
if moose.exists(basePath.path+'/'+groupAnnoInfo["Compartment"]):
255+
if moose.exists(basePath.path+'/'+comptSbmlidMooseIdMap[groupAnnoInfo["Compartment"]]["MooseId"].name):
256+
groupName = p.getName()
257+
if groupName == " ":
258+
groupName = p.getId()
244259
if "Group" in groupAnnoInfo:
245-
if moose.exists(basePath.path+'/'+groupAnnoInfo["Compartment"]+'/'+groupAnnoInfo["Group"]):
246-
if moose.exists(basePath.path+'/'+groupAnnoInfo["Compartment"]+'/'+groupAnnoInfo["Group"]+'/'+p.getName()):
247-
moosegrp = moose.element(basePath.path+'/'+groupAnnoInfo["Compartment"]+'/'+groupAnnoInfo["Group"]+'/'+p.getName())
260+
if moose.exists(basePath.path+'/'+comptSbmlidMooseIdMap[groupAnnoInfo["Compartment"]]["MooseId"].name+'/'+groupAnnoInfo["Group"]):
261+
if moose.exists(basePath.path+'/'+comptSbmlidMooseIdMap[groupAnnoInfo["Compartment"]]["MooseId"].name+'/'+groupAnnoInfo["Group"]+'/'+groupName):
262+
moosegrp = moose.element(basePath.path+'/'+comptSbmlidMooseIdMap[groupAnnoInfo["Compartment"]]["MooseId"].name+'/'+groupAnnoInfo["Group"]+'/'+groupName)
248263
else:
249-
moosegrp = moose.Neutral(basePath.path+'/'+groupAnnoInfo["Compartment"]+'/'+groupAnnoInfo["Group"]+'/'+p.getName())
264+
moosegrp = moose.Neutral(basePath.path+'/'+comptSbmlidMooseIdMap[groupAnnoInfo["Compartment"]]["MooseId"].name+'/'+groupAnnoInfo["Group"]+'/'+groupName)
250265
else:
251-
moose.Neutral(basePath.path+'/'+groupAnnoInfo["Compartment"]+'/'+groupAnnoInfo["Group"])
252-
if moose.exists(basePath.path+'/'+groupAnnoInfo["Compartment"]+'/'+groupAnnoInfo["Group"]+'/'+p.getName()):
253-
moosegrp = moose.element(basePath.path+'/'+groupAnnoInfo["Compartment"]+'/'+groupAnnoInfo["Group"]+'/'+p.getName())
266+
moose.Neutral(basePath.path+'/'+comptSbmlidMooseIdMap[groupAnnoInfo["Compartment"]]["MooseId"].name+'/'+groupAnnoInfo["Group"])
267+
if moose.exists(basePath.path+'/'+comptSbmlidMooseIdMap[groupAnnoInfo["Compartment"]]["MooseId"].name+'/'+groupAnnoInfo["Group"]+'/'+groupName):
268+
moosegrp = moose.element(basePath.path+'/'+comptSbmlidMooseIdMap[groupAnnoInfo["Compartment"]]["MooseId"].name+'/'+groupAnnoInfo["Group"]+'/'+groupName)
254269
else:
255-
moosegrp = moose.Neutral(basePath.path+'/'+groupAnnoInfo["Compartment"]+'/'+groupAnnoInfo["Group"]+'/'+p.getName())
270+
moosegrp = moose.Neutral(basePath.path+'/'+comptSbmlidMooseIdMap[groupAnnoInfo["Compartment"]]["MooseId"].name+'/'+groupAnnoInfo["Group"]+'/'+groupName)
256271
else:
257-
if not moose.exists(basePath.path+'/'+groupAnnoInfo["Compartment"]+'/'+p.getName()):
258-
moosegrp = moose.Neutral(basePath.path+'/'+groupAnnoInfo["Compartment"]+'/'+p.getName())
272+
if not moose.exists(basePath.path+'/'+comptSbmlidMooseIdMap[groupAnnoInfo["Compartment"]]["MooseId"].name+'/'+groupName):
273+
moosegrp = moose.Neutral(basePath.path+'/'+comptSbmlidMooseIdMap[groupAnnoInfo["Compartment"]]["MooseId"].name+'/'+groupName)
259274
else:
260-
moosegrp = moose.element(basePath.path+'/'+groupAnnoInfo["Compartment"]+'/'+p.getName())
275+
moosegrp = moose.element(basePath.path+'/'+comptSbmlidMooseIdMap[groupAnnoInfo["Compartment"]]["MooseId"].name+'/'+groupName)
276+
261277
moosegrpinfo = moose.Annotator(moosegrp.path+'/info')
262278
moosegrpinfo.color = groupAnnoInfo["bgColor"]
263279
else:
@@ -1206,20 +1222,21 @@ def createSpecies(basePath, model, comptSbmlidMooseIdMap,
12061222
poolInfo = moose.Annotator(poolId.path + '/info')
12071223
else:
12081224
poolInfo = moose.element(poolId.path + '/info')
1209-
1225+
12101226
for k, v in list(specAnnoInfo.items()):
12111227
if k == 'xCord':
12121228
poolInfo.x = float(v)
12131229
elif k == 'yCord':
12141230
poolInfo.y = float(v)
12151231
elif k == 'bgColor':
12161232
poolInfo.color = v
1217-
elif k == 'Color':
1233+
elif k == 'textColor':
12181234
poolInfo.textColor = v
12191235
elif k == 'diffConstant':
12201236
poolId.diffConst = float(v)
12211237
elif k == 'motorConstant':
1222-
poolId.motorConst = float(v)
1238+
poolId.motorConst = float(v)
1239+
12231240
specInfoMap[sId] = {
12241241
"Mpath": poolId,
12251242
"const": constant,

python/moose/SBML/validation.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
By:
1919
**********************************************************************/
2020
21+
**********************************************************************/
22+
2019
23+
Jan 19: - returned errorMsg
2124
'''
25+
2226
foundLibSBML_ = False
2327
try:
2428
from libsbml import *
@@ -29,13 +33,15 @@
2933
def validateModel(sbmlDoc):
3034
if sbmlDoc.getNumErrors() > 0:
3135
tobecontinued = False
36+
validError = ""
3237
for i in range(0,sbmlDoc.getNumErrors()):
33-
print (sbmlDoc.getError(i).getMessage())
34-
return False
38+
validError = validError+sbmlDoc.getError(i).getMessage()
39+
#print (validError)
40+
return False, validError
3541

3642
if (not sbmlDoc):
3743
print("validateModel: given a null SBML Document")
38-
return False
44+
return False, "validateModel: given a null SBML Document"
3945

4046
consistencyMessages = ""
4147
validationMessages = ""
@@ -86,7 +92,7 @@ def validateModel(sbmlDoc):
8692
validationMessages = oss
8793

8894
if (noProblems):
89-
return True
95+
return True,""
9096
else:
9197
if consistencyMessages is None:
9298
consistencyMessages = ""
@@ -107,7 +113,7 @@ def validateModel(sbmlDoc):
107113
if validationMessages:
108114
print(validationMessages)
109115

110-
return False
116+
return False,validationMessages
111117
# return ( numConsistencyErrors == 0 and numValidationErrors == 0,
112118
# consistencyMessages)
113119

python/moose/SBML/writeSBML.py

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
** copyright (C) 2003-2017 Upinder S. Bhalla. and NCBS
1414
Created : Friday May 27 12:19:00 2016(+0530)
1515
Version
16-
Last-Updated: Tue 3 Dec 15:15:10 2018(+0530)
16+
Last-Updated: Tue 29 Jan 15:15:10 2019(+0530)
1717
By: HarshaRani
1818
**********************************************************************/
1919
/****************************
20+
2019
21+
Jan 29: getColor are taken from chemConnectUtil, group's width and height are written
2022
2018
2123
Dec 07: using fixXreac's restoreXreacs function to remove xfer
2224
Dec 03: add diff and motor constants to pool
@@ -47,9 +49,10 @@
4749
import os
4850
import moose
4951
from moose.SBML.validation import validateModel
50-
from moose.chemUtil.chemConnectUtil import *
52+
from moose.chemUtil.chemConnectUtil import xyPosition,mooseIsInstance,findCompartment,getColor,setupItem
5153
from moose.chemUtil.graphUtils import *
5254
from moose.fixXreacs import restoreXreacs
55+
import numpy as np
5356

5457
foundLibSBML_ = False
5558
try:
@@ -98,6 +101,7 @@ def mooseWriteSBML(modelpath, filename, sceneitems={}):
98101
if moose.exists(p.path+'/info'):
99102
xcord.append(moose.element(p.path+'/info').x)
100103
ycord.append(moose.element(p.path+'/info').y)
104+
getColor(moose.element(p.path+'/info').path)
101105
recalculatecoordinates(modelpath,mObj,xcord,ycord)
102106
positionInfoexist = False
103107

@@ -158,22 +162,41 @@ def mooseWriteSBML(modelpath, filename, sceneitems={}):
158162
mplugin = cremodel_.getPlugin("groups")
159163
group = mplugin.createGroup()
160164
name = str(idBeginWith(moose.element(key).name))
161-
moosegrpId = name +"_" + str(moose.element(key).getId().value) + "_" + str(moose.element(key).getDataIndex())
162-
group.setId(moosegrpId)
165+
moosegrpId = name +"_" + str(moose.element(key).getId().value) + "_" + str(moose.element(key).getDataIndex())+"_"
166+
group.setId(moosegrpId)
163167
group.setName(name)
164168

165169
group.setKind("collection")
166170
if moose.exists(key.path+'/info'):
167171
ginfo = moose.element(key.path+'/info')
168172
else:
169173
ginfo = moose.Annotator(key.path+'/info')
174+
170175
groupCompartment = findCompartment(key)
176+
171177
grpAnno = "<moose:GroupAnnotation>"
172-
grpAnno = grpAnno + "<moose:Compartment>" + groupCompartment.name + "</moose:Compartment>\n"
178+
grpAnno = grpAnno + "<moose:Compartment>" + groupCompartment.name +"_"+ str(moose.element(groupCompartment).getId().value) +"_"+ str(moose.element(groupCompartment).getDataIndex())+"_"+"</moose:Compartment>\n"
179+
#grpAnno = grpAnno + "<moose:Compartment>" + groupCompartment.name + "_" + str(moose.element(groupCompartment).getId().value) + "_" + str(moose.element(groupCompartment).getDataIndex()) + "_"+ "</moose:Compartment>\n"
173180
if moose.element(key.parent).className == "Neutral":
181+
174182
grpAnno = grpAnno + "<moose:Group>" + key.parent.name + "</moose:Group>\n"
175-
if ginfo.color:
176-
grpAnno = grpAnno + "<moose:bgColor>" + ginfo.color + "</moose:bgColor>\n"
183+
grpparent = key.parent.name + "_" + str(moose.element(key.parent).getId().value) + "_" + str(moose.element(key.parent).getDataIndex()) + "_"
184+
grpAnno = grpAnno + "<moose:Parent>" + grpparent + "</moose:Parent>\n"
185+
else:
186+
grpparent = groupCompartment.name + "_" + str(moose.element(groupCompartment).getId().value) + "_" + str(moose.element(groupCompartment).getDataIndex()) + "_"
187+
grpAnno = grpAnno + "<moose:Parent>" + grpparent + "</moose:Parent>\n"
188+
189+
if moose.exists(key.path+'/info'):
190+
ginfo = moose.element(key.path+'/info')
191+
if ginfo.height and ginfo.width:
192+
grpAnno = grpAnno + "<moose:x>" + str(ginfo.x) + "</moose:x>\n"
193+
grpAnno = grpAnno + "<moose:y>" + str(ginfo.y) + "</moose:y>\n"
194+
grpAnno = grpAnno + "<moose:width>" + str(ginfo.width) + "</moose:width>\n"
195+
grpAnno = grpAnno + "<moose:height>" + str(ginfo.height) + "</moose:height>\n"
196+
if ginfo.color:
197+
grpAnno = grpAnno + "<moose:bgColor>" + ginfo.color + "</moose:bgColor>\n"
198+
if ginfo.notes:
199+
grpAnno = grpAnno + "<moose:Notes>" + ginfo.notes + "</moose:Notes>\n"
177200
grpAnno = grpAnno + "</moose:GroupAnnotation>"
178201
group.setAnnotation(grpAnno)
179202

@@ -1048,8 +1071,12 @@ def writeCompt(modelpath, cremodel_):
10481071
"<moose:surround>" + \
10491072
str(comptID_sbml[compt.surround])+ "</moose:surround>\n" + \
10501073
"<moose:isMembraneBound>" + \
1051-
str(compt.isMembraneBound)+ "</moose:isMembraneBound>\n" + \
1052-
"</moose:CompartmentAnnotation>"
1074+
str(compt.isMembraneBound)+ "</moose:isMembraneBound>\n"
1075+
if moose.exists(compt.path+'/info'):
1076+
if moose.element(compt.path+'/info').notes != "":
1077+
comptAnno = comptAnno + "<moose:Notes>" \
1078+
+ moose.element(compt.path+'/info').notes + "</moose:Notes>"
1079+
comptAnno = comptAnno+ "</moose:CompartmentAnnotation>"
10531080
elif isinstance (compt,moose.CylMesh) :
10541081
size = (compt.volume/compt.numDiffCompts)*pow(10,3)
10551082
comptAnno = "<moose:CompartmentAnnotation><moose:Mesh>" + \
@@ -1059,14 +1086,22 @@ def writeCompt(modelpath, cremodel_):
10591086
"<moose:diffLength>" + \
10601087
str(compt.diffLength)+ "</moose:diffLength>\n" + \
10611088
"<moose:isMembraneBound>" + \
1062-
str(compt.isMembraneBound)+ "</moose:isMembraneBound>\n" + \
1063-
"</moose:CompartmentAnnotation>"
1089+
str(compt.isMembraneBound)+ "</moose:isMembraneBound>\n"
1090+
if moose.exists(compt.path+'/info'):
1091+
if moose.element(compt.path+'/info').notes != "":
1092+
comptAnno = comptAnno + "<moose:Notes>" \
1093+
+ moose.element(compt.path+'/info').notes + "</moose:Notes>"
1094+
comptAnno = comptAnno+ "</moose:CompartmentAnnotation>"
10641095
else:
10651096
comptAnno = "<moose:CompartmentAnnotation><moose:Mesh>" + \
10661097
str(compt.className) + "</moose:Mesh>\n" + \
10671098
"<moose:isMembraneBound>" + \
1068-
str(compt.isMembraneBound)+ "</moose:isMembraneBound>\n" + \
1069-
"</moose:CompartmentAnnotation>"
1099+
str(compt.isMembraneBound)+ "</moose:isMembraneBound>\n"
1100+
if moose.exists(compt.path+'/info'):
1101+
if moose.element(compt.path+'/info').notes != "":
1102+
comptAnno = comptAnno + "<moose:Notes>" \
1103+
+ moose.element(compt.path+'/info').notes + "</moose:Notes>"
1104+
comptAnno = comptAnno+ "</moose:CompartmentAnnotation>"
10701105
if createCompt:
10711106
c1 = cremodel_.createCompartment()
10721107
c1.setId(csetId)

0 commit comments

Comments
 (0)