Exemplo n.º 1
0
def serialize_efm(S, efm_id, model, path):
    efm_txt = os.path.join(path, '%s.txt' % efm_id)
    initial_r_ids = {r_id for r_id in S.r_id2i.keys() if r_id not in S.gr_id2r_id2c}
    r_id2c = S.pws.get_r_id2coeff(efm_id, r_ids=initial_r_ids)
    with open(efm_txt, 'w+') as f:
        f.write('EFM %s of length %d\n\n' % (efm_id, len(r_id2c)))
        write_inputs_outputs(f, model, S.get_boundary_inputs_outputs(efm_id))
        f.write(THIN_DELIMITER)
        write_detailed_r_id2c(model, r_id2c, f)
    return efm_txt
Exemplo n.º 2
0
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 serialize_coupled_reaction_groups(model, path, **kwargs):
    S = kwargs["S"]
    coupled_r_ids = S.coupled_rs
    r_types = S.r_types

    initial_efm_ids = [efm_id for efm_id in S.efm_id2i.keys() if efm_id not in S.gr_id2efm_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))))

    rgroups_txt = os.path.join(path, "coupled_reactions.txt")
    with open(rgroups_txt, "w+") as f:
        f.write("Found %d coupled reaction groups.\n\n" % len(coupled_r_ids))
        f.write(THICK_DELIMITER)

        # types that include coupled reactions
        for rtype_id in sorted(
            (rtype_id for rtype_id in r_types if set(S.gr_id2r_id2c[rtype_id].keys()) & coupled_r_ids),
            key=lambda rtype_id: (-len(S.gr_id2r_id2c[rtype_id]), rtype_id),
        ):
            r_id2c = S.gr_id2r_id2c[rtype_id]
            cur_r_ids = set(r_id2c.keys())
            cur_coupled_ids = cur_r_ids & coupled_r_ids
            cur_simple_ids = cur_r_ids - coupled_r_ids

            write_inputs_outputs(f, model, S.st_matrix.get_inputs_outputs(rtype_id))

            f.write(
                "Type %s common to %d reaction group%s%s:\n\n"
                % (
                    rtype_id,
                    len(cur_coupled_ids),
                    "s" if len(cur_coupled_ids) != 1 else "",
                    (" and %d reaction%s" % (len(cur_simple_ids), "s" if len(cur_simple_ids) > 1 else ""))
                    if cur_simple_ids
                    else "",
                )
            )
            f.write(THIN_DELIMITER)
            for cr_id in sorted(cur_coupled_ids, key=lambda lr_id: (-len(S.gr_id2r_id2c[lr_id]), lr_id)):
                write_coupled_rn_group(cr_id, inverted=r_id2c[cr_id] < 0, tab="\t")
                f.write("\t%s" % THIN_DELIMITER)

            if cur_simple_ids:
                f.write(
                    "\tReaction%s %s also ha%s the same structure.\n\n"
                    % (
                        "s" if len(cur_simple_ids) != 1 else "",
                        ", ".join((("-%s" % r_id) if r_id2c[r_id] < 0 else r_id for r_id in sorted(cur_simple_ids))),
                        "s" if len(cur_simple_ids) == 1 else "ve",
                    )
                )
            f.write(THICK_DELIMITER)

        # coupled reaction that form an unique type
        for cr_id in sorted(S.coupled_rs & S.r_ids, key=lambda lr_id: (-len(S.gr_id2r_id2c[lr_id]), lr_id)):
            write_inputs_outputs(f, model, S.st_matrix.get_inputs_outputs(cr_id))
            write_coupled_rn_group(cr_id)
            f.write(THICK_DELIMITER)

        # non-coupled reaction groups
        f.write(THICK_DELIMITER)
        for rtype_id in sorted(
            (rtype_id for rtype_id in r_types if not set(S.gr_id2r_id2c[rtype_id].keys()) & coupled_r_ids),
            key=lambda rtype_id: (-len(S.gr_id2r_id2c[rtype_id]), rtype_id),
        ):
            write_inputs_outputs(f, model, S.st_matrix.get_inputs_outputs(rtype_id))
            r_id2c = S.gr_id2r_id2c[rtype_id]
            f.write(
                "Type %s is common to reaction%s %s.\n\n"
                % (
                    rtype_id,
                    "s" if len(r_id2c) != 1 else "",
                    ", ".join((("-%s" % r_id) if r_id2c[r_id] < 0 else r_id for r_id in sorted(r_id2c.keys()))),
                )
            )
            f.write(THICK_DELIMITER)
    return rgroups_txt, len(coupled_r_ids)