示例#1
0
def import_sbml(input_model, sbml_file):
    logging.info('parsing ChEBI')
    chebi = parse_simple(get_chebi())

    logging.info('reading generalized model from %s' % sbml_file)
    r_id2g_id, s_id2gr_id, ub_sps = get_quotient_maps(chebi, sbml_file)

    logging.info('fixing labels and compartments')
    check_names(input_model)
    check_compartments(input_model)

    logging.info('annotating with GO')
    go = parse_simple(get_go())
    annotate_compartments(input_model, go)
    c_id2level = comp2level(input_model, go)
    c_id2info, c_id2outs = compute_c_id2info(c_id2level, input_model)

    def get_r_comp(all_comp_ids):
        if len(all_comp_ids) == 1:
            return all_comp_ids.pop()
        get_level = lambda c_id: c_id2level[c_id][0]
        outer_most = min(all_comp_ids, key=get_level)
        inner_most = max(all_comp_ids, key=get_level)
        outer_level, inner_level = get_level(outer_most), get_level(inner_most)
        if outer_level == inner_level or (outer_most
                                          not in c_id2outs[inner_most]):
            candidates = set(c_id2outs[inner_most]) & set(
                c_id2outs[outer_most])
            if candidates:
                return max(candidates, key=get_level)
            else:
                return outer_most
        if inner_level - outer_level > 1:
            return max(c_id2outs[inner_most], key=get_level)
        return outer_most

    logging.info('initialising the graph')
    graph = tlp.newGraph()
    graph.setName(input_model.getId())
    create_props(graph)

    logging.info('adding species nodes')
    id2n = species2nodes(graph, input_model, ub_sps)

    logging.info('adding reaction nodes')
    reactions2nodes(get_r_comp, graph, id2n, input_model)

    # for n in (n for n in graph.getNodes() if TYPE_SPECIES == graph[TYPE][n] and graph.deg(n) > 5 \
    # and not graph[ID][n] in s_id2gr_id):
    # 	graph[UBIQUITOUS][n] = True

    logging.info('duplicating nodes')
    duplicate_nodes(graph)

    logging.info('marking species/reaction groups')
    mark_ancestors(graph, r_id2g_id, s_id2gr_id, c_id2info)
    return graph, c_id2info, c_id2outs, chebi, ub_sps
def map_metabolites_compartments(model_id2dfs, chebi=None):
    if not chebi:
        chebi = parse_simple(get_chebi())

    model_id2m_ids_groups = map_metabolites(model_id2dfs, chebi)
    model_id2c_ids_groups = map_comps(model_id2dfs)

    model_id2c_id2i = defaultdict(dict)
    for i, model_id2c_ids in enumerate(model_id2c_ids_groups):
        for model_id, c_ids in model_id2c_ids.items():
            model_id2c_id2i[model_id].update({c_id: i for c_id in c_ids})

    model_id2m_ids_same_comp_groups = []
    for model_id2m_ids in model_id2m_ids_groups:
        i2m_ids = defaultdict(list)
        for model_id, m_ids in model_id2m_ids.items():
            for m_id in m_ids:
                df, _, _ = model_id2dfs[model_id]
                c_id = df.at[m_id, 'Compartment']
                if c_id in model_id2c_id2i[model_id]:
                    i2m_ids[model_id2c_id2i[model_id][c_id]].append((model_id, m_id))
        for m_ids in i2m_ids.values():
            model_id2m_ids = defaultdict(set)
            if len(m_ids) > 1:
                for model_id, m_id in m_ids:
                    model_id2m_ids[model_id].add(m_id)
            if model_id2m_ids:
                model_id2m_ids_same_comp_groups.append(model_id2m_ids)
    return model_id2c_ids_groups, [it for it in model_id2m_ids_same_comp_groups if len(it) > 1], model_id2c_id2i
