def save_fba_to_excel(c3_model, c4_model, D_fba, D_exp, theNotebook, file_name='pfba_results'): writer = pd.ExcelWriter(theNotebook+'/excel/'+file_name+'.xlsx') L_r_transport = [] for r_obj in c4_model.reactions: if r_obj.id[0:3] == '[MB]': L_r_transport.append(r_obj.id) for exp in D_exp: DF_fba = pd.DataFrame(index=[r_obj.id for r_obj in c3_model.reactions]+L_r_transport,columns=['rxn','lb','ub','MC-Flux','BC-Flux',]) for r_obj in c3_model.reactions: r_id = r_obj.id r_M_flux = D_fba[exp]['[M]_'+r_id] r_B_flux = D_fba[exp]['[B]_'+r_id] r_lb = '0' if r_obj.lower_bound == 0. else '-inf' if r_obj.lower_bound == -inf else str(r_obj.lower_bound) r_ub = '0' if r_obj.upper_bound == 0. else 'inf' if r_obj.upper_bound == inf else str(r_obj.upper_bound) DF_fba.loc[r_id] = [r_obj.reaction,r_lb,r_ub,r_M_flux,r_B_flux] DF_fba.loc['[B]_RBC_h_Ex']=[c4_model.reactions.get_by_id('[B]_RBC_h_Ex').reaction,"-inf","inf","0",D_fba[exp]['[B]_RBC_h_Ex']] for r_id in L_r_transport: r_obj = c4_model.reactions.get_by_id(r_id) r_MB_flux = D_fba[exp][r_id] r_lb = '0' if r_obj.lower_bound == 0. else '-inf' if r_obj.lower_bound == -inf else str(r_obj.lower_bound) r_ub = '0' if r_obj.upper_bound == 0. else 'inf' if r_obj.upper_bound == inf else str(r_obj.upper_bound) DF_fba.loc[r_id] = [r_obj.reaction,r_lb,r_ub,r_MB_flux, float('nan')] sheet = re.sub(r"[\[,:,*,?, \],\\,/]",'_', D_exp[exp]) DF_fba.to_excel(writer,sheet) writer.save()
def save_fva_to_excel(c3_model, c4_model, D_fva, D_exp, theNotebook, file_name='fva_results'): writer = pd.ExcelWriter(theNotebook+'/excel/'+file_name+'.xlsx') L_r_transport = [] for r_obj in c4_model.reactions: if r_obj.id[0:3] == '[MB]': L_r_transport.append(r_obj.id) for exp in D_exp: DF_fva = pd.DataFrame(index=[r_obj.id for r_obj in c3_model.reactions]+L_r_transport,columns=['rxn','lb','ub','MC-min','MC-max','BC-min','BC-max',]) for r_obj in c3_model.reactions: r_id = r_obj.id r_M_max = D_fva[exp].get_value('[M]_'+r_id,'maximum') r_M_min = D_fva[exp].get_value('[M]_'+r_id,'minimum') r_B_max = D_fva[exp].get_value('[B]_'+r_id,'maximum') r_B_min = D_fva[exp].get_value('[B]_'+r_id,'minimum') r_lb = '0' if r_obj.lower_bound == 0. else '-inf' if r_obj.lower_bound == -inf else str(r_obj.lower_bound) r_ub = '0' if r_obj.upper_bound == 0. else 'inf' if r_obj.upper_bound == inf else str(r_obj.lower_bound) DF_fva.loc[r_id] = [r_obj.reaction,r_lb,r_ub,r_M_min,r_M_max,r_B_min,r_B_max] for r_id in L_r_transport: r_obj = c4_model.reactions.get_by_id(r_id) r_MB_max = D_fva[exp].get_value(r_id,'maximum') r_MB_min = D_fva[exp].get_value(r_id,'minimum') r_lb = '0' if r_obj.lower_bound == 0. else '-inf' if r_obj.lower_bound == -inf else str(r_obj.lower_bound) r_ub = '0' if r_obj.upper_bound == 0. else 'inf' if r_obj.upper_bound == inf else str(r_obj.lower_bound) DF_fva.loc[r_id] = [r_obj.reaction,r_lb,r_ub,r_MB_min,r_MB_max, float('nan'), float('nan')] sheet = re.sub(r"[\[,:,*,?, \],\\,/]", '_', D_exp[exp]) DF_fva.to_excel(writer,sheet) writer.save()
def get_fluxes_by_metabolite(D_exp, D_fba, model, m_id, cell): if cell in ['M', 'B']: df = pd.DataFrame(columns=['rxn'] + [D_exp[exp] for exp in D_exp]) m_obj = model.metabolites.get_by_id('[' + cell + ']_' + m_id) for r_obj in m_obj.reactions: if not r_obj.id in df.index: df.loc[r_obj.id] = [r_obj.reaction] + [ round(D_fba[exp][r_obj.id], 3) for exp in D_exp ] return df else: return 'Choose either M or B as cell type'
def plot_transport_BM(D_fba, D_exp, L_r, xaxis_title, save_fig, cut_off): df = pd.DataFrame(index=[], columns=[D_exp[exp] for exp in sorted(D_fba.keys())]) yTickNames_BM = [] D_rid_meanFlux_BM = { r_id: abs( np.mean([0] + [D_fba[x][r_id] for x in D_exp if D_fba[x][r_id] < 0])) for r_id in L_r if abs( np.mean([0] + [D_fba[x][r_id] for x in D_exp if D_fba[x][r_id] < 0])) > cut_off } D_rid_meanFlux_BM = sorted(D_rid_meanFlux_BM.items(), key=operator.itemgetter(1)) for entry in D_rid_meanFlux_BM: r_id = entry[0] for x in D_exp: if D_fba[x][r_id] < 0: df.set_value( r_id, D_exp[x], abs(D_fba[x][r_id]) if not abs(D_fba[x][r_id]) < cut_off else float('nan')) if not r_id[5:-2] in yTickNames_BM: yTickNames_BM.append(r_id[5:-2]) scl = cl.scales['9']['seq']['Blues'] colorscale = [[float(i) / float(len(scl) - 1), scl[i]] for i in range(len(scl))] trace_BM = go.Heatmap(z=df.values.tolist(), x=df.columns, y=yTickNames_BM, colorbar={ 'title': 'Flux [µmol/s/m2]', 'titleside': 'right' }, colorscale=colorscale) layout_BM = go.Layout(width=500, margin={'b': 100}, yaxis={ 'tickmode': 'array', 'tickvals': range(0, len(yTickNames_BM)), 'ticktext': yTickNames_BM, 'title': 'Transport Metabolites' }, xaxis={ 'title': xaxis_title, 'tickangle': 45 if len(D_exp) >= 8 else 0 }, title='Bundlesheat ==> Mesophyll Transport') data_BM = [trace_BM] fig_BM = go.Figure( data=data_BM, layout=layout_BM, ) if save_fig: iplot(fig_BM, image='svg', filename='transport_BM', image_width=500, image_height=500) sleep(5) else: iplot(fig_BM)