def initialize_fluxtools(infile, ofile="", dumpparser=False): DUMP = dumpparser or ("-dump" in sys.argv) if infile=="": if("-in" in sys.argv): i = (sys.argv).index("-in")+1 if i<len(sys.argv): infile=sys.argv[i] else: raise SystemExit("Error: use '-in filename' to specify input file.") else: raise SystemExit("Error: use '-in filename' to specify input file.") if os.path.isfile(infile): datafile = infile else: raise SystemExit("Error: "+os.path.abspath(infile)+" File not found.") outfile=ofile if outfile == "": if ("-out" in sys.argv): i = (sys.argv).index("-out")+1 if i<len(sys.argv): outfile=sys.argv[i] else: outfile="flux-analysis.out" else: outfile="flux-analysis.out" outfile=os.path.abspath(uniqueFileName(outfile)) lines=readmodel(INFILE=datafile)[0] results = invokeParser(lines, dump=DUMP) return (results, outfile)
def runparser(datafile, trace=False): if ("-in" in sys.argv): i = (sys.argv).index("-in")+1 if i<len(sys.argv): infile=sys.argv[i] else: raise SystemExit("Error: expecting filename after -in") elif os.path.isfile(datafile): infile=datafile else: raise SystemExit("Error: interpreter (runparser): No Input File Given; use -in option.") if not os.path.isfile(infile): raise SystemExit("Error: "+os.path.abspath(infile)+" File not found.") rates = {} if os.path.splitext(infile)[1]==".model": (lines, ics, rates, frozenvars, functions,assignments, fullfile)=readmodel(INFILE=infile) else: f=open(infile) lines = f.readlines() f.close() results=invokeParser(lines,dump=trace) # print "runparser: rates=", rates return (results, rates, functions, frozenvars, assignments, ics)
def GetCelleratorNetwork(filename, dump=False): if not os.path.isfile(filename): print "Error: readPLY: unable to locate file ", os.path.abspath( filename) raise SystemExit("File Not Found") rates = {} ics = {} if os.path.splitext(filename)[1] == ".model": (linesOfData, ics, rates, frozenvars, functions, assignments, fullfile) = readmodel(INFILE=filename) else: f = open(filename) linesOfData = f.readlines() f.close() lines = [] for line in linesOfData: line = (line.split("#"))[0].strip() if len(line) > 0: lines.append(line) linesOfData = lines # f = open(filename) # linesOfData = f.readlines() # f.close() n = len(linesOfData) if not ("-quiet" in sys.argv): print n, " lines read from ", os.path.abspath(filename) result = [] reactions = [] for i in range(n): raw = linesOfData[i] r = Reaction(raw) reactions.append(r) if dump: r.Dump() next = _Handle_Cellerator_Reaction(raw, r) # result.append() # print "next = ", next result = result + next species = listspecies(reactions) # result = functionate(result, species) # print "species = ", species # print "result = ", result return (result, rates, ics)
def parser(inputfile="", trace=False): if not (inputfile == ""): infile = str(inputfile) elif ("-in" in sys.argv): i = (sys.argv).index("-in") + 1 if i < len(sys.argv): infile = sys.argv[i] else: raise SystemExit("Error: expecting filename after -in") else: raise SystemExit("No Input File Given; use -in option.") if not os.path.isfile(infile): raise SystemExit("Error: " + os.path.abspath(infile) + " File not found.") if os.path.splitext(infile)[1] == ".model": (lines, ics, rates, frozenvars, functions, assignments, fullfile) = readmodel(INFILE=infile) else: f = open(infile) lines = f.readlines() f.close() # f=open(infile) # lines = f.readlines() # f.close() results = [] rs = [] for line in lines: line = StripComments(line, "#") if line == "": continue r = Reaction(line) if "-dump" in sys.argv: r.Dump() if not r.Arrow() == "Unknown": results.append(r.Input().strip()) rs.append(r) if trace or ("-trace" in sys.argv): print "input: " + str(r.Input()).strip() elif trace or ("-trace" in sys.argv): print "input ignored: " + line.strip() # print results return rs
def GetCelleratorNetwork(filename, dump=False): if not os.path.isfile(filename): print "Error: readPLY: unable to locate file ", os.path.abspath(filename) raise SystemExit("File Not Found") rates={} ics={} if os.path.splitext(filename)[1]==".model": (linesOfData, ics, rates, frozenvars, functions,assignments, fullfile)=readmodel(INFILE=filename) else: f=open(filename) linesOfData = f.readlines() f.close() lines=[] for line in linesOfData: line=(line.split("#"))[0].strip() if len(line)>0: lines.append(line) linesOfData = lines # f = open(filename) # linesOfData = f.readlines() # f.close() n = len(linesOfData) if not ("-quiet" in sys.argv): print n," lines read from ", os.path.abspath(filename) result=[] reactions = [] for i in range(n): raw=linesOfData[i] r=Reaction(raw) reactions.append(r) if dump: r.Dump() next = _Handle_Cellerator_Reaction(raw,r) # result.append() # print "next = ", next result = result + next species = listspecies(reactions) # result = functionate(result, species) # print "species = ", species # print "result = ", result return (result,rates,ics)
def parser(inputfile="", trace=False): if not (inputfile == ""): infile = str(inputfile) elif "-in" in sys.argv: i = (sys.argv).index("-in") + 1 if i < len(sys.argv): infile = sys.argv[i] else: raise SystemExit("Error: expecting filename after -in") else: raise SystemExit("No Input File Given; use -in option.") if not os.path.isfile(infile): raise SystemExit("Error: " + os.path.abspath(infile) + " File not found.") if os.path.splitext(infile)[1] == ".model": (lines, ics, rates, frozenvars, functions, assignments, fullfile) = readmodel(INFILE=infile) else: f = open(infile) lines = f.readlines() f.close() # f=open(infile) # lines = f.readlines() # f.close() results = [] rs = [] for line in lines: line = StripComments(line, "#") if line == "": continue r = Reaction(line) if "-dump" in sys.argv: r.Dump() if not r.Arrow() == "Unknown": results.append(r.Input().strip()) rs.append(r) if trace or ("-trace" in sys.argv): print "input: " + str(r.Input()).strip() elif trace or ("-trace" in sys.argv): print "input ignored: " + line.strip() # print results return rs
def initialize_fluxtools(infile, ofile="", dumpparser=False): DUMP = dumpparser or ("-dump" in sys.argv) if infile == "": if ("-in" in sys.argv): i = (sys.argv).index("-in") + 1 if i < len(sys.argv): infile = sys.argv[i] else: raise SystemExit( "Error: use '-in filename' to specify input file.") else: raise SystemExit( "Error: use '-in filename' to specify input file.") if os.path.isfile(infile): datafile = infile else: raise SystemExit("Error: " + os.path.abspath(infile) + " File not found.") outfile = ofile if outfile == "": if ("-out" in sys.argv): i = (sys.argv).index("-out") + 1 if i < len(sys.argv): outfile = sys.argv[i] else: outfile = "flux-analysis.out" else: outfile = "flux-analysis.out" outfile = os.path.abspath(uniqueFileName(outfile)) lines = readmodel(INFILE=datafile)[0] results = invokeParser(lines, dump=DUMP) return (results, outfile)
def expandReactions(r, dump=False, text=False): inputfile=r if ("-in" in sys.argv): i = (sys.argv).index("-in")+1 if i<len(sys.argv): inputfile=sys.argv[i] else: raise SystemExit("Error: expecting filename after -in") elif inputfile=="": raise SystemExit("No Input File Given; use -in option.") if not os.path.isfile(inputfile): raise SystemExit("Error: "+os.path.abspath(inputfile)+" File not found.") if os.path.splitext(inputfile)[1]==".model": (lines, ics, rates, frozenvars, functions,assignments, fullfile)=readmodel(INFILE=inputfile) else: f=open(inputfile) lines = f.readlines() f.close() expanded=[] expandedInput=[] for line in lines: line = StripComments(line,"#") if line=="": continue # ignore blanks p = Reaction(line) # print "Expander: input: ", line p = expand(p) # print "Expander: expanded: ", p expanded += p tp = map(lambda x:x.Input(),p) expandedInput += tp # if requested, dump the expanded text reactions to the screen if ("-dump" in sys.argv): dump=True if dump: for line in expandedInput: print line # if requested, dump the expanded text reactions to a file # check to see if file is requested if ("-out" in sys.argv): i = (sys.argv).index("-out")+1 if i<len(sys.argv): outputfile=sys.argv[i] else: # print "Error: expecting filename after -out" outputfile = "expanded-reactions.out" outputfile = uniqueFileName(outputfile, type ="out") f=open(outputfile,"w") for line in expandedInput: f.write(line+"\n") f.close() print "Output written to: "+os.path.abspath(outputfile) if text == True: return expandedInput return expanded
def expandReactions(r, dump=False, text=False): inputfile = r if ("-in" in sys.argv): i = (sys.argv).index("-in") + 1 if i < len(sys.argv): inputfile = sys.argv[i] else: raise SystemExit("Error: expecting filename after -in") elif inputfile == "": raise SystemExit("No Input File Given; use -in option.") if not os.path.isfile(inputfile): raise SystemExit("Error: " + os.path.abspath(inputfile) + " File not found.") if os.path.splitext(inputfile)[1] == ".model": (lines, ics, rates, frozenvars, functions, assignments, fullfile) = readmodel(INFILE=inputfile) else: f = open(inputfile) lines = f.readlines() f.close() expanded = [] expandedInput = [] for line in lines: line = StripComments(line, "#") if line == "": continue # ignore blanks p = Reaction(line) # print "Expander: input: ", line p = expand(p) # print "Expander: expanded: ", p expanded += p tp = map(lambda x: x.Input(), p) expandedInput += tp # if requested, dump the expanded text reactions to the screen if ("-dump" in sys.argv): dump = True if dump: for line in expandedInput: print line # if requested, dump the expanded text reactions to a file # check to see if file is requested if ("-out" in sys.argv): i = (sys.argv).index("-out") + 1 if i < len(sys.argv): outputfile = sys.argv[i] else: # print "Error: expecting filename after -out" outputfile = "expanded-reactions.out" outputfile = uniqueFileName(outputfile, type="out") f = open(outputfile, "w") for line in expandedInput: f.write(line + "\n") f.close() print "Output written to: " + os.path.abspath(outputfile) if text == True: return expandedInput return expanded
def generateSimulation(inputfile="", step=1, duration=100, plot=False, format="CSV", \ output="", vars=[], plotcolumns=3, sameplot=True): formattype = {"CSV": "CSV", "TABLE": "TXT", "TSV": "TSV"} # input: model file # return value: none # output: generates simulation plots from model file # if invoked from command line, "solver -in filename -run duration step" holdrates = [] if "-scan" in sys.argv: i = (sys.argv).index("-scan") + 1 if i + 3 < len(sys.argv): holdrates.append(sys.argv[i]) scanstart = sys.argv[i + 1] scanstop = sys.argv[i + 2] scandelta = sys.argv[i + 3] else: sys.exit("Error: expecting 'K min max delta' after keyword -scan") (r, ic, rates, frozenvars, functions, assignments, filename) = readmodel(INFILE=inputfile) (variables, y0, tmpdotpy) = generatePythonFunction(r, rates, ic, frozenvars, functions, assignments, holdrates) f = open(tmpdotpy, "r") rhs = f.readlines() f.close() try: os.remove(tmpdotpy) except: print "Warning: unable to delete " + os.path.abspath(tmpdotpy) pyfile = "" if ("-pyfile" in sys.argv): i = (sys.argv).index("-pyfile") + 1 if i < len(sys.argv): pyfile = sys.argv[i] else: print "Waring: solver: expecting file name after -pyfile option." pyfile = "newsolver.py" if pyfile == "": pyfile = utils.timed_file_name( "solver-for-" + os.path.basename(filename), "py") #pyfile = utils.uniqueFileName(pyfile, type="py") f = open(pyfile, "w") f.write("import numpy as np\n") f.write("import cellerator.solver\n") f.write("from scipy.integrate import odeint\n") f.write("\n") for line in rhs: f.write(line) f.write("\n\n\n") f.write("def thesolver():\n") f.write(" filename =\"" + filename + "\"\n") svars = str(map(utils.deindex, map(str, variables))) f.write(" variables=" + svars + "\n") (runtime, stepsize) = getRunParameters(default_duration=duration, default_step=step) f.write(" runtime = " + str(runtime) + " \n") f.write(" stepsize = " + str(stepsize) + " \n") f.write(" t = np.arange(0,runtime+stepsize,stepsize)\n") f.write(" y0 = " + str(y0) + "\n") if "-mxstep" in sys.argv: i = (sys.argv).index("-mxstep") + 1 if i < len(sys.argv): mxstep = sys.argv[i] else: mxstep = "500000" if len(holdrates) > 0: f.write(" global " + holdrates[0] + "\n") f.write(" results=[]\n") f.write(" " + holdrates[0] + "=" + str(scanstart) + "\n") f.write(" while " + holdrates[0] + " <= " + str(scanstop) + ":\n") f.write(" sol = odeint(ode_function_rhs, y0, t, mxstep=" + mxstep + ")\n") f.write(" " + holdrates[0] + "+=" + str(scandelta) + "\n") f.write(" res=[" + holdrates[0] + "] + list(sol[-1])\n") f.write(" results.append(res)\n") f.write(" print res\n") f.write(" return results\n") f.write("if __name__==\"__main__\":\n") f.write(" thesolver()\n\n") f.close() return os.path.abspath(pyfile) f.write(" sol = odeint(ode_function_rhs, y0, t, mxstep=" + mxstep + ")\n") fmt = format if "-format" in sys.argv: i = (sys.argv).index("-format") + 1 if i < len(sys.argv): fmt = sys.argv[i] fmt = fmt.upper() if not fmt in formattype: print "Error: solver: invalid format = ", fmt, " requested." fmt = "CSV" fmt = formattype[fmt] outfile = output if ("-out" in sys.argv): i = (sys.argv).index("-out") + 1 if i < len(sys.argv): outfile = sys.argv[i] output = utils.uniqueFileName(outfile, type=fmt) else: print "Warning: solver: expecting file name after -out option." #outfile = "solution."+fmt if output == "": basename = os.path.basename(filename) #basename=basename.split(".")[0] outfile = utils.timed_file_name("Solution-for-" + basename, fmt) #f.write(" outfile = \""+utils.uniqueFileName(outfile, type=fmt)+"\"\n") f.write(" outfile = \"" + outfile + "\"\n") f.write(" of = open(outfile,\"w\")\n") f.write(" for i in range(len(t)):\n") f.write(" time = t[i]\n") f.write(" data = map(str,sol[i])\n") demarcater = {"CSV": "\",\"", "TSV": "\"\t\"", "TXT": "\" \""} f.write(" data = str(time) + " + demarcater[fmt] + "+(" + demarcater[fmt] + ".join(data))\n") f.write(" of.write(data+\"\\n\")\n") f.write(" of.close()\n") #print "argv is: ", sys.argv if ("-plot" in sys.argv) or plot == True: plotvars = vars f.write(" plotvars = " + svars + "\n") if ("-plot" in sys.argv): i = (sys.argv).index("-plot") if i > 0: i += 1 while i < len(sys.argv): nextvar = sys.argv[i] i += 1 if nextvar[0] == "-": break nextvar = utils.deindex(str(nextvar)) if nextvar in variables: plotvars.append(nextvar) else: print "Error: requested plot variable: "+nextvar+ " does not "\ + " exist in the model." plotvars = list(set(plotvars)) # remove any dupes f.write(" plotvars = " + str(plotvars) + "\n") if len(plotvars) == 0: plotvars = variables f.write(" plotvars = " + str(plotvars) + "\n") plotcols = plotcolumns if ("-plotcolumns" in sys.argv): i = (sys.argv).index("-plotcolumns") + 1 if i < len(sys.argv): plotcols = int(sys.argv[i]) plotcols = max(plotcols, 1) if ("-sameplot" in sys.argv) or sameplot == True: f.write( " cellerator.solver.samePlot(sol, t, variables, filename, plotvars)\n" ) else: f.write(" cellerator.solver.plotVariables(sol, t, variables, filename,"\ +" plotvars, columns="+str(plotcols)+")\n") f.write(" return\n\n") f.write("if __name__==\"__main__\":\n") f.write(" thesolver()\n\n") f.close() return os.path.abspath(pyfile)
def generateSimulation(inputfile="", step=1, duration=100, plot=False, format="CSV", \ output="", vars=[], plotcolumns=3, sameplot=True): formattype = {"CSV":"CSV","TABLE":"TXT", "TSV":"TSV"} # input: model file # return value: none # output: generates simulation plots from model file # if invoked from command line, "solver -in filename -run duration step" holdrates=[] if "-scan" in sys.argv: i = (sys.argv).index("-scan")+1 if i+3<len(sys.argv): holdrates.append(sys.argv[i]) scanstart = sys.argv[i+1] scanstop = sys.argv[i+2] scandelta = sys.argv[i+3] else: sys.exit("Error: expecting 'K min max delta' after keyword -scan") (r, ic, rates, frozenvars, functions,assignments, filename)=readmodel(INFILE=inputfile) (variables, y0, tmpdotpy) = generatePythonFunction(r, rates, ic, frozenvars, functions, assignments, holdrates) f=open(tmpdotpy,"r") rhs=f.readlines() f.close() try: os.remove(tmpdotpy) except: print "Warning: unable to delete "+os.path.abspath(tmpdotpy) pyfile = "" if ("-pyfile" in sys.argv): i = (sys.argv).index("-pyfile")+1 if i<len(sys.argv): pyfile=sys.argv[i] else: print "Waring: solver: expecting file name after -pyfile option." pyfile = "newsolver.py" if pyfile=="": pyfile=utils.timed_file_name("solver-for-"+os.path.basename(filename),"py") #pyfile = utils.uniqueFileName(pyfile, type="py") f=open(pyfile,"w") f.write("import numpy as np\n") f.write("import cellerator.solver\n") f.write("from scipy.integrate import odeint\n") f.write("\n") for line in rhs: f.write(line) f.write("\n\n\n") f.write("def thesolver():\n") f.write(" filename =\""+filename+"\"\n") svars = str(map(utils.deindex, map(str, variables))) f.write(" variables="+svars+"\n") (runtime, stepsize)=getRunParameters(default_duration=duration,default_step=step) f.write(" runtime = "+str(runtime)+" \n") f.write(" stepsize = "+str(stepsize)+" \n") f.write(" t = np.arange(0,runtime+stepsize,stepsize)\n") f.write(" y0 = "+str(y0)+"\n") if "-mxstep" in sys.argv: i=(sys.argv).index("-mxstep")+1 if i<len(sys.argv): mxstep=sys.argv[i] else: mxstep = "500000" if len(holdrates)>0: f.write(" global " + holdrates[0] + "\n") f.write(" results=[]\n") f.write(" "+holdrates[0]+"="+str(scanstart)+"\n") f.write(" while "+ holdrates[0]+" <= " + str(scanstop) + ":\n") f.write(" sol = odeint(ode_function_rhs, y0, t, mxstep="+mxstep+")\n") f.write(" "+holdrates[0]+"+="+str(scandelta)+"\n") f.write(" res=["+ holdrates[0] + "] + list(sol[-1])\n") f.write(" results.append(res)\n") f.write(" print res\n") f.write(" return results\n") f.write("if __name__==\"__main__\":\n") f.write(" thesolver()\n\n") f.close() return os.path.abspath(pyfile) f.write(" sol = odeint(ode_function_rhs, y0, t, mxstep="+mxstep+")\n") fmt = format if "-format" in sys.argv: i = (sys.argv).index("-format")+1 if i<len(sys.argv): fmt = sys.argv[i] fmt = fmt.upper() if not fmt in formattype: print "Error: solver: invalid format = ", fmt, " requested." fmt = "CSV" fmt = formattype[fmt] outfile=output if ("-out" in sys.argv): i = (sys.argv).index("-out")+1 if i<len(sys.argv): outfile=sys.argv[i] output=utils.uniqueFileName(outfile, type=fmt) else: print "Warning: solver: expecting file name after -out option." #outfile = "solution."+fmt if output=="": basename = os.path.basename(filename) #basename=basename.split(".")[0] outfile = utils.timed_file_name("Solution-for-"+basename,fmt) #f.write(" outfile = \""+utils.uniqueFileName(outfile, type=fmt)+"\"\n") f.write(" outfile = \""+outfile+"\"\n") f.write(" of = open(outfile,\"w\")\n") f.write(" for i in range(len(t)):\n") f.write(" time = t[i]\n") f.write(" data = map(str,sol[i])\n") demarcater={"CSV":"\",\"","TSV":"\"\t\"","TXT":"\" \""} f.write(" data = str(time) + " + demarcater[fmt] + "+(" + demarcater[fmt]+".join(data))\n") f.write(" of.write(data+\"\\n\")\n") f.write(" of.close()\n") #print "argv is: ", sys.argv if ("-plot" in sys.argv) or plot==True: plotvars = vars f.write(" plotvars = "+svars+"\n") if ("-plot" in sys.argv): i = (sys.argv).index("-plot") if i>0: i += 1 while i<len(sys.argv): nextvar = sys.argv[i] i += 1 if nextvar[0]=="-": break nextvar = utils.deindex(str(nextvar)) if nextvar in variables: plotvars.append(nextvar) else: print "Error: requested plot variable: "+nextvar+ " does not "\ + " exist in the model." plotvars = list (set (plotvars) ) # remove any dupes f.write(" plotvars = "+str(plotvars)+"\n") if len(plotvars)==0: plotvars = variables f.write(" plotvars = "+str(plotvars)+"\n") plotcols = plotcolumns if ("-plotcolumns" in sys.argv): i = (sys.argv).index("-plotcolumns") + 1 if i < len(sys.argv): plotcols=int(sys.argv[i]) plotcols = max(plotcols, 1) if ("-sameplot" in sys.argv) or sameplot==True: f.write(" cellerator.solver.samePlot(sol, t, variables, filename, plotvars)\n") else: f.write(" cellerator.solver.plotVariables(sol, t, variables, filename,"\ +" plotvars, columns="+str(plotcols)+")\n") f.write(" return\n\n") f.write("if __name__==\"__main__\":\n") f.write(" thesolver()\n\n") f.close() return os.path.abspath(pyfile)