示例#3
0
def merge_models(in_sbml_list, out_sbml):
    if not in_sbml_list:
        raise ValueError('Provide SBML models to be merged')
    go = parse_simple(get_go())
    chebi = parse_simple(get_chebi())
    i = 0
    model_ids = set()
    go2c_id = {}

    doc = libsbml.SBMLDocument(2, 4)
    model = doc.createModel()
    model.setId('m_merged')
    m_c_ids = set()

    for o_sbml in in_sbml_list:
        o_doc = libsbml.SBMLReader().readSBML(o_sbml)
        set_consistency_level(o_doc)
        o_doc.checkL2v4Compatibility()
        o_doc.setLevelAndVersion(2, 4, False, True)
        o_model = o_doc.getModel()
        logging.info("Processing %s" % o_sbml)
        model_id = get_model_id(i, model_ids, o_model)

        update_model_element_ids(model_id, o_model, go2c_id, go, chebi)
        for e in o_model.getListOfCompartments():
            c_id = e.getId()
            if c_id not in m_c_ids:
                if model.addCompartment(e):
                    copy_compartment(e, model)
                m_c_ids.add(c_id)
        for e in o_model.getListOfSpecies():
            if model.getSpecies(e.getId()):
                continue
            if model.addSpecies(e):
                copy_species(e, model)
        for e in o_model.getListOfReactions():
            if model.addReaction(e):
                copy_reaction(e, model)

    libsbml.writeSBMLToFile(doc, out_sbml)
示例#4
0
def add_boundary_metabolites(in_sbml, out_sbml):
    """
    Creates a boundary compartment with the id 'Boundary',
    and transforms each input/output reaction '*_e <-> ' into '*_e <-> *_b'.
    :param in_sbml: path to the SBML file with the original model
    :param out_sbml: path where to store the resulting SBML file
    """
    input_doc = libsbml.SBMLReader().readSBML(in_sbml)
    model = input_doc.getModel()
    chebi = parse_simple(get_chebi())
    annotate_metabolites(model, chebi)
    create_boundary_species_in_boundary_reactions(model)
    libsbml.SBMLWriter().writeSBMLToFile(input_doc, out_sbml)
示例#5
0
def combine_models(model_id2sbml, model_id2S, path):
    logging.info('Going to merge models...')
    chebi = parse_simple(get_chebi())
    model_id2dfs = get_model_data(model_id2sbml)
    model_id2c_id_groups, model_id2m_id_groups, model_id2c_id2i = \
        map_metabolites_compartments(model_id2dfs, chebi=chebi)
    logging.info('Mapped metabolites and compartments.')
    ignore_m_ids = get_ignored_metabolites(model_id2dfs, get_proton_ch_ids())
    S = join(model_id2m_id_groups, model_id2S)
    ignore_m_ids |= {S.m_id2gr_id[m_id] for m_id in ignore_m_ids if m_id in S.m_id2gr_id}
    merge(S, ignore_m_ids)
    model_id2r_id_groups = get_r_id_groups(S)
    logging.info('Mapped reactions.')
    sbml = os.path.join(path, 'Merged_model.xml')
    model_id2id2id, common_ids, S = simple_merge_models(S, model_id2c_id2i, model_id2dfs, sbml)
    return sbml, S, model_id2id2id, common_ids, model_id2dfs, \
           (model_id2c_id_groups, model_id2m_id_groups, model_id2r_id_groups)
