def create_mat_dict(model): """create a dict mapping model attributes to arrays""" model = model.to_array_based_model() rxns = model.reactions mets = model.metabolites mat = dicttype() mat["mets"] = _cell(mets.list_attr("id")) mat["metNames"] = _cell(mets.list_attr("name")) mat["metFormulas"] = _cell([str(m.formula) for m in mets]) mat["genes"] = _cell(model.genes.list_attr("id")) mat["grRules"] = _cell(rxns.list_attr("gene_reaction_rule")) mat["rxns"] = _cell(rxns.list_attr("id")) mat["rxnNames"] = _cell(rxns.list_attr("name")) mat["subSystems"] = _cell(rxns.list_attr("subsystem")) mat["csense"] = "".join(model._constraint_sense) mat["S"] = model.S # multiply by 1 to convert to float, working around scipy bug # https://github.com/scipy/scipy/issues/4537 mat["lb"] = array(rxns.list_attr("lower_bound")) * 1. mat["ub"] = array(rxns.list_attr("upper_bound")) * 1. mat["b"] = array(mets.list_attr("_bound")) * 1. mat["c"] = array(rxns.list_attr("objective_coefficient")) * 1. mat["rev"] = array(rxns.list_attr("reversibility")) * 1 mat["description"] = str(model.description) return mat
def save_matlab_model(model, file_name): """Save the cobra model as a .mat file. This .mat file can be used directly in the MATLAB version of COBRA. model : :class:`~cobra.core.Model.Model` object file_name : str or file-like object """ model = model.to_array_based_model() rxns = model.reactions mets = model.metabolites mat = dicttype() csense = "" for m in mets: csense += m._constraint_sense mat["mets"] = _cell(mets.list_attr("id")) mat["metNames"] = _cell(mets.list_attr("name")) mat["metFormulas"] = _cell([str(m.formula) for m in mets]) mat["genes"] = _cell(model.genes.list_attr("id")) mat["grRules"] = _cell(rxns.list_attr("gene_reaction_rule")) mat["rxns"] = _cell(rxns.list_attr("id")) mat["rxnNames"] = _cell(rxns.list_attr("name")) mat["subSystems"] = _cell(rxns.list_attr("subsystem")) mat["csense"] = csense mat["S"] = model.S mat["lb"] = array(rxns.list_attr("lower_bound")) mat["ub"] = array(rxns.list_attr("upper_bound")) mat["b"] = array(mets.list_attr("_bound")) mat["c"] = array(rxns.list_attr("objective_coefficient")) mat["rev"] = array(rxns.list_attr("reversibility")) mat["description"] = str(model.description) savemat(file_name, {str(model.description): mat}, appendmat=True, oned_as="column")
def create_mat_dict(model): """create a dict mapping model attributes to arrays""" model = model.to_array_based_model() rxns = model.reactions mets = model.metabolites mat = dicttype() mat["mets"] = _cell(mets.list_attr("id")) mat["metNames"] = _cell(mets.list_attr("name")) mat["metFormulas"] = _cell([str(m.formula) for m in mets]) try: mat["metCharge"] = array(mets.list_attr("charge")) * 1. except TypeError: # can't have any None entries for charge, or this will fail pass mat["genes"] = _cell(model.genes.list_attr("id")) # make a matrix for rxnGeneMat # reactions are rows, genes are columns rxnGene = dok_matrix((len(model.reactions), len(model.genes))) if min(rxnGene.shape) > 0: for i, reaction in enumerate(model.reactions): for gene in reaction.genes: rxnGene[i, model.genes.index(gene)] = 1 mat["rxnGeneMat"] = rxnGene mat["grRules"] = _cell(rxns.list_attr("gene_reaction_rule")) mat["rxns"] = _cell(rxns.list_attr("id")) mat["rxnNames"] = _cell(rxns.list_attr("name")) mat["subSystems"] = _cell(rxns.list_attr("subsystem")) mat["csense"] = "".join(model._constraint_sense) mat["S"] = model.S if model.S is not None else [[]] # multiply by 1 to convert to float, working around scipy bug # https://github.com/scipy/scipy/issues/4537 mat["lb"] = array(rxns.list_attr("lower_bound")) * 1. mat["ub"] = array(rxns.list_attr("upper_bound")) * 1. mat["b"] = array(mets.list_attr("_bound")) * 1. mat["c"] = array(rxns.list_attr("objective_coefficient")) * 1. mat["rev"] = array(rxns.list_attr("reversibility")) * 1 mat["description"] = str(model.id) return mat
def save_matlab_model(model, file_name): """Save the cobra model as a .mat file. This .mat file can be used directly in the MATLAB version of COBRA. .. note:: This function works best with scipy 0.11b1 or later Parameters ---------- model : cobra.Model file_name : str or file-like object """ model = model.to_array_based_model() rxns = model.reactions mets = model.metabolites mat = dicttype() csense = "" for m in mets: csense += m._constraint_sense mat["mets"] = _cell(mets.list_attr("id")) mat["metNames"] = _cell(mets.list_attr("name")) mat["metFormulas"] = _cell([str(m.formula) for m in mets]) mat["genes"] = _cell(model.genes.list_attr("id")) mat["grRules"] = _cell(rxns.list_attr("gene_reaction_rule")) mat["rxns"] = _cell(rxns.list_attr("id")) mat["rxnNames"] = _cell(rxns.list_attr("name")) mat["subSystems"] = _cell(rxns.list_attr("subsystem")) mat["csense"] = csense mat["S"] = model.S mat["lb"] = array(rxns.list_attr("lower_bound")) mat["ub"] = array(rxns.list_attr("upper_bound")) mat["b"] = array(mets.list_attr("_bound")) mat["c"] = array(rxns.list_attr("objective_coefficient")) mat["rev"] = array(rxns.list_attr("reversibility")) mat["description"] = str(model.description) savemat(file_name, {str(model.description): mat}, appendmat=True, oned_as="column")