def extractCompartmentStatistics(bioNumber,useID,reactionDefinitions,speciesEquivalence): ''' Iterate over the translated species and check which compartments are used together, and how. ''' reader = libsbml.SBMLReader() document = reader.readSBMLFromFile(bioNumber) parser =SBML2BNGL(document.getModel(),useID) database = structures.Databases() #call the atomizer (or not) #if atomize: translator,onlySynDec = mc.transformMolecules(parser,database,reactionDefinitions,speciesEquivalence) #else: # translator={} compartmentPairs = {} for element in translator: temp = extractCompartmentCoIncidence(translator[element]) for element in temp: if element not in compartmentPairs: compartmentPairs[element] = temp[element] else: compartmentPairs[element].update(temp[element]) finalCompartmentPairs = {} print '-----' for element in compartmentPairs: if element[0][0] not in finalCompartmentPairs: finalCompartmentPairs[element[0][0]] = {} finalCompartmentPairs[element[0][0]][tuple([element[0][1],element[1][1]])] = compartmentPairs[element] return finalCompartmentPairs
def readFromString(inputString, reactionDefinitions, useID, speciesEquivalence=None, atomize=False): ''' one of the library's main entry methods. Process data from a string ''' try: reader = libsbml.SBMLReader() document = reader.readSBMLFromString(inputString) parser = SBML2BNGL(document.getModel(), useID) bioGrid = False if bioGrid: loadBioGrid() database = structures.Databases() namingConventions = resource_path('config/namingConventions.json') if atomize: translator, onlySynDec = mc.transformMolecules( parser, database, reactionDefinitions, namingConventions, speciesEquivalence, bioGrid) else: translator = {} return analyzeHelper(document, reactionDefinitions, useID, '', speciesEquivalence, atomize, translator)[-2] except: return -5
def analyzeFile(bioNumber,reactionDefinitions,useID,namingConventions,outputFile, speciesEquivalence=None,atomize=False,bioGrid=False): ''' one of the library's main entry methods. Process data from a string ''' reader = libsbml.SBMLReader() document = reader.readSBMLFromFile(bioNumber) parser =SBML2BNGL(document.getModel(),useID) database = structures.Databases() #call the atomizer (or not) bioGridDict = {} if bioGrid: bioGridDict = loadBioGrid() if atomize: translator = mc.transformMolecules(parser,database,reactionDefinitions,namingConventions,speciesEquivalence,bioGrid) else: translator={} returnArray= analyzeHelper(document,reactionDefinitions,useID,outputFile,speciesEquivalence,atomize,translator) with open(outputFile,'w') as f: f.write(returnArray[-2]) #with open('{0}.dict'.format(outputFile),'wb') as f: # pickle.dump(returnArray[-1],f) return returnArray[0:-2]
def processDatabase(): reader = libsbml.SBMLReader() #jsonFiles = [ f for f in listdir('./reactionDefinitions') if f[-4:-1] == 'jso'] history = np.load('stats3.npy') index2 = 0 for index in range(1,410): try: nameStr = 'BIOMD0000000%03d' % (index) document = reader.readSBMLFromFile('XMLExamples/curated/' + nameStr + '.xml') parser = SBML2BNGL(document.getModel()) database = structures.Databases() print nameStr + '.xml' ''' for jsonFile in jsonFiles: try: #print jsonFile, translator = m2c.transformMolecules(parser,database,'reactionDefinitions/' + jsonFile) break except: print 'ERROR',sys.exc_info()[0] continue #translator = m2c.transformMolecules(parser,database,'reactionDefinition2.json') ''' #translator = [] while(history[index2][0] < index): index2=1 print history[index2][0],index if (history[index2][0]==index) and history[index2][1] != 0: print str( int(history[index2][1])) translator = mc.transformMolecules(parser,database,'reactionDefinitions/reactionDefinition' + str( int(history[index2][1])) + '.json') else: translator = {} #print len(parser.getSpecies()),len(translator), evaluation(len(parser.getSpecies()),translator) #translator = {} param2 = parser.getParameters() molecules,species,observables = parser.getSpecies(translator) #print molecules,species,observables print 'translated: {0}/{1}'.format(len(translator),len(observables)), print evaluation(len(observables),translator) param,rules,functions = parser.getReactions(translator) compartments = parser.getCompartments() param += param2 writer.finalText(param,molecules,species,observables,rules,functions,compartments,'output/' + nameStr + '.bngl') with open('output/' + nameStr + '.log', 'w') as f: f.write(parser.writeLog(translator)) except: print 'ERROR',sys.exc_info()[0] continue
def analyzeFile(bioNumber, reactionDefinitions, useID, namingConventions, outputFile, speciesEquivalence=None, atomize=False, bioGrid=False): ''' one of the library's main entry methods. Process data from a file ''' logMess.log = [] logMess.counter = -1 reader = libsbml.SBMLReader() document = reader.readSBMLFromFile(bioNumber) parser = SBML2BNGL(document.getModel(), useID) database = structures.Databases() database.forceModificationFlag = True bioGridDict = {} if bioGrid: bioGridDict = loadBioGrid() #call the atomizer (or not). structured molecules are contained in translator #onlysyndec is a boolean saying if a model is just synthesis of decay reactions if atomize: translator, onlySynDec = mc.transformMolecules(parser, database, reactionDefinitions, namingConventions, speciesEquivalence, bioGrid) else: translator = {} #process other sections of the sbml file (functions reactions etc.) returnArray = analyzeHelper(document, reactionDefinitions, useID, outputFile, speciesEquivalence, atomize, translator) with open(outputFile, 'w') as f: f.write(returnArray[-2]) #with open('{0}.dict'.format(outputFile),'wb') as f: # pickle.dump(returnArray[-1],f) if atomize and onlySynDec: returnArray = list(returnArray) returnArray[0] = -1 return tuple(returnArray[0:-2])
def readFromString(inputString,reactionDefinitions,useID,speciesEquivalence=None,atomize=False): ''' one of the library's main entry methods. Process data from a string ''' reader = libsbml.SBMLReader() document = reader.readSBMLFromString(inputString) parser =SBML2BNGL(document.getModel(),useID) bioGrid = False if bioGrid: loadBioGrid() database = structures.Databases() namingConventions = 'config/namingConventions.json' if atomize: translator = mc.transformMolecules(parser,database,reactionDefinitions,namingConventions,speciesEquivalence,bioGrid) else: translator={} return analyzeHelper(document,reactionDefinitions,useID,'',speciesEquivalence,atomize,translator)[-2]
def analyzeFile(bioNumber,reactionDefinitions,useID,namingConventions,outputFile, speciesEquivalence=None,atomize=False,bioGrid=False): ''' one of the library's main entry methods. Process data from a file ''' logMess.log = [] logMess.counter = -1 reader = libsbml.SBMLReader() document = reader.readSBMLFromFile(bioNumber) parser =SBML2BNGL(document.getModel(),useID) database = structures.Databases() database.forceModificationFlag = True bioGridDict = {} if bioGrid: bioGridDict = loadBioGrid() #call the atomizer (or not). structured molecules are contained in translator #onlysyndec is a boolean saying if a model is just synthesis of decay reactions if atomize: translator,onlySynDec = mc.transformMolecules(parser,database,reactionDefinitions,namingConventions,speciesEquivalence,bioGrid) else: translator={} #process other sections of the sbml file (functions reactions etc.) returnArray= analyzeHelper(document,reactionDefinitions,useID,outputFile,speciesEquivalence,atomize,translator) with open(outputFile,'w') as f: f.write(returnArray[-2]) #with open('{0}.dict'.format(outputFile),'wb') as f: # pickle.dump(returnArray[-1],f) if atomize and onlySynDec: returnArray = list(returnArray) returnArray[0] = -1 return tuple(returnArray[0:-2])
def extractCompartmentStatistics(bioNumber, useID, reactionDefinitions, speciesEquivalence): ''' Iterate over the translated species and check which compartments are used together, and how. ''' reader = libsbml.SBMLReader() document = reader.readSBMLFromFile(bioNumber) parser = SBML2BNGL(document.getModel(), useID) database = structures.Databases() #call the atomizer (or not) #if atomize: translator, onlySynDec = mc.transformMolecules(parser, database, reactionDefinitions, speciesEquivalence) #else: # translator={} compartmentPairs = {} for element in translator: temp = extractCompartmentCoIncidence(translator[element]) for element in temp: if element not in compartmentPairs: compartmentPairs[element] = temp[element] else: compartmentPairs[element].update(temp[element]) finalCompartmentPairs = {} print '-----' for element in compartmentPairs: if element[0][0] not in finalCompartmentPairs: finalCompartmentPairs[element[0][0]] = {} finalCompartmentPairs[element[0][0]][tuple( [element[0][1], element[1][1]])] = compartmentPairs[element] return finalCompartmentPairs
def identifyNamingConvention(): ''' extracts statistics from the code ''' reader = libsbml.SBMLReader() jsonFiles = [ f for f in listdir('./reactionDefinitions') if f[-4:-1] == 'jso' and 'reactionDefinition' in f] translationLevel = [] arrayMolecules = [] rules = 0 #go through all curated models in the biomodels database signal.signal(signal.SIGALRM, handler) for index in range(1,410): bestTranslator = {} nameStr = 'BIOMD0000000%03d' % (index) document = reader.readSBMLFromFile('XMLExamples/curated/' + nameStr + '.xml') parser = SBML2BNGL(document.getModel()) database = structures.Databases() print nameStr + '.xml', naming = 'reactionDefinition0.json' bestUseID = True numberofMolecules = numberOfSpecies = 0 #iterate through our naming conventions and selects that which #creates the most rulified elements in the translator for jsonFile in jsonFiles: for useID in [True,False]: oldmaxi = numberOfSpecies parser = SBML2BNGL(document.getModel(),useID) database = structures.Databases() signal.alarm(30) try: mc.transformMolecules(parser,database,'reactionDefinitions/' + jsonFile,None) except: print '--error',jsonFile,useID continue translator = database.translator numberOfSpecies = max(numberOfSpecies,evaluation(len(parser.getSpecies()),database.translator)) if oldmaxi != numberOfSpecies: naming = jsonFile bestTranslator = translator bestUseID = useID _,rules,_ = parser.getReactions(translator) numberofMolecules = len(translator) _,_,obs = parser.getSpecies() rdfAnnotations = analyzeRDF.getAnnotations(parser,'miriam') #go through the annotation list and assign which species #correspond to which uniprot number (if it exists) #similarly list the number of times each individual element appears analyzeRDF.getAnnotations(parser,'miriam') molecules = {} if naming[-6] != 0: for element in bestTranslator: if len(bestTranslator[element].molecules) == 1: name = bestTranslator[element].molecules[0].name for annotation in rdfAnnotations: if name in rdfAnnotations[annotation]: if name not in molecules: molecules[name] = [0,[]] if annotation not in molecules[name][1]: molecules[name][1].extend(annotation) if name not in molecules: molecules[name] = [1,[]] for rule in rules: if name in rule: molecules[name][0] += 1 # _,rules,_ = parser.getReactions(bestTranslator) #for rule in rules: if len(obs) != 0: print index*1.0,(naming[-6]),numberOfSpecies*1.0/len(obs),numberofMolecules*1.0/len(obs),len(obs)*1.0,bestUseID arrayMolecule = [[x,molecules[x]] for x in molecules] arrayMolecules.append(arrayMolecule) translationLevel.append([nameStr+'.xml',(naming[-6]),numberOfSpecies*1.0/len(obs),numberofMolecules*1.0/len(obs),len(obs)*1.0,bestUseID]) else: arrayMolecules.append([]) with open('stats4.npy','wb') as f: pickle.dump(translationLevel,f)