Skip to content

Commit eb88a23

Browse files
committed
readSBML:compartment name and id is mapped, in Assignment rule a check made for integer in the expression,writeSBML:added autlayout which was required for writting cspace model while writting from command line, for cplxpool's x-y coordinates and motor/diff constant are added,fixXreac: check to see if object exist before _moose.connect, case where object is deleted but _xref_ annotation info field still hold the reference
1 parent 660c91d commit eb88a23

File tree

5 files changed

+80
-19
lines changed

5 files changed

+80
-19
lines changed

python/moose/SBML/readSBML.py

Lines changed: 19 additions & 7 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 : Thu May 13 10:19:00 2016(+0530)
1515
Version
16-
Last-Updated: Sat Jan 19 10:30:00 2019(+0530)
16+
Last-Updated: Mon Jun 16 10:30:00 2019(+0530)
1717
By:HarshaRani
1818
**********************************************************************/
1919
2019:
20+
Jun 06: - both compartment name and Id is mapped to the values in comptSbmlidMooseIdMap
21+
May 23: - check for interger for Assignment expr
2022
Jan 19: - validator flag is set 'on' from True
2123
- groupname if missing in the sbml file then groupid is taken,
2224
if both are missing then its not a valide sbml file
@@ -149,7 +151,8 @@ def mooseReadSBML(filepath, loadpath, solver="ee",validate="on"):
149151
groupInfo = {}
150152
funcDef = {}
151153
modelAnnotaInfo = {}
152-
comptSbmlidMooseIdMap = {}
154+
#comptSbmlidMooseIdMap = {}
155+
comptSbmlidMooseIdMap = dict()
153156
globparameterIdValue = {}
154157

155158
mapParameter(model, globparameterIdValue)
@@ -277,7 +280,7 @@ def checkGroup(basePath,model,comptSbmlidMooseIdMap):
277280
moosegrpinfo = moose.Annotator(moosegrp.path+'/info')
278281
moosegrpinfo.color = groupAnnoInfo["bgColor"]
279282
else:
280-
print ("Compartment not found")
283+
print ("Group's compartment not found in xml file")
281284
if p.getKind() == 2:
282285
if p.getId() not in groupInfo:
283286
memlists = []
@@ -934,6 +937,7 @@ def getMembers(node, ruleMemlist):
934937
found = True
935938
if node == None:
936939
pass
940+
937941
elif node.getType() == libsbml.AST_POWER:
938942
pass
939943

@@ -1009,6 +1013,12 @@ def getMembers(node, ruleMemlist):
10091013
for i in range (0,node.getNumBvars()):
10101014
ruleMemlist.append(node.getChild(i).getName())
10111015
#funcD[funcName] = {"bvar" : bvar, "MathML":node.getRightChild()}
1016+
1017+
elif node.getType() == libsbml.AST_INTEGER:
1018+
#value is constant
1019+
#ruleMemlist.append(node.getValue())
1020+
pass
1021+
10121022
elif node.getType() == libsbml.AST_FUNCTION_POWER:
10131023
msg = msg + "\n moose is yet to handle \""+node.getName() + "\" operator"
10141024
found = False
@@ -1104,7 +1114,7 @@ def createRules(model, specInfoMap, globparameterIdValue):
11041114
funcId.numVars = numVars + 1
11051115

11061116
elif not(i in globparameterIdValue):
1107-
msg = msg + "check the variable name in mathML, this object neither pool or a constant \"" + i+"\" in assignmentRule " +rule.getVariable()
1117+
msg = msg + "check the variable name in mathML, this object neither pool or a constant \"" + str(i)+"\" in assignmentRule " +rule.getVariable()
11081118

11091119
exp = rule.getFormula()
11101120
exprOK = True
@@ -1437,9 +1447,11 @@ def createCompartment(basePath, model, comptSbmlidMooseIdMap):
14371447
mooseCmptId = moose.CubeMesh(basePath.path+'/'+name)
14381448

14391449
mooseCmptId.volume = (msize * unitfactor)
1440-
1441-
comptSbmlidMooseIdMap[sbmlCmptId] = {
1442-
"MooseId": mooseCmptId, "spatialDim": dimension, "size": msize}
1450+
#both compartment name and Id is mapped to the values
1451+
comptSbmlidMooseIdMap.update(dict.fromkeys([sbmlCmptId,name], {"MooseId": mooseCmptId, "spatialDim": dimension, "size": msize}))
1452+
1453+
#comptSbmlidMooseIdMap[sbmlCmptId] = {
1454+
# "MooseId": mooseCmptId, "spatialDim": dimension, "size": msize}
14431455

14441456
for key,value in endo_surr.items():
14451457
if value in comptSbmlidMooseIdMap:

