def __init__(self, genes=None, essential_genes=None, use_nullspace_simplification=True, *args, **kwargs): super(GeneKnockoutOptimization, self).__init__(*args, **kwargs) if genes is None: self.genes = set([g.id for g in self.model.genes]) else: self.genes = genes if essential_genes is None: self.essential_genes = {g.id for g in find_essential_genes(self.model, processes=1)} else: self.essential_genes = set([g.id for g in find_essential_genes(self.model, processes=1)] + essential_genes) # TODO: use genes from groups if use_nullspace_simplification: ns = nullspace(create_stoichiometric_array(self.model)) dead_end_reactions = find_blocked_reactions_nullspace(self.model, ns=ns) dead_end_genes = {g.id for g in self.model.genes if all(r in dead_end_reactions for r in g.reactions)} exclude_genes = self.essential_genes.union(dead_end_genes) genes = [g for g in self.model.genes if g.id not in exclude_genes] self.representation = [g.id for g in genes] else: self.representation = list(self.genes.difference(self.essential_genes)) self._target_type = GENE_KNOCKOUT_TYPE self._decoder = decoders.GeneSetDecoder(self.representation, self.model) self._evaluator = evaluators.KnockoutEvaluator(model=self.model, decoder=self._decoder, objective_function=self.objective_function, simulation_method=self._simulation_method, simulation_kwargs=self._simulation_kwargs)
def __init__(self, genes=None, essential_genes=None, use_nullspace_simplification=True, *args, **kwargs): super(GeneKnockoutOptimization, self).__init__(*args, **kwargs) if genes is None: self.genes = set([g.id for g in self.model.genes]) else: self.genes = genes if essential_genes is None: self.essential_genes = {g.id for g in find_essential_genes(self.model)} else: self.essential_genes = set([g.id for g in find_essential_genes(self.model)] + essential_genes) # TODO: use genes from groups if use_nullspace_simplification: ns = nullspace(create_stoichiometric_array(self.model)) dead_end_reactions = find_blocked_reactions_nullspace(self.model, ns=ns) dead_end_genes = {g.id for g in self.model.genes if all(r in dead_end_reactions for r in g.reactions)} exclude_genes = self.essential_genes.union(dead_end_genes) genes = [g for g in self.model.genes if g.id not in exclude_genes] self.representation = [g.id for g in genes] else: self.representation = list(self.genes.difference(self.essential_genes)) self._target_type = GENE_KNOCKOUT_TYPE self._decoder = decoders.GeneSetDecoder(self.representation, self.model) self._evaluator = evaluators.KnockoutEvaluator(model=self.model, decoder=self._decoder, objective_function=self.objective_function, simulation_method=self._simulation_method, simulation_kwargs=self._simulation_kwargs)
def __init__(self, reactions=None, essential_reactions=None, use_nullspace_simplification=True, *args, **kwargs): super(ReactionKnockoutOptimization, self).__init__(*args, **kwargs) if reactions is None: self.reactions = set([r.id for r in self.model.reactions]) else: self.reactions = reactions logger.debug("Computing essential reactions...") if essential_reactions is None: self.essential_reactions = set( r.id for r in find_essential_reactions(self.model, processes=1)) else: self.essential_reactions = set([ r.id for r in find_essential_reactions(self.model, processes=1) ]) self.essential_reactions.update(essential_reactions) if use_nullspace_simplification: ns = nullspace(create_stoichiometric_array(self.model)) dead_ends = set(find_blocked_reactions_nullspace(self.model, ns=ns)) exchanges = set(self.model.boundary) reactions = [ r for r in self.model.reactions if (r not in exchanges) and (r not in dead_ends) and ( r.id not in self.essential_reactions) ] groups = find_coupled_reactions_nullspace(self.model, ns=ns) groups_keys = [ set(group) for group in groups if any(r.id in reactions for r in group) ] reduced_set = reduce_reaction_set(reactions, groups_keys) to_keep = [r.id for r in reduced_set] else: groups = None to_keep = set(r.id for r in self.model.reactions) to_keep.difference_update(r.id for r in self.model.boundary) to_keep.difference_update(self.essential_reactions) to_keep = list(to_keep) self.representation = to_keep self._target_type = REACTION_KNOCKOUT_TYPE self._decoder = decoders.ReactionSetDecoder(self.representation, self.model, groups=groups) self._evaluator = evaluators.KnockoutEvaluator( model=self.model, decoder=self._decoder, objective_function=self.objective_function, simulation_method=self._simulation_method, simulation_kwargs=self._simulation_kwargs)
def __init__(self, reactions=None, essential_reactions=None, use_nullspace_simplification=True, *args, **kwargs): super(ReactionKnockoutOptimization, self).__init__(*args, **kwargs) if reactions is None: self.reactions = set([r.id for r in self.model.reactions]) else: self.reactions = reactions logger.debug("Computing essential reactions...") if essential_reactions is None: self.essential_reactions = set(r.id for r in find_essential_reactions(self.model, processes=1)) else: self.essential_reactions = set([r.id for r in find_essential_reactions(self.model, processes=1)]) self.essential_reactions.update(essential_reactions) if use_nullspace_simplification: ns = nullspace(create_stoichiometric_array(self.model)) dead_ends = set(find_blocked_reactions_nullspace(self.model, ns=ns)) exchanges = set(self.model.boundary) reactions = [ r for r in self.model.reactions if (r not in exchanges) and ( r not in dead_ends) and ( r.id not in self.essential_reactions) ] groups = find_coupled_reactions_nullspace(self.model, ns=ns) groups_keys = [set(group) for group in groups if any(r.id in reactions for r in group)] reduced_set = reduce_reaction_set(reactions, groups_keys) to_keep = [r.id for r in reduced_set] else: groups = None to_keep = set(r.id for r in self.model.reactions) to_keep.difference_update(r.id for r in self.model.boundary) to_keep.difference_update(self.essential_reactions) to_keep = list(to_keep) self.representation = to_keep self._target_type = REACTION_KNOCKOUT_TYPE self._decoder = decoders.ReactionSetDecoder(self.representation, self.model, groups=groups) self._evaluator = evaluators.KnockoutEvaluator(model=self.model, decoder=self._decoder, objective_function=self.objective_function, simulation_method=self._simulation_method, simulation_kwargs=self._simulation_kwargs)
def __init__(self, design_space_model, objective, variables=None, reference_model=None, exclude=(), normalize_ranges_by=None, points=10): super(DifferentialFVA, self).__init__() self.design_space_model = design_space_model self.design_space_nullspace = nullspace(create_stoichiometric_array(self.design_space_model)) if reference_model is None: self.reference_model = self.design_space_model.copy() fix_objective_as_constraint(self.reference_model) self.reference_nullspace = self.design_space_nullspace else: self.reference_model = reference_model self.reference_nullspace = nullspace(create_stoichiometric_array(self.reference_model)) if isinstance(objective, Reaction): self.objective = objective.id elif isinstance(objective, Metabolite): try: self.reference_model.add_boundary(objective, type='demand') except ValueError: pass try: self.objective = self.design_space_model.add_boundary(objective, type='demand').id except ValueError: self.objective = self.design_space_model.reactions.get_by_id("DM_" + objective.id).id elif isinstance(objective, six.string_types): self.objective = objective else: raise ValueError('You need to provide an objective as a Reaction, Metabolite or a reaction id') if variables is None: # try to establish the current objective reaction obj_var_ids = [variable.name for variable in self.design_space_model.objective.expression.free_symbols] obj_var_ids = [re.sub('_reverse.*', '', id) for id in obj_var_ids] if len(set(obj_var_ids)) != 1: raise ValueError( "The current objective in design_space_model is not a single reaction objective. " "DifferentialFVA does not support composite objectives.") else: self.variables = [self.design_space_model.reactions.get_by_id(obj_var_ids[0]).id] else: self.variables = list() for variable in variables: if isinstance(variable, Reaction): self.variables.append(variable.id) else: self.variables.append(variable) self.exclude = list() for elem in exclude: if isinstance(elem, Reaction): self.exclude.append(elem.id) else: self.exclude.append(elem) design_space_blocked_reactions = find_blocked_reactions_nullspace(self.design_space_model, self.design_space_nullspace) self.exclude += [reaction.id for reaction in design_space_blocked_reactions] reference_blocked_reactions = find_blocked_reactions_nullspace(self.reference_model, self.reference_nullspace) self.exclude += [reaction.id for reaction in reference_blocked_reactions] self.exclude += [reaction.id for reaction in self.design_space_model.boundary] self.exclude += [reaction.id for reaction in self.reference_model.boundary] self.exclude += [reaction.id for reaction in self.design_space_model.reactions if _BIOMASS_RE_.match(reaction.id)] self.exclude = set(self.exclude) self.points = points self.envelope = None self.grid = None self.reference_flux_ranges = None self.reference_flux_dist = None if isinstance(normalize_ranges_by, Reaction): self.normalize_ranges_by = normalize_ranges_by.id else: self.normalize_ranges_by = normalize_ranges_by
def __init__(self, design_space_model, objective, variables=None, reference_model=None, exclude=(), normalize_ranges_by=None, points=10): super(DifferentialFVA, self).__init__() self.design_space_model = design_space_model self.design_space_nullspace = nullspace( create_stoichiometric_array(self.design_space_model)) if reference_model is None: self.reference_model = self.design_space_model.copy() fix_objective_as_constraint(self.reference_model) self.reference_nullspace = self.design_space_nullspace else: self.reference_model = reference_model self.reference_nullspace = nullspace( create_stoichiometric_array(self.reference_model)) if isinstance(objective, Reaction): self.objective = objective.id elif isinstance(objective, Metabolite): try: self.reference_model.add_boundary(objective, type='demand') except ValueError: pass try: self.objective = self.design_space_model.add_boundary( objective, type='demand').id except ValueError: self.objective = self.design_space_model.reactions.get_by_id( "DM_" + objective.id).id elif isinstance(objective, six.string_types): self.objective = objective else: raise ValueError( 'You need to provide an objective as a Reaction, Metabolite or a reaction id' ) if variables is None: # try to establish the current objective reaction obj_var_ids = [ variable.name for variable in self.design_space_model.objective.expression.free_symbols ] obj_var_ids = [re.sub('_reverse.*', '', id) for id in obj_var_ids] if len(set(obj_var_ids)) != 1: raise ValueError( "The current objective in design_space_model is not a single reaction objective. " "DifferentialFVA does not support composite objectives.") else: self.variables = [ self.design_space_model.reactions.get_by_id( obj_var_ids[0]).id ] else: self.variables = list() for variable in variables: if isinstance(variable, Reaction): self.variables.append(variable.id) else: self.variables.append(variable) self.exclude = list() for elem in exclude: if isinstance(elem, Reaction): self.exclude.append(elem.id) else: self.exclude.append(elem) design_space_blocked_reactions = find_blocked_reactions_nullspace( self.design_space_model, self.design_space_nullspace) self.exclude += [ reaction.id for reaction in design_space_blocked_reactions ] reference_blocked_reactions = find_blocked_reactions_nullspace( self.reference_model, self.reference_nullspace) self.exclude += [ reaction.id for reaction in reference_blocked_reactions ] self.exclude += [ reaction.id for reaction in self.design_space_model.exchanges ] self.exclude += [ reaction.id for reaction in self.reference_model.exchanges ] self.exclude += [ reaction.id for reaction in self.design_space_model.reactions if _BIOMASS_RE_.match(reaction.id) ] self.exclude = set(self.exclude) self.points = points self.envelope = None self.grid = None self.reference_flux_ranges = None self.reference_flux_dist = None if isinstance(normalize_ranges_by, Reaction): self.normalize_ranges_by = normalize_ranges_by.id else: self.normalize_ranges_by = normalize_ranges_by
def __init__(self, design_space_model, objective, variables=None, reference_model=None, exclude=(), normalize_ranges_by=None, points=10): super(DifferentialFVA, self).__init__() self.design_space_model = design_space_model self.design_space_nullspace = nullspace( create_stoichiometric_array(self.design_space_model)) if reference_model is None: self.reference_model = self.design_space_model.copy() self.reference_nullspace = self.design_space_nullspace else: self.reference_model = reference_model self.reference_nullspace = nullspace( create_stoichiometric_array(self.reference_model)) if isinstance(objective, Reaction): self.objective = objective.id elif isinstance(objective, Metabolite): try: self.reference_model.add_boundary(objective, type='demand') except ValueError: pass try: self.objective = self.design_space_model.add_boundary( objective, type='demand').id except ValueError: self.objective = self.design_space_model.reactions.get_by_id( "DM_" + objective.id).id elif isinstance(objective, str): self.objective = objective else: raise ValueError( 'You need to provide an objective as a Reaction, Metabolite or a reaction id' ) if variables is None: # try to establish the current objective reaction obj_var_ids = [ variable.name for variable in self.design_space_model.objective.expression.free_symbols ] obj_var_ids = [re.sub('_reverse.*', '', id) for id in obj_var_ids] if len(set(obj_var_ids)) != 1: raise ValueError( "The current objective in design_space_model is not a single reaction objective. " "DifferentialFVA does not support composite objectives.") else: self.variables = [ self.design_space_model.reactions.get_by_id( obj_var_ids[0]).id ] else: self.variables = list() for variable in variables: if isinstance(variable, Reaction): self.variables.append(variable.id) else: self.variables.append(variable) if len(self.variables) > 1: raise NotImplementedError( "We also think that searching the production envelope over " "more than one variable would be a neat feature. However, " "at the moment there are some assumptions in the code that " "prevent this and we don't have the resources to change it. " "Pull request welcome ;-)") self.exclude = list() for elem in exclude: if isinstance(elem, Reaction): self.exclude.append(elem.id) else: self.exclude.append(elem) design_space_blocked_reactions = find_blocked_reactions_nullspace( self.design_space_model, self.design_space_nullspace) self.exclude += [ reaction.id for reaction in design_space_blocked_reactions ] reference_blocked_reactions = find_blocked_reactions_nullspace( self.reference_model, self.reference_nullspace) self.exclude += [ reaction.id for reaction in reference_blocked_reactions ] self.exclude += [ reaction.id for reaction in self.design_space_model.boundary ] self.exclude += [ reaction.id for reaction in self.reference_model.boundary ] self.exclude += [ reaction.id for reaction in self.design_space_model.reactions if _BIOMASS_RE_.match(reaction.id) ] self.exclude = set(self.exclude) self.points = points self.envelope = None self.grid = None self.reference_flux_ranges = None self.reference_flux_dist = None if isinstance(normalize_ranges_by, Reaction): self.normalize_ranges_by = normalize_ranges_by.id else: self.normalize_ranges_by = normalize_ranges_by self.included_reactions = { r.id for r in self.design_space_model.reactions if r.id not in self.exclude } # Re-introduce key reactions in case they were excluded. self.included_reactions.update(self.variables) self.included_reactions.add(self.objective) if self.normalize_ranges_by is not None: self.included_reactions.add(self.normalize_ranges_by) self.included_reactions = sorted(self.included_reactions)