Beispiel #1
0
 def test_add_reaction(self):
     rxn = rpReaction(id='rxn')
     self.pathway.add_reaction(rxn)
     self.assertListEqual(
         self.pathway.get_list_of_reactions(),
         self.reactions + [rxn]
     )
Beispiel #2
0
 def setUp(self):
     self.logger = create_logger(__name__, 'ERROR')
     self.rxn_1 = rpReaction(
         id='rxn_1',
         reactants={
             'MNXM188': 1,
             'MNXM4': 1,
             'MNXM6': 1,
             'MNXM1': 3
         },
         products={
             'CMPD_0000000004': 1,
             'CMPD_0000000003': 1,
             'MNXM13': 1,
             'MNXM15': 3,
             'MNXM5': 1
         },
     )
     self.rxn_2 = rpReaction(
         id='rxn_2',
         reactants={
             'MNXM4': 1,
             'CMPD_0000000003': 2
         },
         products={
             'MNXM1': 1,
             'TARGET_0000000001': 1
         },
     )
     self.rxn_3 = rpReaction(
         id='rxn_3',
         reactants={
             'CMPD_0000000004': 3,
             'MNXM4': 1,
             'MNXM6': 1
         },
         products={
             'MNXM13': 1,
             'MNXM5': 1
         },
     )
     self.reactions = [self.rxn_1, self.rxn_2, self.rxn_3]
     self.sto_mat_1 = [[-3.0, 1.0, 0.0], [-1.0, -1.0, -1.0],
                       [1.0, 0.0, 1.0], [3.0, 0.0, 0.0], [1.0, 0.0, -3.0],
                       [1.0, 0.0, 1.0], [0.0, 1.0, 0.0], [1.0, -2.0, 0.0],
                       [-1.0, 0.0, 0.0], [-1.0, 0.0, -1.0]]
Beispiel #3
0
 def setUp(self):
     self.reactants = {
         "CMPD_0000000010": 1,
         "MNXM1": 1
     }
     self.products = {
         "CMPD_0000000003": 1,
         "MNXM13": 1
     }
     self.ec_numbers = [
         "4.1.1.63"
     ]
     self.id = "rxn"
     self.miriam = {'ec-code': ['4.1.1.63']}
     self.rxn = rpReaction(
         id=self.id,
         miriam=self.miriam,
         reactants=self.reactants,
         products=self.products
     )
     self.rp2_transfo_id = 'TRS_0_0_0'
     self.rule_ids = ['RR-02-a0cc0be463ff412f-16-F']
     self.tmpl_rxn_ids = ['MNXR96458']
     self.rule_score = 0.5982208769718989
     self.selenzy = {
         'UniProtID_1': 65.65,
         'UniProtID_2': 77.77,
     }
     self.inherited_dict = {
         'id': self.id,
         'reactants': self.reactants,
         'products': self.products,
         'ec_numbers': self.ec_numbers,
     }
     self.specific_dict = {
         'idx_in_path': -1,
         'rp2_transfo_id': self.rp2_transfo_id,
         'rule_ids': self.rule_ids,
         'tmpl_rxn_ids': self.tmpl_rxn_ids,
         'rule_score': self.rule_score,
         **{
             'selenzy_'+id: self.selenzy[id]
             for id in self.selenzy
         }
     }
Beispiel #4
0
def __build_all_pathways(
    pathways: Dict,
    transfos: Dict,
    sink_molecules: List,
    rr_reactions: Dict,
    compounds_cache: Dict,
    max_subpaths_filter: int,
    lower_flux_bound: float,
    upper_flux_bound: float,
    logger: Logger = getLogger(__name__)
) -> Dict:
    """Builds pathways based on all combinations over
    reaction rules and template reactions (see
    `build_pathway_combinatorics` documentation).

    Parameters
    ----------
    pathways: Dict
        Metabolic pathways as list of chemical
        reactions where each reaction is defined by:
            - transformation ID,
            - reaction rule ID, and
            - template reaction ID
    transfos: Dict
        Full chemical transformations
    sink_molecules: List
        Sink chemical species IDs
    rr_reactions: Dict
        Reaction rules cache
    compounds_cache: Dict
        Compounds cache
    max_subpaths_filter: int
        Number of pathways (best) kept per master pathway
    lower_flux_bound: float
        Lower flux bound for all new reactions created
    upper_flux_bound: float
        Upper flux bound for all new reactions created
    logger: Logger, optional

    Returns
    -------
    Set of ranked rpPathway objects
    """

    res_pathways = {}

    nb_pathways = 0
    nb_unique_pathways = 0

    ## PATHWAYS
    for path_idx, transfos_lst in pathways.items():

        # Combine over multiple template reactions
        sub_pathways = list(itertools_product(*transfos_lst))

        ## SUB-PATHWAYS
        # # Keep only topX best sub_pathways
        # # within a same master pathway
        res_pathways[path_idx] = []
        for sub_path_idx in range(len(sub_pathways)):

            pathway = rpPathway(
                id=str(path_idx).zfill(3)+'_'+str(sub_path_idx+1).zfill(4),
                logger=logger
            )
            logger.debug(pathway.get_id())

            ## ITERATE OVER REACTIONS
            nb_reactions = len(sub_pathways[sub_path_idx])
            for rxn_idx in range(nb_reactions):

                rxn = sub_pathways[sub_path_idx][rxn_idx]
                transfo_id = rxn['rp2_transfo_id']
                transfo = transfos[transfo_id]
                rule_ids = rxn['rule_ids']
                tmpl_rxn_id = rxn['tmpl_rxn_ids']

                ## COMPOUNDS
                # Template reaction compounds
                added_cmpds = transfo['complement'][rule_ids][tmpl_rxn_id]['added_cmpds']
                # Add missing compounds to the cache
                for side in added_cmpds.keys():
                    for spe_id in added_cmpds[side].keys():
                        logger.debug(f'Add missing compound {spe_id}')
                        if spe_id not in Cache.get_objects():
                            try:
                                rpCompound(
                                    id=spe_id,
                                    smiles=compounds_cache[spe_id]['smiles'],
                                    inchi=compounds_cache[spe_id]['inchi'],
                                    inchikey=compounds_cache[spe_id]['inchikey'],
                                    formula=compounds_cache[spe_id]['formula'],
                                    name=compounds_cache[spe_id]['name']
                                )
                            except KeyError:
                                rpCompound(
                                    id=spe_id
                                )

                ## REACTION
                # Compounds from original transformation
                core_species = {
                    'right': deepcopy(transfo['right']),
                    'left': deepcopy(transfo['left'])
                }
                compounds = __add_compounds(core_species, added_cmpds)
                # revert reaction index (forward)
                rxn_idx_forward = nb_reactions - rxn_idx
                rxn = rpReaction(
                    id='rxn_'+str(rxn_idx_forward),
                    ec_numbers=transfo['ec'],
                    reactants=dict(compounds['left']),
                    products=dict(compounds['right']),
                    lower_flux_bound=lower_flux_bound,
                    upper_flux_bound=upper_flux_bound
                )
                # write infos
                for info_id, info in sub_pathways[sub_path_idx][rxn_idx].items():
                    getattr(rxn, 'set_'+info_id)(info)
                rxn.set_rule_score(rr_reactions[rule_ids][tmpl_rxn_id]['rule_score'])
                rxn.set_idx_in_path(rxn_idx_forward)

                # Add at the beginning of the pathway
                # to have the pathway in forward direction
                # Search for the target in the current reaction
                target_id = [spe_id for spe_id in rxn.get_products_ids() if 'TARGET' in spe_id]
                if target_id != []:
                    target_id = target_id[0]
                else:
                    target_id = None
                logger.debug(f'rxn: {rxn._to_dict()}')
                pathway.add_reaction(
                    rxn=rxn,
                    target_id=target_id
                )

                ## TRUNK SPECIES
                pathway.add_species_group(
                    'trunk',
                    [
                        spe_id
                        for value
                        in core_species.values()
                        for spe_id in value.keys()
                    ]
                )

                ## COMPLETED SPECIES
                pathway.add_species_group(
                    'completed',
                    [
                        spe_id
                        for value
                        in added_cmpds.values()
                        for spe_id in value.keys()
                    ]
                )

            ## SINK
            pathway.set_sink_species(
                list(
                    set(pathway.get_species_ids()) & set(sink_molecules)
                )
            )

            nb_pathways += 1

            ## RANK AMONG ALL SUB-PATHWAYS OF THE CURRENT MASTER PATHWAY
            res_pathways[path_idx] = __keep_unique_pathways(
                res_pathways[path_idx],
                pathway,
                logger
            )

        nb_unique_pathways += len(res_pathways[path_idx])

    # Flatten lists of pathways
    pathways = sum(
        [
            pathways
            for pathways in res_pathways.values()
        ], [])

    # Globally sort pathways
    pathways = sorted(pathways)[-max_subpaths_filter:]

    logger.info(f'Pathways statistics')
    logger.info(f'-------------------')
    logger.info(f'   pathways: {nb_pathways}')
    logger.info(f'   unique pathways: {nb_unique_pathways}')
    logger.info(f'   selected pathways: {len(pathways)} (topX filter = {max_subpaths_filter})')

    # Return topX pathway objects
    return [
        pathway.object
        for pathway in pathways
    ]

    # Transform the list of Item into a list of Pathway
    results = {}
    nb_sel_pathways = 0
    for res_pathway_idx, res_pathway in res_pathways.items():
        results[res_pathway_idx] = [pathway.object for pathway in res_pathway]
        nb_sel_pathways += len(results[res_pathway_idx])

    logger.info(f'Pathways selected: {nb_sel_pathways}/{nb_pathways}')

    return results