def multimodel_pipeline(sbml2parameters, res_dir, treeefm_path, max_efm_number=1000, rewrite=True, org=None):
    create_dirs(res_dir, rewrite)
    get_f_path = lambda f: os.path.join('..', os.path.relpath(f, res_dir)) if f else None
    tab2html = {}

    model_id2sbml, model_id2S, model_id2efm_id2pws = {}, {}, {}

    name2pw = get_name2pw()
    pts = parse_simple(get_pts())
    root_ids = {t.get_id() for t in pts.get_roots()}

    chebi = parse(CHEBI)
    ub_ch_ids = get_ubiquitous_chebi_ids(add_common=True, add_cofactors=True, chebi=chebi)

    efm_id2pws = {}

    model_id2cofactors = {}
    modeld_id2m_id2chebi_id = {}

    for sbml, (r_id2rev, r_id2rev_banned) in sbml2parameters.items():
        doc = libsbml.SBMLReader().readSBML(sbml)
        model = doc.getModel()

        model_name = get_model_name(model=model)
        short_model_name = model_name
        if len(model_name) > 12:
            short_model_name = model_name[:10].strip('-_ ')
            if len(short_model_name) == 10:
                short_model_name += '...'
        safe_m_name = ''.join(ch for ch in short_model_name.replace(' ', '_') if ch.isalnum() or '_' == ch)
        logging.info('Analysing %s...' % model_name)

        # create directories to store results
        logging.info("Preparing directories...")
        m_dir = os.path.join(res_dir, safe_m_name)
        create_dirs(m_dir, rewrite)

        # exchange_rs = get_exchange_reactions(model)
        # csv = '%s/%s.exchanges.csv' % (m_dir, safe_m_name)
        # df2csv(reactions2df(model, exchange_rs), csv)

        cofactors = select_metabolite_ids_by_term_ids(model, ub_ch_ids)

        if r_id2rev:
            constraint_exchange_reactions(model, forsed_r_id2rev=r_id2rev, prohibited_r_id2rev=r_id2rev_banned,
                                          cofactors=cofactors if not r_id2rev_banned else None)

        logging.info("Annotating the model...")
        annotate(model, org=org, reactions=False, pathways=False, chebi=chebi)
        m_id2ch_id = get_species_id2chebi_id(model)

        # copy our model in the result directory
        sbml = os.path.join(m_dir, '%s.constrained.xml' % safe_m_name)
        libsbml.SBMLWriter().writeSBMLToFile(doc, sbml)

        description = model_serializer.serialize(sbml, model, model_name, r_id2rev, m_dir, get_f_path)

        pw2rs = get_pathways(model, pts, name2pw, root_ids)

        logging.info("Performing EFMA...")
        efma_dir = os.path.join(m_dir, 'efma')
        create_dirs(efma_dir, rewrite)

        S, efm_id2pws = analyse_model_efm(model, efma_dir, r_id2rev, tree_efm_path=treeefm_path,
                                          max_efm_number=max_efm_number, rewrite=rewrite, pw2rs=pw2rs)

        for serializer in (efm_serializer.serialize, coupled_reaction_group_serializer.serialize):
            description += \
                serializer(model=model, path=efma_dir, get_f_path=get_f_path, S=S, model_name=model_name)

        if S.gr_id2r_id2c:
            sbml = os.path.join(efma_dir, '%s.folded.xml' % safe_m_name)
            create_folded_model(S, model)
            libsbml.SBMLWriter().writeSBMLToFile(doc, sbml)

        if not S or not S.efm_id2i:
            description += describe('nothing_found.html')

        model_id2sbml[safe_m_name] = sbml
        model_id2S[safe_m_name] = S
        model_id2efm_id2pws[safe_m_name] = efm_id2pws
        model_id2cofactors[safe_m_name] = cofactors
        modeld_id2m_id2chebi_id[safe_m_name] = m_id2ch_id

        tab2html['Analysis of %s' % short_model_name] = description, None

    cofactors = set()
    m_id2ch_id = {}
    if len(model_id2sbml) > 1:
        mm_dir = os.path.join(res_dir, 'merged_model')
        create_dirs(mm_dir)

        sbml, S, model_id2id2id, common_ids, model_id2dfs, mappings = combine_models(model_id2sbml, model_id2S, mm_dir)

        for model_id in model_id2sbml.keys():
            efm_id2pws.update({model_id2id2id[model_id][efm_id]: pws
                               for (efm_id, pws) in model_id2efm_id2pws[model_id].items()
                               if efm_id in model_id2id2id[model_id]})
            cofactors |= {model_id2id2id[model_id][m_id] for m_id in model_id2cofactors[model_id]
                          if m_id in model_id2id2id[model_id]}
            m_id2ch_id.update({model_id2id2id[model_id][m_id]: ch_id
                               for (m_id, ch_id) in modeld_id2m_id2chebi_id[model_id].items()
                               if m_id in model_id2id2id[model_id]})

        tab2html['Model comparison'] = mapping_serializer.serialize(model_id2dfs, *mappings, mm_dir, get_f_path), None
        title = 'Combined model analysis'
    else:
        model_id, sbml = next(model_id2sbml.items())
        efm_id2pws = model_id2efm_id2pws[model_id]
        cofactors = model_id2cofactors[model_id]
        m_id2ch_id = modeld_id2m_id2chebi_id[model_id]
        S = model_id2S[model_id].get_main_S()
        info, title, id2color = '', 'Model analysis', None

    # Communities
    logging.info("Analysing communities...")
    comm_dir = os.path.join(res_dir, 'communities')
    create_dirs(comm_dir, rewrite)

    # id2cluster = detect_communities_by_inputs_of_type(S, 'AMINO ACID', m_id2ch_id, chebi)
    id2cluster = detect_communities_by_boundary_metabolites(S, cofactors=cofactors, threshold=50)

    if id2cluster:
        doc = libsbml.SBMLReader().readSBML(sbml)
        model = doc.getModel()
        description = \
            community_serializer.serialize(model, S, id2cluster, comm_dir, get_f_path, m_id2ch_id, chebi)
        if len(model_id2sbml) > 1:
            tab2html['Model comparison'] = tab2html['Model comparison'][0] + description, None
        else:
            tab2html['EFM communities'] = description, None

    serialize(res_dir, tab2html, title)
