Differences between revisions 16 and 17
Revision 16 as of 2014-12-18 15:02:06
Size: 4485
Editor: david
Comment:
Revision 17 as of 2016-06-20 16:53:56
Size: 4850
Editor: mark
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
In order to carry out linear programming investigations on a model, it is first neccessary to generate a specific representation of the model using {{{ScrumPy's}}} LP module: In order to carry out linear programming investigations on a model, it is first neccessary to generate a linear program associated with the model:
Line 6: Line 6:
import LP
my_lp = LP.lp(m)
lp = m.GetLP()
Line 9: Line 8:
Where {{{m}}} is an existing {{{ScrumPy}}} model and {{{my_lp}}} is an instance of the {{{lp}}} class, defined by the {{{LP}}} module. Linear programming investigations of the model are then carried out by manipulating {{{my_lp}}}. Once the lp object has been generated, no further reference to the LP module is required. Where {{{m}}} is an existing {{{ScrumPy}}} model and{{{ lp}}} is an instance an object representing a linear program set to minimise a set of fluxes subject to the steady-state and irreversibility constraints. No fluxes are are added to the objective function, and no other constraints are set at this point. Linear programming investigations of the model are then carried out by manipulating the {{{lp}}} object.
Line 20: Line 19:
my_lp = LP.lp(m) lp = m.GetLP()
Line 28: Line 27:
my_lp.SetFluxBounds(Bounds) lp.SetFluxBounds(Bounds)
Line 58: Line 57:
my_lp.SetObjDirec("Min") # minimisation
my_lp.SetObjDirec("Max") # maximisation
my_lp.SetObjDirec() # default - minimisation
lp.SetObjDirec("Min") # minimisation
lp.SetObjDirec("Max") # maximisation
lp.SetObjDirec() # default - minimisation
Line 69: Line 68:
my_lp = LP.lp(m)
Line 75: Line 73:
my_lp.SetFixedFlux(Fluxes)
my_lp.SetObjDirec() # we will be minimising
my_
lp.SetObjective(["CO2_tx"]) # minimise CO2 loss
lp.SetFixedFlux(Fluxes)
lp.SetObjective(["CO2_tx"]) # minimise CO2 loss
Line 79: Line 76:
/* More here later, weighted objectives.*/ Note that by convention {{{"_tx"}}} is used to denote a transporter defined such that negative flux indicates loss of the metabolite from the system.

/* More here later, weighted objectives.*/ */
Line 86: Line 85:
my_lp.Solve()
sol = my_lp.GetPrimSol()
lp.Solve()
sol = lp.GetPrimSol()
Line 91: Line 90:
== Filtering solutions ==
{{{#!highlight python

for r in sol:
    if r.endswith("_tx"):
        print r, sol[r]

# or using python's buitin filter and lambda functions:
for r in filter(lambda s: s.endswith("_tx"), sol):
    print r, sol[r]
}}}

Linear Programming with ScrumPy

First Steps

In order to carry out linear programming investigations on a model, it is first neccessary to generate a linear program associated with the model:

   1 lp = m.GetLP()

Where m is an existing ScrumPy model and lp is an instance an object representing a linear program set to minimise a set of fluxes subject to the steady-state and irreversibility constraints. No fluxes are are added to the objective function, and no other constraints are set at this point. Linear programming investigations of the model are then carried out by manipulating the lp object.

Setting Constraints

The initial lp object has two sets of constraints set: the steady state assumption, and irreversible reactions to carry positive or zero flux. Reversible reactions are handled internally and there is no need to explicitly split reversible reactions into their forward and reverse components.

However, no flux constraints are set, nor is an objective function, both of which must done to carry out any useful work.

The most general way in which flux bounds may be set is with the lp.setFluxBounds(BDict) method, where BDict is a python dictionary in which the keys are reaction names and the values are a tuple describing the lower and upper flux bound of that reaction, eg:

   1 lp = m.GetLP()
   2 
   3 Bounds = {
   4      "Glucose_tx" : (0, 1),
   5      "CO2_tx": (-1,0),
   6      "EtOH_tx" : (-2,0)
   7 }
   8 
   9 lp.SetFluxBounds(Bounds)

In this example we are using the convention (NOT enforced by ScrumPy) that _tx appended to a metabolite name represents a transport reaction for that metabolite and that positive flux through a transporter represents import of the metabolite, and conversly negative flux represents export.

Thus in the above example we are sepcifying that the maximum rate of glucose uptake is one flux unit, the maximum rate of CO2 production is one flux unit and the maximum rate of ethanol production is 2 flux units. Note that these limits cannot be reached simultaneously (unless there is an error in the model !).

Of course any reaction, not just transporters can have bounds set in a similar fashion. Flux bounds may be changed or removed at any time. Instead of a numerical value, a bound may specified with the python constant None, this has the effect of removing the upper or lower bound (reversibility considerations still being taken into account). Setting a negative bound on an irreversible reaction will generate and exception (and otherwise ignored).

It often desired to set one or more fluxes to a fixed value ,and although this could be achieved using SetFluxBounds() with lower bound equal to the upper bound there is also the convenience function SetFixedFlux(FDict), e.g.

   1 my_lp = LP.lp(m)
   2 Fluxes = {
   3      "Glucose_tx" : 1,
   4      "EtOH_tx" : -2
   5 }
   6 
   7 my_lp.SetFixedFlux(Fluxes)

This also allows negative flux to be set on an irreversible reaction, allthough a warning will be given. This should be used only very sparingly and with care, as it may lead to numerical instability later.

A constraint may be conveniently removed from a reaction with the UnBoundFlux(reac) method:

   1 my_lp.UnboundFlux("Glucose_tx")

Setting the Objective Function

Firstly it must be specified if the optimisation is a minimisation or maximisation:

   1 lp.SetObjDirec("Min") # minimisation
   2 lp.SetObjDirec("Max") # maximisation
   3 lp.SetObjDirec()      # default - minimisation

If any argument other than the string "Min" or "Max" is given it will be ignored and an exception generated.

Note that here minimisation and maximisation refers to the ABSOLUTE value of the flux in the case of reversible reactions.

Next one must specify which reactions are to be minimised or maximised, this achieved with the SetObjective(ReactionList) method, e.g.

   1 Fluxes = {
   2      "Glucose_tx" : 1,
   3      "EtOH_tx" : -2
   4 }
   5 
   6 lp.SetFixedFlux(Fluxes)
   7 lp.SetObjective(["CO2_tx"]) # minimise CO2 loss

Note that by convention "_tx" is used to denote a transporter defined such that negative flux indicates loss of the metabolite from the system.

Solving the LP

Once lp has been set up, this is as simple as

   1 lp.Solve()
   2 sol = lp.GetPrimSol()
   3 len(sol)
   4 print sol

Filtering solutions

   1 for r in sol:
   2     if r.endswith("_tx"):
   3         print r, sol[r]
   4 
   5 # or using python's buitin filter and lambda functions:
   6 for r in filter(lambda s: s.endswith("_tx"), sol):
   7     print r, sol[r]

None: ScrumPy/Doc/LinProg (last edited 2022-04-27 15:24:19 by mark)