<jupyter_start><jupyter_text>Building a Constraint-Based Model This simple example demonstrates how to create a constraint-based model, its metabolites, reactions, genes, etc. into [Cell Collective](https://cellcollective.org).We’ll use the `3OAS140` reaction from the `STM_1.0 model`:```1.0 malACP[c] + 1.0 h[c] + 1.0 ddcaACP[c] → 1.0 co2[c] + 1.0 ACP[c] + 1.0 3omrsACP[c]```Begin by importing the ccapi module into your workspace.<jupyter_code>import ccapi<jupyter_output><empty_output><jupyter_text>Now, let’s try creating a client object in order to interact with services provided by [Cell Collective](https://cellcollective.org).<jupyter_code>client = ccapi.Client()<jupyter_output><empty_output><jupyter_text>Authenticate your client using a ***password flow type authentication*** scheme.**NOTE**: *Before you can authenticate using ccapi, you must first register an application of the appropriate type on [Cell Collective](https://cellcollective.org). If you do not require a user context, it is read only.*<jupyter_code>def authenticate():
    client.auth(email = "<EMAIL>", password = "<PASSWORD>")

try:
    authenticate()
except ccapi.exception.AuthenticationError:
    client.sign_up(
        email       = "<EMAIL>",
        password    = "<PASSWORD>",
        first_name  = "Test",
        last_name   = "Test",
        institution = "Test"
    )
    
    authenticate()
user = client.me()
user<jupyter_output><empty_output><jupyter_text>Creating a Base Model Create a Base Model using ccapi and instantize it with an authenticated client.<jupyter_code>model = ccapi.Model("Sample Constraint-Based Model", type_ = "metabolic", client = client)
# model.save3()
model<jupyter_output><empty_output><jupyter_text>A `ccapi.Model` consists of various `ccapi.ModelVersion` objects that help you build various versions to a model. By default, a `ccapi.Model` provides you a default model version of a boolean type. You can override the same by passing the `type_`.<jupyter_code># get the default model version
metabolic      = model.versions[0]
metabolic.name = "Version 1"
metabolic<jupyter_output><empty_output><jupyter_text>Adding Metabolites to a Constraint-Based Model First, we need to create a list of metabolite objects for this model.<jupyter_code># create compartment
compartment   = ccapi.Compartment("Cytosol")

# create metabolites

malACP_c      = ccapi.Metabolite("Malonyl-acyl-carrier-protein",
                                 formula     = "C14H22N2O10PRS",
                                 compartment = compartment)
ACP_c         = ccapi.Metabolite("acyl-carrier-protein",
                                 formula     = "C11H21N2O7PRS",
                                 compartment = compartment)
h_c           = ccapi.Metabolite("H",
                                 formula     = "H",
                                 compartment = compartment)
ddcaACP_c     = ccapi.Metabolite("Dodecanoyl-ACP-n-C120ACP",
                                 formula     = "C23H43N2O8PRS",
                                 compartment = compartment)
co2_c         = ccapi.Metabolite("CO2",
                                 formula     = "CO2",
                                 compartment = compartment)
omrsACP_c     = ccapi.Metabolite("3-Oxotetradecanoyl-acyl-carrier-protein",
                                 formula     = "C25H45N2O9PRS",
                                 compartment = compartment)<jupyter_output><empty_output><jupyter_text>Now let us add a list of components to our Constraint-Based Model.<jupyter_code>metabolic.add_metabolites(malACP_c, ACP_c, h_c, ddcaACP_c, co2_c, omrsACP_c)
metabolic
metabolic.compartments
metabolic.metabolites<jupyter_output><empty_output><jupyter_text>Saving a Model Ensure you save your model in order to commit your work.<jupyter_code># model.save3()
metabolic.metabolites<jupyter_output><empty_output><jupyter_text>Adding Reactions to a Model First, we create a `ccapi.Reaction` object.<jupyter_code>reaction = ccapi.Reaction("3 oxoacyl acyl carrier protein synthase n C140",
                         subsystem   = "Cell Envelope Biosynthesis",
                         lower_bound = 0,
                         upper_bound = 1000)<jupyter_output><empty_output><jupyter_text>Adding metabolites to a reaction requires using a dictionary of the metabolites and their stoichiometric coefficients. A group of metabolites can be added all at once, or they can be added one at a time.<jupyter_code>reaction.add_metabolites({
    malACP_c:  -1,
    h_c:       -1,
    ddcaACP_c: -1,
    co2_c:      1,
    ACP_c:      1,
    omrsACP_c:  1
})<jupyter_output><empty_output><jupyter_text>We will now add a reaction to the model<jupyter_code>metabolic.add_reaction(reaction)
# model.save3()
metabolic.reactions<jupyter_output><empty_output><jupyter_text>Adding a Gene Reaction Rule to a Reaction In order to add a Gene Reaction Rule to a Reaction, create genes as follows.<jupyter_code>STM2378 = ccapi.Gene("STM2378")
STM1197 = ccapi.Gene("STM1197")<jupyter_output><empty_output><jupyter_text>Let's consider a Gene Reaction Rule as follows```STM2378 or STM1197```You can add the same using the Regulatory Mechanism provided for Boolean Models as follows:<jupyter_code>reaction.add_regulators(
    ccapi.PositiveRegulator(STM2378),
    ccapi.PositiveRegulator(STM1197)
)
model.save3()
client.request('GET', 'api/model/8').json()<jupyter_output><empty_output><jupyter_text>Creating a Version Let's first create a copy of our Model Version object.<jupyter_code>copy = metabolic.copy()
copy<jupyter_output><empty_output><jupyter_text>Let's change any one of the metabolite names.<jupyter_code>metabolite      = copy.metabolites[0]
metabolite.name = "foobar"

copy.metabolites<jupyter_output><empty_output><jupyter_text>Add this new version to your model.<jupyter_code>model.add_version(copy)
model.save3()<jupyter_output>2020-01-29 10:53:14,337 | ERROR | Error recieved from the server: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot PUT /api/model/8</pre>
</body>
</html><jupyter_text>Dynamic Linking [Experimental] Let's assume that you'd like to reuse certain components within a model.<jupyter_code>model     = ccapi.Model("Test", type_ = "metabolic", client = client)
# model.save3()
metabolic = model.versions[0]
metabolic
compartment = ccapi.Compartment(compartment_id = 1) # Linking to an existing Compartment
metabolite  = ccapi.Metabolite("test", compartment = compartment)

metabolic.add_metabolite(metabolite)
model.save3()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09943743348031017, 'fraction_numerical': 0.0202219857077695, 'mean_word_length': 3.7052932761087267, 'pattern_counts': {'":': 0, '<': 84, '<?xml version=': 0, '>': 84, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '441044', 'license': 'MIT', 'n_tokens_mistral': 2224, 'n_tokens_neox': 2059, 'n_words': 527, 'path': 'docs/source/notebooks/examples/building-constraint-based-models.ipynb', 'repo_name': 'achillesrasquinha/fluxviz', 'size': '6597'}