示例#7
0
          <body>
          <br/>
          <p class="centre">We are visualizing your model now...</p>
          <br/>
          <img class="img-centre" src="http://mimoza.bordeaux.inria.fr/lib/modelmap/ajax-loader.gif" id="img" />
          <div id="hidden" style="visibility:hidden;height:0px;">''')

sys.stdout.flush()

temp = os.dup(sys.stdout.fileno())
try:
    url = '/%s/comp.html' % m_dir_id

    if not os.path.exists(os.path.join('..', m_dir_id, 'comp.html')):
        chebi = parse_simple(get_chebi())
        reader = libsbml.SBMLReader()
        input_document = reader.readSBML(groups_sbml)
        input_model = input_document.getModel()

        # sbml -> tulip graph
        logging.info('sbml -> tlp')
        graph, c_id2info, c_id2outs, chebi, ub_sps = import_sbml(
            input_model, groups_sbml)

        try:
            n2xy = parse_layout_sbml(groups_sbml)
        except LoPlError:
            n2xy = None

        fc, (n2lo,
示例#8
0
def analyse_model(
    sbml,
    out_r_id,
    out_rev,
    res_dir,
    in_m_id,
    out_m_id,
    in_r_id2rev=None,
    threshold=ZERO_THRESHOLD,
    do_fva=True,
    do_fba=True,
    do_efm=True,
    max_efm_number=1000,
    mask_shift=4,
    get_f_path=None,
    tree_efm_path=TREEEFM_PATH,
    main_dir=None,
    rewrite=True,
):
    model_name = get_model_name(sbml)
    logging.info("Analysing %s..." % model_name)

    # create directories to store results
    logging.info("Preparing directories...")
    res_dir = os.path.join(res_dir, "".join(ch for ch in model_name.replace(" ", "_") if ch.isalnum() or "_" == ch))
    create_dirs(res_dir, False)
    if not get_f_path:
        get_f_path = lambda f: os.path.join("..", os.path.relpath(f, res_dir))

    doc = libsbml.SBMLReader().readSBML(sbml)
    model = doc.getModel()

    if in_r_id2rev:
        constraint_exchange_reactions(model, forsed_r_id2rev=in_r_id2rev)
        libsbml.SBMLWriter().writeSBMLToFile(doc, sbml)

    # copy our model in the result directory
    if os.path.normpath(res_dir) != os.path.normpath(os.path.dirname(sbml)):
        shutil.copy(sbml, res_dir)
        sbml = os.path.join(res_dir, os.path.basename(sbml))

    r_id2rev = dict(in_r_id2rev)
    r_id2rev[out_r_id] = out_rev
    description = model_serializer.serialize(sbml, model, model_name, r_id2rev, res_dir, get_f_path)

    r_id2mask, layer2mask, vis_r_ids, main_layer = defaultdict(lambda: 0), {}, set(), None

    cobra_model, opt_val, objective_sense = None, None, MINIMIZE if out_rev else MAXIMIZE

    if do_fva:
        cur_dir = _prepare_dir(res_dir, "fva", "Performing FVA...")
        cobra_model = create_cobra_model_from_sbml_file(sbml) if not cobra_model else cobra_model
        r_id2bounds, opt_val = analyse_by_fva(
            cobra_model=cobra_model, bm_r_id=out_r_id, objective_sense=objective_sense, threshold=threshold
        )
        if opt_val:
            mask_shift = update_vis_layers(
                (r_id for (r_id, (l, u)) in r_id2bounds.items() if l * u > 0),
                "FVA essential",
                r_id2mask,
                layer2mask,
                mask_shift,
                vis_r_ids,
            )
            main_layer = "FVA essential"
            fva_sbml = os.path.join(cur_dir, "Model_FVA.xml")
            sbml = create_fva_model(sbml, r_id2bounds, fva_sbml)
        description += fva_serializer.serialize(
            cobra_model, opt_val, r_id2bounds, objective_sense, out_r_id, cur_dir, get_f_path, sbml
        )
    if do_fba:
        cur_dir = _prepare_dir(res_dir, "fba", "Performing FBA...")
        cobra_model = create_cobra_model_from_sbml_file(sbml) if not cobra_model else cobra_model
        r_id2val, opt_val = analyse_by_fba(
            cobra_model, bm_r_id=out_r_id, objective_sense=objective_sense, threshold=threshold
        )
        if opt_val:
            mask_shift = update_vis_layers(r_id2val.keys(), "FBA", r_id2mask, layer2mask, mask_shift, vis_r_ids)
            main_layer = "FBA"
        description += fba_serializer.serialize(
            cobra_model, opt_val, r_id2val, objective_sense, out_r_id, cur_dir, get_f_path
        )

    S = None
    if do_efm:
        cur_dir = _prepare_dir(res_dir, "efma", "Performing EFMA...", rewrite=rewrite)

        doc = libsbml.SBMLReader().readSBML(sbml)
        model = doc.getModel()

        name2pw = get_name2pw()
        pts = parse_simple(get_pts())
        root_ids = {t.get_id() for t in pts.get_roots()}
        pw2rs = get_pathways(model, pts, name2pw, root_ids)
        S, efm_id2pws = analyse_model_efm(
            model,
            cur_dir,
            r_id2rev,
            tree_efm_path=tree_efm_path,
            max_efm_number=max_efm_number,
            rewrite=rewrite,
            pw2rs=pw2rs,
        )

        for serializer in (efm_serializer.serialize, coupled_reaction_group_serializer.serialize):
            description += serializer(
                model=model,
                path=cur_dir,
                get_f_path=get_f_path,
                in_m_id=in_m_id,
                out_m_id=out_m_id,
                out_r_id=out_r_id,
                S=S,
                model_name=model_name,
                main_dir=main_dir,
            )

        if S.gr_id2r_id2c:
            clique_merged_sbml = os.path.join(cur_dir, "Model_folded.xml")
            r_id2new_r_id = create_folded_model(S, model)
            libsbml.SBMLWriter().writeSBMLToFile(doc, clique_merged_sbml)
            sbml = clique_merged_sbml

            vis_r_ids |= {cl_id for (r_id, cl_id) in r_id2new_r_id.items() if r_id in vis_r_ids}
            for r_id, new_r_id in r_id2new_r_id.items():
                if r_id in r_id2mask:
                    r_id2mask[new_r_id] |= r_id2mask[r_id]

    if not opt_val and (not S or not S.efm_id2i):
        description += describe("nothing_found.html")

    return model_name, S, sbml, vis_r_ids, description, mask_shift, r_id2mask, layer2mask, main_layer
示例#9
0
          <body>
          <br/>
          <p class="centre">We are visualizing your model now...</p>
          <br/>
          <img class="img-centre" src="http://mimoza.bordeaux.inria.fr/lib/modelmap/ajax-loader.gif" id="img" />
          <div id="hidden" style="visibility:hidden;height:0px;">''')

sys.stdout.flush()

temp = os.dup(sys.stdout.fileno())
try:
    url = '/%s/comp.html' % m_dir_id

    if not os.path.exists(os.path.join('..', m_dir_id, 'comp.html')):
        chebi = parse_simple(get_chebi())
        reader = libsbml.SBMLReader()
        input_document = reader.readSBML(groups_sbml)
        input_model = input_document.getModel()

        # sbml -> tulip graph
        logging.info('sbml -> tlp')
        graph, c_id2info, c_id2outs, chebi, ub_sps = import_sbml(input_model, groups_sbml)

        try:
            n2xy = parse_layout_sbml(groups_sbml)
        except LoPlError:
            n2xy = None

        fc, (n2lo, e2lo), hidden_c_ids, c_id_hidden_ubs = graph2geojson(c_id2info, c_id2outs, graph, n2xy, onto=chebi)
        c_id2out_c_id = {}
示例#10
0
__author__ = 'anna'


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description="Generalizes an SBML model.")
    parser.add_argument('--model', required=True, type=str,
                        help="input model in SBML format")
    parser.add_argument('--output_model', default=None, type=str,
                        help="path to the output generalized model in SBML format")
    parser.add_argument('--groups_model', default=None, type=str,
                        help="path to the output model in SBML format with groups extension to encode similar elements")
    parser.add_argument('--verbose', action="store_true", help="print logging information")
    parser.add_argument('--log', default=None, help="a log file")
    params = parser.parse_args()

    prefix = os.path.splitext(params.model)[0]
    if not params.output_model:
        params.output_model = "%s_generalized.xml" % prefix
    if not params.groups_model:
        params.groups_model = "%s_with_groups.xml" % prefix

    if params.verbose:
        logging.basicConfig(level=logging.INFO)

    logging.info("parsing ChEBI...")
    ontology = parse_simple(get_chebi())
    r_id2clu, s_id2clu, _, _ = generalize_model(params.model, ontology, params.groups_model, params.output_model,
                                                ub_chebi_ids={'chebi:ch'})
