In [1]:
import pandas
from time import time
import cobra.test
from cobra.flux_analysis import (
single_gene_deletion, single_reaction_deletion, double_gene_deletion,
double_reaction_deletion)
cobra_model = cobra.test.create_test_model("textbook")
ecoli_model = cobra.test.create_test_model("ecoli")
A commonly asked question when analyzing metabolic models is what will happen if a certain reaction was not allowed to have any flux at all. This can tested using cobrapy by
In [2]:
print('complete model: ', cobra_model.optimize())
with cobra_model:
cobra_model.reactions.PFK.knock_out()
print('pfk knocked out: ', cobra_model.optimize())
For evaluating genetic manipulation strategies, it is more interesting to examine what happens if given genes are knocked out as doing so can affect no reactions in case of redundancy, or more reactions if gene when is participating in more than one reaction.
In [3]:
print('complete model: ', cobra_model.optimize())
with cobra_model:
cobra_model.genes.b1723.knock_out()
print('pfkA knocked out: ', cobra_model.optimize())
cobra_model.genes.b3916.knock_out()
print('pfkB knocked out: ', cobra_model.optimize())
Perform all single gene deletions on a model
In [4]:
deletion_results = single_gene_deletion(cobra_model)
These can also be done for only a subset of genes
In [5]:
single_gene_deletion(cobra_model, cobra_model.genes[:20])
Out[5]:
This can also be done for reactions
In [6]:
single_reaction_deletion(cobra_model, cobra_model.reactions[:20])
Out[6]:
Double deletions run in a similar way. Passing in return_frame=True
will cause them to format the results as a pandas.DataFrame
.
In [7]:
double_gene_deletion(
cobra_model, cobra_model.genes[-5:], return_frame=True).round(4)
Out[7]:
By default, the double deletion function will automatically use multiprocessing, splitting the task over up to 4 cores if they are available. The number of cores can be manually specified as well. Setting use of a single core will disable use of the multiprocessing library, which often aids debugging.
In [8]:
start = time() # start timer()
double_gene_deletion(
ecoli_model, ecoli_model.genes[:300], number_of_processes=2)
t1 = time() - start
print("Double gene deletions for 200 genes completed in "
"%.2f sec with 2 cores" % t1)
start = time() # start timer()
double_gene_deletion(
ecoli_model, ecoli_model.genes[:300], number_of_processes=1)
t2 = time() - start
print("Double gene deletions for 200 genes completed in "
"%.2f sec with 1 core" % t2)
print("Speedup of %.2fx" % (t2 / t1))
Double deletions can also be run for reactions.
In [9]:
double_reaction_deletion(
cobra_model, cobra_model.reactions[2:7], return_frame=True).round(4)
Out[9]: