当前位置: 首页>>代码示例>>Python>>正文


Python Model.compartments方法代码示例

本文整理汇总了Python中cobra.Model.compartments方法的典型用法代码示例。如果您正苦于以下问题:Python Model.compartments方法的具体用法?Python Model.compartments怎么用?Python Model.compartments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cobra.Model的用法示例。


在下文中一共展示了Model.compartments方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: matlab_cobra_struct_to_python_cobra_object

# 需要导入模块: from cobra import Model [as 别名]
# 或者: from cobra.Model import compartments [as 别名]
def matlab_cobra_struct_to_python_cobra_object(matlab_struct):
    """Converts a COBRA toolbox 2.0 struct into a cobra.Model object using the mlabwrap matlab proxy

    """

    from cobra import Model
    try:
        from mlabwrap import mlab as matlab
    except:
        raise Exception('mlabwrap and MATLAB are required to use these functions. '+\
                        'They only function on Mac OS X and GNU/Linux')
    from copy import deepcopy
    from numpy import array
    from cobra.mlab import matlab_cell_to_python_list, matlab_sparse_to_scipy_sparse
    struct_fields = matlab_cell_to_python_list(matlab.fields(matlab_struct, nout=1))
    _S_dok = dok_matrix(matlab_sparse_to_scipy_sparse(matlab_struct.S))
    model_id = 'Matlab_Import'
    if 'description' in struct_fields:
        model_id = str(deepcopy(matlab_struct.description))
    the_model = Model(model_id)
    #Metabolite section
    #Deal with lack of compartment support in the COBRA Toolbox
    the_metabolites = matlab_cell_to_python_list(matlab_struct.mets)
    cobra_metabolites = []
    if the_metabolites[0].endswith(']'):
        [cobra_metabolites.append(Metabolite('%s_%s'%(x[:-3], x[-2]),
                                              compartment=x[-2]))
         for x in the_metabolites]
    elif the_metabolites[0][-2] == '_':
        [cobra_metabolites.append(Metabolite('%s_%s'%(x[:-2],x[-1]),
                                             compartment=x[-1]))
         for x in the_metabolites]
    else:
        raise Exception("Don't know how to extract compartment from %s"%the_metabolites[0])
    _b = deepcopy(matlab_struct.b).tolist()
    if 'metNames' in struct_fields:
        _metabolite_names = matlab_cell_to_python_list(matlab_struct.metNames)
    else:
        _metabolite_names = ['']*len(cobra_metabolites)
        
    if 'csense' in struct_fields:
        if isinstance(matlab_struct.csense, str):
            the_csense = matlab_struct.csense
            _constraint_sense = [the_csense[i] for i in range(len(the_csense))]
        else:
            _constraint_sense = matlab_cell_to_python_list(matlab_struct.csense)
    else:
        _constraint_sense = ['E']*len(cobra_metabolites)
    if 'metFormulas' in struct_fields:
        _metabolite_formulas = matlab_cell_to_python_list(matlab_struct.metFormulas)
    else:
        _metabolite_formulas = ['']*len(the_model.metabolites)
    if 'metCharge' in struct_fields:
        _metabolite_charges = matlab_struct.metCharge.flatten().tolist()
    else:
        _metabolite_charges = [None]*len(cobra_metabolites)
    if 'metCASID' in struct_fields:
        _metabolite_cas_id = matlab_cell_to_python_list(matlab_struct.metCASID)
    else:
        _metabolite_cas_id = [None]*len(cobra_metabolites)
    if 'metKeggID' in struct_fields:
        _metabolite_kegg_id = matlab_cell_to_python_list(matlab_struct.metKeggID)
    else:
        _metabolite_kegg_id = [None]*len(cobra_metabolites)
    the_compartments = {}
    for the_metabolite, b, n, c, f, ch, cas, kegg in zip(cobra_metabolites,
                                                         _b,
                                                         _metabolite_names,
                                                         _constraint_sense,
                                                         _metabolite_formulas,
                                                         _metabolite_charges,
                                                         _metabolite_cas_id,
                                                         _metabolite_kegg_id):
        the_metabolite._bound = b[0]
        the_metabolite.name = n
        the_metabolite._constraint_sense = c
        the_metabolite.formula = Formula(f)

        the_compartments[the_metabolite.compartment] = the_metabolite.compartment
        if ch is not None:
            the_metabolite.charge = the_metabolite.notes['CHARGE'] = int(ch)
        if cas is not None and cas != '':
            the_metabolite.notes['CASID'] = cas
        if kegg is not None and kegg != '':
            the_metabolite.notes['KEGGID'] = kegg
            
    metabolite_dict = dict([(x.id, x)
                            for x in cobra_metabolites])
    #Reaction section
    cobra_reactions = map(Reaction, matlab_cell_to_python_list(matlab_struct.rxns))
    _objective_coefficients = deepcopy(matlab_struct.c).tolist()
    _lower_bounds = deepcopy(matlab_struct.lb).tolist()
    _upper_bounds = deepcopy(matlab_struct.ub).tolist()
    _reversibility = deepcopy(matlab_struct.rev).tolist()
    if 'rxnNames' in struct_fields:
        _reaction_names = matlab_cell_to_python_list(matlab_struct.rxnNames)
    else:
        _reaction_names = ['']*len(the_model.reactions)
    if 'grRules' in struct_fields:
        _gene_reaction_rules = matlab_cell_to_python_list(matlab_struct.grRules)
#.........这里部分代码省略.........
开发者ID:Ajami712,项目名称:cobrapy,代码行数:103,代码来源:mlab.py


注:本文中的cobra.Model.compartments方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。