Example #1
0
def processFile(translator,parser,outputFile):
    param2 = parser.getParameters()
    molecules,species,observables = parser.getSpecies(translator)
    compartments = parser.getCompartments()
    param,rules,functions = parser.getReactions(translator,True)
    param += param2
    writer.finalText(param,molecules,species,observables,rules,functions,compartments,outputFile)
Example #2
0
def main():
	
    parser = OptionParser()
    parser.add_option("-i","--input",dest="input",
    	default='input.xml',type="string",
    	help="The input SBML file in xml format. Default = 'input.xml'",metavar="FILE")
    parser.add_option("-o","--output",dest="output",
    	default='output.bngl',type="string",
    	help="the output file where we will store our matrix. Default = output.bngl",metavar="FILE")
          
    (options, args) = parser.parse_args()
    reader = SBMLReader()
    document = reader.readSBMLFromFile(options.input)
    print options.input
    parser =SBML2BNGL(document.getModel())
    param2 = parser.getParameters()
    param,rules,functions = parser.getReactions()
    
    
    molecules,species,observables = parser.getSpecies()
    
    param += param2
    print rules
         
    writer.finalText(param,molecules,species,observables,rules,functions,options.output)
Example #3
0
def processFile(translator, parser, outputFile):
    param2 = parser.getParameters()
    molecules, species, observables = parser.getSpecies(translator)
    compartments = parser.getCompartments()
    param, rules, functions = parser.getReactions(translator, True)
    param += param2
    writer.finalText(param, molecules, species, observables, rules, functions,
                     compartments, outputFile)
Example #4
0
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 = m2c.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
Example #5
0
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
    
    '''
Example #6
0
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
Example #7
0
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
    '''