def main(): fileName = 'complex/output19.bngl' console.bngl2xml(fileName) species, rules, par = readBNGXML.parseXML('output19.xml') #print rules transformationCenter = [] transformationContext = [] #extract the context of such reactions for idx, rule in enumerate(rules): tatomicArray, ttransformationCenter, ttransformationContext, \ tproductElements,tactionNames,tlabelArray = extractAtomic.extractTransformations([rule]) #atomicArray.append(tatomicArray) transformationCenter.append(ttransformationCenter) transformationContext.append(ttransformationContext) redundantDict, patternDictList = extractRedundantContext( rules, transformationCenter, transformationContext) #print redundantDict #construct rule based patterns based on the reaction patterns they have to match and #the ones they have to discriminate for center in patternDictList: for rate in patternDictList[center]: match = patternDictList[center][rate] notContext = [] for cRate in [x for x in patternDictList[center] if x != rate]: notContext.append([ transformationContext[x] for x in redundantDict[center][cRate] ]) ruleSet = [rules[x] for x in redundantDict[center][rate]] createMetaRule(ruleSet, match) newRules = range(0, len(rules)) for center in redundantDict: for context in redundantDict[center]: for element in range(1, len(redundantDict[center][context])): newRules.remove(redundantDict[center][context][element]) #for element in newRules: # print str(rules[element][0]) newRulesArray = [] for element in newRules: newRulesArray.append('{0}\n'.format(str(rules[element][0]))) lines = readFile(fileName) startR = lines.index('begin reaction rules\n') endR = lines.index('end reaction rules\n') startP = lines.index('begin parameters\n') endP = lines.index('end parameters\n') newPar = findNewParameters(lines[startP + 1:endP], par) newLines = lines[0:endP] + newPar + lines[ endP:startR + 1] + newRulesArray + lines[endR:len(lines)] f = open(fileName + 'reduced.bngl', 'w') f.writelines(newLines) '''
def main(): fileName = 'complex/output19.bngl' console.bngl2xml(fileName) species,rules,par= readBNGXML.parseXML('output19.xml') #print rules transformationCenter = [] transformationContext = [] #extract the context of such reactions for idx,rule in enumerate(rules): tatomicArray, ttransformationCenter, ttransformationContext, \ tproductElements,tactionNames,tlabelArray = extractAtomic.extractTransformations([rule]) #atomicArray.append(tatomicArray) transformationCenter.append(ttransformationCenter) transformationContext.append(ttransformationContext) print transformationCenter[6] redundantDict,patternDictList = extractRedundantContext(rules,transformationCenter,transformationContext) print redundantDict[(('Prot(iMod~U)',), ('Prot(egfr)', 'EGFR(prot)'))] #print redundantDict #construct rule based patterns based on the reaction patterns they have to match and #the ones they have to discriminate for center in patternDictList: for rate in patternDictList[center]: match = patternDictList[center][rate] notContext = [] for cRate in [x for x in patternDictList[center] if x != rate]: notContext.append([transformationContext[x] for x in redundantDict[center][cRate]]) ruleSet = [rules[x] for x in redundantDict[center][rate]] createMetaRule(ruleSet,match) newRules = range(0,len(rules)) for center in redundantDict: for context in redundantDict[center]: for element in range(1,len(redundantDict[center][context])): newRules.remove(redundantDict[center][context][element]) #for element in newRules: # print str(rules[element][0]) newRulesArray = [] for element in newRules: newRulesArray.append('{0}\n'.format(str(rules[element][0]))) lines = readFile(fileName) startR = lines.index('begin reaction rules\n') endR = lines.index('end reaction rules\n') startP = lines.index('begin parameters\n') endP = lines.index('end parameters\n') newPar = findNewParameters(lines[startP+1:endP],par) newLines = lines[0:endP] + newPar + lines[endP:startR+1] + newRulesArray + lines[endR:len(lines)] f = open(fileName + 'reduced.bngl','w') f.writelines(newLines) '''
def createDataStructures(bnglContent): ''' create an atomized biomodels in a temporary file to obtain relevant bng information ''' pointer = tempfile.mkstemp(suffix='.bngl', text=True) with open(pointer[1], 'w') as f: f.write(bnglContent) retval = os.getcwd() os.chdir(tempfile.tempdir) consoleCommands.bngl2xml(pointer[1]) xmlfilename = '.'.join(pointer[1].split('.')[0:-1]) + '.xml' os.chdir(retval) return readBNGXML.parseXML(xmlfilename)
def createDataStructures(bnglContent): ''' create an atomized biomodels in a temporary file to obtain relevant bng information ''' pointer = tempfile.mkstemp(suffix='.bngl',text=True) with open(pointer[1],'w') as f: f.write(bnglContent) retval = os.getcwd() os.chdir(tempfile.tempdir) consoleCommands.bngl2xml(pointer[1]) xmlfilename = '.'.join(pointer[1].split('.')[0:-1]) + '.xml' os.chdir(retval) return readBNGXML.parseXML(xmlfilename)
def createDataStructures(bnglContent): """ create an atomized biomodels in a temporary file to obtain relevant bng information """ pointer = tempfile.mkstemp(suffix=".bngl", text=True) with open(pointer[1], "w") as f: f.write(bnglContent) retval = os.getcwd() os.chdir(tempfile.tempdir) consoleCommands.bngl2xml(pointer[1]) xmlfilename = ".".join(pointer[1].split(".")[0:-1]) + ".xml" os.chdir(retval) return readBNGXML.parseXML(xmlfilename)
def spaceCoveredCDF(): spaceCoveredArray = [] atomizationDict = {} with open("sortedD.dump", "rb") as f: atomizationStats = pickle.load(f) for element in atomizationStats: try: atomizationDict[element["index"]] = element["atomization"] except: continue for element in range(1, 491): try: if element in atomizationDict and atomizationDict[element] > 0: console.bngl2xml("complex/output{0}.bngl".format(element), timeout=10) species, _, _ = readBNGXML.parseXML("output{0}.xml".format(element)) observablesLen = readBNGXML.getNumObservablesXML("output{0}.xml".format(element)) try: spaceCoveredArray.append([atomizationDict[element], spaceCovered(species, observablesLen)]) print element, spaceCoveredArray[-1] except CycleError: spaceCoveredArray.append([atomizationDict[element], 0]) print element, -1 except (IOError, IndexError): print continue with open("spaceCovered.dump", "wb") as f: pickle.dump(spaceCoveredArray, f) atomization, space = zip(*spaceCoveredArray) heatmap, xedges, yedges = np.histogram2d(space, atomization, bins=8) extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] plt.clf() plt.imshow(heatmap, extent=extent) plt.xlabel("space") plt.ylabel("atomization") plt.show() plt.savefig("spaceCovered.png")
def extractStatistics(): number = 151 console.bngl2xml('complex/output{0}.bngl'.format(number)) species,rules,parameterDict= readBNGXML.parseXML('output{0}.xml'.format(number)) #print rules transformationCenter = [] transformationContext = [] k = [] actions = Counter() actionSet = Counter() for idx,rule in enumerate(rules): tatomicArray, ttransformationCenter, ttransformationContext, \ tproductElements,tactionNames,tlabelArray = extractAtomic.extractTransformations([rule]) #atomicArray.append(tatomicArray) transformationCenter.append(ttransformationCenter) transformationContext.append(ttransformationContext) k.append(len(rule[0].actions)) #if len(rule[0].actions) > 3: # print rule[0].reactants actions.update([x.action for x in rule[0].actions]) tmp = [x.action for x in rule[0].actions] tmp.sort() actionSet.update([tuple(tmp)]) #print actions #print actionSet print 'number of species',len(species) print 'avg number o actions',np.average(k),np.std(k) centerDict = groupByReactionCenter(transformationCenter) print 'singletons',len({x:centerDict[x] for x in centerDict if len(centerDict[x]) == 1}) tmp = [[tuple(set(x)),len(centerDict[x])] for x in centerDict] #reactionCenterGraph(species,tmp) #tmp.sort(key=lambda x:x[1],reverse=True) print 'number of reaction centers',len(centerDict.keys()) print 'number of rules',len(rules) #print 'unique',[x for x in centerDict[element] if len(centerDict[element]) == 1] redundantDict = groupByReactionCenterAndRateAndActions(rules,centerDict) #print redundantDict tmp2 = [('$\\tt{{{0}}}$'.format(tuple(set(x))),tuple(set(y[:-1])),y[-1],len (redundantDict[x][y])) for x in redundantDict for y in redundantDict[x] if 'kr' not in y[-1]] tmp2 = set(tmp2) tmp2 = list(tmp2) tmp2.sort(key=lambda x:x[3],reverse=True) tmp2.sort(key=lambda x:x[0],reverse=True) tmp2 = ['{0} & {1} & {2} & {3}\\\\\n'.format(element[0],element[1],element[2],element[3]) for element in tmp2] with open('table.tex','w') as f: f.write('\\begin{tabular}{|cccc|}\n') f.write('\\hline\n') f.write('Reaction Centers & Action & Score\\\\\\hline\n') for element in tmp2: f.write(element) f.write('\\hline\n') f.write('\\end{tabular}\n') #print redundantDict x = [len(centerDict[x]) for x in centerDict] #122,138 print 'average number of reactions with same rate and reaction cneter',np.average(x),np.std(x) print 'total number of clusters',len(x) print x
def main(): with open('linkArray.dump', 'rb') as f: linkArray = pickle.load(f) with open('xmlAnnotationsExtended.dump','rb') as f: annotations = pickle.load(f) speciesEquivalence = {} onlyDicts = [ x for x in listdir('./complex')] onlyDicts = [x for x in onlyDicts if '.bngl.dict' in x] for x in onlyDicts: with open('complex/{0}'.format(x),'rb') as f: speciesEquivalence[int(x.split('.')[0][6:])] = pickle.load(f) for cidx,cluster in enumerate(linkArray): #FIXME:only do the first cluster cidx = 1 cluster = linkArray[1] if len(cluster) == 1: continue annotationsDict = {idx:x for idx,x in enumerate(annotations)} annotationsIncidence = {} #get the model intersection points for element in annotationsDict: for species in annotationsDict[element]: key = annotationsDict[element][species][1] if key not in annotationsIncidence: annotationsIncidence[key] = set([]) annotationsIncidence[key].add(element) #number = 19 graph = nx.Graph() nodeDict = {} superNodeDict = {} for element in annotationsIncidence: if len(annotationsIncidence[element]) > 1: tmpEdgeList = [] names= [] for idx in annotationsIncidence[element]: for species in [x for x in annotationsDict[idx] if annotationsDict[idx][x][1] == element]: try: if isinstance(speciesEquivalence[idx][species],str): tmpEdgeList.append('{0}_{1}'.format(idx,speciesEquivalence[idx][species][0:-2])) names.append(speciesEquivalence[idx][species][0:-2]) else: tmpEdgeList.append('{0}_{1}'.format(idx,speciesEquivalence[idx][species].molecules[0].name)) names.append(speciesEquivalence[idx][species].molecules[0].name) except: print 'error',idx,species names = [x.lower() for x in names] name = Counter(names).most_common(5) name = [x for x in name if len(x[0])> 1] if len(name) == 0: name = Counter(names).most_common(1) for edge in tmpEdgeList: superNodeDict[edge] = name[0][0] if len(name) > 0: superNodeDict[name[0][0]] = len(tmpEdgeList) #pairs = [(x,y,1) for x in tmpEdgeList for y in tmpEdgeList if x!= y] #graph.add_weighted_edges_from(pairs) print superNodeDict for x in cluster: number = x-1 try: console.bngl2xml('complex/output{0}.bngl'.format(number)) species,rules,parameterDict,observableList= readBNGXML.parseXML('output{0}.xml'.format(number)) except: continue nodeDict[number] = simpleGraph(graph,species,observableList,number,superNodeDict) nx.write_gml(graph,'cgraph_{0}.gml'.format(cidx)) break
def main(): with open('linkArray.dump', 'rb') as f: linkArray = pickle.load(f) with open('xmlAnnotationsExtended.dump', 'rb') as f: annotations = pickle.load(f) speciesEquivalence = {} onlyDicts = [x for x in listdir('./complex')] onlyDicts = [x for x in onlyDicts if '.bngl.dict' in x] for x in onlyDicts: with open('complex/{0}'.format(x), 'rb') as f: speciesEquivalence[int(x.split('.')[0][6:])] = pickle.load(f) for cidx, cluster in enumerate(linkArray): #FIXME:only do the first cluster cidx = 0 cluster = linkArray[0] if len(cluster) == 1: continue annotationsDict = {idx: x for idx, x in enumerate(annotations)} annotationsIncidence = {} #get the model intersection points for element in annotationsDict: for species in annotationsDict[element]: key = annotationsDict[element][species][1] if key not in annotationsIncidence: annotationsIncidence[key] = set([]) annotationsIncidence[key].add(element) #number = 19 graph = nx.Graph() nodeDict = {} superNodeDict = {} for element in annotationsIncidence: if len(annotationsIncidence[element]) > 1: tmpEdgeList = [] names = [] for idx in annotationsIncidence[element]: for species in [ x for x in annotationsDict[idx] if annotationsDict[idx][x][1] == element ]: try: print speciesEquivalence[idx] if isinstance(speciesEquivalence[idx][species], str): tmpEdgeList.append('{0}_{1}'.format( idx, speciesEquivalence[idx][species][0:-2])) names.append( speciesEquivalence[idx][species][0:-2]) else: tmpEdgeList.append('{0}_{1}'.format( idx, speciesEquivalence[idx] [species].molecules[0].name)) names.append(speciesEquivalence[idx] [species].molecules[0].name) except: print '----', 'error', idx, species pass names = [x.lower() for x in names] name = Counter(names).most_common(5) name = [x for x in name if len(x[0]) > 1] if len(name) == 0: name = Counter(names).most_common(1) for edge in tmpEdgeList: superNodeDict[edge] = name[0][0] if len(name) > 0: superNodeDict[name[0][0]] = len(tmpEdgeList) #pairs = [(x,y,1) for x in tmpEdgeList for y in tmpEdgeList if x!= y] #graph.add_weighted_edges_from(pairs) print superNodeDict for x in cluster: number = x - 1 try: console.bngl2xml('complex/output{0}.bngl'.format(number)) species, rules, parameterDict, observableList = readBNGXML.parseXML( 'output{0}.xml'.format(number)) except: continue nodeDict[number] = simpleGraph(graph, species, observableList, number, superNodeDict) nx.write_gml(graph, 'cgraph_{0}.gml'.format(cidx)) break
def extractStatistics(): number = 151 console.bngl2xml('complex/output{0}.bngl'.format(number)) species, rules, parameterDict = readBNGXML.parseXML( 'output{0}.xml'.format(number)) #print rules transformationCenter = [] transformationContext = [] k = [] actions = Counter() actionSet = Counter() for idx, rule in enumerate(rules): tatomicArray, ttransformationCenter, ttransformationContext, \ tproductElements,tactionNames,tlabelArray = extractAtomic.extractTransformations([rule]) #atomicArray.append(tatomicArray) transformationCenter.append(ttransformationCenter) transformationContext.append(ttransformationContext) k.append(len(rule[0].actions)) #if len(rule[0].actions) > 3: # print rule[0].reactants actions.update([x.action for x in rule[0].actions]) tmp = [x.action for x in rule[0].actions] tmp.sort() actionSet.update([tuple(tmp)]) #print actions #print actionSet print 'number of species', len(species) print 'avg number o actions', np.average(k), np.std(k) centerDict = groupByReactionCenter(transformationCenter) print 'singletons', len( {x: centerDict[x] for x in centerDict if len(centerDict[x]) == 1}) tmp = [[tuple(set(x)), len(centerDict[x])] for x in centerDict] #reactionCenterGraph(species,tmp) #tmp.sort(key=lambda x:x[1],reverse=True) print 'number of reaction centers', len(centerDict.keys()) print 'number of rules', len(rules) #print 'unique',[x for x in centerDict[element] if len(centerDict[element]) == 1] redundantDict = groupByReactionCenterAndRateAndActions(rules, centerDict) #print redundantDict tmp2 = [('$\\tt{{{0}}}$'.format(tuple(set(x))), tuple(set(y[:-1])), y[-1], len(redundantDict[x][y])) for x in redundantDict for y in redundantDict[x] if 'kr' not in y[-1]] tmp2 = set(tmp2) tmp2 = list(tmp2) tmp2.sort(key=lambda x: x[3], reverse=True) tmp2.sort(key=lambda x: x[0], reverse=True) tmp2 = [ '{0} & {1} & {2} & {3}\\\\\n'.format(element[0], element[1], element[2], element[3]) for element in tmp2 ] with open('table.tex', 'w') as f: f.write('\\begin{tabular}{|cccc|}\n') f.write('\\hline\n') f.write('Reaction Centers & Action & Score\\\\\\hline\n') for element in tmp2: f.write(element) f.write('\\hline\n') f.write('\\end{tabular}\n') #print redundantDict x = [len(centerDict[x]) for x in centerDict] #122,138 print 'average number of reactions with same rate and reaction cneter', np.average( x), np.std(x) print 'total number of clusters', len(x) print x