def compareModelsStructurally(self, fileName1, fileName2, mapping, componentMapping): molecules, _, _ = readBNGXML.parseXML(fileName1) molecules2, _, _ = readBNGXML.parseXML(fileName2) moleculeName1 = {x.name: x for x in molecules} moleculeName2 = {x.name: x for x in molecules2} intersectionDict = {} nameIntersection = moleculeIntersection(moleculeName1.keys(), moleculeName2.keys(), mapping) componentNameIntersection = defaultdict(lambda: defaultdict(str)) for molecule in nameIntersection: for component in moleculeName1[molecule].components: if component.name in [ x.name for x in moleculeName2[ nameIntersection[molecule]].components ]: componentNameIntersection[nameIntersection[molecule]][ component.name] = component.name elif (molecule in componentMapping and component.name in componentMapping[molecule]): componentNameIntersection[nameIntersection[molecule]][ component.name.lower()] = componentMapping[molecule][ component.name].lower() return nameIntersection, componentNameIntersection
def askQuestions(inputfile, molecule, center, context=None): _, rules, _ = readBNGXML.parseXML(inputfile) transformationCenter = [] transformationContext = [] transformationProduct = [] atomicArray = [] actionNames = [] for idx, rule in enumerate(rules): tatomicArray, ttransformationCenter, ttransformationContext, \ tproductElements, tactionNames, tlabelArray = extractAtomic.extractTransformations( [rule],True) if any([ molecule in y and center in y for x in ttransformationCenter for y in x ]): if context: print str(rule[0]).split(':')[0] print[ y for x in ttransformationContext for y in x if context in y and molecule in y ] else: print rule
def main2(): number = 48 species,rules,parameterDict,observableList= readBNGXML.parseXML('output{0}.xml'.format(number)) graph = nx.Graph() simpleGraph(graph,species,observableList,number) nx.write_gml(graph,'graph_{0}.gml'.format(number))
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 main2(): number = 48 species, rules, parameterDict, observableList = readBNGXML.parseXML( 'output{0}.xml'.format(number)) graph = nx.Graph() simpleGraph(graph, species, observableList, number) nx.write_gml(graph, 'graph_{0}.gml'.format(number))
def compareModelsStructurally(self, fileName1, fileName2, mapping,componentMapping): molecules, _, _ = readBNGXML.parseXML(fileName1) molecules2, _, _ = readBNGXML.parseXML(fileName2) moleculeName1 = {x.name: x for x in molecules} moleculeName2 = {x.name: x for x in molecules2} intersectionDict = {} nameIntersection = moleculeIntersection(moleculeName1.keys(), moleculeName2.keys(), mapping) componentNameIntersection = defaultdict(lambda: defaultdict(str)) for molecule in nameIntersection: for component in moleculeName1[molecule].components: if component.name in [x.name for x in moleculeName2[nameIntersection[molecule]].components]: componentNameIntersection[nameIntersection[molecule]][component.name] = component.name elif molecule in componentMapping and component.name in componentMapping[molecule]: componentNameIntersection[nameIntersection[molecule]][component.name.lower()] = componentMapping[molecule][component.name].lower() return nameIntersection,componentNameIntersection
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 getBondsFromFileName(fileName): constructedFilename = '../curated/{0}.xml'.format(fileName) moleculeTypes, rules, _ = readBNGXML.parseXML(constructedFilename) bonds = set([]) for molecule in moleculeTypes: for component in molecule.components: name = [x.name for x in moleculeTypes if x.name.lower() == component.name] if len(name) > 0: bonds.add(tuple(sorted([molecule.name, name[0]]))) return bonds
def __init__(self, fileName,rawFileName=None): self.molecules, self.rules, _ = readBNGXML.parseXML(fileName) self.dependencies, self.patternXreactions, _, _ = componentGroups.getContextRequirements(fileName, collapse=False) self.transposePatternsReactions() self.reverseDependencies = componentGroups.reverseContextDict(self.dependencies) self.moleculeMotifDict, self.motifMoleculeDict = self.classifyPairsByMotif(self.reverseDependencies) if rawFileName: self.processRawSBML(rawFileName) else: self.rawRules = {}
def getContextRequirements(inputfile, collapse=True, motifFlag=False, excludeReverse=False): """ Receives a BNG-XML file and returns the contextual dependencies implied by this file """ molecules, rules, parameters = readBNGXML.parseXML(inputfile) moleculeStateMatrix = {} label, center, context, product, atomicArray, actions, doubleAction = extractCenterContext( rules, excludeReverse=excludeReverse ) return getStateTransitionDiagram(label, center, product, context, actions, molecules, rules, parameters)
def getContextRequirements(inputfile, collapse=True, motifFlag=False, excludeReverse=False): """ Receives a BNG-XML file and returns the contextual dependencies implied by this file """ molecules, rules, parameters = readBNGXML.parseXML(inputfile) moleculeStateMatrix = {} label, center, context, product, atomicArray, actions, doubleAction = extractCenterContext( rules, excludeReverse=excludeReverse) return getStateTransitionDiagram(label, center, product, context, actions, molecules, rules, parameters)
def askQuestions(inputfile, molecule, center, context=None): _, rules, _ = readBNGXML.parseXML(inputfile) ruleArray = [] contextArray = [] for idx, rule in enumerate(rules): tatomicArray, ttransformationCenter, ttransformationContext, \ tproductElements, tactionNames, tlabelArray = extractAtomic.extractTransformations( [rule], True) if any([molecule in y and center in y for x in ttransformationCenter for y in x]): if context: ruleArray.append(str(rule[0]).split(':')[0]) contextArray.append([y for x in ttransformationContext for y in x if context in y and molecule in y]) else: print rule return ruleArray, contextArray
def getContextRequirements(inputfile): """ Receives a BNG-XML file and returns the contextual dependencies implied by this file """ molecules, rules, _ = readBNGXML.parseXML(inputfile) center, context, product, atomicArray, actions = extractCenterContext(rules) reactionCenterStateDictionary = getRestrictedChemicalStates(product, context) backupstatedictionary = deepcopy(reactionCenterStateDictionary) #print reactionCenterStateDictionary['STAT3%0'] #chemicalStates = getChemicalStates(rules) #totalStateDictionary = sortChemicalStates(chemicalStates) requirementDependencies = detectDependencies(reactionCenterStateDictionary, molecules) removeIndirectDependencies(requirementDependencies, backupstatedictionary) getMutualExclusions(requirementDependencies, molecules) #requirementDependencies = removeCounter(requirementDependencies)\ #raise Exception return requirementDependencies
def getContextRequirements(inputfile): """ Receives a BNG-XML file and returns the contextual dependencies implied by this file """ molecules, rules, _ = readBNGXML.parseXML(inputfile) center, context, product, atomicArray, actions = extractCenterContext( rules) reactionCenterStateDictionary = getRestrictedChemicalStates( product, context) backupstatedictionary = deepcopy(reactionCenterStateDictionary) #print reactionCenterStateDictionary['STAT3%0'] #chemicalStates = getChemicalStates(rules) #totalStateDictionary = sortChemicalStates(chemicalStates) requirementDependencies = detectDependencies(reactionCenterStateDictionary, molecules) removeIndirectDependencies(requirementDependencies, backupstatedictionary) getMutualExclusions(requirementDependencies, molecules) #requirementDependencies = removeCounter(requirementDependencies)\ #raise Exception return requirementDependencies
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 getContextRequirements(inputfile, collapse=True, motifFlag=False, excludeReverse=False): """ Receives a BNG-XML file and returns the contextual dependencies implied by this file """ molecules, rules, _ = readBNGXML.parseXML(inputfile) label, center, context, product, atomicArray, actions, doubleAction = extractCenterContext( rules, excludeReverse=excludeReverse) reactionCenterStateDictionary, doubleActionDict = getRestrictedChemicalStates( label, product, context, doubleAction) #molecule1 = 'socs3' #molecule2 = 'shp2' #print(molecule1,molecule2) #print('---',reactionCenterStateDictionary['gp130%0'][(molecule1,1,'')][molecule2]) #print('---',reactionCenterStateDictionary['gp130%0'][(molecule2,1,'')][molecule1]) #print(reactionCenterStateDictionary['STAT3%0'][('astmod',0,'AST')]) #print('++++') # print(reactionCenterStateDictionary['Ras%0'][('Ras_GTPmod',0,'Ras_GTP')]['Ras_GDPmod']) backupstatedictionary = deepcopy(reactionCenterStateDictionary) # print(reactionCenterStateDictionary['EGFR%1'][('_Pmod',0,'_P')]) # print(reactionCenterStateDictionary['EGFR%0'][('_Pmod',0,'_P')]) # return # chemicalStates = getChemicalStates(rules) # totalStateDictionary = sortChemicalStates(chemicalStates) # print(reactionCenterStateDictionary['Shc%0'][('egfr', 0, '')]['mmod']) requirementDependencies = detectDependencies(reactionCenterStateDictionary, molecules) #print(requirementDependencies['JAK']['requirement']) # repression for molecule in reactionCenterStateDictionary: moleculeName = molecule.split('%')[0] for element in reactionCenterStateDictionary[molecule]: if not isActive(element[1:]): for partner in reactionCenterStateDictionary[molecule][ element]: for state in reactionCenterStateDictionary[molecule][ element][partner]: if isActive(state): repressiveState = (partner, state[0], state[1]) if (reverseState(moleculeName, element, molecules), repressiveState ) in requirementDependencies[moleculeName][ 'requirement']: # or (repressiveState, element[0]) in requirementDependencies[moleculeName]['independent']: requirementDependencies[moleculeName][ 'repression'].add( (repressiveState[0], element[0])) if motifFlag: requirementDependencies = getMotifRelationships( requirementDependencies, molecules) exclusionCliques = getExclusionClusters(requirementDependencies) removeIndirectDependencies(requirementDependencies, backupstatedictionary, motifFlag) else: removeIndirectDependencies(requirementDependencies, backupstatedictionary) getMutualExclusions(requirementDependencies, molecules) exclusionCliques = {} processNodes = defaultdict(lambda: defaultdict(lambda: defaultdict(list))) if motifFlag: # double interactions print(doubleActionDict['gp130']) multiInteractionDict = defaultdict( lambda: defaultdict(lambda: defaultdict(list))) doubleInteractions = defaultdict( lambda: defaultdict(lambda: defaultdict(list))) for molecule in [ x for x in requirementDependencies if x in doubleActionDict ]: for motif in requirementDependencies[molecule]: for relationship in requirementDependencies[molecule][motif]: for combination1 in [True, False]: for combination2 in [True, False]: if len(doubleActionDict[molecule][relationship[0]] [combination1][ relationship[1]][combination2]) > 0: multiInteractionDict[molecule][( combination1, combination2)][relationship] = tuple( doubleActionDict[molecule][ relationship[0]][combination1][ relationship[1]][combination2]) doubleInteractions[molecule][tuple( relationship)][motif].append( (combination1, combination2)) for molecule in doubleInteractions: for relationship in doubleInteractions[molecule]: for motif in doubleInteractions[molecule][relationship]: if motif in [ 'partialIndependence-', 'partialIndependence+', 'fullIndependence' ]: requirementDependencies[molecule][motif].remove( relationship) for combination in doubleInteractions[molecule][ relationship][motif]: label = multiInteractionDict[molecule][combination][ relationship] if combination[0] and combination[1]: requirementDependencies[molecule][ 'doubleActivation'].append(relationship) processNodes[molecule]['doubleActivation'][ relationship] = '{0}_{1}'.format( molecule, '_'.join(label)) elif not combination[0] and combination[1]: if motif in ['ordering']: requirementDependencies[molecule][ motif].remove(relationship) elif motif in ['repression']: if ( relationship[1], relationship[0] ) in requirementDependencies[molecule][motif]: requirementDependencies[molecule][ motif].remove( (relationship[1], relationship[0])) requirementDependencies[molecule][ 'reprordering'].append(relationship) processNodes[molecule]['reprordering'][ relationship] = '{0}_{1}'.format( molecule, '_'.join(label)) elif not combination[0] and not combination[1]: processNodes[molecule]['doubleRepression'][ relationship] = '{0}_{1}'.format( molecule, '_'.join(label)) if motif == 'repression': requirementDependencies[molecule][ motif].remove(relationship) requirementDependencies[molecule][ 'doubleRepression'].append(relationship) elif motif == 'partialIndependence-': # requirementDependencies[molecule][motif].remove(relationship) requirementDependencies[molecule][ 'doubleRepression'].append(relationship) return requirementDependencies, backupstatedictionary, exclusionCliques, processNodes
def processRawSBML(self,inputfile): _, rawrules, _ = readBNGXML.parseXML(inputfile) self.rawRules = {x[0].label: x[0] for x in rawrules}
def getContextRequirements(inputfile, collapse=True, motifFlag=False, excludeReverse = False): """ Receives a BNG-XML file and returns the contextual dependencies implied by this file """ molecules, rules, _ = readBNGXML.parseXML(inputfile) label, center, context, product, atomicArray, actions, doubleAction = extractCenterContext(rules, excludeReverse=excludeReverse) reactionCenterStateDictionary, doubleActionDict = getRestrictedChemicalStates(label, product, context, doubleAction) print(reactionCenterStateDictionary['STAT3%0'].keys()) print(reactionCenterStateDictionary['STAT3%0'][('astmod',0,'AST')]) print '++++' # print reactionCenterStateDictionary['Ras%0'][('Ras_GTPmod',0,'Ras_GTP')]['Ras_GDPmod'] backupstatedictionary = deepcopy(reactionCenterStateDictionary) # print reactionCenterStateDictionary['EGFR%1'][('_Pmod',0,'_P')] # print reactionCenterStateDictionary['EGFR%0'][('_Pmod',0,'_P')] # return # chemicalStates = getChemicalStates(rules) # totalStateDictionary = sortChemicalStates(chemicalStates) # print reactionCenterStateDictionary['Shc%0'][('egfr', 0, '')]['mmod'] requirementDependencies = detectDependencies(reactionCenterStateDictionary, molecules) print requirementDependencies['JAK']['requirement'] # repression for molecule in reactionCenterStateDictionary: moleculeName = molecule.split('%')[0] for element in reactionCenterStateDictionary[molecule]: if not isActive(element[1:]): for partner in reactionCenterStateDictionary[molecule][element]: for state in reactionCenterStateDictionary[molecule][element][partner]: if isActive(state): repressiveState = (partner, state[0], state[1]) if (reverseState(moleculeName, element, molecules), repressiveState) in requirementDependencies[moleculeName]['requirement']: # or (repressiveState, element[0]) in requirementDependencies[moleculeName]['independent']: requirementDependencies[moleculeName]['repression'].add((repressiveState[0], element[0])) if motifFlag: requirementDependencies = getMotifRelationships(requirementDependencies, molecules) exclusionCliques = getExclusionClusters(requirementDependencies) removeIndirectDependencies(requirementDependencies, backupstatedictionary, motifFlag) else: removeIndirectDependencies(requirementDependencies, backupstatedictionary) getMutualExclusions(requirementDependencies, molecules) exclusionCliques = {} processNodes = defaultdict(lambda: defaultdict(lambda: defaultdict(list))) if motifFlag: # double interactions #print doubleActionDict['MEK']['pmod'] multiInteractionDict = defaultdict(lambda: defaultdict(lambda: defaultdict(list))) doubleInteractions = defaultdict(lambda: defaultdict(lambda: defaultdict(list))) for molecule in [x for x in requirementDependencies if x in doubleActionDict]: for motif in requirementDependencies[molecule]: for relationship in requirementDependencies[molecule][motif]: for combination1 in [True, False]: for combination2 in [True, False]: if len(doubleActionDict[molecule][relationship[0]][combination1][relationship[1]][combination2]) > 0: multiInteractionDict[molecule][(combination1, combination2)][relationship] = tuple(doubleActionDict[molecule][relationship[0]][combination1][relationship[1]][combination2]) doubleInteractions[molecule][tuple(relationship)][motif].append((combination1, combination2)) for molecule in doubleInteractions: for relationship in doubleInteractions[molecule]: for motif in doubleInteractions[molecule][relationship]: if motif in ['partialIndependence-', 'partialIndependence+', 'fullIndependence']: requirementDependencies[molecule][motif].remove(relationship) for combination in doubleInteractions[molecule][relationship][motif]: label = multiInteractionDict[molecule][combination][relationship] if combination[0] and combination[1]: requirementDependencies[molecule]['doubleActivation'].append(relationship) processNodes[molecule]['doubleActivation'][relationship] = '{0}_{1}'.format(molecule, '_'.join(label)) elif not combination[0] and combination[1]: if motif in ['ordering']: requirementDependencies[molecule][motif].remove(relationship) elif motif in ['repression']: if (relationship[1], relationship[0]) in requirementDependencies[molecule][motif]: requirementDependencies[molecule][motif].remove((relationship[1], relationship[0])) requirementDependencies[molecule]['reprordering'].append(relationship) processNodes[molecule]['reprordering'][relationship] = '{0}_{1}'.format(molecule, '_'.join(label)) elif not combination[0] and not combination[1]: processNodes[molecule]['doubleRepression'][relationship] = '{0}_{1}'.format(molecule, '_'.join(label)) if motif == 'repression': requirementDependencies[molecule][motif].remove(relationship) requirementDependencies[molecule]['doubleRepression'].append(relationship) elif motif == 'partialIndependence-': # requirementDependencies[molecule][motif].remove(relationship) requirementDependencies[molecule]['doubleRepression'].append(relationship) return requirementDependencies, backupstatedictionary, exclusionCliques, processNodes
def getInformationContent(fileName): console.bngl2xml(fileName,timeout=30) xmlFileName = '.'.join(fileName.split('.')[:-1])+'.xml' xmlFileName =xmlFileName.split('/')[-1] species,reactions,_= readBNGXML.parseXML(xmlFileName) calculateSpeciesInformationContent(species)
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 = 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