python/moose/SBML/writeSBML.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
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 29 Jan 15:15:10 2019(+0530)
16+
Last-Updated: Thr 18 July 15:15:10 2019(+0530)
1717
By: HarshaRani
1818
**********************************************************************/
1919
/****************************
2020
2019
21+
July 18: added a call for autolayout, this was required for cspace model while trying to write from cmd line
22+
while writting file, the filepath is checked
23+
now even for cplxpool's x and y co-ordinates,diff and motor constant are added
2124
Jan 29: getColor are taken from chemConnectUtil, group's width and height are written
2225
2018
2326
Dec 07: using fixXreac's restoreXreacs function to remove xfer
@@ -49,7 +52,7 @@
4952
import os
5053
import moose
5154
from moose.SBML.validation import validateModel
52-
from moose.chemUtil.chemConnectUtil import xyPosition,mooseIsInstance,findCompartment,getColor,setupItem
55+
from moose.chemUtil.chemConnectUtil import xyPosition,mooseIsInstance,findCompartment,getColor,setupItem,setupMeshObj
5356
from moose.chemUtil.graphUtils import *
5457
from moose.fixXreacs import restoreXreacs
5558
import numpy as np
@@ -61,6 +64,21 @@
6164
except Exception as e:
6265
pass
6366

67+
def checkPath( dirName):
68+
path = dirName
69+
if (dirName == "~" or not dirName ):
70+
if dirName:
71+
dirName = os.path.expanduser(dirName)
72+
else:
73+
dirName = os.getcwd()
74+
75+
if os.access(dirName, os.W_OK) is not True:
76+
dirName = os.getcwd()
77+
print(path +" not writable, writting to "+dirName+ " directory")
78+
return dirName
79+
else:
80+
return dirName
81+
6482
def mooseWriteSBML(modelpath, filename, sceneitems={}):
6583
global foundLibSBML_
6684
msg = " "
@@ -74,6 +92,7 @@ def mooseWriteSBML(modelpath, filename, sceneitems={}):
7492

7593
#sbmlDoc = SBMLDocument(3, 1)
7694
filepath, filenameExt = os.path.split(filename)
95+
filepath = checkPath(filepath)
7796
if filenameExt.find('.') != -1:
7897
filename = filenameExt[:filenameExt.find('.')]
7998
else:
@@ -188,13 +207,14 @@ def mooseWriteSBML(modelpath, filename, sceneitems={}):
188207

189208
if moose.exists(key.path+'/info'):
190209
ginfo = moose.element(key.path+'/info')
210+
textColor,color = getColor(ginfo)
191211
if ginfo.height and ginfo.width:
192212
grpAnno = grpAnno + "<moose:x>" + str(ginfo.x) + "</moose:x>\n"
193213
grpAnno = grpAnno + "<moose:y>" + str(ginfo.y) + "</moose:y>\n"
194214
grpAnno = grpAnno + "<moose:width>" + str(ginfo.width) + "</moose:width>\n"
195215
grpAnno = grpAnno + "<moose:height>" + str(ginfo.height) + "</moose:height>\n"
196216
if ginfo.color:
197-
grpAnno = grpAnno + "<moose:bgColor>" + ginfo.color + "</moose:bgColor>\n"
217+
grpAnno = grpAnno + "<moose:bgColor>" + color + "</moose:bgColor>\n"
198218
if ginfo.notes:
199219
grpAnno = grpAnno + "<moose:Notes>" + ginfo.notes + "</moose:Notes>\n"
200220
grpAnno = grpAnno + "</moose:GroupAnnotation>"
@@ -207,11 +227,16 @@ def mooseWriteSBML(modelpath, filename, sceneitems={}):
207227
consistencyMessages = ""
208228
SBMLok = validateModel(sbmlDoc)
209229
if (SBMLok):
210-
writeTofile = filepath + "/" + filename + '.xml'
230+
if filepath != [" ","\/","/"]:
231+
writeTofile = filepath + "/" + filename + '.xml'
232+
else:
233+
writeTofile = filename+'.xml'
234+
235+
#writeTofile = filepath + "/" + filename + '.xml'
211236
writeSBMLToFile(sbmlDoc, writeTofile)
212237
return True, consistencyMessages, writeTofile
213238

214-
if (not SBMLok):
239+
else:
215240
#cerr << "Errors encountered " << endl
216241
consistencyMessages = "Errors encountered"
217242
return -1, consistencyMessages
@@ -952,6 +977,14 @@ def writeSpecies(modelpath, cremodel_, sbmlDoc, sceneitems,speGroup):
952977
if spename.find(
953978
"cplx") != -1 and isinstance(moose.element(spe.parent), moose.EnzBase):
954979
enz = spe.parent
980+
if not moose.exists(spe.path+'/info'):
981+
cplxinfo = moose.Annotator(spe.path+'/info')
982+
enzpath = moose.element(spe.parent.path+'/info')
983+
984+
cplxinfo.x = moose.element(moose.element(spe.parent.path+'/info').x)
985+
986+
cplxinfo.y = int((moose.element(spe.parent.path+'/info').y))+10
987+
955988
if (moose.element(enz.parent), moose.PoolBase):
956989
# print " found a cplx name ",spe.parent,
957990
# moose.element(spe.parent).parent
@@ -1038,6 +1071,13 @@ def writeSpecies(modelpath, cremodel_, sbmlDoc, sceneitems,speGroup):
10381071
speciAnno = speciAnno + "<moose:motorConstant>" + str(spe.motorConst)+ "</moose:motorConstant>\n"
10391072
speciAnno = speciAnno + "</moose:ModelAnnotation>"
10401073
s1.setAnnotation(speciAnno)
1074+
else:
1075+
#E.g cplx doesn't have info field but findsim layout expecting diffConstant and motorConstant.
1076+
speciAnno = "<moose:ModelAnnotation>\n"
1077+
speciAnno = speciAnno + "<moose:diffConstant>" + str(0.0) + "</moose:diffConstant>\n"
1078+
speciAnno = speciAnno + "<moose:motorConstant>" + str(0.0)+ "</moose:motorConstant>\n"
1079+
speciAnno = speciAnno + "</moose:ModelAnnotation>"
1080+
s1.setAnnotation(speciAnno)
10411081
return True
10421082

10431083

@@ -1205,6 +1245,11 @@ def recalculatecoordinates(modelpath, mObjlist,xcord,ycord):
12051245
else:
12061246
srcdesConnection = {}
12071247
setupItem(modelpath,srcdesConnection)
1248+
meshEntry,xmin,xmax,ymin,ymax,positionInfoExist,sceneitems = setupMeshObj(modelpath)
1249+
if not positionInfoExist:
1250+
sceneitems = autoCoordinates(meshEntry,srcdesConnection)
1251+
sceneitems = autoCoordinates(meshEntry,srcdesConnection)
1252+
12081253
#print srcdesConnection
12091254
'''
12101255
#meshEntry,xmin,xmax,ymin,ymax,positionInfoExist,sceneitems = setupMeshObj(modelpath)

python/moose/chemUtil/chemConnectUtil.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import matplotlib
1818
import os
1919
import random
20-
20+
import re
2121

2222
direct = os.path.dirname(__file__)
2323
colormap_file = open(os.path.join(direct, "rainbow2.pkl"), "r")
@@ -69,7 +69,7 @@ def colorCheck(fc_bgcolor):
6969
if isinstance(fc_bgcolor,str):
7070
if fc_bgcolor.startswith("#"):
7171
fc_bgcolor = fc_bgcolor
72-
elif fc_bgcolor.isdigit():
72+
elif (fc_bgcolor.isdigit()):
7373
""" color is int a map from int to r,g,b triplets from pickled color map file """
7474
tc = (int(fc_bgcolor))*2
7575
if tc < len(colorMap):
@@ -89,7 +89,7 @@ def colorCheck(fc_bgcolor):
8989
hexlist ="#"
9090
for n in fc_bgcolor:
9191
if c < 3:
92-
hexlist = hexlist+str("%02x" % int(n))
92+
#hexlist = hexlist+str("%02x" % int(n))
9393
c = c+1;
9494
fc_bgcolor = hexlist
9595
return(fc_bgcolor)

python/moose/fixXreacs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ def restoreXreacs( basepath ):
188188
args = j.split( ' ' )
189189
assert( len( args ) == 4 )
190190
#_moose.showfield( args[0] )
191-
_moose.connect( args[0], args[1], args[2], args[3] )
191+
#check to see if object exist before moose.connect, cases where object is deleted but
192+
#_xref_ annotation info field still holds the reference
193+
if (_moose.exists(args[0]) and _moose.exists(args[2])):
194+
_moose.connect( args[0], args[1], args[2], args[3] )
192195
#print( "Reconnecting {}".format( args ) )
193196
#_moose.showfield( args[0] )
194197
restoreOldRates( oldRates, msgs )

python/moose/genesis/writeKkit.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,8 @@ def getColorCheck(color,GENESIS_COLOR_SEQUENCE):
664664
index = color
665665
return index
666666
elif isinstance(color, tuple):
667-
color = [int(x) for x in color[0:3]]
667+
#color = [int(x) for x in color[0:3]]
668+
color = map(int,color)[0:3]
668669
index = nearestColorIndex(color, GENESIS_COLOR_SEQUENCE)
669670
return index
670671
elif isinstance(color, int):
@@ -691,8 +692,8 @@ def writeCompartment(modelpath,compts,f):
691692
if compt.name != "kinetics":
692693
x = xmin+6
693694
y = ymax+1
694-
f.write("simundump group /kinetics/" + compt.name + " 0 " + "blue" + " " + "green" + " x 0 0 \"\" defaultfile \\\n" )
695-
f.write( " defaultfile.g 0 0 0 " + str(int(x)) + " " + str(int(y)) + " 0\n")
695+
#f.write("simundump group /kinetics/" + compt.name + " 0 " + "blue" + " " + "green" + " x 0 0 \"\" defaultfile \\\n" )
696+
#f.write( " defaultfile.g 0 0 0 " + str(int(x)) + " " + str(int(y)) + " 0\n")
696697
i = 0
697698
l = len(compts)
698699
geometry = ""

0 commit comments

Comments
 (0)