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 ==
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);
120 def getAllIds(allElements):
122 if (allElements ==
None or allElements.getSize() == 0):
125 for i
in range (0, allElements.getSize()):
126 current = allElements.get(i);
127 if (current.isSetId()
and current.getTypeCode() != libsbml.SBML_LOCAL_PARAMETER):
128 result.append(current.getId());
134 """Usage: setIdFromNames filename output
144 start = time.time() * 1000;
146 stop = time.time() * 1000;
150 print " filename: {0}".format( filename);
151 print " read time (ms): {0}".format( stop - start);
154 errors = document.getNumErrors(libsbml.LIBSBML_SEV_ERROR);
156 print " error(s): {0}".format(errors);
157 document.printErrors();
163 allElements = document.getListOfAllElements();
166 allIds = getAllIds(allElements);
169 trans = SetIdFromNames(allIds);
172 start = time.time() * 1000;
173 document.getModel().renameIDs(allElements, trans);
174 stop = time.time() * 1000;
175 print " rename time (ms): {0}".format(stop - start);
178 start = time.time() * 1000;
180 stop = time.time() * 1000;
181 print " write time (ms): {0}".format(stop - start);
186 if __name__ ==
'__main__':