def test_single_deletion(self): cobra_model = self.model initialize_growth_medium(cobra_model, 'LB') #Expected growth rates for the salmonella model with deletions in LB medium the_loci = ['STM4081', 'STM0247', 'STM3867', 'STM2952'] the_genes = tpiA, metN, atpA, eno = map(cobra_model.genes.get_by_id, the_loci) id_to_name = dict([(x.id, x.name) for x in the_genes]) growth_dict = {'fba':{tpiA.id:2.41, metN.id:2.44, atpA.id:1.87, eno.id:1.81}, 'moma':{ tpiA.id:1.62, metN.id:2.4, atpA.id:1.40, eno.id:0.33}} #MOMA requires cplex or gurobi if get_solver_name(qp=True) is None: growth_dict.pop('moma') for method, the_growth_rates in growth_dict.items(): element_list = the_growth_rates.keys() results = single_deletion(cobra_model, element_list=element_list, method=method) rates = results[0] statuses = results[1] for the_gene in element_list: self.assertEqual(statuses[the_gene], 'optimal') self.assertAlmostEqual(rates[the_gene], the_growth_rates[the_gene], places=2)
def test_single_reaction_deletion(self): cobra_model = self.model results, status = single_deletion( cobra_model, element_list=cobra_model.reactions[100:110:2], element_type="reaction") self.assertEqual(len(results), 5) self.assertEqual(len(status), 5) for status_value in status.values(): self.assertEqual(status_value, "optimal") expected_results = {'3OAS140': 0, '3OAS160': 0.38001, '3OAS180': 0.38001, '3OAS60': 0, '3PEPTabcpp': 0.38001} for reaction, value in results.items(): self.assertAlmostEqual(value, expected_results[reaction], 5)
def test_single_deletion(self): cobra_model = self.model initialize_growth_medium(cobra_model, 'LB') #Expected growth rates for the salmonella model with deletions in LB medium the_loci = ['STM4081', 'STM0247', 'STM3867', 'STM2952'] the_genes = tpiA, metN, atpA, eno = map(cobra_model.genes.get_by_id, the_loci) id_to_name = dict([(x.id, x.name) for x in the_genes]) growth_dict = { 'fba': { tpiA.id: 2.41, metN.id: 2.44, atpA.id: 1.87, eno.id: 1.81 }, 'moma': { tpiA.id: 1.62, metN.id: 2.4, atpA.id: 1.40, eno.id: 0.33 } } #MOMA requires cplex or gurobi try: get_solver_name(qp=True) except: growth_dict.pop('moma') for method, the_growth_rates in growth_dict.items(): element_list = the_growth_rates.keys() results = single_deletion(cobra_model, element_list=element_list, method=method) rates = results[0] statuses = results[1] for the_gene in element_list: self.assertEqual(statuses[the_gene], 'optimal') self.assertAlmostEqual(rates[the_gene], the_growth_rates[the_gene], places=2)
def deletion_analysis( cobra_model, the_medium=None, deletion_type="single", work_directory=None, growth_cutoff=0.001, the_problem="return", n_processes=6, element_type="gene", solver="glpk", error_reporting=None, method="fba", element_list=None, ): """Performs single and/or double deletion analysis on all the genes in the model. Provides an interface to parallelize the deletion studies. cobra_model: A Model object. the_medium: Is None, a string, or a dictionary. If a string then the initialize_growth_medium function expects that cobra_model has an attribute dictionary called media_compositions, which is a dictionary of dictionaries for various medium compositions. Where a medium composition is a dictionary of exchange reaction ids for the medium components and the exchange fluxes for each medium component; note that these fluxes must be negative because they are being exchanged into the system. deletion_type: 'single', 'double', or 'double-only' work_directory: None or String indicating where to save the output from the simulations. growth_cutoff: Float. Indiates the minimum growth rate that is considered viable. the_problem: Is None, 'return', or an LP model object for the solver. element_type: 'gene' or 'reaction' solver: 'glpk', 'gurobi', or 'cplex' n_processes: number of parallel processes to break the double deletion simulations into. error_reporting: None or True element_list: None or a list of genes to delete from the model. Returns: Nothing. However, the script will add attributes single_deletion_* and double_deletion_* to cobra_model containing the simulation results. """ if element_type == "reaction": warn("deletion_analysis is not perfect for element_type = 'reaction'") # When using ppmap, it's easier to feed in the parameters as a list, # if the defaults need to be changed if isinstance(cobra_model, list): tmp_model = cobra_model cobra_model = tmp_model[0] if len(tmp_model) > 1: the_medium = tmp_model[1] if len(tmp_model) > 2: deletion_type = tmp_model[2] if len(tmp_model) > 3: work_directory = tmp_model[3] if len(tmp_model) > 4: growth_cutoff = tmp_model[4] if the_medium is not None: initialize_growth_medium(cobra_model, the_medium) the_problem = cobra_model.optimize(the_problem=the_problem, solver=solver) # Store the basal model for the simulations if element_list is None: element_list = getattr(cobra_model, element_type + "s") if deletion_type != "double_only": cobra_model.single_deletion_growth_wt = cobra_model.solution.f growth_rate_dict, growth_solution_status_dict, problem_dict = single_deletion( deepcopy(cobra_model), element_list=element_list, the_problem=the_problem, element_type=element_type, solver=solver, error_reporting=error_reporting, method=method, ) del problem_dict cobra_model.single_deletion_growth_dict = growth_rate_dict cobra_model.single_deletion_solution_status_dict = growth_solution_status_dict setattr(cobra_model, "single_deletion_%ss" % element_type, deepcopy(growth_rate_dict.keys())) cobra_model.single_deletion_lethal = [x for x in growth_rate_dict.keys() if growth_rate_dict[x] < growth_cutoff] cobra_model.single_deletion_growth_medium = the_medium cobra_model.single_deletion_nonlethal = list( set(growth_rate_dict.keys()).difference(cobra_model.single_deletion_lethal) ) if work_directory is not None: if not path.lexists(work_directory): mkdir(work_directory) with open(work_directory + the_medium + "_single_" + cobra_model.description, "w") as out_file: dump(cobra_model, out_file) if deletion_type == "double" or deletion_type == "double_only": # It appears that the glpk interface no longer works will with sending # a glpk.LPX object through ppmap, so just set the basis to return if the_problem: the_problem = "return" cobra_model.double_deletion_growth_medium = the_medium cobra_model.double_deletion_growth_wt = cobra_model.solution.f if not __parallel_mode_available: if n_processes > 0: print "Couldn't import ppmap from cobra.external is parallel python installed?" return else: cobra_model = double_deletion_parallel( deepcopy(cobra_model), genes_of_interest=element_list, the_problem=the_problem, n_processes=n_processes, element_type=element_type, solver=solver, error_reporting=error_reporting, method=method, ) # This indicates the genes that were run through double deletion but # the x and y lists specify the order setattr(cobra_model, "double_deletion_%ss" % element_type, deepcopy(cobra_model.genes)) if work_directory is not None: with open(work_directory + the_medium + "_double_" + cobra_model.description, "w") as out_file: dump(cobra_model, out_file) return cobra_model
cobra_model = create_test_model(salmonella_pickle) initialize_growth_medium(cobra_model, 'LB') target_genes = ['STM4081', 'STM0247', 'STM3867', 'STM2952'] # Expected growth rates for the salmonella model after a deletions in LB medium expected_growth_rates = { "STM4081": 2.41, "STM0247": 2.43, "STM3867": 1.87, "STM2952": 1.81} start_time = time() # start timer # Perform deletions for all genes in the list rates, statuses = single_deletion(cobra_model, target_genes) total_time = time() - start_time # stop timer # print out results passed_string = 'PASSED: %s simulation (%1.3f) ~= expectation (%1.2f)' failed_string = 'FAILED: %s simulation (%1.3f) != expectation (%1.2f)' for gene_locus, rate in rates.items(): # get gene name from gene locus (i.e. STM4081 -> tpiA) name = cobra_model.genes.get_by_id(gene_locus).name # test if the simulation failed if statuses[gene_locus] != "optimal": print("deletion failed for %s (%s)" % (name, gene_locus)) if abs(rate - expected_growth_rates[gene_locus]) > 0.01: print(failed_string % (name, rate, expected_growth_rates[gene_locus])) else:
def double_gene_deletion_moma(cobra_model, gene_list_1=None, gene_list_2=None, method='moma', single_deletion_growth_dict=None, solver='glpk', growth_tolerance=1e-8, error_reporting=None): """This will disable reactions for all gene pairs from gene_list_1 and gene_list_2 and then run simulations to optimize for the objective function. The contribution of each reaction to the objective function is indicated in cobra_model.reactions[:].objective_coefficient vector. NOTE: We've assumed that there is no such thing as a synthetic rescue with this modeling framework. cobra_model: a cobra.Model object gene_list_1: Is None or a list of genes. If None then both gene_list_1 and gene_list_2 are assumed to correspond to cobra_model.genes. gene_list_2: Is None or a list of genes. If None then gene_list_2 is assumed to correspond to gene_list_1. method: 'fba' or 'moma' to run flux balance analysis or minimization of metabolic adjustments. single_deletion_growth_dict: A dictionary that provides the growth rate information for single gene knock outs. This can speed up simulations because nonviable single deletion strains imply that all double deletion strains will also be nonviable. solver: 'glpk', 'gurobi', or 'cplex'. error_reporting: None or True growth_tolerance: float. The effective lower bound on the growth rate for a single deletion that is still considered capable of growth. Returns a dictionary of the gene ids in the x dimension (x) and the y dimension (y), and the growth simulation data (data). """ #BUG: Since this might be called from ppmap, the modules need to #be imported. Modify ppmap to take depfuncs from numpy import zeros nan = float('nan') from cobra.flux_analysis.single_deletion import single_deletion from cobra.manipulation import delete_model_genes, undelete_model_genes ##TODO: Use keywords instead if isinstance(cobra_model, dict): tmp_dict = cobra_model cobra_model = tmp_dict['cobra_model'] if 'gene_list_1' in tmp_dict: gene_list_1 = tmp_dict['gene_list_1'] if 'gene_list_2' in tmp_dict: gene_list_2 = tmp_dict['gene_list_2'] if 'method' in tmp_dict: method = tmp_dict['method'] if 'single_deletion_growth_dict' in tmp_dict: single_deletion_growth_dict = tmp_dict['single_deletion_growth_dict'] if 'solver' in tmp_dict: solver = tmp_dict['solver'] if 'error_reporting' in tmp_dict: error_reporting = tmp_dict['error_reporting'] else: cobra_model = cobra_model #this is a slow way to revert models. wt_model = cobra_model #NOTE: It may no longer be necessary to use a wt_model #due to undelete_model_genes if gene_list_1 is None: gene_list_1 = cobra_model.genes elif not hasattr(gene_list_1[0], 'id'): gene_list_1 = map(cobra_model.genes.get_by_id, gene_list_1) #Get default values to use if the deletions do not alter any reactions cobra_model.optimize(solver=solver) basal_f = cobra_model.solution.f if method.lower() == 'moma': wt_model = cobra_model.copy() combined_model = None single_gene_set = set(gene_list_1) if gene_list_2 is not None: if not hasattr(gene_list_2[0], 'id'): gene_list_2 = map(cobra_model.genes.get_by_id, gene_list_2) single_gene_set.update(gene_list_2) #Run the single deletion analysis to account for double deletions that #target the same gene and lethal deletions. We assume that there #aren't synthetic rescues. single_deletion_growth_dict = single_deletion(cobra_model, list(single_gene_set), method=method, solver=solver)[0] if gene_list_2 is None or gene_list_1 == gene_list_2: number_of_genes = len(gene_list_1) gene_list_2 = gene_list_1 deletion_array = zeros([number_of_genes, number_of_genes]) ##TODO: Speed up this triangular process #For the case where the contents of the lists are the same cut the work in half. #There might be a faster way to do this by using a triangular array function #in numpy #Populate the diagonal from the single deletion lists for i, the_gene in enumerate(gene_list_1): deletion_array[i, i] = single_deletion_growth_dict[the_gene.id] for i, gene_1 in enumerate(gene_list_1[:-1]): #TODO: Since there cannot be synthetic rescues we can assume #that the whole row for a lethal deletion #will be equal to that deletion. if single_deletion_growth_dict[gene_1.id] < growth_tolerance: tmp_solution = single_deletion_growth_dict[gene_1.id] for j in range(i+1, number_of_genes): deletion_array[j, i] = deletion_array[i, j] = tmp_solution else: for j, gene_2 in enumerate(gene_list_1[i+1:], i+1): if single_deletion_growth_dict[gene_2.id] < growth_tolerance: tmp_solution = single_deletion_growth_dict[gene_2.id] else: delete_model_genes(cobra_model, [gene_1, gene_2]) if cobra_model._trimmed: if method.lower() == 'fba': #Assumes that the majority of perturbations don't change #reactions which is probably false cobra_model.optimize(solver=solver, error_reporting=error_reporting) the_status = cobra_model.solution.status tmp_solution = cobra_model.solution.f elif method.lower() == 'moma': try: moma_solution = moma(wt_model, cobra_model, combined_model=combined_model, solver=solver) tmp_solution = float(moma_solution.pop('objective_value')) the_status = moma_solution.pop('status') combined_model = moma_solution.pop('combined_model') del moma_solution except: tmp_solution = nan the_status = 'failed' if the_status not in ['opt', 'optimal'] and \ error_reporting: print('%s / %s: %s status: %s'%(gene_1, gene_2, solver, the_status)) #Reset the model to orginial form. undelete_model_genes(cobra_model) else: tmp_solution = basal_f deletion_array[j, i] = deletion_array[i, j] = tmp_solution else: deletion_array = zeros([len(gene_list_1), len(gene_list_2)]) #Now deal with the case where the gene lists are different for i, gene_1 in enumerate(gene_list_1): if single_deletion_growth_dict[gene_1.id] <= 0: for j in range(len(gene_list_2)): deletion_array[i, j] = 0. else: for j, gene_2 in enumerate(gene_list_2): #Assume no such thing as a synthetic rescue if single_deletion_growth_dict[gene_2.id] <= growth_tolerance: tmp_solution = single_deletion_growth_dict[gene_2.id] else: delete_model_genes(cobra_model, [gene_1, gene_2]) if cobra_model._trimmed: if method.lower() == 'fba': cobra_model.optimize(solver=solver) tmp_solution = cobra_model.solution.f the_status = cobra_model.solution.status elif method.lower() == 'moma': try: moma_solution = moma(wt_model, cobra_model, combined_model=combined_model, solver=solver) tmp_solution = float(moma_solution.pop('objective_value')) the_status = moma_solution.pop('status') combined_model = moma_solution.pop('combined_model') del moma_solution except: tmp_solution = nan the_status = 'failed' if the_status not in ['opt', 'optimal'] and \ error_reporting: print('%s / %s: %s status: %s'%(repr(gene_1), repr(gene_2), solver, cobra_model.solution.status)) #Reset the model to wt form undelete_model_genes(cobra_model) else: tmp_solution = basal_f deletion_array[i, j] = tmp_solution if hasattr(gene_list_1, 'id'): gene_list_1 = [x.id for x in gene_list_1] if hasattr(gene_list_2, 'id'): gene_list_2 = [x.id for x in gene_list_2] return({'x': gene_list_1, 'y': gene_list_2, 'data': deletion_array})
def double_gene_deletion( cobra_model, gene_list_1=None, gene_list_2=None, method="fba", single_deletion_growth_dict=None, the_problem="return", solver="glpk", error_reporting=None, ): """This will disable reactions for all gene pairs from gene_list_1 and gene_list_2 and then run simulations to optimize for the objective function. The contribution of each reaction to the objective function is indicated in cobra_model.reactions[:].objective_coefficient vector. cobra_model: a cobra.Model object gene_list_1: Is None or a list of genes. If None then both gene_list_1 and gene_list_2 are assumed to correspond to cobra_model.genes. gene_list_2: Is None or a list of genes. If None then gene_list_2 is assumed to correspond to gene_list_1. method: 'fba' or 'moma' to run flux balance analysis or minimization of metabolic adjustments. single_deletion_growth_dict: A dictionary that provides the growth rate information for single gene knock outs. This can speed up simulations because nonviable single deletion strains imply that all double deletion strains will also be nonviable. the_problem: Is None, 'return', or an LP model object for the solver. solver: 'glpk', 'gurobi', or 'cplex'. error_reporting: None or True Returns a dictionary of the genes in the x dimension (x), the y dimension (y), and the growth simulation data (data). """ # BUG: Since this might be called from ppmap, the modules need to # be imported. Modify ppmap to take depfuncs from numpy import zeros, nan from cobra.flux_analysis.single_deletion import single_deletion from cobra.manipulation import initialize_growth_medium from cobra.manipulation import delete_model_genes, undelete_model_genes ##TODO: Use keywords instead if isinstance(cobra_model, dict): tmp_dict = cobra_model cobra_model = tmp_dict["cobra_model"] if "gene_list_1" in tmp_dict: gene_list_1 = tmp_dict["gene_list_1"] if "gene_list_2" in tmp_dict: gene_list_2 = tmp_dict["gene_list_2"] if "method" in tmp_dict: method = tmp_dict["method"] if "the_problem" in tmp_dict: the_problem = tmp_dict["the_problem"] if "single_deletion_growth_dict" in tmp_dict: single_deletion_growth_dict = tmp_dict["single_deletion_growth_dict"] if "solver" in tmp_dict: solver = tmp_dict["solver"] if "error_reporting" in tmp_dict: error_reporting = tmp_dict["error_reporting"] else: cobra_model = cobra_model # this is a slow way to revert models. wt_model = cobra_model # NOTE: It may no longer be necessary to use a wt_model # due to undelete_model_genes if gene_list_1 is None: gene_list_1 = cobra_model.genes elif not hasattr(gene_list_1[0], "id"): gene_list_1 = map(cobra_model.genes.get_by_id, gene_list_1) # Get default values to use if the deletions do not alter any reactions the_problem = cobra_model.optimize(the_problem=the_problem, solver=solver) basal_f = cobra_model.solution.f if method.lower() == "moma": wt_model = cobra_model.copy() the_problem = "return" combined_model = None single_gene_set = set(gene_list_1) if gene_list_2: if not hasattr(gene_list_2[0], "id"): gene_list_2 = map(cobra_model.genes.get_by_id, gene_list_2) single_gene_set.update(gene_list_2) # Run the single deletion analysis to account for double deletions that # target the same gene and lethal deletions. We assume that there # aren't synthetic rescues. single_deletion_growth_dict = single_deletion( cobra_model, list(single_gene_set), method=method, the_problem=the_problem, solver=solver, error_reporting=error_reporting, )[0] if gene_list_2 is None or gene_list_1 == gene_list_2: deletion_array = zeros([len(gene_list_1), len(gene_list_1)]) ##TODO: Speed up this triangular process # For the case where the contents of the lists are the same cut the work in half. # There might be a faster way to do this by using a triangular array function # in numpy # Populate the diagonal from the single deletion lists for i, the_gene in enumerate(gene_list_1): deletion_array[i, i] = single_deletion_growth_dict[the_gene.id] for i in range(len(gene_list_1) - 1): gene_1 = gene_list_1[i] # TODO: Since there cannot be synthetic rescues we can assume # that the whole row for a lethal deletion # will be equal to that deletion. if single_deletion_growth_dict[gene_1.id] <= 0: for j in range(i + 1, len(gene_list_1)): deletion_array[j, i] = deletion_array[i, j] = single_deletion_growth_dict[gene_2.id] else: for j in range(i + 1, len(gene_list_1)): if single_deletion_growth_dict[gene_1.id] <= 0: tmp_solution = single_deletion_growth_dict[gene_1.id] else: gene_2 = gene_list_1[j] delete_model_genes(cobra_model, [gene_1, gene_2]) if cobra_model._trimmed: if method.lower() == "fba": # Assumes that the majority of perturbations don't change # reactions which is probably false cobra_model.optimize( the_problem=the_problem, solver=solver, error_reporting=error_reporting ) the_status = cobra_model.solution.status tmp_solution = cobra_model.solution.f elif method.lower() == "moma": try: moma_solution = moma( wt_model, cobra_model, combined_model=combined_model, solver=solver, the_problem=the_problem, ) tmp_solution = float(moma_solution.pop("objective_value")) the_problem = moma_solution.pop("the_problem") the_status = moma_solution.pop("status") combined_model = moma_solution.pop("combined_model") del moma_solution except: tmp_solution = nan the_status = "failed" if the_status not in ["opt", "optimal"] and error_reporting: print "%s / %s: %s status: %s" % (gene_1, gene_2, solver, the_status) # Reset the model to orginial form. undelete_model_genes(cobra_model) else: tmp_solution = basal_f deletion_array[j, i] = deletion_array[i, j] = tmp_solution else: deletion_array = zeros([len(gene_list_1), len(gene_list_2)]) # Now deal with the case where the gene lists are different for i, gene_1 in enumerate(gene_list_1): if single_deletion_growth_dict[gene_1.id] <= 0: for j in range(len(gene_list_2)): deletion_array[i, j] = 0.0 else: for j, gene_2 in enumerate(gene_list_2): # Assume no such thing as a synthetic rescue if single_deletion_growth_dict[gene_2.id] <= 0: tmp_solution = single_deletion_growth_dict[gene_2.id] else: delete_model_genes(cobra_model, [gene_1, gene_2]) if cobra_model._trimmed: if method.lower() == "fba": cobra_model.optimize( the_problem=the_problem, solver=solver, error_reporting=error_reporting ) tmp_solution = cobra_model.solution.f the_status = cobra_model.solution.status elif method.lower() == "moma": try: moma_solution = moma( wt_model, cobra_model, combined_model=combined_model, solver=solver, the_problem=the_problem, ) tmp_solution = float(moma_solution.pop("objective_value")) the_problem = moma_solution.pop("the_problem") the_status = moma_solution.pop("status") combined_model = moma_solution.pop("combined_model") del moma_solution except: tmp_solution = nan the_status = "failed" if the_status not in ["opt", "optimal"] and error_reporting: print "%s / %s: %s status: %s" % ( repr(gene_1), repr(gene_2), solver, cobra_model.solution.status, ) # Reset the model to wt form undelete_model_genes(cobra_model) else: tmp_solution = basal_f deletion_array[i, j] = tmp_solution return {"x": gene_list_1, "y": gene_list_2, "data": deletion_array}
def double_gene_deletion_moma(cobra_model, gene_list_1=None, gene_list_2=None, method='moma', single_deletion_growth_dict=None, solver='glpk', growth_tolerance=1e-8, error_reporting=None): """This will disable reactions for all gene pairs from gene_list_1 and gene_list_2 and then run simulations to optimize for the objective function. The contribution of each reaction to the objective function is indicated in cobra_model.reactions[:].objective_coefficient vector. NOTE: We've assumed that there is no such thing as a synthetic rescue with this modeling framework. cobra_model: a cobra.Model object gene_list_1: Is None or a list of genes. If None then both gene_list_1 and gene_list_2 are assumed to correspond to cobra_model.genes. gene_list_2: Is None or a list of genes. If None then gene_list_2 is assumed to correspond to gene_list_1. method: 'fba' or 'moma' to run flux balance analysis or minimization of metabolic adjustments. single_deletion_growth_dict: A dictionary that provides the growth rate information for single gene knock outs. This can speed up simulations because nonviable single deletion strains imply that all double deletion strains will also be nonviable. solver: 'glpk', 'gurobi', or 'cplex'. error_reporting: None or True growth_tolerance: float. The effective lower bound on the growth rate for a single deletion that is still considered capable of growth. Returns a dictionary of the gene ids in the x dimension (x) and the y dimension (y), and the growth simulation data (data). """ #BUG: Since this might be called from ppmap, the modules need to #be imported. Modify ppmap to take depfuncs from numpy import zeros nan = float('nan') from cobra.flux_analysis.single_deletion import single_deletion from cobra.manipulation import delete_model_genes, undelete_model_genes ##TODO: Use keywords instead if isinstance(cobra_model, dict): tmp_dict = cobra_model cobra_model = tmp_dict['cobra_model'] if 'gene_list_1' in tmp_dict: gene_list_1 = tmp_dict['gene_list_1'] if 'gene_list_2' in tmp_dict: gene_list_2 = tmp_dict['gene_list_2'] if 'method' in tmp_dict: method = tmp_dict['method'] if 'single_deletion_growth_dict' in tmp_dict: single_deletion_growth_dict = tmp_dict[ 'single_deletion_growth_dict'] if 'solver' in tmp_dict: solver = tmp_dict['solver'] if 'error_reporting' in tmp_dict: error_reporting = tmp_dict['error_reporting'] else: cobra_model = cobra_model #this is a slow way to revert models. wt_model = cobra_model #NOTE: It may no longer be necessary to use a wt_model #due to undelete_model_genes if gene_list_1 is None: gene_list_1 = cobra_model.genes elif not hasattr(gene_list_1[0], 'id'): gene_list_1 = map(cobra_model.genes.get_by_id, gene_list_1) #Get default values to use if the deletions do not alter any reactions cobra_model.optimize(solver=solver) basal_f = cobra_model.solution.f if method.lower() == 'moma': wt_model = cobra_model.copy() combined_model = None single_gene_set = set(gene_list_1) if gene_list_2 is not None: if not hasattr(gene_list_2[0], 'id'): gene_list_2 = map(cobra_model.genes.get_by_id, gene_list_2) single_gene_set.update(gene_list_2) #Run the single deletion analysis to account for double deletions that #target the same gene and lethal deletions. We assume that there #aren't synthetic rescues. single_deletion_growth_dict = single_deletion(cobra_model, list(single_gene_set), method=method, solver=solver)[0] if gene_list_2 is None or gene_list_1 == gene_list_2: number_of_genes = len(gene_list_1) gene_list_2 = gene_list_1 deletion_array = zeros([number_of_genes, number_of_genes]) ##TODO: Speed up this triangular process #For the case where the contents of the lists are the same cut the work in half. #There might be a faster way to do this by using a triangular array function #in numpy #Populate the diagonal from the single deletion lists for i, the_gene in enumerate(gene_list_1): deletion_array[i, i] = single_deletion_growth_dict[the_gene.id] for i, gene_1 in enumerate(gene_list_1[:-1]): #TODO: Since there cannot be synthetic rescues we can assume #that the whole row for a lethal deletion #will be equal to that deletion. if single_deletion_growth_dict[gene_1.id] < growth_tolerance: tmp_solution = single_deletion_growth_dict[gene_1.id] for j in range(i + 1, number_of_genes): deletion_array[j, i] = deletion_array[i, j] = tmp_solution else: for j, gene_2 in enumerate(gene_list_1[i + 1:], i + 1): if single_deletion_growth_dict[ gene_2.id] < growth_tolerance: tmp_solution = single_deletion_growth_dict[gene_2.id] else: delete_model_genes(cobra_model, [gene_1, gene_2]) if cobra_model._trimmed: if method.lower() == 'fba': #Assumes that the majority of perturbations don't change #reactions which is probably false cobra_model.optimize( solver=solver, error_reporting=error_reporting) the_status = cobra_model.solution.status tmp_solution = cobra_model.solution.f elif method.lower() == 'moma': try: moma_solution = moma( wt_model, cobra_model, combined_model=combined_model, solver=solver) tmp_solution = float( moma_solution.pop('objective_value')) the_status = moma_solution.pop('status') combined_model = moma_solution.pop( 'combined_model') del moma_solution except: tmp_solution = nan the_status = 'failed' if the_status not in ['opt', 'optimal'] and \ error_reporting: print('%s / %s: %s status: %s' % (gene_1, gene_2, solver, the_status)) #Reset the model to orginial form. undelete_model_genes(cobra_model) else: tmp_solution = basal_f deletion_array[j, i] = deletion_array[i, j] = tmp_solution else: deletion_array = zeros([len(gene_list_1), len(gene_list_2)]) #Now deal with the case where the gene lists are different for i, gene_1 in enumerate(gene_list_1): if single_deletion_growth_dict[gene_1.id] <= 0: for j in range(len(gene_list_2)): deletion_array[i, j] = 0. else: for j, gene_2 in enumerate(gene_list_2): #Assume no such thing as a synthetic rescue if single_deletion_growth_dict[ gene_2.id] <= growth_tolerance: tmp_solution = single_deletion_growth_dict[gene_2.id] else: delete_model_genes(cobra_model, [gene_1, gene_2]) if cobra_model._trimmed: if method.lower() == 'fba': cobra_model.optimize(solver=solver) tmp_solution = cobra_model.solution.f the_status = cobra_model.solution.status elif method.lower() == 'moma': try: moma_solution = moma( wt_model, cobra_model, combined_model=combined_model, solver=solver) tmp_solution = float( moma_solution.pop('objective_value')) the_status = moma_solution.pop('status') combined_model = moma_solution.pop( 'combined_model') del moma_solution except: tmp_solution = nan the_status = 'failed' if the_status not in ['opt', 'optimal'] and \ error_reporting: print('%s / %s: %s status: %s' % (repr(gene_1), repr(gene_2), solver, cobra_model.solution.status)) #Reset the model to wt form undelete_model_genes(cobra_model) else: tmp_solution = basal_f deletion_array[i, j] = tmp_solution if hasattr(gene_list_1, 'id'): gene_list_1 = [x.id for x in gene_list_1] if hasattr(gene_list_2, 'id'): gene_list_2 = [x.id for x in gene_list_2] return ({'x': gene_list_1, 'y': gene_list_2, 'data': deletion_array})
cobra_model = create_test_model(salmonella_pickle) initialize_growth_medium(cobra_model, 'LB') target_genes = ['STM4081', 'STM0247', 'STM3867', 'STM2952'] # Expected growth rates for the salmonella model after a deletions in LB medium expected_growth_rates = { "STM4081": 2.41, "STM0247": 2.43, "STM3867": 1.87, "STM2952": 1.81 } start_time = time() # start timer # Perform deletions for all genes in the list rates, statuses = single_deletion(cobra_model, target_genes) total_time = time() - start_time # stop timer # print out results passed_string = 'PASSED: %s simulation (%1.3f) ~= expectation (%1.2f)' failed_string = 'FAILED: %s simulation (%1.3f) != expectation (%1.2f)' for gene_locus, rate in rates.items(): # get gene name from gene locus (i.e. STM4081 -> tpiA) name = cobra_model.genes.get_by_id(gene_locus).name # test if the simulation failed if statuses[gene_locus] != "optimal": print("deletion failed for %s (%s)" % (name, gene_locus)) if abs(rate - expected_growth_rates[gene_locus]) > 0.01: print(failed_string % (name, rate, expected_growth_rates[gene_locus])) else: