Exemple #1
0
 def test_global_eff(self):
     # Load regular data
     expected_medium = {
         'EX_MNXM83': 2000.0, 
         'EX_pi_e': 2000.0, 
         'EX_fe3_e': 1000.0
     }
     specie_missing_id = 'MNXM83'
     medium = load_medium_file(os_path.join(self.medium_path, 'medium.fmt.c.csv'))
     medium = crossref_medium_id(
         df=medium,
         model=self.rpsbml,
         compartment_id='MNXC2'
     )
     exchange = self.rpsbml.build_exchange_reaction('c')
     df = merge_medium_exchange(
         medium = medium,
         exchange_reaction = exchange
     )
     rpsbml = add_missing_specie(
         self.rpsbml,
         df,
         'c'
     )
     cobra_model = None
     with NamedTemporaryFile(dir=self.temp_d, delete=False) as tempf:
         rpsbml.write_to_file(tempf.name)
         cobra_model=cobra_io.read_sbml_model(tempf.name, use_fbc_package=True)
     cobra_model.medium = df_to_medium(df)
     self.assertEqual(
         cobra_model.medium,
         expected_medium
     )
     tempf.close()
     remove(tempf.name)
Exemple #2
0
 def test_add_missing_specie(self):
     # Load regular data
     specie_missing_id = 'MNXM83'
     medium = load_medium_file(os_path.join(self.medium_path, 'medium.fmt.c.csv'))
     medium = crossref_medium_id(
         df=medium,
         model=self.rpsbml,
         compartment_id='MNXC2'
     )
     exchange = self.rpsbml.build_exchange_reaction('c')
     df = merge_medium_exchange(
         medium = medium,
         exchange_reaction = exchange
     )
     rpsbml = add_missing_specie(
         self.rpsbml,
         df,
         'c'
     )
     # Return type
     self.assertIsInstance(rpsbml, rpSBML)
     # Values
     species = rpsbml.getModel().getListOfSpecies()
     self.assertEqual(
         len([x for x in species if x.getId() == specie_missing_id]),
         1
     )
     specie = rpsbml.getModel().getSpecies(specie_missing_id)
     self.assertTrue(specie.getBoundaryCondition())
Exemple #3
0
 def test_merge_medium_exchange(self):
     # Load
     medium = load_medium_file(os_path.join(self.medium_path, 'medium.fmt.a.csv'))
     medium = crossref_medium_id(
         df=medium,
         model=self.rpsbml,
         compartment_id='MNXC2'
     )
     exchange = self.rpsbml.build_exchange_reaction('c')
     df = merge_medium_exchange(
         medium = medium,
         exchange_reaction = exchange
     )
     # Return type.
     self.assertIsInstance(
         df,
         pd.DataFrame
     )
     # Values.
     self.assertEqual(
         df.shape,
         (331, len(exchange.columns)+len(df.columns)-2)
     )
     self.assertEqual(
         df[self.__MEDIUM_HEADER_COMPOUND_ID].isna().sum(),
         331-2
     )
     self.assertTrue(pd.api.types.is_float_dtype(df[self.__MEDIUM_HEADER_BOUND]))
Exemple #4
0
    def test_df_to_medium(self):
        # Load.
        medium_a = load_medium_file(os_path.join(self.medium_path, 'medium.fmt.a.csv'))
        dfa = crossref_medium_id(
            df=medium_a,
            model=self.rpsbml,
            compartment_id='MNXC2'
        )
        exchange = self.rpsbml.build_exchange_reaction('c')
        dfa = merge_medium_exchange(
            medium = dfa,
            exchange_reaction = exchange
        )
 
        # Return Type.
        dfad = dfa.drop(columns=['reaction_name'])
        medium = df_to_medium(dfad)
        self.assertIsInstance(medium, Dict)
        dfad = dfa.drop(columns=['reaction_name'])
        medium = df_to_medium(dfad)
        self.assertIsInstance(medium, Dict)
        medium = df_to_medium(dfa)
        self.assertIsInstance(medium, Dict)
        # Values.
        self.assertEqual(
            sum([x.startswith('EX') for x in medium.keys()]),
            dfa.shape[0]
        )
