Example #1
0
def open_meta_ns(meta_graph, ns):
    root = meta_graph.getRoot()
    for n in sorted(ns, key=lambda mn: -root[VIEW_META_GRAPH][mn].getId()):
        inner_ns = root[VIEW_META_GRAPH][n].getNodes()
        meta_graph.openMetaNode(n)
        for inner_n in inner_ns:
            root[VIEW_SIZE][inner_n] = get_n_size(meta_graph, inner_n)
Example #2
0
def open_meta_ns(meta_graph, ns):
    root = meta_graph.getRoot()
    for n in sorted(ns, key=lambda mn: -root[VIEW_META_GRAPH][mn].getId()):
        inner_ns = root[VIEW_META_GRAPH][n].getNodes()
        meta_graph.openMetaNode(n)
        for inner_n in inner_ns:
            root[VIEW_SIZE][inner_n] = get_n_size(meta_graph, inner_n)
Example #3
0
def comp_to_meta_node(meta_graph, c_id, go_id, c_name, out_comp, do_layout=True, n2xy=None):
    root = meta_graph.getRoot()
    ns = [n for n in meta_graph.getNodes() if root[COMPARTMENT_ID][n] == c_id]
    if not ns:
        return None
    comp_n = meta_graph.createMetaNode(ns, False)
    comp_graph = root[VIEW_META_GRAPH][comp_n]
    if do_layout:
        if n2xy:
            apply_node_coordinates(comp_graph, n2xy)
        else:
            layout(comp_graph)
    root[NAME][comp_n] = c_name
    root[COMPARTMENT_ID][comp_n] = out_comp
    root[TYPE][comp_n] = TYPE_COMPARTMENT
    root[VIEW_SHAPE][comp_n] = COMPARTMENT_SHAPE
    root[ID][comp_n] = c_id
    root[TERM][comp_n] = go_id
    root[VIEW_SIZE][comp_n] = get_n_size(meta_graph, comp_n)
    for meta_e in root.getInOutEdges(comp_n):
        sample_e = next((ee for ee in root[VIEW_META_GRAPH][meta_e] if not root[UBIQUITOUS][ee]),
                        next(iter(root[VIEW_META_GRAPH][meta_e])))
        root[UBIQUITOUS][meta_e] = root[UBIQUITOUS][sample_e]
        root[STOICHIOMETRY][meta_e] = sum(root[STOICHIOMETRY][ee] for ee in root[VIEW_META_GRAPH][meta_e])
        # todo: this is not True but will help with cycle detection
        root[REVERSIBLE][meta_e] = root[REVERSIBLE][sample_e] and not root[UBIQUITOUS][sample_e]
    return comp_n
Example #4
0
def factor_nodes(graph, ns=None):
    root = graph.getRoot()
    if not ns:
        ns = graph.getNodes()

    ancestor2nodes = defaultdict(list)
    for node in ns:
        ancestor = root[ANCESTOR_ID][node]
        if ancestor:
            ancestor = ancestor, root[TYPE][node], root[COMPARTMENT_ID][node]
            ancestor2nodes[ancestor].append(node)

    meta_ns = []
    for (ancestor, type_, c_id), nodes in ((k, ns) for (k, ns) in ancestor2nodes.items() if len(ns) > 1):
        sample_n = nodes[0]
        meta_n = graph.createMetaNode(nodes, False)

        for prop in [COMPARTMENT_ID, TYPE, REVERSIBLE, UBIQUITOUS, VIEW_SHAPE, RELATED_COMPARTMENT_IDS]:
            root[prop][meta_n] = root[prop][sample_n]
        root[ID][meta_n] = root[ANCESTOR_ID][sample_n]
        root[VIEW_SIZE][meta_n] = get_n_size(root, meta_n)
        root[NAME][meta_n] = root[ANCESTOR_NAME][sample_n]

        if TYPE_REACTION == type_:
            root[REVERSIBLE][meta_n] = False
            root[TRANSPORT][meta_n] = False
            for sample_n in nodes:
                if root[REVERSIBLE][sample_n]:
                    root[REVERSIBLE][meta_n] = True
                if root[TRANSPORT][sample_n]:
                    root[TRANSPORT][meta_n] = True
            root[TERM][meta_n] = "\nor\n".join({root[TERM][it] for it in nodes})
            for ub in root.getInOutNodes(meta_n):
                if root[UBIQUITOUS][ub]:
                    clone_ids = set(root[CLONE_ID][ub].split(',')) if root[CLONE_ID][ub] else set()
                    clone_ids.add(root[ID][meta_n])
                    root[CLONE_ID][ub] = ','.join(sorted(clone_ids))
        else:
            root[TERM][meta_n] = root[ANCESTOR_TERM][sample_n]

        for meta_e in root.getInOutEdges(meta_n):
            sample_e = next(iter(root[VIEW_META_GRAPH][meta_e]))
            root[UBIQUITOUS][meta_e] = root[UBIQUITOUS][sample_e]
            root[STOICHIOMETRY][meta_e] = root[STOICHIOMETRY][sample_e]
            # todo: this is not True but will help with cycle detection
            root[REVERSIBLE][meta_e] = not root[UBIQUITOUS][meta_e]

        meta_ns.append(meta_n)
    return meta_ns
