Пример #1
0
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
Пример #2
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
Пример #3
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)
Пример #4
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)
Пример #5
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)
Пример #6
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,
Пример #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, e2lo), hidden_c_ids, c_id_hidden_ubs = graph2geojson(c_id2info, c_id2outs, graph, n2xy, onto=chebi)
        c_id2out_c_id = {}
Пример #8
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'})
Пример #9
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)
Пример #10
0
    parser.add_argument('--model', required=True, type=str,
                        help="input model in SBML format "
                             "or a directory containing several models if they first need to be merged")
    parser.add_argument('--chebi', default=None, type=str, help="path to the ChEBI ontology file in OBO 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('--merged_model', default=None, type=str,
                        help="path to the output merged model in SBML format")
    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()

    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, '*'))