<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Growth media
The availability of nutrients has a major impact on metabolic fluxes and cobrapy provides some helpers to manage the exchanges between the external environment and your metabolic model. In experimental settings the "environment" is usually constituted by the growth medium, ergo the concentrations of all metabolites and co-factors available to the modeled organism. However, constraint-based metabolic models only consider fluxes. Thus, you can not simply use concentrations since fluxes have the unit mmol / [gDW h] (concentration per gram dry weight of cells and hour). 
Also, you are setting an upper bound for the particular import flux and not the flux itself. There are some crude approximations. For instance, if you supply 1 mol of glucose every 24h to 1 gram of bacteria you might set the upper exchange flux for glucose to 1 mol / [1 gDW * 24 h] since that is the nominal maximum that can be imported. There is no guarantee however that glucose will be consumed with that flux. Thus, the preferred data for exchange fluxes are direct flux measurements as the ones obtained from timecourse exa-metabolome measurements for instance. 
So how does that look in COBRApy? The current growth medium of a model is managed by the medium attribute.
Step1:  This will return a dictionary that contains the upper flux bounds for all active exchange fluxes (the ones having non-zero flux bounds). Right now we see that we have enabled aerobic growth. You can modify a growth medium of a model by assigning a dictionary to model.medium that maps exchange reactions to their respective upper import bounds. For now let us enforce anaerobic growth by shutting off the oxygen import.
Step2:  As we can see oxygen import is now removed from the list of active exchanges and we can verify that this also leads to a lower growth rate.
Step3:  There is a small trap here. model.medium can not be assigned to directly. So the following will not work
Step4:  As you can see EX_co2_e is not set to zero. This is because model.medium is just a copy of the current exchange fluxes. Assigning to it directly with model.medium[...] = ... will not change the model. You have to assign an entire dictionary with the changed import flux upper bounds
Step5:  Setting the growth medium also connects to the context manager, so you can set a specific growth medium in a reversible manner.
Step6:  So the medium change is only applied within the with block and reverted automatically.
Minimal media
In some cases you might be interested in the smallest growth medium that can maintain a specific growth rate, the so called "minimal medium". For this we provide the function minimal_medium which by default obtains the medium with the lowest total import flux. This function needs two arguments
Step7:  So we see that growth is actually limited by glucose import.
Alternatively you might be interested in a minimal medium with the smallest  number of active imports. This can be achieved by using the minimize_components argument (note that this uses a MIP formulation and will therefore be much slower).
Step8:  When minimizing the number of import fluxes there may be many alternative solutions. To obtain several of those you can also pass a positive integer to minimize_components which will give you at most that many alternative solutions. Let us try that with our model and also use the open_exchanges argument which will assign a large upper bound to all import reactions in the model. The return type will be a pandas.DataFrame.
Step9:  So there are 4 alternative solutions in total. One aerobic and three anaerobic ones using different carbon sources.
Boundary reactions
Apart from exchange reactions there are other types of boundary reactions such as demand or sink reactions. cobrapy uses various heuristics to identify those and they can be accessed by using the appropriate attribute.
For exchange reactions
Step10:  For demand reactions
Step11:  For sink reactions
Step12:  All boundary reactions (any reaction that consumes or introduces mass into the system) can be obtained with the boundary attribute
<ASSISTANT_TASK:>
Python Code:
from cobra.io import load_model
model = load_model("textbook")
model.medium
Explanation: Growth media
The availability of nutrients has a major impact on metabolic fluxes and cobrapy provides some helpers to manage the exchanges between the external environment and your metabolic model. In experimental settings the "environment" is usually constituted by the growth medium, ergo the concentrations of all metabolites and co-factors available to the modeled organism. However, constraint-based metabolic models only consider fluxes. Thus, you can not simply use concentrations since fluxes have the unit mmol / [gDW h] (concentration per gram dry weight of cells and hour). 
Also, you are setting an upper bound for the particular import flux and not the flux itself. There are some crude approximations. For instance, if you supply 1 mol of glucose every 24h to 1 gram of bacteria you might set the upper exchange flux for glucose to 1 mol / [1 gDW * 24 h] since that is the nominal maximum that can be imported. There is no guarantee however that glucose will be consumed with that flux. Thus, the preferred data for exchange fluxes are direct flux measurements as the ones obtained from timecourse exa-metabolome measurements for instance. 
So how does that look in COBRApy? The current growth medium of a model is managed by the medium attribute.
End of explanation
medium = model.medium
medium["EX_o2_e"] = 0.0
model.medium = medium
model.medium
Explanation: This will return a dictionary that contains the upper flux bounds for all active exchange fluxes (the ones having non-zero flux bounds). Right now we see that we have enabled aerobic growth. You can modify a growth medium of a model by assigning a dictionary to model.medium that maps exchange reactions to their respective upper import bounds. For now let us enforce anaerobic growth by shutting off the oxygen import.
End of explanation
model.slim_optimize()
Explanation: As we can see oxygen import is now removed from the list of active exchanges and we can verify that this also leads to a lower growth rate.
End of explanation
model.medium["EX_co2_e"] = 0.0
model.medium
Explanation: There is a small trap here. model.medium can not be assigned to directly. So the following will not work:
End of explanation
medium = model.medium
medium["EX_co2_e"] = 0.0
model.medium = medium
model.medium  # now it worked
Explanation: As you can see EX_co2_e is not set to zero. This is because model.medium is just a copy of the current exchange fluxes. Assigning to it directly with model.medium[...] = ... will not change the model. You have to assign an entire dictionary with the changed import flux upper bounds:
End of explanation
model = load_model("textbook")
with model:
    medium = model.medium
    medium["EX_o2_e"] = 0.0
    model.medium = medium
    print(model.slim_optimize())