Exemple #5
0
def runFBA(
    pathway: rpPathway,
    gem_sbml_path: str,
    compartment_id: str,
    objective_rxn_id: str = 'rxn_target',
    biomass_rxn_id: str = 'biomass',
    sim_type: str = 'fraction',
    fraction_coeff: float = 0.75,
    merge: bool = False,
    ignore_orphan_species: bool = True,
    medium_compartment_id: str = 'MNXC2',
    df_medium_base: pd.DataFrame = None,
    df_medium_user: pd.DataFrame = None,
    logger: Logger = getLogger(__name__)
) -> Dict:
    """Single rpSBML simulation

    :param file_name: The name of the model
    :param pathway_fn: Path to the pathway file (JSON)
    :param gem_sbml: Path to the GEM file
    :param sim_type: The type of simulation to use. Available simulation types include: fraction, fba, rpfba
    :param src_rxn_id: The reaction id of the source reaction.
    :param target_reaction: The reaction id of the target reaction. Note that if fba or rpfba options are used, then these are ignored
    :param source_coefficient: The source coefficient
    :param target_coefficient: The target coefficient
    :param is_max: Maximise or minimise the objective'last', i
    :param fraction_of: The fraction of the optimum. Note that this value is ignored is fba is used
    :param tmpOutputFolder: The path to the output document
    :param merge: Output the merged model (Default: False)
    :param pathway_id: The id of the heterologous pathway (Default: rp_pathway)
    :param objective_id: Overwrite the auto-generated id of the results (Default: None)
    :param compartment_id: The SBML compartment id (Default: MNXC3)
    :param fill_orphan_species: Add pseudo reactions that consume/produce single parent species. Note in development
    :param species_group_id: The id of the central species (Default: central_species)
    :param sink_species_group_id: The id of the sink species (Default: rp_sink_species)
 not 
    :type inputTar: str
    :type gem_sbml_path: str
    :type sim_type: str
    :type src_rxn_id: str
    :type target_reaction: str
    :type source_coefficient: float
    :type target_coefficient: float
    :type is_max: bool
    :type fraction_of: float
    :type tmpOutputFolder: str
    :type merge: bool
    :type num_workers: int
    :type pathway_id: str
    :type objective_id: str
    :type compartment_id: str
    :type fill_orphan_species: bool
    :type species_group_id: str
    :type sink_species_group_id: str

    :return: Succcess or failure of the function
    :rtype: bool
    """

    logger.debug('           pathway_fn: ' + str(pathway))
    logger.debug('        gem_sbml_path: ' + str(gem_sbml_path))
    logger.debug('             sim_type: ' + str(sim_type))
    logger.debug('     objective_rxn_id: ' + objective_rxn_id)
    logger.debug('       biomass_rxn_id: ' + str(biomass_rxn_id))
    logger.debug('       fraction_coeff: ' + str(fraction_coeff))
    logger.debug('                merge: ' + str(merge))
    logger.debug('       compartment_id: ' + str(compartment_id))
    logger.debug('ignore_orphan_species: ' + str(ignore_orphan_species))

    ## MODEL
    # Create rpSBML object
    rpsbml_gem = rpSBML(inFile=gem_sbml_path, logger=logger)
    # Check compartment ID
    compartment_id = check_SBML_compartment(rpsbml=rpsbml_gem,
                                            compartment_id=compartment_id,
                                            logger=logger)
    if compartment_id is None:
        return None
    # Check biomass reaction ID
    biomass_rxn_id = check_SBML_rxnid(rpsbml=rpsbml_gem,
                                      rxn_id=biomass_rxn_id,
                                      logger=logger)
    if biomass_rxn_id is None:
        return None

    # PATHWAY
    rpsbml = build_rpsbml(pathway=pathway, logger=logger)
    # Check objective reaction ID
    objective_rxn_id = check_SBML_rxnid(rpsbml=rpsbml,
                                        rxn_id=objective_rxn_id,
                                        logger=logger)
    if objective_rxn_id is None:
        return None

    ## MERGE
    # Merge predicted pathway with the full model
    # missing_species are species that are not detected in the model
    logger.info('Merging rpSBML models: ' + rpsbml.getName() + ' and ' +
                rpsbml_gem.getName() + '...')
    (rpsbml_merged, reactions_in_both, missing_species,
     compartment_id) = rpSBML.merge(pathway=rpsbml,
                                    model=rpsbml_gem,
                                    compartment_id=compartment_id,
                                    logger=logger)
    if rpsbml_merged is None:
        return None
    logger.debug('rpsbml_merged: ' + str(rpsbml_merged))
    logger.debug('reactions_in_both: ' + str(reactions_in_both))
    cobra_rpsbml_merged = rpSBML.cobraize(rpsbml_merged)

    ## MEDIUM
    df_medium = pd.DataFrame()
    if is_df_medium_defined(df_medium_base) or is_df_medium_defined(
            df_medium_user):
        # Check medium compartment id.
        medium_compartment_id = check_SBML_compartment(
            rpsbml=cobra_rpsbml_merged,
            compartment_id=medium_compartment_id,
            logger=logger)
        if medium_compartment_id is None:
            logger.warning(
                'Medium id not find in the model -> ignore modifications')
        else:
            # CrossRef ids
            df_medium_base = crossref_medium_id(
                df=df_medium_base,
                model=cobra_rpsbml_merged,
                compartment_id=medium_compartment_id,
                logger=logger)
            df_medium_user = crossref_medium_id(
                df=df_medium_user,
                model=cobra_rpsbml_merged,
                compartment_id=medium_compartment_id,
                logger=logger)
            # Merge coumponds.
            df_medium = merge_medium(first=df_medium_base,
                                     second=df_medium_user)

            # Select exchange reaction
            df_exchange_reaction = cobra_rpsbml_merged.build_exchange_reaction(
                compartment_id=medium_compartment_id)

            # Merge df medium with exchange reactions
            df_medium = merge_medium_exchange(
                medium=df_medium, exchange_reaction=df_exchange_reaction)

            # Add specie missing in the model
            cobra_rpsbml_merged = add_missing_specie(
                model=cobra_rpsbml_merged,
                df=df_medium,
                compartment_id=medium_compartment_id,
                logger=logger)

    # Detect orphan species among missing ones in the model,
    # i.e. that are only consumed or produced
    if ignore_orphan_species:
        hidden_species = build_hidden_species(rpsbml=rpsbml,
                                              missing_species=missing_species,
                                              compartment_id=compartment_id,
                                              logger=logger)
    else:
        hidden_species = []

    # NOTE: reactions is organised with key being the rpsbml reaction and value being the rpsbml_gem value`
    # BUG: when merging the rxn_sink (very rare cases) can be recognised if another reaction contains the same species as a reactant
    ## under such as scenario the algorithm will consider that they are the same -- TODO: overwrite it
    if objective_rxn_id in reactions_in_both:
        logger.warning(
            'The target_reaction ('+str(objective_rxn_id)+') ' \
          + 'has been detected in model ' + str(gem_sbml_path.getName()) + ', ' \
          + 'ignoring this model...'
        )
        return None

    ######## FBA ########
    results = {}
    if sim_type.lower() in ['fba', 'pfba']:
        objective_id = cobra_rpsbml_merged.find_or_create_objective(
            rxn_id=objective_rxn_id,
            obj_id=f'brs_obj_{objective_rxn_id}',
        )
        cobra_results, minimal_medium = runCobra(sim_type=sim_type,
                                                 rpsbml=cobra_rpsbml_merged,
                                                 hidden_species=hidden_species,
                                                 objective_id=objective_id,
                                                 fraction_coeff=fraction_coeff,
                                                 medium=df_medium,
                                                 logger=logger)
    else:
        (cobra_results, results_biomass, objective_id,
         minimal_medium) = rp_fraction(rpsbml=cobra_rpsbml_merged,
                                       objective_rxn_id=objective_rxn_id,
                                       biomass_rxn_id=biomass_rxn_id,
                                       hidden_species=hidden_species,
                                       fraction_coeff=fraction_coeff,
                                       medium=df_medium,
                                       logger=logger)
        results['biomass'] = results_biomass

    # print(cobra_results.objective_value)
    # print(cobra_results.status)
    # print(cobra_results.fluxes)
    # print(cobra_results.shadow_prices)

    results[sim_type] = cobra_results

    # Write results for merged model
    write_results_to_rpsbml(rpsbml=cobra_rpsbml_merged,
                            objective_id=objective_id,
                            cobra_results=cobra_results,
                            sim_type=sim_type,
                            logger=logger)

    _results = build_results(results=results,
                             pathway=pathway,
                             compartment_id=compartment_id)
    _results['ignored_species'] = deepcopy(hidden_species)

    # Remove the Cobra standard ('compound@compartment') from all compounds
    pathway.uncobraize()
    _results = uncobraize_results(_results, cobra_suffix(compartment_id))

    # Write results into the pathway
    write_results_to_pathway(pathway, _results, logger)

    _results['minimal_medium'] = minimal_medium
    return _results