def calculate_phenotype_phase_plane(model, reaction1_name, reaction2_name, reaction1_range_max=20, reaction2_range_max=20, reaction1_npoints=50, reaction2_npoints=50, solver='glpk', n_processes=1, tolerance=1e-6): """calculates the growth rates while varying the uptake rates for two reactions. returns: an object containing the growth rates for the uptake rates. To plot the result, call the plot function of the returned object. Example: data = calculate_phenotype_phase_plane(my_model, "EX_foo", "EX_bar") data.plot() """ data = phenotypePhasePlaneData( str(reaction1_name), str(reaction2_name), reaction1_range_max, reaction2_range_max, reaction1_npoints, reaction2_npoints) # find the objects for the reactions and metabolites index1 = model.reactions.index(model.reactions.get_by_id(data.reaction1_name)) index2 = model.reactions.index(model.reactions.get_by_id(data.reaction2_name)) metabolite1_name = str(model.reactions.get_by_id( \ reaction1_name)._metabolites.keys()[0]) metabolite2_name = str(model.reactions.get_by_id( \ reaction2_name)._metabolites.keys()[0]) if n_processes > reaction1_npoints: # limit the number of processes n_processes = reaction1_npoints range_add = reaction1_npoints / n_processes # prepare the list of arguments for each _calculate_subset call arguments_list = [] i = arange(reaction1_npoints) j = arange(reaction2_npoints) for n in range(n_processes): start = n * range_add if n != n_processes - 1: r1_range = data.reaction1_fluxes[start:start + range_add] i_list = i[start:start + range_add] else: r1_range = data.reaction1_fluxes[start:] i_list = i[start:] arguments_list.append({"model": model.copy(), "index1": index1, "index2": index2, "metabolite1_name": metabolite1_name, "metabolite2_name": metabolite2_name, "reaction1_fluxes": r1_range, "reaction2_fluxes": data.reaction2_fluxes.copy(), "i_list": i_list, "j_list": j.copy(), "tolerance": tolerance, "solver": solver}) if n_processes > 1: results = list(ppmap(n_processes, _calculate_subset, arguments_list)) else: results = [_calculate_subset(arguments_list[0])] for result_list in results: for result in result_list: i = result[0] j = result[1] data.growth_rates[i, j] = result[2] data.shadow_prices1[i, j] = result[3] data.shadow_prices2[i, j] = result[4] return data
def __double_gene_deletion_parallel(cobra_model, number_of_processes=4, genes_of_interest=None, method = 'fba', the_problem='return', solver='glpk', error_reporting=None): """Provides a wrapper to run the double_deletion function on multicore systems. cobra_model: a Model object number_of_processes: is the number of parallel processes to start genes_of_interest: Is None, a list of genes, or a list of two lists of genes. If None then double_deletion is run on all genes in cobra_model.genes. If a list of genes then double_deletion is run for all combinations of genes in double_deletion. If a list of of two lists of genes then double_deletion is run for each member of one list vs. each member of the second list. method: 'fba' or 'moma' to run flux balance analysis or minimization of metabolic adjustments. the_problem: Is None or 'reuse' solver: 'glpk', 'gurobi', or 'cplex'. error_reporting: None or True returns a dictionary with the keys x, y, and data data: A numpy array of the simulation results for the growth_rates x: A list of the genes for the x dimension of data. y: A list of the genes for the y dimension of y. **NOTE: While the genes in x and y correspond to the content from the input gene_lists, they are not guaranteed to be in the same order as the gene_lists because the subprocesses may run at different speeds. """ if not __parallel_mode_available: print 'Parallel mode not available is Parallel Python installed' return from numpy import vstack if the_problem: the_problem='return' if not genes_of_interest: #If no genes_of_interest are specified then assume we want to #compare all genetic interactions in the network second_gene_list = all_genes = [x.id for x in cobra_model.genes] elif isinstance(genes_of_interest[0], str): #If genes_of_interest is a list then assume the list be scanned #for interactions with all genes in the network all_genes = genes_of_interest second_gene_list = all_genes elif hasattr(genes_of_interest[0], 'id'): #Make sure we're dealing with strings instead of objects because we #haven't audited this for thread safety second_gene_list = all_genes = [x.id for x in genes_of_interest] elif hasattr(genes_of_interest[0], '__iter__'): second_gene_list = all_genes = genes_of_interest[0] if len(genes_of_interest) == 2: second_gene_list = genes_of_interest[1] if hasattr(all_genes[0], 'id'): all_genes = [x.id for x in all_genes] if hasattr(second_gene_list[0], 'id'): second_gene_list = [x.id for x in second_gene_list] #Get basic numbers to guide how the problem should be divided for parallel execution. transpose_results = False if len(all_genes) < len(second_gene_list): all_genes, second_gene_list = second_gene_list, all_genes transpose_results = True total_gene_count = len(all_genes) if total_gene_count < number_of_processes: number_of_processes = total_gene_count division_count = total_gene_count / number_of_processes the_rows = [] for i in range(number_of_processes-1): the_rows.append({'cobra_model': cobra_model.copy(), 'method': method, 'gene_list_1': deepcopy(all_genes[i*division_count:division_count*(i+1)]), 'gene_list_2': deepcopy(second_gene_list), 'the_problem': the_problem, 'solver': solver, 'error_reporting': error_reporting}) the_rows.append({'cobra_model': cobra_model.copy(), 'method': method, 'gene_list_1': deepcopy(all_genes[(number_of_processes-1)*division_count:]), 'gene_list_2': deepcopy(second_gene_list), 'the_problem': the_problem, 'solver': solver, 'error_reporting': error_reporting}) tmp_pp = list(ppmap(number_of_processes, double_gene_deletion, the_rows)) gene_list_x = tmp_pp[0]['x'] gene_list_y = tmp_pp[0]['y'] double_deletion_data = tmp_pp[0]['data'] if transpose_results: gene_list_x, gene_list_y = gene_list_y, gene_list_x double_deletion_data = double_deletion_data.transpose() for the_result in tmp_pp[1:]: gene_list_x += the_result['x'] double_deletion_data = vstack((double_deletion_data, the_result['data'])) #cobra_model.double_deletion_growth_rate = double_deletion_data #cobra_model.double_deletion_genes_x = gene_list_x #cobra_model.double_deletion_genes_y = gene_list_y return({'x': gene_list_x, 'y': gene_list_y, 'data': double_deletion_data})
def double_gene_deletion_parallel( cobra_model, n_processes=4, genes_of_interest=None, method="fba", the_medium=None, the_problem="return", solver="glpk", error_reporting=None, ): """Provides a wrapper to run the double_deletion function on multicore systems. cobra_model: a Model object n_processes: is the number of parallel processes to start genes_of_interest: Is None, a list of genes, or a list of two lists of genes. If None then double_deletion is run on all genes in cobra_model.genes. If a list of genes then double_deletion is run for all combinations of genes in double_deletion. If a list of of two lists of genes then double_deletion is run for each member of one list vs. each member of the second list. method: 'fba' or 'moma' to run flux balance analysis or minimization of metabolic adjustments. 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. the_problem: Is None or 'reuse' solver: 'glpk', 'gurobi', or 'cplex'. error_reporting: None or True Adds the following attributes to the cobra_model: double_deletion_growth_rate: A numpy array of the simulation results double_deletion_genes_x: A list of the genes for the x dimension of double_deletion_growth_rate. double_deletion_genes_y: A list of the genes for the y dimension of double_deletion_growth_rate. """ if not __parallel_mode_available: print "Parallel mode not available is Parallel Python installed" return if the_problem: the_problem = "return" if the_medium: initialize_growth_medium(cobra_model, the_medium) if not genes_of_interest: # If no genes_of_interest are specified then assume we want to # compare all genetic interactions in the network all_genes = [x.id for x in cobra_model.genes] second_gene_list = all_genes elif isinstance(genes_of_interest[0], str): # If genes_of_interest is a list then assume the list be scanned # for interactions with all genes in the network all_genes = genes_of_interest second_gene_list = all_genes elif hasattr(genes_of_interest[0], "id"): # Make sure we're dealing with strings instead of objects because we # haven't audited this for thread safety second_gene_list = all_genes = [x.id for x in genes_of_interest] elif hasattr(genes_of_interest[0], "__iter__"): all_genes = genes_of_interest[0] if len(genes_of_interest) == 2: second_gene_list = genes_of_interest[1] else: second_gene_list = all_genes # Get basic numbers to guide how the problem should be divided for parallel execution. total_gene_count = len(all_genes) division_count = total_gene_count / n_processes the_rows = [] for i in range(n_processes - 1): the_rows.append( { "cobra_model": cobra_model.copy(), "method": method, "gene_list_1": deepcopy(all_genes[i * division_count : division_count * (i + 1)]), "gene_list_2": deepcopy(second_gene_list), "the_problem": the_problem, "solver": solver, "error_reporting": error_reporting, } ) the_rows.append( { "cobra_model": cobra_model.copy(), "method": method, "gene_list_1": deepcopy(all_genes[(n_processes - 1) * division_count :]), "gene_list_2": deepcopy(second_gene_list), "the_problem": the_problem, "solver": solver, "error_reporting": error_reporting, } ) tmp_pp = list(ppmap(n_processes, double_gene_deletion, the_rows)) gene_list_x = tmp_pp[0]["x"] gene_list_y = tmp_pp[0]["y"] double_deletion_data = tmp_pp[0]["data"] for the_result in tmp_pp[1:]: gene_list_x += the_result["x"] double_deletion_data = vstack((double_deletion_data, the_result["data"])) cobra_model.double_deletion_growth_rate = double_deletion_data cobra_model.double_deletion_genes_x = gene_list_x cobra_model.double_deletion_genes_y = gene_list_y return cobra_model
def __double_gene_deletion_parallel(cobra_model, number_of_processes=4, genes_of_interest=None, method='fba', the_problem='return', solver='glpk', error_reporting=None): """Provides a wrapper to run the double_deletion function on multicore systems. cobra_model: a Model object number_of_processes: is the number of parallel processes to start genes_of_interest: Is None, a list of genes, or a list of two lists of genes. If None then double_deletion is run on all genes in cobra_model.genes. If a list of genes then double_deletion is run for all combinations of genes in double_deletion. If a list of of two lists of genes then double_deletion is run for each member of one list vs. each member of the second list. method: 'fba' or 'moma' to run flux balance analysis or minimization of metabolic adjustments. the_problem: Is None or 'reuse' solver: 'glpk', 'gurobi', or 'cplex'. error_reporting: None or True returns a dictionary with the keys x, y, and data data: A numpy array of the simulation results for the growth_rates x: A list of the genes for the x dimension of data. y: A list of the genes for the y dimension of y. **NOTE: While the genes in x and y correspond to the content from the input gene_lists, they are not guaranteed to be in the same order as the gene_lists because the subprocesses may run at different speeds. """ if not __parallel_mode_available: print 'Parallel mode not available is Parallel Python installed' return from numpy import vstack if the_problem: the_problem = 'return' if not genes_of_interest: #If no genes_of_interest are specified then assume we want to #compare all genetic interactions in the network second_gene_list = all_genes = [x.id for x in cobra_model.genes] elif isinstance(genes_of_interest[0], str): #If genes_of_interest is a list then assume the list be scanned #for interactions with all genes in the network all_genes = genes_of_interest second_gene_list = all_genes elif hasattr(genes_of_interest[0], 'id'): #Make sure we're dealing with strings instead of objects because we #haven't audited this for thread safety second_gene_list = all_genes = [x.id for x in genes_of_interest] elif hasattr(genes_of_interest[0], '__iter__'): second_gene_list = all_genes = genes_of_interest[0] if len(genes_of_interest) == 2: second_gene_list = genes_of_interest[1] if hasattr(all_genes[0], 'id'): all_genes = [x.id for x in all_genes] if hasattr(second_gene_list[0], 'id'): second_gene_list = [x.id for x in second_gene_list] #Get basic numbers to guide how the problem should be divided for parallel execution. transpose_results = False if len(all_genes) < len(second_gene_list): all_genes, second_gene_list = second_gene_list, all_genes transpose_results = True total_gene_count = len(all_genes) if total_gene_count < number_of_processes: number_of_processes = total_gene_count division_count = total_gene_count / number_of_processes the_rows = [] for i in range(number_of_processes - 1): the_rows.append({ 'cobra_model': cobra_model.copy(), 'method': method, 'gene_list_1': deepcopy(all_genes[i * division_count:division_count * (i + 1)]), 'gene_list_2': deepcopy(second_gene_list), 'the_problem': the_problem, 'solver': solver, 'error_reporting': error_reporting }) the_rows.append({ 'cobra_model': cobra_model.copy(), 'method': method, 'gene_list_1': deepcopy(all_genes[(number_of_processes - 1) * division_count:]), 'gene_list_2': deepcopy(second_gene_list), 'the_problem': the_problem, 'solver': solver, 'error_reporting': error_reporting }) tmp_pp = list(ppmap(number_of_processes, double_gene_deletion, the_rows)) gene_list_x = tmp_pp[0]['x'] gene_list_y = tmp_pp[0]['y'] double_deletion_data = tmp_pp[0]['data'] if transpose_results: gene_list_x, gene_list_y = gene_list_y, gene_list_x double_deletion_data = double_deletion_data.transpose() for the_result in tmp_pp[1:]: gene_list_x += the_result['x'] double_deletion_data = vstack( (double_deletion_data, the_result['data'])) #cobra_model.double_deletion_growth_rate = double_deletion_data #cobra_model.double_deletion_genes_x = gene_list_x #cobra_model.double_deletion_genes_y = gene_list_y return ({'x': gene_list_x, 'y': gene_list_y, 'data': double_deletion_data})