Example #5
0
def species2nodes(graph, input_model, ub_sps):
    id2n = {}
    for s in input_model.getListOfSpecies():
        _id = s.getId()
        ub = (_id in ub_sps)

        # todo
        if SKIP_UBS and ub:
            continue

        n = graph.addNode()
        comp = input_model.getCompartment(s.getCompartment())
        graph[COMPARTMENT_ID][n] = comp.getId()
        graph[ID][n] = _id
        id2n[_id] = n

        chebi_id = get_chebi_id(s)
        if chebi_id:
            graph[TERM][n] = get_chebi_id(s)
        name = s.getName()
        if name:
            # remove compartment from the name,
            # e.g. H2O [peroxisome] --> H2O
            if comp.getName():
                name = name.replace('[%s]' % comp.getName(), '').strip()
                name = name.replace('[%s]' % comp.getId(), '').strip()
            graph[NAME][n] = name
        graph[TYPE][n] = TYPE_SPECIES
        graph[UBIQUITOUS][n] = ub
        graph[VIEW_SHAPE][n] = SPECIES_SHAPE
        graph[VIEW_SIZE][n] = get_n_size(graph, n)
        formulas = get_formulas(s)
        if formulas:
            formula = next(iter(formulas))
            if formula and formula != '.':
                graph[FORMULA][n] = formula
    return id2n
Example #6
0
def reactions2nodes(get_r_comp, graph, id2n, input_model):
    # get_sp_comp = lambda _id: graph[COMPARTMENT][id2n[_id]]
    get_sp_comp = lambda _id: input_model.getSpecies(_id).getCompartment()

    def link_reaction_to_species(reaction_node,
                                 sp_ref,
                                 all_comps,
                                 is_reactant=True,
                                 reversible=False):
        s_id = sp_ref.getSpecies()

        all_comps.add(get_sp_comp(s_id))

        # todo:
        if SKIP_UBS and s_id not in id2n:
            return
        species_node = id2n[s_id]
        e = graph.addEdge(species_node,
                          reaction_node) if is_reactant else graph.addEdge(
                              reaction_node, species_node)
        stoich = sp_ref.getStoichiometry()
        if not stoich:
            stoich = sp_ref.getStoichiometryMath()
        if not stoich:
            stoich = 1
        graph[STOICHIOMETRY][e] = stoich
        graph[NAME][e] = input_model.getSpecies(s_id).getName()
        ub = graph[UBIQUITOUS][species_node]
        graph[UBIQUITOUS][e] = ub
        graph[REVERSIBLE][e] = reversible and not ub

    for r in input_model.getListOfReactions():
        name = r.getName()
        # do not add fake isa reactions
        # if name.find("isa ") != -1 and 1 == r.getNumReactants() == r.getNumProducts() and get_sp_comp(
        # r.getListOfReactants().get(0).getSpecies()) == get_sp_comp(
        # 		r.getListOfProducts().get(0).getSpecies()):
        # 	continue

        n = graph.addNode()
        graph[TERM][n] = get_gene_association(r)
        graph[ID][n] = r.getId()
        graph[NAME][n] = name
        graph[TYPE][n] = TYPE_REACTION
        graph[REVERSIBLE][n] = r.getReversible()

        graph[VIEW_SHAPE][n] = REACTION_SHAPE
        graph[VIEW_SIZE][n] = get_n_size(graph, n)

        all_comps = set()
        for sp_ref in r.getListOfReactants():
            link_reaction_to_species(n,
                                     sp_ref,
                                     all_comps,
                                     is_reactant=True,
                                     reversible=r.getReversible())
        for sp_ref in r.getListOfProducts():
            link_reaction_to_species(n,
                                     sp_ref,
                                     all_comps,
                                     is_reactant=False,
                                     reversible=r.getReversible())

        graph[TRANSPORT][n] = len(all_comps) > 1
        graph[COMPARTMENT_ID][n] = get_r_comp(all_comps)