def antimonyToSBML(self, sb_str, SBO=False): """ Converts an Antimony string to raw SBML. :param sb_str: The raw Antimony string :returns: A 2-tuple (module_name, raw_sbml) """ import antimony as sb if not SBO: sbo_parser = antimonySBOParser(sb_str) try: sb_str = sbo_parser.elideSBOTerms() except: pass # try to load the Antimony code` code = sb.loadAntimonyString(sb_str) # if errors, bail if self.checkAntimonyReturnCode(code): errors = sb.getLastError() raise RuntimeError( 'Errors encountered when trying to load model into Antimony:\n{}' .format(errors)) module = sb.getMainModuleName() sbml = sb.getSBMLString(module) if not SBO: sbml = sbo_parser.addSBOsToSBML(sbml) return (module, sbml)
def flattenMotif(combined): #flatten the combined model by converting it to sbml and then converting back to Antimony antimony.clearPreviousLoads() code = antimony.loadAntimonyString(combined) if code <= 0: textfile = open('combined.txt', 'w') textfile.write(combined) textfile.close() raise AssertionError( 'combined model is not flattenable. Are you using the right species names? ' + 'Combined model saved as: ' + str(os.getcwd()) + '\\combined.txt') sbmlStr = antimony.getSBMLString('combined') antimony.loadSBMLString(sbmlStr) flatComb = antimony.getAntimonyString('combined') ####TODO #Delete extraneous mRNA -> Protein reactions at P_c connections #look for p_c#, regex #delete second equation with occurance of p_c#curr, regex #todo: remove extraneous species + parameters in the removed equation ####TODO return (flatComb)
def antimonyToSBML(self, sb_str, SBO=False): """ Converts an Antimony string to raw SBML. :param sb_str: The raw Antimony string :returns: A 2-tuple (module_name, raw_sbml) """ import antimony as sb if not SBO: sbo_parser = antimonySBOParser(sb_str) try: sb_str = sbo_parser.elideSBOTerms() except: pass # try to load the Antimony code` code = sb.loadAntimonyString(sb_str) # if errors, bail if self.checkAntimonyReturnCode(code): errors = sb.getLastError() raise RuntimeError('Errors encountered when trying to load model into Antimony:\n{}'.format(errors)) module = sb.getMainModuleName() sbml = sb.getSBMLString(module) if not SBO: sbml = sbo_parser.addSBOsToSBML(sbml) return (module, sbml)
def loada(sbstr): import antimony as sb r = sb.loadAntimonyString(sbstr) if r < 0: raise RuntimeError('Failed to load Antimony model: {}'.format( sb.getLastError())) return roadrunner.RoadRunner(sb.getSBMLString(sb.getModuleNames()[-1]))
def translate_to_sbml_string(self, model_file: str = '', model_string: str = ''): """ Returns string of SBML model specification translated from Antimony or CellML model specification file or string :param model_file: relative path to model specification file :param model_string: model specification string :return {str,str}: string of SBML model specification, string of main module name """ # Just to be sure, call clear previous loads antimony.clearPreviousLoads() # Loading from model string or file? if model_file == '': res_load = antimony.loadString(model_string) else: model_path_normalized = self.normalize_path(model_file) res_load = antimony.loadFile(model_path_normalized) if res_load == -1: AntimonyTranslatorError(self, getAntimonyMessage=True) # Get main loaded module main_module_name = antimony.getMainModuleName() if not main_module_name: AntimonyTranslatorError(self, getAntimonyMessage=True) # Return string of SBML model specification translated_model_string = antimony.getSBMLString(main_module_name) if not translated_model_string: AntimonyTranslatorError(self, getAntimonyMessage=True) else: return translated_model_string, main_module_name
def cellmlStrToSBML(CellMLStr): """Convert a cellml string into the equivalent SBML string: sbmlStr = cellMLStrToSBML('mymodel.cellml') """ if antimony.loadCellMLFile(CellMLStr) < 0: raise Exception("Error calling cellMLStrToSBML" + antimony.getLastError()) return antimony.getSBMLString(None)
def cellmlFileToSBML(CellMLFileName): """Load a cellml file and return the equivalent SBML string: sbmlStr = cellMLToSBML('mymodel.cellml') """ if antimony.loadCellMLFile(CellMLFileName) < 0: raise Exception("Error calling loadCellMLFile" + antimony.getLastError()) return antimony.getSBMLString(None)
def __antimony_to_sbml(ant): try: isfile = os.path.isfile(ant) except ValueError: isfile = False if isfile: code = antimony.loadAntimonyFile(ant) else: code = antimony.loadAntimonyString(ant) __check_antimony_return_code(code) mid = antimony.getMainModuleName() return antimony.getSBMLString(mid)
def antimonyTosbml(antStr): """Convert an antimony string into SBML: sbmlStr = antimonyTosbml (antimonyStr) """ err = antimony.loadAntimonyString(antStr) if err < 0: raise Exception("Antimony: " + antimony.getLastError()) Id = antimony.getMainModuleName() return antimony.getSBMLString(Id)
def cellmlToSBML(cellml): """ Convert CellML to SBML string. :param cellml: CellML string or file :type cellml: str | file :return: SBML :rtype: str """ if os.path.isfile(cellml): code = antimony.loadCellMLFile(cellml) else: code = antimony.loadCellMLString(cellml) _checkAntimonyReturnCode(code) return antimony.getSBMLString(None)
def antimonyToSBML(ant): """ Convert Antimony to SBML string. :param ant: Antimony string or file :type ant: str | file :return: SBML :rtype: str """ if os.path.isfile(ant): code = antimony.loadAntimonyFile(ant) else: code = antimony.loadAntimonyString(ant) _checkAntimonyReturnCode(code) mid = antimony.getMainModuleName() return antimony.getSBMLString(mid)
def loadAntimonyModel(antStr): """Load an Antimony string: r = loadAntModel (antimonyStr) """ err = antimony.loadAntimonyString(antStr) if err < 0: raise Exception("Antimony: " + antimony.getLastError()) Id = antimony.getMainModuleName() sbmlStr = antimony.getSBMLString(Id) rr = roadrunner.RoadRunner(sbmlStr) antimony.clearPreviousLoads() return rr
def antimonyToSBML(self, sb_str): """ Converts an Antimony string to raw SBML. :param sb_str: The raw Antimony string :returns: A 2-tuple (module_name, raw_sbml) """ import antimony as sb # try to load the Antimony code` code = sb.loadAntimonyString(sb_str) # if errors, bail if self.checkAntimonyReturnCode(code): errors = sb.getLastError() raise RuntimeError('Errors encountered when trying to load model:\n{}'.format(errors)) module = sb.getMainModuleName() sbml = sb.getSBMLString(module) return (module, sbml)
plotlegend = True #Legende plotten? verbose = 2 #Verbose level: 0 = garnicht, 1 = Statistik 1x je outerLoop, 2 = Statistik nach jedem N, 3 = +Schleifenzaehler (Haengt sich die Simulation auf?) Nscale=100 #Produkt der naechsten 3 Variablen ist die Anzahl der insg. simulierten Pfade outerLoop = 1 #Anzahl Wiederholungen festlegen (multipliziert sich mit AnzahlPfade zur Anzahl der simulierten Pfade je N) Nrange= range(3) #Bereich von N festlegen. range(5) == 0,1,2,3,4. Daher Multiplikator N = (N+1)*Nscale AnzahlPfade = 40 #Anzahl Wiederholungen festlegen (multipliziert sich mit outerLoop). Fuer schoene Plots hier Wert 40-100. Fuer grosse N: doplot=False, hier 1, dafuer bei outerLoop gewuenschte Werte eintragen. #--------------stay-above-this-line----- #Modell laden, kann auch URL sein z.B. von www.biomodels.net--------- if os.path.isfile(antStr): #----------- start copy&paste from tellurium.py code = antimony.loadAntimonyFile(antStr) else: code = antimony.loadAntimonyString(antStr) if code < 0: raise Exception('Antimony: {}'.format(antimony.getLastError())) mid = antimony.getMainModuleName() sbml = antimony.getSBMLString(mid) r = roadrunner.RoadRunner(sbml) #----------- end copy&paste from tellurium.py #----------------the-next-three-lines-should-be-user-adapted-------- r.setIntegrator('gillespie') #Integrator auswaehlen. Andere unterstuetzen z.B. ODEs r.getIntegrator().setValue('variable_step_size', True) #Parameter fuer Integrator setzen, anderer Integrator unterstuetzt andere Parameter Config.setValue(Config.MAX_OUTPUT_ROWS,pow(10,7)) #bei 64-bit pow(10,11) oder weniger bei wenig Arbeitsspeicher #-----------end-user-parameters----------- Reactions = 0 start_time = time.time() selectToSave = ['N'] + selectToPlot form=(1,len(selectToSave)) r.selections = selectToSave simuliertePfade = 0 PfadeGesamt=outerLoop*AnzahlPfade*len(Nrange) if os.path.isfile(path+name): #Header in CSV-Datei automatisch erzeugen, falls Datei noch nicht existiert f=open(path+name,'ab')
import antimony ant = ''' model stoch() species A,B,C,D,E A -> B; k1 B -> C; k2 C -> D; k3 D -> C; k4 E -> C; k5 A = 100000 B = 1000 C = 1000 D = 1000 E = 100000 k1 = 0.2 k2 = 0.05 k3 = 0.1 k4 = 0.01 k5 = 0.05 end ''' antimony.loadAntimonyString(ant) sbmlstr = antimony.getSBMLString(antimony.getMainModuleName()) #print(sbmlstr) with open('stoch_l3.xml', 'w') as f: f.write(sbmlstr)
# #repeat for n-1 modules # #!!module names have to be identical to model names from imported models!! # A : model1(); # B : model2(); # #repeat for all models I have # #specify a global input node # var species p_c; # A.p_input is p_c; # B.p_output is p_c; # #repeat for n-1 modules # end # ''' #properly flatten the combined model antimony.loadAntimonyString(combined) sbmlstr = antimony.getSBMLString('combined') antimony.loadSBMLString(sbmlstr) flatcomb = antimony.getAntimonyString('combined') r = te.loada(flatcomb) r.exportToAntimony('combined.txt') r.draw(layout='dot') #combined = reads + ''' #model combined # var species p_c; # #!!module names have to be identical to model names from imported models!! # A : ffl1(); # B : ffl1(); # A.p_input is p_c; # B.p_output is p_c;
def getSBMLStr(self): antimony.clearPreviousLoads() antimony.loadString(self.getRawStr()) return antimony.getSBMLString(antimony.getModuleNames()[-1])
import antimony with open('../model/rnadecay.sb') as f: sb = f.read() print(sb) res = antimony.loadString(sb) if res < 0: print(antimony.getLastError()) print(res) print(antimony.getModuleNames()) print(antimony.getModuleNames()[-1]) sbml = antimony.getSBMLString(antimony.getModuleNames()[-1]) with open('../model/rnadecay_sbml.xml', 'w') as f: f.write(sbml)
Nscale = 100 #Produkt der naechsten 3 Variablen ist die Anzahl der insg. simulierten Pfade outerLoop = 1 #Anzahl Wiederholungen festlegen (multipliziert sich mit AnzahlPfade zur Anzahl der simulierten Pfade je N) Nrange = range( 3 ) #Bereich von N festlegen. range(5) == 0,1,2,3,4. Daher Multiplikator N = (N+1)*Nscale AnzahlPfade = 40 #Anzahl Wiederholungen festlegen (multipliziert sich mit outerLoop). Fuer schoene Plots hier Wert 40-100. Fuer grosse N: doplot=False, hier 1, dafuer bei outerLoop gewuenschte Werte eintragen. #--------------stay-above-this-line----- #Modell laden, kann auch URL sein z.B. von www.biomodels.net--------- if os.path.isfile(antStr): #----------- start copy&paste from tellurium.py code = antimony.loadAntimonyFile(antStr) else: code = antimony.loadAntimonyString(antStr) if code < 0: raise Exception('Antimony: {}'.format(antimony.getLastError())) mid = antimony.getMainModuleName() sbml = antimony.getSBMLString(mid) r = roadrunner.RoadRunner(sbml) #----------- end copy&paste from tellurium.py #----------------the-next-three-lines-should-be-user-adapted-------- r.setIntegrator( 'gillespie') #Integrator auswaehlen. Andere unterstuetzen z.B. ODEs r.getIntegrator().setValue( 'variable_step_size', True ) #Parameter fuer Integrator setzen, anderer Integrator unterstuetzt andere Parameter Config.setValue(Config.MAX_OUTPUT_ROWS, pow( 10, 7)) #bei 64-bit pow(10,11) oder weniger bei wenig Arbeitsspeicher #-----------end-user-parameters----------- Reactions = 0 start_time = time.time() selectToSave = ['N'] + selectToPlot form = (1, len(selectToSave)) r.selections = selectToSave
import antimony with open('transcription.sb') as f: sb = f.read() #print(sb) res = antimony.loadString(sb) if res < 0: print(antimony.getLastError()) #print(res) #print(antimony.getModuleNames()) #print(antimony.getModuleNames()[-1]) sbml = antimony.getSBMLString(antimony.getModuleNames()[-1]) with open('transcription_sbml.xml', 'w') as f: f.write(sbml)