print(model.slim_optimize())
model.medium
Explanation: Setting the growth medium also connects to the context manager, so you can set a specific growth medium in a reversible manner.
End of explanation
from cobra.medium import minimal_medium
max_growth = model.slim_optimize()
minimal_medium(model, max_growth)
Explanation: So the medium change is only applied within the with block and reverted automatically.
Minimal media
In some cases you might be interested in the smallest growth medium that can maintain a specific growth rate, the so called "minimal medium". For this we provide the function minimal_medium which by default obtains the medium with the lowest total import flux. This function needs two arguments: the model and the minimum growth rate (or other objective) the model has to achieve.
End of explanation
minimal_medium(model, 0.1, minimize_components=True)
Explanation: So we see that growth is actually limited by glucose import.
Alternatively you might be interested in a minimal medium with the smallest  number of active imports. This can be achieved by using the minimize_components argument (note that this uses a MIP formulation and will therefore be much slower).
End of explanation
minimal_medium(model, 0.8, minimize_components=8, open_exchanges=True)
Explanation: When minimizing the number of import fluxes there may be many alternative solutions. To obtain several of those you can also pass a positive integer to minimize_components which will give you at most that many alternative solutions. Let us try that with our model and also use the open_exchanges argument which will assign a large upper bound to all import reactions in the model. The return type will be a pandas.DataFrame.
End of explanation
ecoli = load_model("iJO1366")
ecoli.exchanges[0:5]
Explanation: So there are 4 alternative solutions in total. One aerobic and three anaerobic ones using different carbon sources.
Boundary reactions
Apart from exchange reactions there are other types of boundary reactions such as demand or sink reactions. cobrapy uses various heuristics to identify those and they can be accessed by using the appropriate attribute.
For exchange reactions:
End of explanation
ecoli.demands
Explanation: For demand reactions:
End of explanation
ecoli.sinks
Explanation: For sink reactions:
End of explanation
ecoli.boundary[0:10]
Explanation: All boundary reactions (any reaction that consumes or introduces mass into the system) can be obtained with the boundary attribute:
End of explanation
<END_TASK>