Program that renames all SIds that also have names specified. The new identifiers will be derived from the name, with all invalid characters removed.
54 class SetIdFromNames(libsbml.IdentifierTransformer):
55 def __init__(self, ids):
57 libsbml.IdentifierTransformer.__init__(self)
59 self.existingIds = ids
63 def transform(self, element):
65 if element
is None or element.getTypeCode() == libsbml.SBML_LOCAL_PARAMETER:
66 return libsbml.LIBSBML_OPERATION_SUCCESS
69 if element.isSetName() ==
False or element.getId() == element.getName():
70 return libsbml.LIBSBML_OPERATION_SUCCESS
73 newId = self.getValidIdForName(element.getName())
79 self.existingIds.append(newId)
81 return libsbml.LIBSBML_OPERATION_SUCCESS
83 def nameToSbmlId(self, name):
88 if '0' <= name[count]
and name[count] <=
'9':
90 for count
in range (0, end):
91 if ((
'0' <= name[count]
and name[count] <=
'9')
or 92 (
'a' <= name[count]
and name[count] <=
'z')
or 93 (
'A' <= name[count]
and name[count] <=
'Z')):
94 IdStream.append(name[count])
97 Id =
''.join(IdStream)
98 if Id[len(Id) - 1] !=
'_':
106 def getValidIdForName(self, name):
107 baseString = self.nameToSbmlId(name)
110 while self.existingIds.count(id) != 0:
111 id =
"{0}_{1}".format(baseString, count)
119 def getAllIds(allElements):
121 if allElements
is None or allElements.getSize() == 0:
124 for i
in range (0, allElements.getSize()):
125 current = allElements.get(i)
126 if current.isSetId()
and current.getTypeCode() != libsbml.SBML_LOCAL_PARAMETER:
127 result.append(current.getId())
132 """Usage: setIdFromNames filename output 142 start = time.time() * 1000
144 stop = time.time() * 1000
147 print (
" filename: {0}".format( filename))
148 print (
" read time (ms): {0}".format( stop - start))
151 errors = document.getNumErrors(libsbml.LIBSBML_SEV_ERROR)
153 print (
" error(s): {0}".format(errors))
154 document.printErrors()
159 allElements = document.getListOfAllElements()
162 allIds = getAllIds(allElements)
165 trans = SetIdFromNames(allIds)
168 start = time.time() * 1000
169 document.getModel().renameIDs(allElements, trans)
170 stop = time.time() * 1000
171 print (
" rename time (ms): {0}".format(stop - start))
174 start = time.time() * 1000
176 stop = time.time() * 1000
177 print (
" write time (ms): {0}".format(stop - start))
182 if __name__ ==
'__main__':