{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Global Configuration" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With cobra > 0.13.4, we introduce a global configuration object. For now, you can configure default reaction bounds and optimization solver which will be respected by newly created reactions and models." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The configuration object" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can get a configuration object[1](#f1) in the following way:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import cobra" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "cobra_config = cobra.Configuration()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1The configuration object is a [singleton](https://en.wikipedia.org/wiki/Singleton_pattern). That means only one instance can exist and it is respected everywhere in COBRApy." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reaction bounds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The object has the following attributes which you can inspect but also change as desired." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1000.0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cobra_config.lower_bound" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1000.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cobra_config.upper_bound" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-1000.0, 1000.0)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cobra_config.bounds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Changing bounds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you modify the above values before creating a reaction they will be used." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "cobra_config.bounds = -10, 20" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Reaction identifierR1
Name
Memory address0x07f0426135fd0
Stoichiometry\n", "

-->

\n", "

-->

\n", "
GPR
Lower bound0.0
Upper bound20
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cobra.Reaction(\"R1\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please note that by default reactions are irreversible. You can change this behavior by unsetting the lower bound argument." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Reaction identifierR2
Name
Memory address0x07f04260d4438
Stoichiometry\n", "

<=>

\n", "

<=>

\n", "
GPR
Lower bound-10
Upper bound20
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cobra.Reaction(\"R2\", lower_bound=None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**N.B.: Most models define reaction bounds explicitly which takes precedence over the configured values.**" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from cobra.test import create_test_model" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "model = create_test_model(\"textbook\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Reaction identifierACt2r
NameR acetate reversible transport via proton - symport
Memory address0x07f042607c780
Stoichiometry\n", "

ac_e + h_e <=> ac_c + h_c

\n", "

Acetate + H+ <=> Acetate + H+

\n", "
GPR
Lower bound-1000.0
Upper bound1000.0
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.reactions.ACt2r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solver" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can define the default solver used by newly instantiated models. The default solver depends on your environment. In order we test for the availability of Gurobi, CPLEX, and GLPK. GLPK is assumed to always be present in the environment." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.solver" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Changing solver" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "cobra_config.solver = \"glpk_exact\"" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "new_model = create_test_model(\"textbook\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_model.solver" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Changing global configuration values is mostly useful at the beginning of a work session." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Table of Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 1 }