def plot_fluxes( flux_dict, escher_map, output_file='map.html', height=600, width=800, reaction_scale=None, min_flux=-10, max_flux=10, ): if min_flux is None: min_flux = min(flux_dict) if max_flux is None: max_flux = max(flux_dict) if reaction_scale is None: reaction_scale = [{ 'type': 'value', 'value': min_flux, 'color': 'red', 'size': 32 }, { 'type': 'value', 'value': 0, 'color': '#c8c8c8', 'size': 12 }, { 'type': 'value', 'value': max_flux, 'color': 'green', 'size': 32 }] builder = Builder(height=height, width=width, map_json=escher_map, reaction_scale=reaction_scale) builder.reaction_data = flux_dict builder.save_html(output_file) builder.close()
# Load a COBRA model builder.model_name = 'e_coli_core' # Find any reactions that are in the map and not in the model, and turn them red builder.highlight_missing = True get_ipython().getoutput("wget -nc http://bigg.ucsd.edu/static/models/iJO1366.json") builder.model = cobra.io.load_json_model('iJO1366.json') # Run FBA with the model and add the flux data to the map solution = builder.model.optimize() builder.reaction_data = solution.fluxes # Add some data for metabolites builder.metabolite_data = solution.shadow_prices # Simplify the map by hiding some labels builder.hide_secondary_metabolites = True builder.hide_all_labels = True builder.reaction_scale = [ { 'type': 'min', 'color': '#000000', 'size': 12 }, { 'type': 'median', 'color': '#ffffff', 'size': 20 }, { 'type': 'max', 'color': '#ff0000', 'size': 25 }
def animate_fluxes( flux_time_data, escher_map, outputfile='animation.mp4', height=600, width=800, time_interval_ms=100, chrome=DEFAULT_CHROME, reaction_scale=None, min_flux=None, max_flux=None, time_size=12, time_unit='h', x_time=0.95, y_time=0.9, ): if min_flux is None: min_flux = flux_time_data.min().min() if max_flux is None: max_flux = flux_time_data.max().max() if reaction_scale is None: reaction_scale = [{ 'type': 'value', 'value': min_flux, 'color': 'red', 'size': 32 }, { 'type': 'value', 'value': 0, 'color': '#c8c8c8', 'size': 12 }, { 'type': 'value', 'value': max_flux, 'color': 'green', 'size': 32 }] builder = Builder(height=height, width=width, map_json=escher_map, embedd_css=EMBEDD_CSS, menu='none', reaction_scale=reaction_scale) myimages = [] fig = plt.figure() XVFB_DOCKER = '/usr/bin/xvfb-run -a -s \"-screen 0 {}x{}x24\"'.format( width, height) SCREENSHOT = "--headless --disable-gpu --no-sandbox --virtual-time-budget=10000 --screenshot=\'{}\' {}" for t, fluxes in flux_time_data.iterrows(): builder.reaction_data = fluxes builder.save_html('tmp.html', ) # Hacky hack hack ... # Use chrome to make a screenshot cmd = "{} {} {}".format(XVFB_DOCKER, chrome, SCREENSHOT.format('tmp.png', 'tmp.html')) os.system(cmd) # Add time text ylim = plt.gca().get_ylim() xlim = plt.gca().get_xlim() y = (ylim[1] - ylim[0]) * y_time + ylim[0] x = (xlim[1] - xlim[0]) * x_time + xlim[0] text = plt.text(x, y, '{:.1f} {}'.format(t, time_unit), horizontalalignment='right', fontsize=time_size) img = mgimg.imread('tmp.png') imgplot = plt.imshow(img) # append AxesImage object to the list myimages.append([imgplot, text]) plt.axis('off') fig.tight_layout() anim = animation.ArtistAnimation(fig, myimages, interval=time_interval_ms) anim.save(outputfile, dpi=300) builder.close()
map_name='e_coli_core.Core metabolism', model_name='e_coli_core', ) Core model = cobra.io.load_json_model('e_coli_core.json') reverse = True step = 0.1 timestep = 0.1 duration = 1500 # seconds lim = [0, 0.5] val = lim[-1] for _ in range(int(duration / timestep)): model.reactions.EX_o2_e.lower_bound = -val solution = model.optimize() Core.reaction_data = solution.fluxes if val <= lim[0]: reverse = True if val >= lim[1]: reverse = False if reverse: val += step else: val -= step sleep(timestep) Core.save_html("Core.html") CarbonMeta = Builder(model_json="Recon3D.json", map_name='RECON1.Carbohydrate metabolism') CarbonMeta.save_html("CarbonMeta.html")