Reading, simulating, and writing in pyCOBRA

  • Daniel Hyduke
    Daniel Hyduke

    You’ll need libsbml with python bindings and your solver installed to run this one.  The default solver is set to ‘glpk’; however, you can choose ‘cplex’ or ‘gurobi’.

    If you feel that you’ve got a useful example that will aid in the learning process please post to the pyCOBRA Developer’s forum.

    The following code is from examples/

    #This file provides a simple example of how to read a COBRA SBML model xml file,
    #perform a simple optimization, and then save the model as an SBML xml file.
    # The model is from the Salmonella enterica Typhimurium LT2 publication in
    # 2011 in BMC Sys Bio 5:8
    # The simulated growth rate should be ~0.478.
    from import create_cobra_model_from_sbml_file
    from import write_cobra_model_to_sbml_file

    solver = 'glpk' #Change to 'gurobi' or 'cplex' if you have that solver installed instead.
    test_directory = 'files/'
    sbml_file = test_directory + 'salmonella.xml'
    sbml_out_file = test_directory + 'salmonella.out.xml'
    sbml_level = 2
    sbml_version = 1 #Writing version 4 is not completely supported.
    #Read in the sbml file.
    cobra_model = create_cobra_model_from_sbml_file(sbml_file, print_time=True)
    #Run the optimization for the objective reaction and medium composition
    #set in the file.
    print '\nSimulated growth rate is %1.3f'%cobra_model.solution.f

    #Save the model to an SBML file
    print '\nConverting Model to SBML and saving as ' + sbml_out_file
    write_cobra_model_to_sbml_file(cobra_model, s