Wednesday, July 13, 2016

PythonCyc: Using the Pathway Tools Python API

Pathway Tools is implemented using the Common Lisp (CL) programming language, but the PythonCyc package creates a bridge between Python and CL. That is, the PythonCyc package allows you to interact with Pathway Tools using the Python language. With PythonCyc you can write Python programs to execute Pathway Tools metabolic models, as well asto extract and modify data stored in Pathway/Genome Databases (PGDBs). It is also possible to call from Python many functions defined in Pathway Tools that manipulate genes, pathways, reactions, proteins, and more.


Getting Started


The PythonCyc package was been developed at SRI and first released with Pathway Tools version 19.5. PythonCyc needs to be installed separately from Pathway Tools. The instructions to install PythonCyc are part of the PythonCyc tutorial at PythonCyc at GitHub. Once installed, you will need to start Pathway Tools with the python server enabled, which enables PythonCyc to send and receive data from Pathway Tools. The running Pathway Tools can be on a dedicated server from which multiple users of Python can access remotely, although a dedicated server is not required. The details of how to start the Python server are given in the PythonCyc tutorial. After starting a Python interpreter, you will see a prompt, which is typically >>>. You then must import the pythoncyc package as in
>>> import pythoncyc

Extracting Data From a PGDB


The PythonCyc package extracts data from a PGDB and transforms the data into Python objects or values. You can then process the objects and values using Python and modify the data stored in the PGDBs. Multiple PGDBs can be accessed and modified at the same time with PythonCyc. For example, assuming that your running Pathway Tools has the PGDBs MetaCyc and EcoCyc, you can gain access to both PGDBs by evaluating
>>> meta = pythoncyc.select_organism('meta')
>>> ecoli = pythoncyc.select_organism('ecoli')

The Python variables meta and ecoli are bound to Python objects that enable communication with Pathway Tools and access the PGDBs MetaCyc and EcoCyc, respectively.

You can then extract data from the PGDBs such as reactions, proteins, compounds, pathways, genes, and more, into the Python world. For example, this Python line will extract all reactions of EcoCyc:
>>> ecoli_reactions = ecoli.reactions

The variable ecoli is used to access the PGDB with orgid ecoli, which we call EcoCyc. The field reactions corresponds to the class of reactions. The variable ecoli_reactionsis bound to a Python object representing the class of all reactions of the EcoCyc PGDB. Many other classes exist, such as genes, pathways, proteins, compounds, and more. This particular way of extracting data is efficient, because all the reaction frame ids are sent from Pathway Tools to the Python interpreter in one communication. All instances, that is, all reactions, can be accessed via the instances field, which is a list of Python objects, each object representing one reaction. For example, the following Python code prints all reactions as Python objects:
>>> for x in ecoli_reactions.instances:
>>>   print x

Another way to access all the objects in a PGDB, which is familiar to Lisp programmers using Pathway Tools, is to use the frame ids. These ids are unique for each object in a PGDB. For example, the glycolysis I pathway of MetaCyc has the frame id GLYCOLYSIS. That pathway can be directly retrieved by using that frame id:
>>> glycolysisI = meta['GLYCOLYSIS']

This last Python command accesses Pathway Tools, transfers the data representing the pathway with frame id GLYCOLYSIS and stores that data locally as a Python object, assigning that object to variable glycolysisI. The entire object content can be displayed by typing the name of the variable at the prompt. More examples of how to retrieve objects and how to process them using Python can be found in the PythonCyc tutorial (see link above).

A more basic mechanism to access the data from a PGDB is also available where no Python objects are created. This mechanism simply transfers the data needed and is based on methods get_slot_value and get_slot_values. For example, the following expression will retrieve a single value from slot GIBBS-0 of reaction RXN-9000 of MetaCyc:
>>> meta.get_slot_value('RXN-9000', 'GIBBS-0')

The get_slot_values method retrieves a list of values as in
>>> meta.get_slot_values('TRP', 'CHEMICAL-FORMULA')

For this last command, the chemical formula is represented as a list of atom names and integer coefficients.

Predefined Functions


PythonCyc has more than 150 methods to access functions defined in Pathway Tools. For example, the function reactions_of_compound returns the reactions involving a specific compound. It can be called in the following way:
>>> meta.reactions_of_compound(meta.trp)

This last command returns a list of reactions frame ids, from MetaCyc, where TRP is a reactant or product. The complete list of methods available is given at PythonCyc API.

Modifying the data contained in PGDBs is also possible by using the basic Python methods put_slot_value and put_slot_values. Modifications to a PGDB make sense only for PGDBs that are enabled for updates, and in general MetaCyc and EcoCyc are resident with the memory of a Pathway Tools executable and cannot be modified. Assuming that you have created a PGDB with orgid mypgdb, the following modifies the slot GIBBS-0 for reaction RXN-9000 in that PGDB:
>>> mypgdb = pythoncyc.select_organism('mypgdb')
>>> mypgdb.put_slot_value('RXN-9000','GIBBS-0',2.7)

The data is modified directly in the PGDB. The method save_pgdb saves a PGDB after all desired modifications have been done, as in
>>> mypgdb.save_pgdb()

Running MetaFlux from Python


Finally, the MetaFlux tool, which can solve flux balance models, can be accessed using PythonCyc by calling the method run_fba and providing a file name of a file that contains a description of the flux balance model. Another Pathway Tools blog post will describe in more detail how MetaFlux can be used with PythonCyc.

No comments:

Post a Comment