示例#11
0
def process_sbml(sbml,
                 verbose,
                 ub_ch_ids=None,
                 web_page_prefix=None,
                 generalize=True,
                 log_file=None,
                 id2mask=None,
                 layer2mask=DEFAULT_LAYER2MASK,
                 tab2html=None,
                 title=None,
                 h1=None,
                 id2color=None,
                 tabs={ABOUT_TAB, DOWNLOAD_TAB},
                 info='',
                 invisible_layers=None,
                 sbgn=True,
                 cytoscape=True):
    """
    Generalizes and visualizes a given SBML model.
    :param sbml: a path to the input SBML file
    :param verbose: if logging information should be printed
    :param ub_ch_ids: optional, ChEBI ids to be considered as ubiquitous. If left None, will be calculated automatically.
    :param web_page_prefix: optional, how this model's webpage will be identified.
    If left None an identifier will be generated based on the SBML file's md5.
    :param generalize: optional, whether the generalization should be performed. The default is True
    :param log_file: optional, a file where the logging information should be redirected
    (only needed if verbose is set to True)
    :param id2mask: optional,
    :param layer2mask: optional, a dict storing the correspondence between a layer name and an its id mask
    :param tab2html: optional,
    :param title: optional, the title for the web page
    :param h1: optional, the main header of the web page
    :param id2color: optional,
    :param tabs: optional, a set of names of tabs that should be shown
    :param info: optional, additional information to be displayed in the bottom of the web page
    :param invisible_layers: optional, the layers of the visualized metabolic map that should be hidden
    :return: void
    """
    # Read the SBML
    reader = libsbml.SBMLReader()
    doc = reader.readSBML(sbml)
    model = doc.getModel()
    if not model:
        raise Exception(
            "The model should be in SBML format, check your file %s" % sbml)
    model_id = model.getId()
    if not model_id:
        sbml_name = os.path.splitext(os.path.basename(sbml))[0]
        model.setId(sbml_name)
        model_id = sbml_name

    # Prepare the output directories
    web_page_prefix = web_page_prefix if web_page_prefix else check_md5(sbml)
    sbml_dir = dirname(abspath(sbml))
    directory = os.path.join(sbml_dir, web_page_prefix)
    if not os.path.exists(directory):
        os.makedirs(directory)
    lib_path = os.path.join(directory, 'lib')
    if not os.path.exists(lib_path):
        copytree(get_lib(), lib_path)

    # Prepare the logger
    if verbose:
        logging.captureWarnings(True)
        logging.basicConfig(level=logging.INFO,
                            format='%(asctime)s: %(message)s',
                            datefmt="%Y-%m-%d %H:%M:%S",
                            filename=log_file)

    # Generalize the model if needed
    groups_sbml = os.path.join(directory, '%s_with_groups.xml' % model_id)
    gen_sbml = os.path.join(directory, '%s_generalized.xml' % model_id)
    if check_for_groups(sbml, SBO_CHEMICAL_MACROMOLECULE,
                        GROUP_TYPE_UBIQUITOUS):
        if sbml != groups_sbml:
            if not libsbml.SBMLWriter().writeSBMLToFile(doc, groups_sbml):
                raise Exception("Could not write your model to %s" %
                                groups_sbml)
    else:
        chebi = parse_simple(get_chebi())
        if generalize:
            logging.info('Generalizing the model...')
            generalize_model(sbml,
                             chebi,
                             groups_sbml,
                             gen_sbml,
                             ub_chebi_ids=ub_ch_ids)
        else:
            gen_sbml = None
            logging.info('Ubiquitizing the model...')
            ubiquitize_model(sbml, chebi, groups_sbml, ub_chebi_ids=ub_ch_ids)

    # Visualize the model
    reader = libsbml.SBMLReader()
    input_document = reader.readSBML(groups_sbml)
    input_model = input_document.getModel()

    root, c_id2info, c_id2outs, chebi, ub_sps = import_sbml(
        input_model, groups_sbml)

    c_id2out_c_id = {}
    for c_id, c_info in c_id2info.items():
        _, _, (_, out_c_id) = c_info
        if out_c_id:
            c_id2out_c_id[c_id] = out_c_id
    try:
        n2xy = parse_layout_sbml(sbml)
        if n2xy:
            logging.info('Found layout in the model...')
            r_size = next((n2xy[r.getId()][1][0]
                           for r in input_model.getListOfReactions()
                           if r.getId() in n2xy), None)
            if r_size:
                scale_factor = REACTION_SIZE / r_size
                if scale_factor != 1:
                    keys = n2xy.keys()
                    for n_id in keys:
                        value = n2xy[n_id]
                        if isinstance(value, dict):
                            value = {
                                r_id:
                                (scale(xy,
                                       scale_factor), scale(wh, scale_factor))
                                for (r_id, (xy, wh)) in value.items()
                            }
                        else:
                            xy, wh = value
                            value = scale(xy, scale_factor), scale(
                                wh, scale_factor)
                        n2xy[n_id] = value
    except LoPlError:
        n2xy = None
    fc, (n2lo, e2lo), hidden_c_ids, c_id_hidden_ubs = \
        graph2geojson(c_id2info, c_id2outs, root, n2xy, id2mask=id2mask, onto=chebi,
                      colorer=color if not id2color else lambda graph: color_by_id(graph, id2color))
    if n2lo:
        groups_document = reader.readSBML(groups_sbml)
        groups_model = groups_document.getModel()
        if gen_sbml:
            gen_document = reader.readSBML(gen_sbml)
            gen_model = gen_document.getModel()
        else:
            gen_model = False
        save_as_layout_sbml(groups_model, gen_model, groups_sbml, gen_sbml,
                            n2lo, ub_sps)

        if sbgn:
            groups_sbgn = os.path.join(directory, '%s.sbgn' % model_id)
            gen_sbgn = os.path.join(directory,
                                    '%s_generalized.sbgn' % model_id)

            try:
                save_as_sbgn(n2lo, e2lo, groups_model, groups_sbgn)
                logging.info('   exported as SBGN %s' % groups_sbgn)
            except Exception as e:
                logging.error("Didn't manage to save to SBGN: %s" % e)

            if gen_model:
                try:
                    save_as_sbgn(n2lo, e2lo, gen_model, gen_sbgn)
                    logging.info('   exported as SBGN %s' % groups_sbgn)
                except Exception as e:
                    logging.error("Didn't manage to save to SBGN: %s" % e)

        if cytoscape:
            out_json = os.path.join(directory, '%s.cyjs' % model_id)
            save_as_cytoscape_json(n2lo, model, out_json, ub_sps)
            logging.info('   exported as Cytoscape json %s' % out_json)

            if gen_model:
                out_json = os.path.join(directory,
                                        '%s_generalized.cyjs' % model_id)
                save_as_cytoscape_json(n2lo, gen_model, out_json, ub_sps)

    # Serialize the result
    serialize(directory=directory,
              m_dir_id=web_page_prefix,
              input_model=input_model,
              c_id2level2features=fc,
              c_id2out_c_id=c_id2out_c_id,
              hidden_c_ids=hidden_c_ids,
              c_id_hidden_ubs=c_id_hidden_ubs,
              tabs=tabs,
              groups_sbml=groups_sbml,
              layer2mask=layer2mask,
              tab2html=tab2html,
              title=title,
              h1=h1,
              invisible_layers=invisible_layers)
