def getAssignmentRules(self,paramRules,parameters,molecules): compartmentList = [['cell',1]] compartmentList.extend([[self.__getRawCompartments(x)[0],self.__getRawCompartments(x)[2]] for x in self.model.getListOfCompartments()]) arules = [] aParameters = {} zRules = paramRules removeParameters = [] newSeedSpecies = [] artificialReactions = [] artificialObservables = {} for arule in self.model.getListOfRules(): rawArule = self.__getRawAssignmentRules(arule) #newRule = rawArule[1].replace('+',',').strip() if rawArule[3] == True: rateLaw1 = rawArule[1][0] rateLaw2 = rawArule[1][1] arules.append(writer.bnglFunction(rateLaw1,'arRate{0}'.format(rawArule[0]),[],compartments=compartmentList,reactionDict=self.reactionDictionary)) arules.append(writer.bnglFunction(rateLaw2,'armRate{0}'.format(rawArule[0]),[],compartments=compartmentList,reactionDict=self.reactionDictionary)) artificialReactions.append(writer.bnglReaction([],[[rawArule[0],1]],'{0},{1}'.format('arRate{0}'.format(rawArule[0]),'armRate{0}'.format(rawArule[0])),self.tags,{},isCompartments=True,comment = '#rateLaw')) if rawArule[0] in paramRules: removeParameters.append('{0} 0'.format(rawArule[0])) zRules.remove(rawArule[0]) else: for element in parameters: if re.search('^{0}\s'.format(rawArule[0]),element): removeParameters.append(element) elif rawArule[2] == True: artificialObservables[rawArule[0]] = writer.bnglFunction(rawArule[1][0],rawArule[0]+'()',[],compartments=compartmentList,reactionDict=self.reactionDictionary) if rawArule[0] in zRules: zRules.remove(rawArule[0]) #if rawArule[0] in paramRules: # removeParameters.append('{0} 0'.format(rawArule[0])) else: if rawArule[0] not in paramRules: ruleName = 'ar' + rawArule[0] else: ruleName = rawArule[0] zRules.remove(rawArule[0]) arules.append(writer.bnglFunction(rawArule[1][0],ruleName,[],compartments=compartmentList,reactionDict=self.reactionDictionary)) aParameters[rawArule[0]] = 'ar' + rawArule[0] ''' elif rawArule[2] == True: for parameter in parameters: if re.search('^{0}\s'.format(rawArule[0]),parameter): print '////',rawArule[0] ''' #arules.append('%s = %s' %(rawArule[0],newRule)) return aParameters,arules,zRules,artificialReactions,removeParameters,artificialObservables
def getReactions(self, translator=[],isCompartments=False,extraParameters={}): ''' returns a triple containing the parameters,rules,functions ''' rules = [] parameters = [] functions = [] tester = NumericStringParser() functionTitle = 'functionRate' for index,reaction in enumerate(self.model.getListOfReactions()): parameterDict = {} rawRules = self.__getRawRules(reaction) #newRate = self.updateFunctionReference(rawRules,extraParameters) if len(rawRules[2]) >0: for parameter in rawRules[2]: parameters.append('%s %f' % (parameter[0],parameter[1])) parameterDict[parameter[0]] = parameter[1] compartmentList = [['cell',1]] compartmentList.extend([[self.__getRawCompartments(x)[0],self.__getRawCompartments(x)[2]] for x in self.model.getListOfCompartments()]) functionName = '%s%d()' % (functionTitle,index) if 'delay' in rawRules[3][0]: logMess('ERROR','BNG cannot handle delay functions in function %s' % functionName) if rawRules[4]: functions.append(writer.bnglFunction(rawRules[3][0],functionName,rawRules[0],compartmentList,parameterDict,self.reactionDictionary)) functionName2 = '%s%dm()' % (functionTitle,index) functions.append(writer.bnglFunction(rawRules[3][1],functionName2,rawRules[0],compartmentList,parameterDict,self.reactionDictionary)) self.reactionDictionary[rawRules[5]] = '({0} - {1})'.format(functionName, functionName2) functionName = '{0},{1}'.format(functionName,functionName2) else: functions.append(writer.bnglFunction(rawRules[3][0],functionName,rawRules[0],compartmentList,parameterDict,self.reactionDictionary)) self.reactionDictionary[rawRules[5]] = '{0}'.format(functionName) rules.append(writer.bnglReaction(rawRules[0],rawRules[1],functionName,self.tags,translator,isCompartments,rawRules[4])) return parameters, rules,functions
def getReactions(self): rules = [] parameters = [] functions = [] functionTitle = 'functionRate' for index,reaction in enumerate(self.model.getListOfReactions()): rawRules = self.__getRawRules(reaction) functionName = '%s%d()' % (functionTitle,index) rules.append(writer.bnglReaction(rawRules[0],rawRules[1],functionName)) if len(rawRules[2]) >0: parameters.append('%s %f' % (rawRules[2][0][0],rawRules[2][0][1])) functions.append(writer.bnglFunction(rawRules[3],functionName)) return parameters, rules,functions
def getAssignmentRules(self, zparams, parameters, molecules): ''' this method obtains an SBML rate rules and assignment rules. They require special handling since rules are often both defined as rules and parameters initialized as 0, so they need to be removed from the parameters list ''' compartmentList = [['cell',1]] compartmentList.extend([[self.__getRawCompartments(x)[0], self.__getRawCompartments(x)[2]] for x in self.model.getListOfCompartments()]) arules = [] aParameters = {} zRules = zparams removeParameters = [] artificialReactions = [] artificialObservables = {} for arule in self.model.getListOfRules(): rawArule = self.__getRawAssignmentRules(arule) #tmp.remove(rawArule[0]) #newRule = rawArule[1].replace('+',',').strip() if rawArule[3] == True: #it is an rate rule if rawArule[0] in self.boundaryConditionVariables: aParameters[rawArule[0]] = 'arj' + rawArule[0] tmp = list(rawArule) tmp[0] = 'arj' + rawArule[0] rawArule = tmp rateLaw1 = rawArule[1][0] rateLaw2 = rawArule[1][1] arules.append(writer.bnglFunction(rateLaw1, 'arRate{0}'.format(rawArule[0]),[],compartments=compartmentList, reactionDict=self.reactionDictionary)) arules.append(writer.bnglFunction(rateLaw2, 'armRate{0}'.format(rawArule[0]),[],compartments=compartmentList, reactionDict=self.reactionDictionary)) artificialReactions.append(writer.bnglReaction([], [[rawArule[0],1]],'{0},{1}'.format('arRate{0}'.format(rawArule[0]), 'armRate{0}'.format(rawArule[0])), self.tags, {}, isCompartments=True, comment = '#rateLaw')) #arules.append(writer.bnglFunction('({0}) - ({1})'.format(rawArule[1][0],rawArule[1][1]), '{0}'.format(rawArule[0]),[],compartments=compartmentList, reactionDict=self.reactionDictionary)) if rawArule[0] in zparams: removeParameters.append('{0} 0'.format(rawArule[0])) zRules.remove(rawArule[0]) else: for element in parameters: #TODO: if for whatever reason a rate rule #was defined as a parameter that is not 0 #remove it. This might not be exact behavior logMess("WARNING","A name corresponds both as a non zero parameter \ and a rate rule, verify behavior") if re.search('^{0}\s'.format(rawArule[0]), element): removeParameters.append(element) elif rawArule[2] == True: #it is an assigment rule if rawArule[0] in zRules: zRules.remove(rawArule[0]) if rawArule[0] in self.boundaryConditionVariables: aParameters[rawArule[0]] = 'arj' + rawArule[0] tmp = list(rawArule) tmp[0] = 'arj' + rawArule[0] rawArule= tmp artificialObservables[rawArule[0]] = writer.bnglFunction(rawArule[1][0],rawArule[0]+'()',[],compartments=compartmentList,reactionDict=self.reactionDictionary) else: ''' if for whatever reason you have a rule that is not assigment or rate and it is initialized as a non zero parameter, give it a new name ''' if rawArule[0] not in zparams: ruleName = 'ar' + rawArule[0] else: ruleName = rawArule[0] zRules.remove(rawArule[0]) arules.append(writer.bnglFunction(rawArule[1][0],ruleName,[],compartments=compartmentList,reactionDict=self.reactionDictionary)) aParameters[rawArule[0]] = 'ar' + rawArule[0] ''' elif rawArule[2] == True: for parameter in parameters: if re.search('^{0}\s'.format(rawArule[0]),parameter): print '////',rawArule[0] ''' #arules.append('%s = %s' %(rawArule[0],newRule)) return aParameters,arules,zRules,artificialReactions,removeParameters,artificialObservables
def getReactions(self, translator={}, isCompartments=False, extraParameters={},atomize=False): ''' @returns: a triple containing the parameters,rules,functions ''' ##@FIXME:this part of the code is there so that we only generate the functions list once through different #iterations of this call. This is because we cannot create a clone of the 'math' object for this #reaction and it is being permanently changed every call. It's ugly but it works. Change for something #better when we figure out how to clone the math object if not hasattr(self.getReactions,'functionFlag'): self.getReactions.__func__.functionFlag = False or (not atomize) rules = [] parameters = [] functions = [] functionTitle = 'functionRate' for index, reaction in enumerate(self.model.getListOfReactions()): parameterDict = {} #symmetry factors for components with the same name sl,sr = self.reduceComponentSymmetryFactors(reaction,translator,functions) rawRules = self.__getRawRules(reaction,[sl,sr],self.getReactions.functionFlag) if len(rawRules['parameters']) >0: for parameter in rawRules['parameters']: parameters.append('r%d_%s %f' % (index+1, parameter[0], parameter[1])) parameterDict[parameter[0]] = parameter[1] compartmentList = [['cell',1]] compartmentList.extend([[self.__getRawCompartments(x)[0],self.__getRawCompartments(x)[2]] for x in self.model.getListOfCompartments()]) threshold = 0 if rawRules['numbers'][0] > threshold: functionName = '%s%d()' % (functionTitle,index) else: #append reactionNumbers to parameterNames finalString = str(rawRules['rates'][0]) for parameter in parameterDict: finalString = re.sub(r'(\W|^)({0})(\W|$)'.format(parameter), r'\1{0}\3'.format('r{0}_{1}'.format(index+1,parameter)), finalString) functionName = finalString if self.getReactions.functionFlag and 'delay' in rawRules['rates'][0]: logMess('ERROR','BNG cannot handle delay functions in function %s' % functionName) if rawRules['reversible']: if rawRules['numbers'][0] > threshold: if self.getReactions.functionFlag: functions.append(writer.bnglFunction(rawRules['rates'][0], functionName, rawRules['reactants'], compartmentList, parameterDict, self.reactionDictionary)) if rawRules['numbers'][1] > threshold: functionName2 = '%s%dm()' % (functionTitle,index) if self.getReactions.functionFlag: functions.append(writer.bnglFunction(rawRules['rates'][1],functionName2,rawRules['reactants'],compartmentList,parameterDict,self.reactionDictionary)) self.reactionDictionary[rawRules['reactionID']] = '({0} - {1})'.format(functionName, functionName2) functionName = '{0},{1}'.format(functionName, functionName2) else: finalString = str(rawRules['rates'][1]) for parameter in parameterDict: finalString = re.sub(r'(\W|^)({0})(\W|$)'.format(parameter),r'\1{0}\3'.format('r{0}_{1}'.format(index+1,parameter)),finalString) functionName = '{0},{1}'.format(functionName,finalString) else: if rawRules['numbers'][0] > threshold: if self.getReactions.functionFlag: functions.append(writer.bnglFunction(rawRules['rates'][0], functionName, rawRules['reactants'], compartmentList, parameterDict,self.reactionDictionary)) self.reactionDictionary[rawRules['reactionID']] = '{0}'.format(functionName) #reactants = [x for x in rawRules[0] if x[0] not in self.boundaryConditionVariables] #products = [x for x in rawRules[1] if x[0] not in self.boundaryConditionVariables] reactants = [x for x in rawRules['reactants']] products = [x for x in rawRules['products']] rules.append(writer.bnglReaction(reactants,products,functionName,self.tags,translator,(isCompartments or ((len(reactants) == 0 or len(products) == 0) and self.getReactions.__func__.functionFlag)),rawRules['reversible'],reactionName=rawRules['reactionID'])) if atomize: self.getReactions.__func__.functionFlag = not self.getReactions.functionFlag return parameters, rules,functions
def getSBMLFunctions(self): functions = {} for function in enumerate(self.model.getListOfFunctionDefinitions()): functionInfo = self.__getRawFunctions(function) functions[functionInfo[0]] = (writer.bnglFunction(functionInfo[1],functionInfo[0],[],reactionDict=self.reactionDictionary)) return functions