Esempio n. 1
0
def test_remove_genes() -> None:
    """Test gene removal."""
    m = Model("test")
    m.add_reactions([Reaction("r" + str(i + 1)) for i in range(8)])
    assert len(m.reactions) == 8
    rxns = m.reactions
    rxns.r1.gene_reaction_rule = "(a and b) or (c and a)"
    rxns.r2.gene_reaction_rule = "(a and b and d and e)"
    rxns.r3.gene_reaction_rule = "(a and b) or (b and c)"
    rxns.r4.gene_reaction_rule = "(f and b) or (b and c)"
    rxns.r5.gene_reaction_rule = "x"
    rxns.r6.gene_reaction_rule = "y"
    rxns.r7.gene_reaction_rule = "x or     z"
    rxns.r8.gene_reaction_rule = ""
    assert "a" in m.genes
    assert "x" in m.genes
    remove_genes(m, ["a"], remove_reactions=False)
    assert "a" not in m.genes
    assert "x" in m.genes
    assert rxns.r1.gene_reaction_rule == ""
    assert rxns.r2.gene_reaction_rule == ""
    assert rxns.r3.gene_reaction_rule == "b and c"
    assert rxns.r4.gene_reaction_rule == "(f and b) or (b and c)"
    assert rxns.r5.gene_reaction_rule == "x"
    assert rxns.r6.gene_reaction_rule == "y"
    assert rxns.r7.genes == {m.genes.x, m.genes.z}
    assert rxns.r8.gene_reaction_rule == ""
    remove_genes(m, ["x"], remove_reactions=True)
    assert len(m.reactions) == 7
    assert "r5" not in m.reactions
    assert "x" not in m.genes
    assert rxns.r1.gene_reaction_rule == ""
    assert rxns.r2.gene_reaction_rule == ""
    assert rxns.r3.gene_reaction_rule == "b and c"
    assert rxns.r4.gene_reaction_rule == "(f and b) or (b and c)"
    assert rxns.r6.gene_reaction_rule == "y"
    assert rxns.r7.gene_reaction_rule == "z"
    assert rxns.r7.genes == {m.genes.z}
    assert rxns.r8.gene_reaction_rule == ""
def getModelWithRemoveGene(model0, gene_remove0):
    model1 = model0.copy()
    remove_genes(model1, gene_remove0,
                 remove_reactions=True)  # this script can't be used
    return model1
Esempio n. 3
0
def convert_ids(model):
    """Converts metabolite and reaction ids to the new style.

    Returns a tuple with the new model and a dictionary of old ids set up like this:

    {'reactions': {'new_id': 'old_id'},
     'metabolites': {'new_id': 'old_id'},
     'genes': {'new_id': 'old_id'}}

    """
    # loop through the ids:
    metabolite_id_dict = defaultdict(list)
    reaction_id_dict = defaultdict(list)
    gene_id_dict = defaultdict(list)

    # fix metabolites
    for metabolite in model.metabolites:
        new_id = id_for_new_id_style(fix_legacy_id(metabolite.id,
                                                   use_hyphens=False),
                                     is_metabolite=True)
        metabolite_id_dict[new_id].append(metabolite.id)
        metabolite.id = new_id
    model.metabolites._generate_index()

    # take out the _b metabolites
    remove_boundary_metabolites(model)

    # load fixes for gene_reaction_rule's
    rule_prefs = _get_rule_prefs()

    # separate ids and compartments, and convert to the new_id_style
    for reaction in model.reactions:
        # save the original id
        current_id = reaction.id
        # apply new id style
        reaction.id = id_for_new_id_style(
            fix_legacy_id(reaction.id, use_hyphens=False))
        # normalize pseudoreaction IDs
        try:
            _normalize_pseudoreaction(reaction)
        except ConflictingPseudoreaction as e:
            logging.warn(str(e))
        # don't merge reactions with conflicting new_id's
        while reaction.id in reaction_id_dict:
            reaction.id = increment_id(reaction.id)
        reaction_id_dict[reaction.id].append(current_id)
        # fix the gene reaction rules
        reaction.gene_reaction_rule = _check_rule_prefs(
            rule_prefs, reaction.gene_reaction_rule)
    model.reactions._generate_index()

    # update the genes
    for gene in list(model.genes):
        new_id = scrub_gene_id(gene.id)
        gene_id_dict[new_id].append(gene.id)
        for reaction in gene.reactions:
            reaction.gene_reaction_rule = re.sub(
                r'\b' + re.escape(gene.id) + r'\b', new_id,
                reaction.gene_reaction_rule)

    # remove old genes
    from cobra.manipulation import remove_genes
    remove_genes(model,
                 [gene for gene in model.genes if len(gene.reactions) == 0])

    # fix the model id
    cobra_id = re.sub(r'[^a-zA-Z0-9_]', '_', model.id)
    model.id = cobra_id

    old_ids = {
        'metabolites': metabolite_id_dict,
        'reactions': reaction_id_dict,
        'genes': gene_id_dict
    }
    return model, old_ids