示例#12
0
    if not params.chebi:
        params.chebi = get_chebi()
    prefix = os.path.splitext(params.model)[0]
    if not params.merged_model:
        params.merged_model = "%s.xml" % prefix
    if not params.output_model:
        params.output_model = "%s_generalized.xml" % prefix
    if not params.groups_model:
        params.groups_model = "%s_with_groups.xml" % prefix

    if params.verbose:
        logging.basicConfig(level=logging.INFO)

    logging.info("parsing ChEBI...")
    ontology = parse_simple(params.chebi)
    if os.path.isdir(params.model):
        in_sbml_list = ['%s/%s' % (params.model, f) for f in glob.glob(os.path.join(params.model, '*'))
                        if os.path.splitext(f)[1] in [".xml", ".sbml"]]
        for sbml in in_sbml_list:
            doc = libsbml.SBMLReader().readSBML(sbml)
            model = doc.getModel()
            print(sbml, model.getNumSpecies(), model.getNumReactions())
        merge_models(in_sbml_list, params.merged_model)
        sbml = params.merged_model
    else:
        sbml = params.model
    r_id2clu, s_id2clu, _, _ = generalize_model(sbml, ontology, params.groups_model, params.output_model,
                                                ub_chebi_ids={'chebi:ch'})
    if os.path.isdir(params.model):
        serialize_generalization(r_id2clu, s_id2clu, params.groups_model, ontology,