def processFunctions(functions, sbmlfunctions, artificialObservables, tfunc): ''' this method goes through the list of functions and removes all sbml elements that are extraneous to bngl ''' for idx in range(0, len(functions)): for sbml in sbmlfunctions: if sbml in functions[idx]: functions[idx] = writer.extendFunction(functions[idx], sbml, sbmlfunctions[sbml]) functions[idx] = re.sub(r'(\W|^)(time)(\W|$)', r'\1time()\3', functions[idx]) functions[idx] = re.sub(r'(\W|^)(Time)(\W|$)', r'\1time()\3', functions[idx]) functions[idx] = re.sub(r'(\W|^)(t)(\W|$)', r'\1time()\3', functions[idx]) #functions.extend(sbmlfunctions) dependencies2 = {} for idx in range(0, len(functions)): dependencies2[functions[idx].split(' = ')[0].split('(') [0].strip()] = [] for key in artificialObservables: oldfunc = functions[idx] functions[idx] = (re.sub(r'(\W|^)({0})([^\w(]|$)'.format(key), r'\1\2()\3', functions[idx])) if oldfunc != functions[idx]: dependencies2[functions[idx].split(' = ')[0].split('(') [0]].append(key) for element in sbmlfunctions: oldfunc = functions[idx] key = element.split(' = ')[0].split('(')[0] if re.search('(\W|^){0}(\W|$)'.format(key), functions[idx].split(' = ')[1]) != None: dependencies2[functions[idx].split(' = ')[0].split('(') [0]].append(key) for element in tfunc: key = element.split(' = ')[0].split('(')[0] if key in functions[idx].split(' = ')[1]: dependencies2[functions[idx].split(' = ')[0].split('(') [0]].append(key) ''' for counter in range(0,3): for element in dependencies2: if len(dependencies2[element]) > counter: dependencies2[element].extend(dependencies2[dependencies2[element][counter]]) ''' fd = [] for function in functions: #print function,dependencies2[function.split(' = ' )[0].split('(')[0]],function.split(' = ' )[0].split('(')[0],0 fd.append([ function, resolveDependencies(dependencies2, function.split(' = ')[0].split('(')[0], 0) ]) fd = sorted(fd, key=lambda rule: rule[1]) functions = [x[0] for x in fd] return functions
def processFunctions(functions,sbmlfunctions,artificialObservables,tfunc): ''' this method goes through the list of functions and removes all sbml elements that are extraneous to bngl ''' for idx in range(0,len(functions)): for sbml in sbmlfunctions: if sbml in functions[idx]: functions[idx] = writer.extendFunction(functions[idx],sbml,sbmlfunctions[sbml]) functions[idx] =re.sub(r'(\W|^)(time)(\W|$)',r'\1time()\3',functions[idx]) functions[idx] =re.sub(r'(\W|^)(Time)(\W|$)',r'\1time()\3',functions[idx]) functions[idx] =re.sub(r'(\W|^)(t)(\W|$)',r'\1time()\3',functions[idx]) #functions.extend(sbmlfunctions) dependencies2 = {} for idx in range(0,len(functions)): dependencies2[functions[idx].split(' = ')[0].split('(')[0].strip()] = [] for key in artificialObservables: oldfunc = functions[idx] functions[idx] = (re.sub(r'(\W|^)({0})([^\w(]|$)'.format(key),r'\1\2()\3',functions[idx])) if oldfunc != functions[idx]: dependencies2[functions[idx].split(' = ')[0].split('(')[0]].append(key) for element in sbmlfunctions: oldfunc = functions[idx] key = element.split(' = ')[0].split('(')[0] if re.search('(\W|^){0}(\W|$)'.format(key),functions[idx].split(' = ')[1]) != None: dependencies2[functions[idx].split(' = ')[0].split('(')[0]].append(key) for element in tfunc: key = element.split(' = ')[0].split('(')[0] if key in functions[idx].split(' = ')[1]: dependencies2[functions[idx].split( ' = ')[0].split('(')[0]].append(key) ''' for counter in range(0,3): for element in dependencies2: if len(dependencies2[element]) > counter: dependencies2[element].extend(dependencies2[dependencies2[element][counter]]) ''' fd = [] for function in functions: #print function,dependencies2[function.split(' = ' )[0].split('(')[0]],function.split(' = ' )[0].split('(')[0],0 fd.append([function,resolveDependencies(dependencies2,function.split(' = ' )[0].split('(')[0],0)]) fd = sorted(fd,key= lambda rule:rule[1]) functions = [x[0] for x in fd] return functions
def analyzeHelper(document,reactionDefinitions,useID,outputFile,speciesEquivalence,atomize,translator,bioGrid = False): ''' taking the atomized dictionary and a series of data structure, this method does the actual string output. ''' useArtificialRules = False parser =SBML2BNGL(document.getModel(),useID) database = structures.Databases() #translator,log,rdf = m2c.transformMolecules(parser,database,reactionDefinitions,speciesEquivalence) #try: #bioGridDict = {} #if biogrid: # bioGridDict = biogrid() #if atomize: # translator = mc.transformMolecules(parser,database,reactionDefinitions,speciesEquivalence,bioGridDict) #else: # translator={} parser =SBML2BNGL(document.getModel(),useID) #except: # print 'failure' # return None,None,None,None #translator = {} param,zparam = parser.getParameters() molecules,initialConditions,observables,speciesDict = parser.getSpecies(translator,[x.split(' ')[0] for x in param]) #finally, adjust parameters and initial concentrations according to whatever initialassignments say param,zparam,initialConditions = parser.getInitialAssignments(translator,param,zparam,molecules,initialConditions) compartments = parser.getCompartments() functions = [] assigmentRuleDefinedParameters = [] reactionParameters,rules,rateFunctions = parser.getReactions(translator,len(compartments)>1,atomize=atomize) functions.extend(rateFunctions) aParameters,aRules,nonzparam,artificialRules,removeParams,artificialObservables = parser.getAssignmentRules(zparam,param,molecules) for element in nonzparam: param.append('{0} 0'.format(element)) param = [x for x in param if x not in removeParams] tags = '@{0}'.format(compartments[0].split(' ')[0]) if len(compartments) == 1 else '@cell' molecules.extend([x.split(' ')[0] for x in removeParams]) if len(molecules) == 0: compartments = [] observables.extend('Species {0} {0}'.format(x.split(' ')[0]) for x in removeParams) for x in removeParams: initialConditions.append(x.split(' ')[0] + tags + ' ' + x.split(' ')[1]) ##Comment out those parameters that are defined with assignment rules ##TODO: I think this is correct, but it may need to be checked tmpParams = [] for idx,parameter in enumerate(param): for key in artificialObservables: if re.search('^{0}\s'.format(key),parameter)!= None: assigmentRuleDefinedParameters.append(idx) tmpParams.extend(artificialObservables) tmpParams.extend(removeParams) tmpParams = set(tmpParams) correctRulesWithParenthesis(rules,tmpParams) for element in assigmentRuleDefinedParameters: param[element] = '#' + param[element] deleteMolecules = [] deleteMoleculesFlag = True for key in artificialObservables: flag = -1 for idx,observable in enumerate(observables): if 'Species {0} {0}()'.format(key) in observable: flag = idx if flag != -1: observables.pop(flag) functions.append(artificialObservables[key]) flag = -1 if '{0}()'.format(key) in molecules: flag = molecules.index('{0}()'.format(key)) if flag != -1: if deleteMoleculesFlag: deleteMolecules.append(flag) else: deleteMolecules.append(key) #result =validateReactionUsage(molecules[flag],rules) #if result != None: # logMess('ERROR','Pseudo observable {0} in reaction {1}'.format(molecules[flag],result)) #molecules.pop(flag) flag = -1 for idx,specie in enumerate(initialConditions): if ':{0}('.format(key) in specie: flag = idx if flag != -1: initialConditions[flag] = '#' + initialConditions[flag] for flag in sorted(deleteMolecules,reverse=True): if deleteMoleculesFlag: logMess('WARNING:Simulation','{0} reported as function, but usage is ambiguous'.format(molecules[flag]) ) result =validateReactionUsage(molecules[flag],rules) if result != None: logMess('ERROR:Simulation','Pseudo observable {0} in reaction {1}'.format(molecules[flag],result)) molecules.pop(flag) else: logMess('WARNING:Simulation','{0} reported as species, but usage is ambiguous.'.format(flag) ) artificialObservables.pop(flag) functions.extend(aRules) sbmlfunctions = parser.getSBMLFunctions() processFunctions(functions,sbmlfunctions,artificialObservables,rateFunctions) for interation in range(0,3): for sbml2 in sbmlfunctions: for sbml in sbmlfunctions: if sbml == sbml2: continue if sbml in sbmlfunctions[sbml2]: sbmlfunctions[sbml2] = writer.extendFunction(sbmlfunctions[sbml2],sbml,sbmlfunctions[sbml]) functions = reorderFunctions(functions) functions = changeNames(functions,aParameters) # print [x for x in functions if 'functionRate60' in x] functions = unrollFunctions(functions) rules = changeRates(rules,aParameters) if len(compartments) > 1 and 'cell 3 1.0' not in compartments: compartments.append('cell 3 1.0') #sbml always has the 'cell' default compartment, even when it #doesn't declare it elif len(compartments) == 0 and len(molecules) != 0: compartments.append('cell 3 1.0') if len(artificialRules) + len(rules) == 0: logMess('ERROR:Simulation','The file contains no reactions') if useArtificialRules or len(rules) == 0: rules =['#{0}'.format(x) for x in rules] evaluate = evaluation(len(observables),translator) artificialRules.extend(rules) rules = artificialRules else: artificialRules =['#{0}'.format(x) for x in artificialRules] evaluate = evaluation(len(observables),translator) rules.extend(artificialRules) commentDictionary = {} if atomize: commentDictionary['notes'] = "'This is an atomized translation of an SBML model created on {0}.".format(time.strftime("%d/%m/%Y")) else: commentDictionary['notes'] = "'This is a plain translation of an SBML model created on {0}.".format(time.strftime("%d/%m/%Y")) commentDictionary['notes'] += " The original model has {0} molecules and {1} reactions. The translated model has {2} molecules and {3} rules'".format(parser.model.getNumSpecies(),parser.model.getNumReactions(),len(molecules),len(set(rules))) meta = parser.getMetaInformation(commentDictionary) from collections import OrderedDict finalString = writer.finalText(meta,param+reactionParameters,molecules,initialConditions,list(OrderedDict.fromkeys(observables)),list(OrderedDict.fromkeys(rules)),functions,compartments,outputFile) #print outputFile logMess('INFO:Summary','File contains {0} molecules out of {1} original SBML species'.format(len(molecules),len(observables))) #store a logfile try: if len(logMess.log) > 0: with open(outputFile + '.log', 'w') as f: for element in logMess.log: f.write(element + '\n') except AttributeError: print "error" except IOError: pass #print "" #rate of each classified rule evaluate2 = 0 if len(observables) == 0 else len(molecules)*1.0/len(observables) return len(rules),len(observables),evaluate,evaluate2,len(compartments), parser.getSpeciesAnnotation(),finalString,speciesDict '''
def analyzeFile(bioNumber,reactionDefinitions,useID,outputFile,speciesEquivalence=None): useArtificialRules = False reader = libsbml.SBMLReader() document = reader.readSBMLFromFile('XMLExamples/curated/BIOMD%010i.xml' % bioNumber) parser =SBML2BNGL(document.getModel(),useID) database = structures.Databases() try: translator,log = m2c.transformMolecules(parser,database,reactionDefinitions,speciesEquivalence) #translator={} except: print 'failure' return None,None #translator = {} param,zparam = parser.getParameters() molecules,species,observables = parser.getSpecies(translator) compartments = parser.getCompartments() functions = [] idxArray = [] _,rules,tfunc = parser.getReactions(translator,True) functions.extend(tfunc) aParameters,aRules,nonzparam,artificialRules,removeParams,artificialObservables = parser.getAssignmentRules(zparam,param,molecules) for element in nonzparam: param.append('{0} 0'.format(element)) param = [x for x in param if x not in removeParams] tags = '@{0}'.format(compartments[0].split(' ')[0]) if len(compartments) == 1 else '@cell' molecules.extend([x.split(' ')[0] for x in removeParams]) if len(molecules) == 0: compartments = [] observables.extend('Species {0} {0}'.format(x.split(' ')[0]) for x in removeParams) for x in removeParams: species.append(x.split(' ')[0] + tags + ' ' + x.split(' ')[1]) ##Comment out those parameters that are defined with assignment rules ##TODO: I think this is correct, but it may need to be checked for idx,parameter in enumerate(param): for key in artificialObservables: if re.search('^{0}\s'.format(key),parameter)!= None: idxArray.append(idx) for element in idxArray: param[element] = '#' + param[element] for key in artificialObservables: flag = -1 for idx,observable in enumerate(observables): if 'Species {0} {0}()'.format(key) in observable: flag = idx if flag != -1: observables.pop(flag) functions.append(artificialObservables[key]) flag = -1 if '{0}()'.format(key) in molecules: flag = molecules.index('{0}()'.format(key)) if flag != -1: molecules.pop(flag) flag = -1 for idx,specie in enumerate(species): if ':{0}('.format(key) in specie: flag = idx if flag != -1: species[flag] = '#' + species[flag] functions.extend(aRules) sbmlfunctions = parser.getSBMLFunctions() for interation in range(0,3): for sbml2 in sbmlfunctions: for sbml in sbmlfunctions: if sbml == sbml2: continue if sbml in sbmlfunctions[sbml2]: sbmlfunctions[sbml2] = writer.extendFunction(sbmlfunctions[sbml2],sbml,sbmlfunctions[sbml]) for idx in range(0,len(functions)): for sbml in sbmlfunctions: if sbml in functions[idx]: functions[idx] = writer.extendFunction(functions[idx],sbml,sbmlfunctions[sbml]) #functions.extend(sbmlfunctions) dependencies2 = {} for idx in range(0,len(functions)): dependencies2[functions[idx].split(' = ')[0].split('(')[0].strip()] = [] for key in artificialObservables: oldfunc = functions[idx] functions[idx] = (re.sub(r'(\W|^)({0})([^\w(]|$)'.format(key),r'\1\2()\3',functions[idx])) if oldfunc != functions[idx]: dependencies2[functions[idx].split(' = ')[0].split('(')[0]].append(key) for element in sbmlfunctions: oldfunc = functions[idx] key = element.split(' = ')[0].split('(')[0] if re.search('(\W|^){0}(\W|$)'.format(key),functions[idx].split(' = ')[1]) != None: dependencies2[functions[idx].split(' = ')[0].split('(')[0]].append(key) for element in tfunc: key = element.split(' = ')[0].split('(')[0] if key in functions[idx].split(' = ')[1]: dependencies2[functions[idx].split( ' = ')[0].split('(')[0]].append(key) ''' for counter in range(0,3): for element in dependencies2: if len(dependencies2[element]) > counter: dependencies2[element].extend(dependencies2[dependencies2[element][counter]]) ''' fd = [] for function in functions: fd.append([function,resolveDependencies(dependencies2,function.split(' = ' )[0].split('(')[0],0)]) fd = sorted(fd,key= lambda rule:rule[1]) functions = [x[0] for x in fd] if len(param) == 0: param.append('dummy 0') #functions.extend(aRules) if len(compartments) > 1 and 'cell 3 1.0' not in compartments: compartments.append('cell 3 1.0') if len(artificialRules) + len(rules) == 0: logMess('ERROR','The file contains no reactions') if useArtificialRules or len(artificialRules) > 0: rules =['#{0}'.format(x) for x in rules] evaluate = evaluation(len(artificialRules),translator) artificialRules.extend(rules) writer.finalText(param,molecules,species,observables,artificialRules,functions,compartments,outputFile) else: artificialRules =['#{0}'.format(x) for x in artificialRules] evaluate = evaluation(len(rules),translator) rules.extend(artificialRules) writer.finalText(param,molecules,species,observables,rules,functions,compartments,outputFile) print outputFile if len(logMess.log) > 0: with open(outputFile + '.log', 'w') as f: for element in logMess.log: f.write(element + '\n') return len(rules), evaluate
def analyzeHelper(document, reactionDefinitions, useID, outputFile, speciesEquivalence, atomize, translator, bioGrid=False): ''' taking the atomized dictionary and a series of data structure, this method does the actual string output. ''' useArtificialRules = False parser = SBML2BNGL(document.getModel(), useID) database = structures.Databases() #translator,log,rdf = m2c.transformMolecules(parser,database,reactionDefinitions,speciesEquivalence) #try: #bioGridDict = {} #if biogrid: # bioGridDict = biogrid() #if atomize: # translator = mc.transformMolecules(parser,database,reactionDefinitions,speciesEquivalence,bioGridDict) #else: # translator={} parser = SBML2BNGL(document.getModel(), useID) #except: # print 'failure' # return None,None,None,None #translator = {} param, zparam = parser.getParameters() molecules, initialConditions, observables, speciesDict = parser.getSpecies( translator, [x.split(' ')[0] for x in param]) #finally, adjust parameters and initial concentrations according to whatever initialassignments say param, zparam, initialConditions = parser.getInitialAssignments( translator, param, zparam, molecules, initialConditions) compartments = parser.getCompartments() functions = [] assigmentRuleDefinedParameters = [] reactionParameters, rules, rateFunctions = parser.getReactions( translator, len(compartments) > 1, atomize=atomize) functions.extend(rateFunctions) aParameters, aRules, nonzparam, artificialRules, removeParams, artificialObservables = parser.getAssignmentRules( zparam, param, molecules) for element in nonzparam: param.append('{0} 0'.format(element)) param = [x for x in param if x not in removeParams] tags = '@{0}'.format( compartments[0].split(' ')[0]) if len(compartments) == 1 else '@cell' molecules.extend([x.split(' ')[0] for x in removeParams]) if len(molecules) == 0: compartments = [] observables.extend('Species {0} {0}'.format(x.split(' ')[0]) for x in removeParams) for x in removeParams: initialConditions.append( x.split(' ')[0] + tags + ' ' + x.split(' ')[1]) ##Comment out those parameters that are defined with assignment rules ##TODO: I think this is correct, but it may need to be checked tmpParams = [] for idx, parameter in enumerate(param): for key in artificialObservables: if re.search('^{0}\s'.format(key), parameter) != None: assigmentRuleDefinedParameters.append(idx) tmpParams.extend(artificialObservables) tmpParams.extend(removeParams) tmpParams = set(tmpParams) correctRulesWithParenthesis(rules, tmpParams) for element in assigmentRuleDefinedParameters: param[element] = '#' + param[element] deleteMolecules = [] deleteMoleculesFlag = True for key in artificialObservables: flag = -1 for idx, observable in enumerate(observables): if 'Species {0} {0}()'.format(key) in observable: flag = idx if flag != -1: observables.pop(flag) functions.append(artificialObservables[key]) flag = -1 if '{0}()'.format(key) in molecules: flag = molecules.index('{0}()'.format(key)) if flag != -1: if deleteMoleculesFlag: deleteMolecules.append(flag) else: deleteMolecules.append(key) #result =validateReactionUsage(molecules[flag],rules) #if result != None: # logMess('ERROR','Pseudo observable {0} in reaction {1}'.format(molecules[flag],result)) #molecules.pop(flag) flag = -1 for idx, specie in enumerate(initialConditions): if ':{0}('.format(key) in specie: flag = idx if flag != -1: initialConditions[flag] = '#' + initialConditions[flag] for flag in sorted(deleteMolecules, reverse=True): if deleteMoleculesFlag: logMess( 'WARNING:Simulation', '{0} reported as function, but usage is ambiguous'.format( molecules[flag])) result = validateReactionUsage(molecules[flag], rules) if result != None: logMess( 'ERROR:Simulation', 'Pseudo observable {0} in reaction {1}'.format( molecules[flag], result)) molecules.pop(flag) else: logMess( 'WARNING:Simulation', '{0} reported as species, but usage is ambiguous.'.format( flag)) artificialObservables.pop(flag) functions.extend(aRules) sbmlfunctions = parser.getSBMLFunctions() processFunctions(functions, sbmlfunctions, artificialObservables, rateFunctions) for interation in range(0, 3): for sbml2 in sbmlfunctions: for sbml in sbmlfunctions: if sbml == sbml2: continue if sbml in sbmlfunctions[sbml2]: sbmlfunctions[sbml2] = writer.extendFunction( sbmlfunctions[sbml2], sbml, sbmlfunctions[sbml]) functions = reorderFunctions(functions) functions = changeNames(functions, aParameters) # print [x for x in functions if 'functionRate60' in x] functions = unrollFunctions(functions) rules = changeRates(rules, aParameters) if len(compartments) > 1 and 'cell 3 1.0' not in compartments: compartments.append('cell 3 1.0') #sbml always has the 'cell' default compartment, even when it #doesn't declare it elif len(compartments) == 0 and len(molecules) != 0: compartments.append('cell 3 1.0') if len(artificialRules) + len(rules) == 0: logMess('ERROR:Simulation', 'The file contains no reactions') if useArtificialRules or len(rules) == 0: rules = ['#{0}'.format(x) for x in rules] evaluate = evaluation(len(observables), translator) artificialRules.extend(rules) rules = artificialRules else: artificialRules = ['#{0}'.format(x) for x in artificialRules] evaluate = evaluation(len(observables), translator) rules.extend(artificialRules) commentDictionary = {} if atomize: commentDictionary[ 'notes'] = "'This is an atomized translation of an SBML model created on {0}.".format( time.strftime("%d/%m/%Y")) else: commentDictionary[ 'notes'] = "'This is a plain translation of an SBML model created on {0}.".format( time.strftime("%d/%m/%Y")) commentDictionary[ 'notes'] += " The original model has {0} molecules and {1} reactions. The translated model has {2} molecules and {3} rules'".format( parser.model.getNumSpecies(), parser.model.getNumReactions(), len(molecules), len(set(rules))) meta = parser.getMetaInformation(commentDictionary) from collections import OrderedDict finalString = writer.finalText(meta, param + reactionParameters, molecules, initialConditions, list(OrderedDict.fromkeys(observables)), list(OrderedDict.fromkeys(rules)), functions, compartments, outputFile) #print outputFile logMess( 'INFO:Summary', 'File contains {0} molecules out of {1} original SBML species'.format( len(molecules), len(observables))) #store a logfile try: if len(logMess.log) > 0: with open(outputFile + '.log', 'w') as f: for element in logMess.log: f.write(element + '\n') except AttributeError: print "error" except IOError: pass #print "" #rate of each classified rule evaluate2 = 0 if len( observables) == 0 else len(molecules) * 1.0 / len(observables) return len(rules), len(observables), evaluate, evaluate2, len( compartments), parser.getSpeciesAnnotation(), finalString, speciesDict '''