Esempio n. 4
0
File: parse.py Progetto: SBRG/ome
def convert_ids(model):
    """Converts metabolite and reaction ids to the new style.

    Returns a tuple with the new model and a dictionary of old ids set up like this:

    {'reactions': {'new_id': 'old_id'},
     'metabolites': {'new_id': 'old_id'},
     'genes': {'new_id': 'old_id'}}

    """
    # loop through the ids:
    metabolite_id_dict = defaultdict(list)
    reaction_id_dict = defaultdict(list)
    gene_id_dict = defaultdict(list)

    # fix metabolites
    for metabolite in model.metabolites:
        new_id = id_for_new_id_style(fix_legacy_id(metabolite.id, use_hyphens=False),
                                     is_metabolite=True)
        metabolite_id_dict[new_id].append(metabolite.id)
        if new_id != metabolite.id:
            # new_id already exists, then merge
            if new_id in model.metabolites:
                new_id = add_duplicate_tag(new_id)
                while new_id in model.metabolites:
                    new_id = increment_id(new_id)
            metabolite.id = new_id
    model.metabolites._generate_index()

    # take out the _b metabolites
    remove_boundary_metabolites(model)

    # load fixes for gene_reaction_rule's
    rule_prefs = _get_rule_prefs()

    # separate ids and compartments, and convert to the new_id_style
    for reaction in model.reactions:
        # apply new id style
        new_style_id = id_for_new_id_style(fix_legacy_id(reaction.id, use_hyphens=False))

        # normalize pseudoreaction IDs
        try:
            pseudo_id = _normalize_pseudoreaction(new_style_id, reaction)
        except ConflictingPseudoreaction as e:
            logging.warn(str(e))

        new_id = pseudo_id if pseudo_id is not None else new_style_id

        # don't merge reactions with conflicting new_id's
        if new_id != reaction.id and new_id in model.reactions:
            new_id = add_duplicate_tag(new_id)
            while new_id in model.reactions:
                new_id = increment_id(new_id)

        reaction_id_dict[new_id].append(reaction.id)
        reaction.id = new_id

        # fix the gene reaction rules
        reaction.gene_reaction_rule = _check_rule_prefs(rule_prefs, reaction.gene_reaction_rule)

    model.reactions._generate_index()

    # update the genes
    for gene in list(model.genes):
        new_id = scrub_gene_id(gene.id)
        gene_id_dict[new_id].append(gene.id)
        for reaction in gene.reactions:
            reaction.gene_reaction_rule = re.sub(r'\b' + re.escape(gene.id) + r'\b', new_id,
                                                 reaction.gene_reaction_rule)

    # remove old genes
    from cobra.manipulation import remove_genes
    remove_genes(model, [gene for gene in model.genes
                         if len(gene.reactions) == 0])

    # fix the model id
    bigg_id = re.sub(r'[^a-zA-Z0-9_]', '_', model.id)
    model.id = bigg_id

    old_ids = {'metabolites': metabolite_id_dict,
               'reactions': reaction_id_dict,
               'genes': gene_id_dict}

    return model, old_ids