def EssentialReactions(self,tol=1e-10): sdel = single_deletion(self, element_type='reaction') rv = [] for reac in sdel[1]: if sdel[1][reac] == "infeasible": rv.append(reac) return rv
def EssentialGenes(self,tol=1e-10): sdel = single_deletion(self, element_type='gene') rv = [] for gene in sdel[1]: if sdel[1][gene] == "infeasible": rv.append(gene) return rv
def pruneModel(model, rxnToRemove_dict, solver_arg): rxnToRemoveEssn_dict = {} rxnRemoved_dict = {} rxnRetained_dict = {} for rxnid in rxnToRemove_dict.keys(): #Single reaction deletion is performed only for reactions labelled as "False" if rxnToRemove_dict[rxnid] == False: growth_rate_dict, solution_status_dict, problem_dict = single_deletion( model, list([rxnid]), element_type='reaction', solver=solver_arg) #Checks optimality first. if str(solution_status_dict.values()[0]) == 'optimal': #Full list of reactions and predicted growth rates upon their deletions rxnToRemoveEssn_dict[rxnid] = float( growth_rate_dict.values()[0]) #Checks growth rate upon reaction deletion if float(growth_rate_dict.values()[0]) >= 0.01: model.remove_reactions(rxnid) #List of reactions removed from the template model rxnRemoved_dict[rxnid] = float( growth_rate_dict.values()[0]) logging.debug("Removed reaction: %s, %s, %s, %s", rxnid, growth_rate_dict.values()[0], len(model.reactions), len(model.metabolites)) else: #List of reactions retained in the template model rxnRetained_dict[rxnid] = float( growth_rate_dict.values()[0]) logging.debug("Retained reaction: %s, %s, %s, %s", rxnid, growth_rate_dict.values()[0], len(model.reactions), len(model.metabolites)) #Removing metabolites that are not used in the reduced model prune_unused_metabolites(model) modelPruned = copy.deepcopy(model) return modelPruned, rxnToRemoveEssn_dict, rxnRemoved_dict, rxnRetained_dict
from cobra.test import create_test_model, salmonella_pickle, ecoli_pickle from cobra.flux_analysis import single_deletion from cobra.flux_analysis import double_deletion cobra_model = create_test_model(salmonella_pickle) ### Single Deletions # Perform all single gene deletions on a model start = time() # start timer() growth_rates, statuses = single_deletion(cobra_model) print("All single gene deletions completed in %.2f sec" % (time() - start)) # Prints: # All single gene deletions completed in 4.01 sec # These can also be done for only a subset of genes single_deletion(cobra_model, element_list=cobra_model.genes[:100]) # Single deletions can also be run on reactions start = time() # start timer() growth_rates, statuses = single_deletion(cobra_model, element_type="reaction") print("All single reaction deletions completed in %.2f sec" % (time() - start)) # Prints:
from math import floor from cobra.manipulation import initialize_growth_medium from cobra.test import salmonella_pickle #This is the name of the test file with open(salmonella_pickle) as in_file: cobra_model = load(in_file) 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]) the_growth_rates = {tpiA.id:2.41, metN.id:2.43, atpA.id:1.87, eno.id:1.81} #expected growth rates after deletion #Perform a single gene deletion the_results = single_deletion(cobra_model, [tpiA]) gene_list = the_growth_rates.keys() #Perform deletions for all genes in the list start_time = time() rates, statuses, problems = single_deletion(cobra_model, gene_list) for the_gene, v in statuses.items(): if v != 'optimal': print '\t\tdeletion %s was not optimal'%the_gene for the_gene, v in rates.items(): v = floor(100*v)/100 if v != the_growth_rates[the_gene]: print '\t\tFAILED: %s simulation (%1.3f) != expectation (%1.2f)'%(id_to_name[the_gene],
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, problems = 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:
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, problems = 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 SingleDeletion(self, element_list=None,method='fba', element_type='gene', solver=None): return single_deletion(self, element_list=element_list, method=method, element_type=element_type, solver=solver)
# . from time import time from cobra.test import create_test_model, salmonella_pickle, ecoli_pickle from cobra.flux_analysis import single_deletion from cobra.flux_analysis import double_deletion cobra_model = create_test_model(salmonella_pickle) ### Single Deletions # Perform all single gene deletions on a model start = time() # start timer() growth_rates, statuses = single_deletion(cobra_model) print("All single gene deletions completed in %.2f sec" % (time() - start)) # Prints: # All single gene deletions completed in 4.01 sec # These can also be done for only a subset of genes single_deletion(cobra_model, element_list=cobra_model.genes[:100]) # Single deletions can also be run on reactions start = time() # start timer() growth_rates, statuses = single_deletion(cobra_model, element_type="reaction") print("All single reaction deletions completed in %.2f sec" % (time() - start)) # Prints: # All single reaction deletions completed in 7.41 sec