def serialize_efms(model, path, S): initial_efm_ids = {efm_id for efm_id in S.efm_id2i.keys() if efm_id not in S.gr_id2efm_ids} initial_r_ids = {r_id for r_id in S.r_id2i.keys() if r_id not in S.gr_id2r_id2c} all_fm_intersection = S.get_efm_intersection(initial_efm_ids, r_ids=initial_r_ids) all_fm_intersection_folded = S.get_efm_intersection(initial_efm_ids) def get_key(r_id): mr_id, mc = '', 0 gr_id, gc = '', 0 if r_id in S.r_id2gr_id: gr_id = S.r_id2gr_id[r_id] gc = S.gr_id2r_id2c[gr_id][r_id] if gr_id in S.r_id2gr_id: mr_id = S.r_id2gr_id[gr_id] mc = S.gr_id2r_id2c[mr_id][gr_id] in_intersection = 1 if r_id in all_fm_intersection or r_id in all_fm_intersection_folded \ or gr_id and gr_id in all_fm_intersection_folded \ or mr_id and mr_id in all_fm_intersection_folded else 0 return in_intersection, (mr_id, mc), (gr_id, gc), r_id pathways_txt = os.path.join(path, 'pathways.txt') with open(pathways_txt, 'w+') as f: f.write('Found %d EFMs, grouped into %d behaviours:\n\n' % (len(initial_efm_ids), len(S.efm_ids))) f.write(THICK_DELIMITER) if all_fm_intersection and len(all_fm_intersection): f.write('All EFMs contain following %d reactions:\n\t%s.\n\n' % (len(all_fm_intersection), r_id2c_to_string(all_fm_intersection, binary=True, get_key=get_key))) f.write(THICK_DELIMITER) for fm_id in sorted(S.efm_ids): write_pathway(model=model, pathway_id=fm_id, S=S, get_key=get_key, f=f) f.write(THICK_DELIMITER) return pathways_txt, (len(initial_efm_ids), len(S.efm_ids))
def write_coupled_rn_group(cr_id, inverted=False, tab=""): r_id2c = S.gr_id2r_id2c[cr_id] f.write( "%sCoupled reaction group %s of length %d%s:\n\n\t\t%s\n\n" % ( tab, cr_id, len(r_id2c), " (Inputs and Outputs are inverted)" if inverted else "", r_id2c_to_string(r_id2c), ) ) efm_ids = sorted(S.get_efm_ids_by_r_id(cr_id, efm_ids=initial_efm_ids)) f.write("%sFound in %d EFMs: %s.\n\n" % (tab, len(efm_ids), ", ".join((str(efm_id) for efm_id in efm_ids))))
def write_folded_efm(f_efm_id, S, get_key, f, tab=''): if f_efm_id not in S.folded_efms: write_efm(efm_id=f_efm_id, S=S, get_key=get_key, f=f, tab=tab) return f.write('%s%s of length %d:\n\n%s\t%s\n\n' % (tab, f_efm_id, S.get_len(f_efm_id), tab, r_id2c_to_string(S.pws.get_r_id2coeff(f_efm_id, r_ids=S.r_ids), get_key=get_key))) efm_ids = S.gr_id2efm_ids[f_efm_id] f.write('%s\tContains %s' % (tab, ('the following %d EFMs:\n\n' % len(efm_ids)) if len(efm_ids) != 1 else '')) started = False for efm_id in efm_ids: if started: f.write('%s\t\t%s' % (tab, TINY_DELIMITER)) else: started = True write_efm(efm_id=efm_id, S=S, get_key=get_key, f=f, tab='%s\t\t' % tab, no_first_tab=len(efm_ids) == 1)
def write_pathway(model, pathway_id, S, get_key, f): write_inputs_outputs(f, model, S.get_boundary_inputs_outputs(pathway_id)) if pathway_id not in S.pathways: write_folded_efm(f_efm_id=pathway_id, S=S, get_key=get_key, f=f) return f.write('%s of length %d:\n\n\t%s\n\n' % (pathway_id, S.get_len(pathway_id), r_id2c_to_string(S.pws.get_r_id2coeff(pathway_id, r_ids=S.r_ids), get_key=get_key))) folded_efm_ids = S.gr_id2efm_ids[pathway_id] f.write('\tContains %d elements:\n\n' % len(folded_efm_ids)) started = False for f_efm_id in folded_efm_ids: if started: f.write('\t\t%s' % THIN_DELIMITER) else: started = True write_folded_efm(f_efm_id=f_efm_id, S=S, get_key=get_key, f=f, tab='\t\t')
def write_efm(efm_id, S, get_key, f, tab='', no_first_tab=False): initial_r_ids = {r_id for r_id in S.r_id2i.keys() if r_id not in S.gr_id2r_id2c} f.write('%s%s of length %d:\n\n%s\t%s\n\n' % ('' if no_first_tab else tab, efm_id, S.get_len(efm_id, r_ids=initial_r_ids), tab, r_id2c_to_string(S.pws.get_r_id2coeff(efm_id, r_ids=initial_r_ids), get_key=get_key)))