SBML.org — the global portal for all things SBML

copySBMLparameters.py

copySBMLparameters.py

Language: Python
Summary Simple program that copies parameter values from one SBML file to another.
Author(s) Axel Kowald (Axel.Kowald AT rub.de)
Version 4 Dez. 2008
Needs libSBML version 3
Source This page

Description

This script copies reaction parameter values from one SBML file to another. We use libSBML to go through all reactions and all parameters of the source file. If the same reaction/parameter combination exists in the destination file the numerical values is copied. What is it good for? If you have created a model in CellDesigner and then made a parameter fit with Copasi, you can use this to get the parameter values back into CellDesigner without loosing layout information. Export the copasi model as SBML and copy the parameter values into the CellDesigner SBML file using this script.

Code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#--------------------------------------------------------------------------
#                              copySBMLparameters.py
#--------------------------------------------------------------------------
'''
This script copies reaction parameter values from one SBML file to another.
We use libSBML to go through all reactions and all parameters of the source file.
If the same reaction/parameter combination exists in the destination file the
numerical values is copied.
What is it good for?
If you have created a model in CellDesigner and then made a parameter fit with
Copasi, you can use this to get the parameter values back into CellDesigner
without loosing layout information. Export the copasi model as SBML and copy
the parameter values into the CellDesigner SBML file using this script.

Usage: copySBMLparameters.py SBMLsource SBMLdestination        

Axel Kowald (Axel.Kowald@rub.de)
4.12.2008
'''

from   __future__ import division   # proper division 5/2 = 2.5
import sys        # System specific functions like argv[]
import os         # z.B. file & dir manipulation
import string     # string funktionen
import datetime
import libsbml    # SBML handling 

#--------------------------------------------------------------------------
#                             PROGRAM START
#--------------------------------------------------------------------------

def main(srcFile,dstFile):
    print '********* copySBMLparameters.py by Axel Kowald  %s ********************'% datetime.date.today()
    
    # read in source SBML file
    # See doc at: http://sbml.org/Software/libSBML/docs/python-api/
    reader = libsbml.SBMLReader()
    sbmlSrcDoc = reader.readSBML(srcFile)
    sbmlDstDoc = reader.readSBML(dstFile)
    
    # Replace global parameters
    nReplaced = 0
    paramL = sbmlSrcDoc.getModel().getListOfParameters()
    print 'Found %d global parameters.'%len(paramL)
    dstModel = sbmlDstDoc.getModel()
    for param in paramL:
        oldValue = 'NonExistent'
        if dstModel.getParameter(param.getId())!=None:
            paramDst = dstModel.getParameter(param.getId())
            oldValue = paramDst.getValue()
            paramDst.setValue(param.getValue())
        # if rDst                
        if str(oldValue)=='NonExistent':
            print '%s(%s) = %f'%(param.getId(),param.getName(),param.getValue())
        else:
            print '%s(%s) = %f.  Replaced old value = %f'%(param.getId(),param.getName(),param.getValue(),oldValue)
            nReplaced += 1
    # for param        
        
    
    # Go through list of local parameters in list of src reactions
    reactionsSrcL = sbmlSrcDoc.getModel().getListOfReactions()
    print 'Looking for local parameters...'
    print 'Found %d reactions in source file'%len(reactionsSrcL)
    for reaction in reactionsSrcL:
        print reaction.getId()
        paramL = reaction.getKineticLaw().getListOfParameters()
        for param in paramL:
            oldValue = replaceParam(sbmlDstDoc,reaction.getId(),param)
            if str(oldValue)=='NonExistent':
                print '\t%s = %f'%(param.getId(),param.getValue())
            else:
                print '\t%s = %f.  Replaced old value = %f'%(param.getId(),param.getValue(),oldValue)
                nReplaced += 1
        # for param        
    # for reaction
    print 'Replaced %d parameter values in %s'%(nReplaced,dstFile)
    
    writer = libsbml.SBMLWriter()
    writer.writeSBML(sbmlDstDoc,dstFile)
# end def main()


#--------------------------------------------------------------------------
#                             FUNKTIONEN
#--------------------------------------------------------------------------

# --------------------- replaceParam --------------------------------
def replaceParam(sbmlDstDoc,rId,param):
    '''If there is a reaction with id rId in sbmlDstDoc and it has a parameter
       with the same id as param, then overwrite its value with the value of
       param.
       Return old param value or "NonExistent"
    '''
    
    oldValue = 'NonExistent'
    # Is the required reaction in reactionsL ?
    model = sbmlDstDoc.getModel()
    rDst  = model.getReaction(rId)
    if rDst!=None and rDst.getKineticLaw().getParameter(param.getId())!=None:
        paramDst = rDst.getKineticLaw().getParameter(param.getId())
        oldValue = paramDst.getValue()
        paramDst.setValue(param.getValue())
    # if rDst        
        
    return oldValue
# end def replaceParam


# --------------------------------------------------------------------
#                      main start
# --------------------------------------------------------------------
if __name__ == "__main__":
   if len(sys.argv) != 3:
      print "Usage: SBMLsource SBMLdestination"
      sys.exit()
   # end if
   
   main(sys.argv[1], sys.argv[2])
# if __name__


Installation instructions

Install libSBML if you have not already done so. Save the script to a file named "copySBMLparameters.py" on your computer, make the script executable, and then invoke it on the command line:

Usage

copySBMLparameters.py  /path/to/source/SBMLfile.xml  /path/to/destination/SBMLfile.xml

Known problems/issues

None.

Licensing terms and distribution

This software is released under the terms of the LGPL.

Retrieved from "http://sbml.org/Community/Programs/copySBMLparameters.py"

This page was last modified 08:24, 11 May 2009.



Please use our issue tracking system for any questions or suggestions about this website. This page was last modified 08:24, 11 May 2009.