<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Gap-fill a model in PyFBA
by Daniel Cuevas
Introduction
In this notebook, we will present the steps to generate a genome-scale metabolic model from RAST annotations, gap-fill the model on rich LB type media, and save the model to hard disk.
The required files and information for this notebook
Step1:  Generate model
The first step shows how to build the model from RAST functional roles.
Step2:  The model has been generated and is now ready to use for flux-balance analysis simulations. Running flux-balance analysis will show the model does not contain all required metabolism to grow in the LB media.
Here are the LB media contents. For PyFBA media files are stored in directory indicated by environmental variable 'PYFBA_MEDIA_DIR'. This step is only to show file contents but is not required for gap-filling.
Step3:  Gap-fill model on LB media
Each model object in PyFBA contains a gapfill() function that requires two arguments
Step4:  We can view the reactions that were gap-filled into the model.
Step5:  Save model
The second step shows how to save the model to hard disk.
Step6:  Model has been stored. Here is a directory listing of the files that were created.
<ASSISTANT_TASK:>
Python Code:
import sys
import os
import PyFBA
Explanation: Gap-fill a model in PyFBA
by Daniel Cuevas
Introduction
In this notebook, we will present the steps to generate a genome-scale metabolic model from RAST annotations, gap-fill the model on rich LB type media, and save the model to hard disk.
The required files and information for this notebook:
* List of functional roles from RAST (normally labeled 'assigned_functions' from the Genome Directory download).
* Organism name
* Organism ID
* Media file
* Close genomes functional roles file
* Directory on hard disk to store model
End of explanation
model_functions_file = "data/citrobacter.assigned_functions"
close_genomes_functions_file = "data/close_genomes_functions"
org_name = "Citrobacter sedlakii"
org_id = "Citrobacter sedlakii"
model = PyFBA.model.roles_to_model(model_functions_file, org_id, org_name)
Explanation: Generate model
The first step shows how to build the model from RAST functional roles.
End of explanation
lb_media_file = os.path.join(os.environ["PYFBA_MEDIA_DIR"], "ArgonneLB.txt")
with open(lb_media_file) as f:
    for l in f:
        print(l, end="")
# status := optimization status of FBA simplex solver
# flux_value := biomass flux value (objective function)
# growth := boolean whether the model was able to grow or not
status, flux_value, growth = model.run_fba("ArgonneLB.txt")
print("Growth:", growth)
Explanation: The model has been generated and is now ready to use for flux-balance analysis simulations. Running flux-balance analysis will show the model does not contain all required metabolism to grow in the LB media.
Here are the LB media contents. For PyFBA media files are stored in directory indicated by environmental variable 'PYFBA_MEDIA_DIR'. This step is only to show file contents but is not required for gap-filling.
End of explanation
success = model.gapfill("ArgonneLB.txt", close_genomes_functions_file, use_flux=True, verbose=1)
if not success:
    print("Model was unable to gap-fill!")
Explanation: Gap-fill model on LB media
Each model object in PyFBA contains a gapfill() function that requires two arguments:
1. Media file
2. Close genomes functional roles file
The other two arguments here, use_flux and verbose, are optional.
* use_flux is a boolean flag that will identify which reactions that were added during the first phase of gap-filling have a non-active or zero flux. These reactions are then removed before the second phase of gap-filling occurs. This lowers the number of reactions that must be tested during second phase, thus speeding up the gap-filling process.
* verbose is an integer flag that will output status update to stderr.
End of explanation
for n, rid in enumerate(model.gf_reactions, start=1):
    print("({}) {}: {}".format(n, rid, model.reactions[rid].equation))
Explanation: We can view the reactions that were gap-filled into the model.
End of explanation
model_directory = "save_citrobacter_sedlakii"
PyFBA.model.save_model(model, model_directory)
Explanation: Save model
The second step shows how to save the model to hard disk.
End of explanation
for f in os.listdir(model_directory):
    fp = os.path.join(model_directory, f)
    print(f, ": ", os.path.getsize(fp), "B", sep="")
Explanation: Model has been stored. Here is a directory listing of the files that were created.
End of explanation
<END_TASK>