def translate(combine, filename): sbmlstrlist = [] sedmlstrlist = [] outputstrlist = [] outputstr = '"End of code generated by Import Combine as PhrasedML plugin ' + time.strftime( "%m/%d/%Y") + '"\n"Extracted from ' + filename + '"\n' zipextloc, sbmlloclist, sedmlloclist = manifestsearch(combine) for i in range(len(sbmlloclist)): sbml = te.readFromFile(os.path.join(zipextloc, sbmlloclist[i])) try: transtext = te.sbmlToAntimony(sbml) except Exception as e: transtext = """*********************WARNING********************* Failed to translate the SBML model to Antimony string. Please check that the SBML file is valid. *********************WARNING*********************""" transtext = transtext + "\n\n" + str(e) sbmlstrlist.append(transtext) for j in range(len(sedmlloclist)): sedmlstr = pl.convertFile(os.path.join(zipextloc, sedmlloclist[j])) sedmlstr = sedmlstr.replace('"compartment"', '"compartment_"') sedmlstr = sedmlstr.replace("'compartment'", "'compartment_'") sedmlstrlist.append(sedmlstr) for k in range(len(sedmlstrlist)): outputstrlist.append( "AntimonyModel = '''\n" + sbmlstrlist[0] + "'''\n\nPhrasedMLstr = '''\n" + sedmlstrlist[k] + "'''\n\nimport tellurium as te\n\nexp = te.experiment([AntimonyModel], [PhrasedMLstr])\nexp.execute(PhrasedMLstr)\n\n" + outputstr) delseq(zipextloc) return outputstrlist
def translate(combine, filename): sbmlstrlist = [] sedmlstrlist = [] outputstrlist = [] outputstr = "# -*- coding: utf-8 -*-\n\n" + '"Generated by Import Combine with PhrasedML plugin ' + time.strftime( "%m/%d/%Y" ) + '"\n"Extracted from ' + filename + '"\n\n# Translated SBML\n' zipextloc, sbmlloclist, sedmlloclist = manifestsearch(combine) for i in range(len(sbmlloclist)): sbml = te.readFromFile(zipextloc + sbmlloclist[i]) sbmlstrlist.append(te.sbmlToAntimony(sbml)) for j in range(len(sedmlloclist)): sedmlstr = pl.convertFile(zipextloc + sedmlloclist[j]) sedmlstr = sedmlstr.replace('"compartment"', '"compartment_"') sedmlstr = sedmlstr.replace("'compartment'", "'compartment_'") sedmlstrlist.append(sedmlstr) for k in range(len(sedmlstrlist)): outputstrlist.append(outputstr + "AntimonyModel = '''\n" + sbmlstrlist[0] + "'''\n\nPhrasedMLstr = '''\n" + sedmlstrlist[k] + "'''") delseq(zipextloc) return outputstrlist
def load_and_translate(self, sbmlfile, pythonfile, editor, set_current=True): """ Read filename as combine archive, unzip, translate, reconstitute in Python, and create an editor instance and return it *Warning* This is loading file, creating editor but not executing the source code analysis -- the analysis must be done by the editor plugin (in case multiple editorstack instances are handled) """ sbmlfile = str(sbmlfile) self.emit(SIGNAL('starting_long_process(QString)'), _("Loading %s...") % sbmlfile) text, enc = encoding.read(sbmlfile) sbmlstr = te.readFromFile(sbmlfile) text = "import tellurium as te\n\nr = te.loada('''\n" + str(te.sbmlToAntimony(sbmlstr)) + "''')" widgeteditor = editor.editorstacks[0] finfo = widgeteditor.create_new_editor(pythonfile, enc, text, set_current, new=True) index = widgeteditor.data.index(finfo) widgeteditor._refresh_outlineexplorer(index, update=True) self.emit(SIGNAL('ending_long_process(QString)'), "") if widgeteditor.isVisible() and widgeteditor.checkeolchars_enabled \ and sourcecode.has_mixed_eol_chars(text): name = os.path.basename(pythonfile) QMessageBox.warning(self, widgeteditor.title, _("<b>%s</b> contains mixed end-of-line " "characters.<br>Spyder will fix this " "automatically.") % name, QMessageBox.Ok) widgeteditor.set_os_eol_chars(index) widgeteditor.is_analysis_done = False finfo.editor.set_cursor_position('eof') finfo.editor.insert_text(os.linesep) return finfo, sbmlfile
def getODEsFromSBMLFile(fileName): """ Given a SBML file name, this function returns the model as a string of rules and ODEs Args: fileName (string): The path and name of the SBML file Example: >>> print (te.getODEsFromSBMLFile ('mymodel.xml')) """ sbmlStr = _te.readFromFile(fileName) extractor = _ODEExtractor(sbmlStr) return extractor._toString()
def translate(combine, filename): sbmlstrlist = [] sedmlstrlist = [] outputstrlist = [] rePath = r"loadSBMLModel\((.*)\)" reFig = r"savefig\((.*)\)" outputstr = '"End of code generated by Import Combine plugin ' + time.strftime( "%m/%d/%Y") + '"\n"Extracted from ' + filename + '"\n' zipextloc, sbmlloclist, sedmlloclist = manifestsearch(combine) for i in range(len(sbmlloclist)): sbml = te.readFromFile(os.path.join(zipextloc, sbmlloclist[i])) try: transtext = te.sbmlToAntimony(sbml) except Exception as e: transtext = """*********************WARNING********************* Failed to translate the SBML model to Antimony string. Please check that the SBML file is valid. *********************WARNING*********************""" transtext = transtext + "\n\n" + str(e) sbmlstrlist.append(transtext) for j in range(len(sedmlloclist)): sedmlstr = te.sedmlToPython( os.path.join(zipextloc, sedmlloclist[j])) lines = sedmlstr.splitlines() for i, s in enumerate(lines): reSearchPath = re.split(rePath, s) if len(reSearchPath) > 1: s = s.replace("loadSBMLModel", "loada") s = s.replace(reSearchPath[1], "AntimonyModel") lines[i] = s lines.insert( i - 1, "AntimonyModel = '''\n" + sbmlstrlist[0] + "'''\n") for i, s in enumerate(lines): reSearchFig = re.split(reFig, s) if len(reSearchFig) > 1: del lines[i] sedmlstr = '\n'.join(lines) sedmlstrlist.append(sedmlstr) for k in range(len(sedmlstrlist)): outputstrlist.append(sedmlstrlist[k] + '\n\n' + outputstr) delseq(zipextloc) return outputstrlist
def combineMotifs(): #Summary of motifs included numReads = len(motifList) print('-------------------------------') print('There are ' + str(numReads) + ' motifs.') #print(motifCopiedNum) ###See above TODO print('-------------------------------') #Label all models with an Alphabetically ordered key reads = collections.OrderedDict({}) for i, ascii in zip(list(range(numReads)), product_gen(string.ascii_uppercase)): reads[ascii] = te.readFromFile(motifList[i]) ### Initialize models = list([reads.values()[0]]) species = [] currStr = reads.values()[0] splitStr = re.split('(model)( [\*]?)(.*?\(*\))*\\n\\n', currStr) modules = list([reads.keys()[0] + " : " + splitStr[3] + "; \n"]) modelList = list([splitStr[3]]) inputs = [] outputs = [] ### Construct the combined model Antimony Code for i in np.arange(1, numReads): currStr = reads.values()[i] splitStr = re.split('(model)( [\*]?)(.*?\(*\))*\\n\\n', currStr) if splitStr[3] not in modelList: models.append(reads.values()[i]) modelList.append(splitStr[3]) #Note: the modules have to call the model name within the .txt file for each motif, not the name of the .txt file modules.append(reads.keys()[i] + " : " + splitStr[3] + "; \n") #i species.append("var species p_c" + str(i) + "; \n") #i inputs.append(reads.keys()[i - 1] + ".p_i" + " is p_c" + str(i) + "; \n") #i-1 outputs.append(reads.keys()[i] + ".p_o" + " is p_c" + str(i) + "; \n") #i #combine lists to create a final 'combined' Antimony model combined = "".join("".join(models) + 'model combined()\n' + "".join(modules) + "".join(species) + "".join(inputs) + "".join(outputs) + 'end') return (combined)
def translate(combine, filename): sbmlstrlist = [] sedmlstrlist = [] outputstrlist = [] outputstr = "# -*- coding: utf-8 -*-\n\n" + '"Generated by Import Combine with PhrasedML plugin ' + time.strftime("%m/%d/%Y") + '"\n"Extracted from ' + filename + '"\n\n# Translated SBML\n' zipextloc, sbmlloclist, sedmlloclist = manifestsearch(combine) for i in range(len(sbmlloclist)): sbml = te.readFromFile(zipextloc + sbmlloclist[i]) sbmlstrlist.append(te.sbmlToAntimony(sbml)) for j in range(len(sedmlloclist)): sedmlstr = pl.convertFile(zipextloc + sedmlloclist[j]) sedmlstr = sedmlstr.replace('"compartment"', '"compartment_"') sedmlstr = sedmlstr.replace("'compartment'", "'compartment_'") sedmlstrlist.append(sedmlstr) for k in range(len(sedmlstrlist)): outputstrlist.append(outputstr + "AntimonyModel = '''\n" + sbmlstrlist[0] + "'''\n\nPhrasedMLstr = '''\n" + sedmlstrlist[k] + "'''") delseq(zipextloc) return outputstrlist
def translate(combine, filename): sbmlstrlist = [] sedmlstrlist = [] outputstrlist = [] outputstr = '"End of code generated by Import Combine as PhrasedML plugin ' + time.strftime("%m/%d/%Y") + '"\n"Extracted from ' + filename + '"\n' zipextloc, sbmlloclist, sedmlloclist = manifestsearch(combine) for i in range(len(sbmlloclist)): sbml = te.readFromFile(zipextloc + sbmlloclist[i]) sbmlstrlist.append(te.sbmlToAntimony(sbml)) for j in range(len(sedmlloclist)): sedmlstr = pl.convertFile(zipextloc + sedmlloclist[j]) sedmlstr = sedmlstr.replace('"compartment"', '"compartment_"') sedmlstr = sedmlstr.replace("'compartment'", "'compartment_'") sedmlstrlist.append(sedmlstr) for k in range(len(sedmlstrlist)): outputstrlist.append("AntimonyModel = '''\n" + sbmlstrlist[0] + "'''\n\nPhrasedMLstr = '''\n" + sedmlstrlist[k] + "'''\n\nimport tellurium as te\n\nexp = te.experiment([AntimonyModel], [PhrasedMLstr])\nexp.execute(PhrasedMLstr)\n\n" + outputstr) delseq(zipextloc) return outputstrlist
def translate(combine, filename): sbmlstrlist = [] sedmlstrlist = [] outputstrlist = [] rePath = r"loadSBMLModel\((.*)\)" reFig = r"savefig\((.*)\)" outputstr = '"End of code generated by Import Combine plugin ' + time.strftime("%m/%d/%Y") + '"\n"Extracted from ' + filename + '"\n' zipextloc, sbmlloclist, sedmlloclist = manifestsearch(combine) for i in range(len(sbmlloclist)): sbml = te.readFromFile(zipextloc + sbmlloclist[i]) sbmlstrlist.append(te.sbmlToAntimony(sbml)) for j in range(len(sedmlloclist)): sedmlstr = te.sedmlToPython(zipextloc + sedmlloclist[j]) lines = sedmlstr.splitlines() for i,s in enumerate(lines): reSearchPath = re.split(rePath, s) if len(reSearchPath) > 1: s = s.replace("loadSBMLModel", "loada") s = s.replace(reSearchPath[1],"AntimonyModel") lines[i] = s lines.insert(i - 1, "AntimonyModel = '''\n" + sbmlstrlist[0] + "'''\n") for i,s in enumerate(lines): reSearchFig = re.split(reFig, s) if len(reSearchFig) > 1: del lines[i] sedmlstr = '\n'.join(lines) sedmlstrlist.append(sedmlstr) for k in range(len(sedmlstrlist)): outputstrlist.append(sedmlstrlist[k] + '\n\n' + outputstr) delseq(zipextloc) return outputstrlist
def translate(combine, filename): tempPath = [[]] sbmlstrlist = [] sedmlstrlist = [] outputstrlist = [] rePath = r"(\w*).load\('(.*)'\)" reLoad = r"(\w*) = roadrunner.RoadRunner\(\)" reSelections = r'(\w+)\.selections = \[(.*),?\]' outputstr = "# -*- coding: utf-8 -*-\n\n" + '"Generated by Import Combine plugin ' + time.strftime( "%m/%d/%Y" ) + '"\n"Extracted from ' + filename + '"\n\n# Translated SBML\n' zipextloc, sbmlloclist, sedmlloclist = manifestsearch(combine) for i in range(len(sbmlloclist)): sbml = te.readFromFile(zipextloc + sbmlloclist[i]) sbmlstrlist.append(te.sbmlToAntimony(sbml)) for j in range(len(sedmlloclist)): sedmlstr = se.sedml_to_python(zipextloc + sedmlloclist[j]) lines = sedmlstr.splitlines() for i, s in enumerate(lines): reSearchPath = re.split(rePath, s) if len(reSearchPath) > 1: modpath = getbasename(reSearchPath[2])[:-4].replace( '-', '_') if modpath[0].isdigit() == True: modpath = 'Model_' + modpath tempPath.append([reSearchPath[1], modpath]) else: tempPath.append([reSearchPath[1], modpath]) #if '.xml' in s: del lines[i] for i, s in enumerate(lines): reSearchLoad = re.split(reLoad, s) if len(reSearchLoad) > 1: for k in range(len(tempPath) - 1): if reSearchLoad[1] == tempPath[k + 1][0]: s = s.replace( "roadrunner.RoadRunner()", "te.loada(" + str(tempPath[k + 1][1]) + ")") lines[i] = s #if '"compartment"' or "'compartment'" in sedmlstr: # reSearchSelections = re.split(reSelections, s) # if len(reSearchSelections) > 1: # Vars = reSearchSelections[2].replace(' ','').split(',') # Vars = [s.replace('"','') for s in Vars] # for i in range(len(Vars)): # if Vars[i] == "compartment": # Vars[i] = "compartment_" if not "import tellurium" in sedmlstr: if "import roadrunner" in sedmlstr: for i, s in enumerate(lines): if "import roadrunner" in s: del lines[i] lines.insert(i, "import tellurium as te") else: pass else: outputstr = outputstr + "import tellurium as te\n\n" sedmlstr = '\n'.join(lines) # List of replacements sedmlstr = sedmlstr.replace('"compartment"', '"compartment_"') sedmlstr = sedmlstr.replace("'compartment'", "'compartment_'") sedmlstrlist.append(sedmlstr) for k in range(len(sedmlstrlist)): outputstrlist.append(outputstr + tempPath[1][1] + " = '''\n" + sbmlstrlist[0] + "'''\n\n" + sedmlstrlist[k]) delseq(zipextloc) return outputstrlist
def sbmlconv(zipextloc): sbmlloc, sedmlloc = manifestsearch(zipextloc) sbml = te.readFromFile(zipextloc + sbmlloc) sbmlantimony = te.sbmlToAntimony(sbml) return sbmlantimony
def sbmlconv(zipextloc): #sbml conversion into antimony str sbmlloc, sedmlloc = manifestsearch(zipextloc) sbml = te.readFromFile(zipextloc + sbmlloc) sbmlantimony = te.sbmlToAntimony(sbml) return sbmlantimony
linewidth=2.0, lineStyle='-', color='black', alpha=0.8) r.k1 = r.k1 + 0.2 # Turn the notices back on te.noticesOn() # #### File helpers for reading and writing # In[3]: # create tmp file import tempfile ftmp = tempfile.NamedTemporaryFile(suffix=".xml") print(ftmp.name) # load model r = te.loada('S1 -> S2; k1*S1; k1 = 0.1; S1 = 10') # save to file te.saveToFile(ftmp.name, r.getMatlab()) # or easier via r.exportToMatlab(ftmp.name) # load file sbmlstr = te.readFromFile(ftmp.name) print(sbmlstr) # In[4]:
import re np.set_printoptions(linewidth=160) def product_gen(n): for r in itertools.count(1): for i in itertools.product(n, repeat=r): yield "".join(i) #%% Test model: putting two models together numReads = 2 reads = {} #for i in np.arange(0,numReads): reads[0] = te.readFromFile('FFL1_ant.txt') reads[1] = te.readFromFile('FFL2_ant.txt') #generalize later #combine this with for loop below this later # Note: The string object name has to match the model name readsalpha = zip(string.ascii_uppercase[:numReads], reads.values()) models = reads[0] species = "" currStr = readsalpha[0][1] splitStr = re.split('(model)( [\*]?)(.*?)\\n\\n', currStr) modules = readsalpha[0][0] + " : " + splitStr[3] + "; \n" inputs = "" outputs = "" for i in np.arange(1, numReads):
def translate(combine, filename): tempPath = [[]] sbmlstrlist = [] sedmlstrlist = [] outputstrlist = [] rePath = r"(\w*).load\('(.*)'\)" reLoad = r"(\w*) = roadrunner.RoadRunner\(\)" reSelections = r'(\w+)\.selections = \[(.*),?\]' outputstr = "# -*- coding: utf-8 -*-\n\n" + '"Generated by Import Combine plugin ' + time.strftime("%m/%d/%Y") + '"\n"Extracted from ' + filename + '"\n\n# Translated SBML\n' zipextloc, sbmlloclist, sedmlloclist = manifestsearch(combine) for i in range(len(sbmlloclist)): sbml = te.readFromFile(zipextloc + sbmlloclist[i]) sbmlstrlist.append(te.sbmlToAntimony(sbml)) for j in range(len(sedmlloclist)): sedmlstr = se.sedml_to_python(zipextloc + sedmlloclist[j]) lines = sedmlstr.splitlines() for i,s in enumerate(lines): reSearchPath = re.split(rePath, s) if len(reSearchPath) > 1: modpath = getbasename(reSearchPath[2])[:-4].replace('-','_') if modpath[0].isdigit() == True: modpath = 'Model_' + modpath tempPath.append([reSearchPath[1], modpath]) else: tempPath.append([reSearchPath[1], modpath]) #if '.xml' in s: del lines[i] for i,s in enumerate(lines): reSearchLoad = re.split(reLoad, s) if len(reSearchLoad) > 1: for k in range(len(tempPath) - 1): if reSearchLoad[1] == tempPath[k + 1][0]: s = s.replace("roadrunner.RoadRunner()", "te.loada(" + str(tempPath[k + 1][1])+ ")") lines[i] = s #if '"compartment"' or "'compartment'" in sedmlstr: # reSearchSelections = re.split(reSelections, s) # if len(reSearchSelections) > 1: # Vars = reSearchSelections[2].replace(' ','').split(',') # Vars = [s.replace('"','') for s in Vars] # for i in range(len(Vars)): # if Vars[i] == "compartment": # Vars[i] = "compartment_" if not "import tellurium" in sedmlstr: if "import roadrunner" in sedmlstr: for i,s in enumerate(lines): if "import roadrunner" in s: del lines[i] lines.insert(i, "import tellurium as te") else: pass else: outputstr = outputstr + "import tellurium as te\n\n" sedmlstr = '\n'.join(lines) # List of replacements sedmlstr = sedmlstr.replace('"compartment"', '"compartment_"') sedmlstr = sedmlstr.replace("'compartment'", "'compartment_'") sedmlstrlist.append(sedmlstr) for k in range(len(sedmlstrlist)): outputstrlist.append(outputstr + tempPath[1][1] + " = '''\n" + sbmlstrlist[0] + "'''\n\n" + sedmlstrlist[k]) delseq(zipextloc) return outputstrlist
r.k1 = r.k1 + 0.2 # Turn the notices back on te.noticesOn() # #### File helpers for reading and writing # In[3]: # create tmp file import tempfile ftmp = tempfile.NamedTemporaryFile(suffix=".xml") print(ftmp.name) # load model r = te.loada('S1 -> S2; k1*S1; k1 = 0.1; S1 = 10') # save to file te.saveToFile(ftmp.name, r.getMatlab()) # or easier via r.exportToMatlab(ftmp.name) # load file sbmlstr = te.readFromFile(ftmp.name) print(sbmlstr) # In[4]: