def analyse_by_fva(cobra_model, bm_r_id, objective_sense=MAXIMIZE, threshold=0): cobra_bm_r_id = format_r_id(bm_r_id) opt_value = optimise_biomass(cobra_model, cobra_bm_r_id, objective_sense, level=logging.DEBUG) if opt_value: opt_value = round_value(opt_value) r_id2bounds = get_r_id2fva_bounds(cobra_model, threshold=threshold) return r_id2bounds, opt_value return {}, None
def analyse_by_fba(cobra_model, bm_r_id, objective_sense=MAXIMIZE, threshold=0): cobra_bm_r_id = format_r_id(bm_r_id) opt_value = optimise_biomass(cobra_model, cobra_bm_r_id, objective_sense, level=logging.DEBUG) if opt_value: opt_value = round_value(opt_value) r_id2val = get_fluxes_larger_than_threshold(cobra_model, threshold=threshold) return r_id2val, opt_value return {}, None
def create_fva_model(sbml, r_id2bounds, new_sbml): def r_updater(r): l_b, u_b = r_id2bounds[format_r_id(r.id)] set_bounds(r, l_b, max(u_b, 0)) if l_b * u_b >= 0: r.setReversible(False) r_ids = set(r_id2bounds.keys()) | {format_r_id(r_id, False) for r_id in r_id2bounds.keys()} r_ids2sbml(r_ids, sbml, new_sbml, 'FVA', r_updater) return new_sbml
def update_vis_layers(r_ids, layer, id2mask, layer2mask, mask_shift, vis_r_ids): if not r_ids: return mask_shift l_mask = 1 << mask_shift layer2mask[layer] = l_mask for r_id in r_ids: o_r_id = format_r_id(r_id, remove_prefix=False) vis_r_ids |= {r_id, o_r_id} id2mask[r_id] |= l_mask id2mask[o_r_id] |= l_mask return mask_shift + 1
def serialize_fva(model, r_id2bounds, path, objective_sense, out_r_id): out_r_id = format_r_id(out_r_id) title = '%s reaction %s (%s): %.4g\n==================================\n'\ % (objective_sense, out_r_id, get_cobra_r_formula(model.reactions.get_by_id(out_r_id), comp=True), r_id2bounds[out_r_id][0]) values2r_ids = defaultdict(set) for r_id, (min_v, max_v) in r_id2bounds.items(): values2r_ids[(min_v, max_v)].add(r_id) keys = sorted(values2r_ids.keys(), key=lambda min_max_v: (-abs(min_max_v[0] - min_max_v[1]), min_max_v[0], min_max_v[1])) ess_count, var_count = 0, 0 with open(path, 'w+') as f: if title: f.write(title + '\n') f.write("==============================\nESSENTIAL FLUXES\n==============================\n") for (min_v, max_v) in keys: if min_v * max_v > 0: v_r_ids = values2r_ids[(min_v, max_v)] value = format_values(min_v, max_v) ess_count += len(v_r_ids) for r_id in sorted(v_r_ids): f.write("%s\t%s: %s\n" % (value, r_id, get_cobra_r_formula(model.reactions.get_by_id(r_id), comp=True))) f.write('\n') f.write("==============================\nOTHER FLUXES\n==============================\n") for (min_v, max_v) in keys: if min_v * max_v <= 0: v_r_ids = values2r_ids[(min_v, max_v)] value = format_values(min_v, max_v) var_count += len(v_r_ids) for r_id in sorted(v_r_ids): f.write("%s\t%s: %s\n" % (value, r_id, get_cobra_r_formula(model.reactions.get_by_id(r_id), comp=True))) f.write('\n') f.write("==============================\n") f.write("In total, found %d essential reactions and %d variable reactions" % (ess_count, var_count)) return ess_count, var_count
def r_updater(r): l_b, u_b = r_id2bounds[format_r_id(r.id)] set_bounds(r, l_b, max(u_b, 0)) if l_b * u_b >= 0: r.setReversible(False)