Beispiel #5
0
    def setUp(self):
        self.target = rpCompound(
            id='TARGET_0000000001',
            smiles='[H]OC(=O)C([H])=C([H])C([H])=C([H])C(=O)O[H]'
        )
        species = {
            # "TARGET_0000000001": rpCompound(
            #     id="TARGET_0000000001",
            #     smiles="[H]OC(=O)C([H])=C([H])C([H])=C([H])C(=O)O[H]",
            #     inchi="InChI=1S/C6H6O4/c7-5(8)3-1-2-4-6(9)10/h1-4H,(H,7,8)(H,9,10)",
            #     inchikey="TXXHDPDFNKHHGW-UHFFFAOYSA-N"
            # ),
            "TARGET_0000000001": self.target,
            "CMPD_0000000010": rpCompound(
                id="CMPD_0000000010",
                smiles="[H]OC(=O)c1c([H])c([H])c(O[H])c(O[H])c1[H]",
                inchi="InChI=1S/C7H6O4/c8-5-2-1-4(7(10)11)3-6(5)9/h1-3,8-9H,(H,10,11)",
                inchikey="YQUVCSBJEUQKSH-UHFFFAOYSA-N"
            ),
            "MNXM23": rpCompound(
                id="MNXM23",
                formula="C3H3O3",
                smiles="CC(=O)C(=O)O]",
                inchi="InChI=1S/C3H4O3/c1-2(4)3(5)6/h1H3,(H,5,6)",
                inchikey="LCTONWCANYUPML-UHFFFAOYSA-N",
                name="pyruvate"
            ),
            "CMPD_0000000025": rpCompound(
                id="CMPD_0000000025",
                smiles="[H]OC(=O)c1c([H])c([H])c([H])c(O[H])c1[H]",
                inchi="InChI=1S/C7H6O3/c8-6-3-1-2-5(4-6)7(9)10/h1-4,8H,(H,9,10)",
                inchikey="IJFXRHURBJZNAO-UHFFFAOYSA-N"
            ),
            "CMPD_0000000003": rpCompound(
                id="CMPD_0000000003",
                smiles="[H]Oc1c([H])c([H])c([H])c([H])c1O[H]",
                inchi="InChI=1S/C6H6O2/c7-5-3-1-2-4-6(5)8/h1-4,7-8H",
                inchikey="YCIMNLLNPGFGHC-UHFFFAOYSA-N"
            ),
            "MNXM337": rpCompound(
                id="MNXM337",
                smiles="[H]OC(=O)C(OC1([H])C([H])=C(C(=O)O[H])C([H])=C([H])C1([H])O[H])=C([H])[H]",
                inchi="InChI=1S/C10H10O6/c1-5(9(12)13)16-8-4-6(10(14)15)2-3-7(8)11/h2-4,7-8,11H,1H2,(H,12,13)(H,14,15)",
                inchikey="WTFXTQVDAKGDEY-UHFFFAOYSA-N"
            ),
            "MNXM2": rpCompound(
                id="MNXM2",
                smiles="[H]O[H]",
                inchi="InChI=1S/H2O/h1H2",
                inchikey="XLYOFNOQVPJJNP-UHFFFAOYSA-N"
            ),
            "MNXM13": rpCompound(
                id="MNXM13",
                smiles="O=C=O",
                inchi="InChI=1S/CO2/c2-1-3",
                inchikey="CURLTUGMZLYLDI-UHFFFAOYSA-N",
                formula="CO2",
                name="CO2"
            ),
            "MNXM5": rpCompound(
                id="MNXM5",
                smiles="N=C(O)c1ccc[n+](C2OC(COP(=O)(O)OP(=O)(O)OCC3OC(n4cnc5c(N)ncnc54)C(OP(=O)(O)O)C3O)C(O)C2O)c1",
                inchi="InChI=1S/C21H28N7O17P3/c22-17-12-19(25-7-24-17)28(8-26-12)21-16(44-46(33,34)35)14(30)11(43-21)6-41-48(38,39)45-47(36,37)40-5-10-13(29)15(31)20(42-10)27-3-1-2-9(4-27)18(23)32/h1-4,7-8,10-11,13-16,20-21,29-31H,5-6H2,(H7-,22,23,24,25,32,33,34,35,36,37,38,39)/p+1",
                inchikey="XJLXINKUBYWONI-UHFFFAOYSA-O",
                formula="C21H25N7O17P3",
                name="NADP(+)"
            ),
            "MNXM4": rpCompound(
                id="MNXM4",
                smiles="O=O",
                inchi="InChI=1S/O2/c1-2",
                inchikey="MYMOFIZGZYHOMD-UHFFFAOYSA-N"
            ),
            "MNXM1": rpCompound(
                id="MNXM1",
                smiles="[H+]",
                inchi="InChI=1S/p+1",
                inchikey="GPRLSGONYQIRFK-UHFFFAOYSA-N"
            ),
            "MNXM6": rpCompound(
                id="MNXM6",
                smiles="[H]N=C(O[H])C1=C([H])N(C2([H])OC([H])(C([H])([H])OP(=O)(O[H])OP(=O)(O[H])OC([H])([H])C3([H])OC([H])(n4c([H])nc5c(N([H])[H])nc([H])nc54)C([H])(OP(=O)(O[H])O[H])C3([H])O[H])C([H])(O[H])C2([H])O[H])C([H])=C([H])C1([H])[H]",
                inchi="InChI=1S/C21H30N7O17P3/c22-17-12-19(25-7-24-17)28(8-26-12)21-16(44-46(33,34)35)14(30)11(43-21)6-41-48(38,39)45-47(36,37)40-5-10-13(29)15(31)20(42-10)27-3-1-2-9(4-27)18(23)32/h1,3-4,7-8,10-11,13-16,20-21,29-31H,2,5-6H2,(H2,23,32)(H,36,37)(H,38,39)(H2,22,24,25)(H2,33,34,35)",
                inchikey="ACFIXJIJDZMPPO-UHFFFAOYSA-N"
            )
        }

        self.reactants = {
            "CMPD_0000000003": 1,
            "MNXM4": 1
        }
        self.products = {
            self.target.get_id(): 1,
            "MNXM1": 2
        }
        self.rxn = rpReaction(
            id="rxn_4",
            ec_numbers=[
                "1.13.11.1"
            ],
            reactants=self.reactants,
            products=self.products
        )
        self.reactions = [
            self.rxn,
            rpReaction(
                id="rxn_3",
                ec_numbers=[
                    "4.1.1.63"
                ],
                reactants={
                    "CMPD_0000000010": 1,
                    "MNXM1": 1
                },
                products={
                    "CMPD_0000000003": 1,
                    "MNXM13": 1
                }
            ),
            rpReaction(
                id="rxn_2",
                ec_numbers=[
                    "1.14.13.23"
                ],
                reactants={
                    "CMPD_0000000025": 1,
                    "MNXM4": 1,
                    "MNXM6": 1,
                    "MNXM1": 1
                },
                products={
                    "CMPD_0000000010": 1,
                    "MNXM2": 1,
                    "MNXM5": 1
                }
            ),
            rpReaction(
                id="rxn_1",
                ec_numbers=[
                    "4.1.3.45"
                ],
                reactants={
                    "MNXM337": 1
                },
                products={
                    "CMPD_0000000025": 1,
                    "MNXM23": 1
                }
            )
        ]
        self.fba = 0.57290585662576
        self.thermo = {
            "dG0_prime": {
                "value": -884.6296371353768,
                "error": 9.819227446307337,
                "units": "kilojoule / mole"
            },
            "dGm_prime": {
                "value": -884.6296371353768,
                "error": 9.819227446307337,
                "units": "kilojoule / mole"
            },
            "dG_prime": {
                "value": -884.6296371353768,
                "error": 9.819227446307337,
                "units": "kilojoule / mole"
            }
        }
        self.rp2_transfo_id = 'TRS_0_0_0'
        self.rule_ids = ['RR-02-a0cc0be463ff412f-16-F']
        self.tmpl_rxn_ids = ['MNXR96458']
        self.idx_in_path = 1
        self.rule_score = 0.5982208769718989
        self.id = 'pathway'
        cache = rrCache(
            attrs=[
                'comp_xref',
                'deprecatedCompID_compid',
            ]
        )
        self.compartments = [
            {
                'id': 'MNXC3',
                'name': 'cytosol',
                'annot': cache.get('comp_xref')[
                    cache.get('deprecatedCompID_compid')[
                        'MNXC3'
                    ]
                ]
            }
        ]

        self.parameters = {
            "upper_flux_bound": {
                "value": 999999.0,
                "units": "mmol_per_gDW_per_hr"
            },
            "lower_flux_bound": {
                "value": 0.0,
                "units": "mmol_per_gDW_per_hr"
            }
        }
        self.unit_def = {
            "mmol_per_gDW_per_hr": [
                {
                    "kind": 23,
                    "exponent": 1,
                    "scale": -3,
                    "multiplier": 1.0
                },
                {
                    "kind": 8,
                    "exponent": 1,
                    "scale": 0,
                    "multiplier": 1.0
                },
                {
                    "kind": 28,
                    "exponent": 1,
                    "scale": 0,
                    "multiplier": 3600.0
                }
            ],
            "kj_per_mol": [
                {
                    "kind": 13,
                    "exponent": 1,
                    "scale": 3,
                    "multiplier": 1.0
                },
                {
                    "kind": 13,
                    "exponent": -1,
                    "scale": 1,
                    "multiplier": 1.0
                }
            ]
        }
        self.pathway = rpPathway(
            id=self.id,
        )
        self.pathway.set_parameters(self.parameters)
        self.pathway.set_unit_defs(self.unit_def)
        self.rxn.set_rp2_transfo_id(self.rp2_transfo_id)
        self.rxn.set_rule_ids(self.rule_ids)
        self.rxn.set_tmpl_rxn_ids(self.tmpl_rxn_ids)
        self.rxn.set_idx_in_path(self.idx_in_path)
        self.rxn.set_rule_score(self.rule_score)
        self.pathway.add_reaction(rxn=self.rxn, target_id=self.target.get_id())
        for rxn in self.reactions[1:]:
            self.pathway.add_reaction(rxn)
        self.sink = ['MNXM23', 'MNXM6', 'MNXM13']
        self.pathway.set_sink(self.sink)
        for key, value in self.thermo.items():
            self.pathway.add_thermo_info(key, value)
        self.pathway.set_fba_fraction(self.fba)