COBRA on Matlab 64
From Symbiopedia
Last Edited: --Ben 08:46, 20 February 2009 (PST)
Also see Installing COBRA toolbox for MATLAB for how we get it working on the 32 bit machines, or Bens MATLAB x64 libsbml sbml toolbox notes
The Constraint-based Reconstruction and Analysis (COBRA) Toolbox was developed by Palsson's group at UCSD. The COBRA toolbox is described in the Nature Protocols article:
Quantitative prediction of cellular metabolism with constraint-based models: the COBRA Toolbox
Scott A Becker, Adam M Feist, Monica L Mo, Gregory Hannum, Bernhard Ø Palsson & Markus J Herrgard
Nature Protocols 2, 727 - 738 (2007) Published online: 29 March 2007
doi:10.1038/nprot.2007.99
Downloads and documentation are available at the Palsson group website's COBRA toolbox page.
This page documents the download and installation of 3 sets of files: a linear programming solver, the SBML toolbox, and the COBRA toolbox. It discusses my attempts to get them to work on Windows XP 64, running Matlab release 2008b.
Contents |
Linear programming solver (GLPK) download and installation
Several lp solvers can be used with the COBRA toolbox. Two freeware options are lp_solver and glpk. Following the UCSD group's sugguestion, we will be using glpk. Since glpkmex is not available as a pre-compiled versions for Windows XPx64, we'll have to compile it. To do that, we need to get GLPK, compile it, and then use a script that comes with glpkmex to compile glpkmex within matlab.
First we need to get a compiler and set it up to work with Matlab.
We will compile glpkmex from within Matlab, but for that, we need a compiler, since the 64-bit release of MATLAB 2008b doesn't come with a compiler (like the 32-bit version, which has LCC).
From the matlab knowledge base we learn that Matlab works with the Windows SDK (particularly, see this response, so we'll go with that.
- Download and install Microsoft's Visual C++, which is also available from Microsoft as a free download. You have to register it to use it for more than 30 days, but the trial basis is fine for our purposes.
- Next, download the Windows SDK from Microsoft. It's okay to do the online install, and accept all the default options given.
- Get the Windows SDK and Visual C++ to talk to each other: Go to the Add or Remove Programs control panel (Start Menu → Settings → Control Panel → Add or Remove Programs), and select "Microsoft Windows SDK for Windows Server 2008", and press the "Change" button. Click on "Change", and then check the box to add the Visual C++ compilers.
Note:The next three steps are optional for the GLPK install, but (are? may be?) useful for getting libsbml to work, which is the next section. - Launch the Windows SDK Command Window (Start, All Programs, Microsoft Windows SDK v6.1, Command Window)
- CD to \Program Files\Microsoft\Windows\v6.1\Setup>
- Type: WindowsSdkVer.exe -version:v6.1
- Now we need add a Windows system variable to tell Matlab where compiler is (there are other ways of doing this, but this is permanent, and not too hard). Edit the Windows system variable by going to Start → Control Panel → System → Advanced (tab) → Environment (button). Create a New System Variable, with name MSSDK, and value C:\Program Files\Microsoft SDKs\Windows\v6.1
- Finally, configure the Matlab compiler mex to use the SDK you've installed. At the Matlab command prompt, type "mex -setup". Matlab should be able to locate the installed compiler, which it identifies as "[1] Microsoft Visual C++ 2008 Express in C:\Program Files (x86)\Microsoft Visual Studio 9.0 "
Next we need to get and compile GLPK.
- Download the GLPK source file from GNU. Choose the .tar.gz file with the most recent date (it was version 4.36 at the time I wrote this).
- To uncompress the .tar.gz file, you'll need 7-zip from 7-zip.org. Download and install the appropriate executable file.
- Use 7-zip to uncompress and un-tar the archive to a folder on your desktop. #Navigate to the extracted folder (in my case, ...\Desktop\glpk\glpk-4.36), and open the w64 directory. Double click the "Build_GLPK-with_VC9" batch file. A command window should pop up, and a bunch of lines should flash by. If all is well, the final lines should be "OPTIMAL SOLUTION FOUND", and the time and memory used to do so. If you see this, you have succeeded at building glpk.lib and glpsol.exe - congratulations! These two files should now be in the ...\Desktop\glpk\glpk-4.36\w64\ directory now.
- To make the next steps easier, move the glpk-4.36 folder from the glpk folder on your desktop to the root directory of your hard drive (ie, C:\). Rename the glpk-4.36 folder you moved to glpk. (In other words, you should now have a folder at C:\glpk\doc\).
Next we need to get and edit the source code for glpkmex.
- Download the glpkmex source files, which are available from sourceforge (the file you want is called glpkmex-2.4-src.zip).
- Unzip the archive to your desktop. You'll get a folder called glpkmex-2.4-src. Rename this folder "glpkmex-2.4" and move it to your Matlab toolbox folder (ie, C:\Program Files\MATLAB\R2008b\toolbox)
- In this glpkmex-2.4 folder, Double click the file glpkcc.cpp to open it in Visual C++. Edit as follows (this is following the advice from the Help-glpk mailing list thread that starts here):
- on line 374, replace glp_ulong by glp_long
- on line 375, change 'lib_mem_usage' to 'glp_mem_usage'
Now we're ready to compile and use glpkmex.
The author of GLPKMex, Nicolo Giorgetti, has made a really useful script installer for GLPKMex. We'll use this from within Matlab, but it takes a little tweaking to work right.
- First, add the glpkmex folder to your Matlab path:
- In Matlab, go to "Set Path" from the "File" drop menu.
- In the "Set Path" dialog box that pops up, choose "Add with Subfolders"
- Navigate to the glpkmex folder you added to your toolbox folder and click "ok"
- Click "Save" in the "Set Path" diaglog box, then "Close"
- At the Matlab command line, type "makeglpkmex". You will be asked a series of questions. Answer as follows:
- Do you want to use graphic installer? Y/N [Y]: n
- GLPK path: C:\glpk\w64
- GLPK include directory: C:\glpk\include\
- Specify full GLPK libglpk.a path: C:\glpk\w64\glpk.lib
- Do you compile GLPKMEX with CYGWIN? Y/N [Y]: n
- Test your glpk installation by typing "glpktest1" and then "glpktest2" at the Matlab command prompt. You shouldn't get any errors.
Congratulations, you should now have a working 64-bit linear programming solver for Matlab!
SBML Toolbox download and installation -- IN PROGRESS
The COBRA Toolbox is not fully compatible with version 3 of the SBML Toolbox. For our purposes, we require version 2 (2.0.2 works on 32-bit machines). See the SBML Toolbox website for more information about the SBML toolbox's functionality.
As with GLPKMex, the matlab code in the SBML Toolbox depends on dynamically linked libraries and executable files which have not been precompiled for 64-bit windows versions. To get the SBML toolbox working, on the 64-bit machine, we'll have to do some linking and compiling to make new .dlls, then replace some files in the existing SBML toolbox. This is a painful process.
First, we need to get libSBML working
Note: according to the dependency walker, the Xerces binary has the DWMAPI.dll dependency issue, too. May have to compile it, too, if this doesn't work. (But may not, since Matlab has its own Xerces .dlls installed - though that's version 2.7, which is "bad" for libsbml... but Matlab's xerces also has the DWMAPI.dll thing)
- Xerces-2.5 is ideal, but a 64-bit binary for this older version is unavailable. Version 2.8 is available for download from here: http://apache.siamwebhosting.com/xerces/c/2/binaries/xerces-c_2_8_0-x86_64-windows-vc_8_0.zip . Download it and unzip to your desktop.
- Test xerces from the command line with: the following command (include the quotes): "C:\Documents and Settings\XXXXX\Desktop\xerces-c_2_8_0-x86_64-windows-vc_8_0\SAXCount.exe" (note that you'll need to change XXXXX to your username)
- Next, get libsbml 2.3.5 It's available for download here: http://sourceforge.net/project/showfiles.php?group_id=71971&package_id=71670 . Download it and extract to the desktop.
- Go to \Desktop\libsbml-2.3.5\win32 and run the batch file "createInclude"
- make a folder called "bin" in \Desktop\libsbml-2.3.5\win32
- Copy the files xerces-depdom_2_8.dll and xerces-c_2_8.dll from C:\Documents and Settings\XXXXX\Desktop\xerces-c_2_8_0-x86_64-windows-vc_8_0\bin to C:\Documents and Settings\bdh32\Desktop\libsbml-2.3.5\win32\bin
- copy all the files in C:\Documents and Settings\XXXXX\Desktop\xerces-c_2_8_0-x86_64-windows-vc_8_0\lib to C:\Documents and Settings\bdh32\Desktop\libsbml-2.3.5\win32\bin (we don't really need them all, but it doesn't hurt to copy them over)
- Make the following changes to the libsbml source code files:
- in \libsbml-2.3.5\include\sbml\common\extern.h change line 56 from #if (WIN32 && ! defined CYGWIN && ! defined LIBSBML_STATIC ) to #if (WIN64 && ! defined CYGWIN && ! defined LIBSBML_STATIC )
- in libsbml-2.3.5\include\sbml\common\libsbml-config.h change line 56 from #if defined(WIN32) && !defined(CYGWIN) to #if defined(WIN64) && !defined(CYGWIN)
- NOTE we may have to come back to libsbml-2.3.5\src\bindings\matlab\BuildTranslate_Win32.c
- in libsbml-2.3.5\src\common\extern.h change line 56 from #if (WIN32 && ! defined CYGWIN && ! defined LIBSBML_STATIC ) to #if (WIN64 && ! defined CYGWIN && ! defined LIBSBML_STATIC )
- in libsbml-2.3.5\src\common\libsbml-config.h change line 56 from #if defined(WIN32) && !defined(CYGWIN) to #if defined(WIN64) && !defined(CYGWIN)
- in libsbml-2.3.5\src\validator\test\TestFile.cpp change line 54 from #if defined(WIN32) && !defined(CYGWIN) to #if defined(WIN64) && !defined(CYGWIN)
- in libsbml-2.3.5\src\validator\test\tps\dirent.h change lines 71-73 from
#ifndef _WIN32
# error This file is only currently defined for compilation on Win32 systems
#endif /* _WIN32
to
/*#ifndef _WIN32
*# error This file is only currently defined for compilation on Win32 systems
*#endif /* _WIN32 */
- in libsbml-2.3.5\src\xml\ExpatXMLString.cpp change line 107 from #if defined(WIN32) && !defined(CYGWIN) to #if defined(WIN64) && !defined(CYGWIN)
Next, start with the instructions from http://maliciousattacker.blogspot.com/2008/10/creating-64-bit-applications-with.htmlto get Microsoft Visual C++ Express to compile the 64-bit binaries:
- Launch a Platform SDK command shell by clicking Start -> Microsoft Windows SDK v 6.1-> CMD Shell.
- Launch VCExpress from the Platform SDK command line:
- type cd "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE"
- then type VCExpress.exe /useenv
- Open the libsbml project: File -> Open -> Project/solution then navigate to libsbml-2.3.5\win32\MSVC6\ and select libsbml (the one with the icon that does NOT look like an infinity sign). Go ahead and convert the project to the new version of Visual C++
- Go to Project -> properties, and make the following tweaks to the environment:
- Change the Configuration to "Release"
- under Linker -> Advanced, change the "Target Machine" to MachineX64 (/MACHINE:X64)
- under C/C++, change the Debug Information Format to "Program Database (/Zi)
- under C/C++ -> Code Generation, change the "Runtime Library" to "Multi-threaded (/MT)"
- under C/C++ -> Preprocessor, change the first item listed in "Preprocessor Definition" from WIN32 to WIN64.
- click ok
- Next, go to Tools -> Options, and make the following changes:
- Under Projects and Solutions -> VC++ Directories do the following:
- Show directories for "Include files". Add the following:
- \Desktop\libsbml-2.3.5\include
- \Desktop\libsbml-2.3.5\include\sbml
- C\Desktop\xerces-c_2_8_0-x86_64-windows-vc_8_0\include
- Show directories for "Library Files". Add the following:
- \Desktop\libsbml-2.3.5\win32\bin
- Show directories for "Include files". Add the following:
- click ok
- Under Projects and Solutions -> VC++ Directories do the following:
- Build the project:
- Build -> Batch Build
- select the check box on the line that says "Release"
- click "Clean"
- Build -> Batch Build
- click "Build"
- Save file to the \libsbml-2.3.5\win32\bin directory
and... building and linking worked. Quit out of MSVC. Check libsbml.dll with dependency walker - it has the DWMAPI.dll dependency included (oh well), but none of the other problems I saw before. We'll see if it works anyway...
Next, we need to get the SBML Toolbox working
Now that we've got a 64-bit build of libsbml, we can attack the SBML Toolbox itself.
- Download the .src.zip for Version 2.0.2 from http://sourceforge.net/project/showfiles.php?group_id=71971&package_id=129013 . Extract it to your desktop.
The binaries in the package that we need to build are the SBML Toolbox functions TranslateSBML, ReadSBML, and OutputSBML.
- Go to Desktop\SBMLToolbox-2.0.2\win32\bin, and make a new folder called "old"
- Move everything in Desktop\SBMLToolbox-2.0.2\win32\bin to old
- Copy libsbml.lib, libsbml.dll, xerces-c_2.lib, and xerces-c_2_8.dll from \Desktop\libsbml-2.3.5\win32\bin to Desktop\SBMLToolbox-2.0.2\win32\bin
- Make the following changes to source code in the SBML Toolbox:
- In SBMLToolbox-2.0.2\win32\include\sbml\common\extern.h change line 56 from #if (WIN32 && ! defined CYGWIN && ! defined LIBSBML_STATIC ) to #if (WIN64 && ! defined CYGWIN && ! defined LIBSBML_STATIC )
- In SBMLToolbox-2.0.2\win32\include\sbml\common\libsbml-config.h change line 56 from #if (WIN32 && ! defined CYGWIN && ! defined LIBSBML_STATIC ) to #if (WIN64 && ! defined CYGWIN && ! defined LIBSBML_STATIC )
- In SBMLToolbox-2.0.2\toolbox\TranslateSBML.cpp change lines 61 and 62 to
- include "sbml/xml/ParseMessage.h"
- include "sbml/util/util.h"
- In SBMLToolbox-2.0.2\toolbox\ReadAndValidateSBML.cpp change lines 61 and 62 to
- include "sbml/xml/ParseMessage.h"
- include "sbml/util/util.h"
- Make TranslateSBML:
- In Matlab, make \Desktop\SBMLToolbox-2.0.2\toolbox\ your working directory.
- Make TranslateSBML:
- At the Matlab prompt, type "mex TranslateSBML.c -I'..\win32\include' ..\win32\bin\libsbml.lib"
- Make ReadAndValidateSBML (Note: There is a typo in BuildRead_Win32.m - using the extern directory that doesn't exist):
- In Matlab, type "mex ReadAndValidateSBML.c -I'..\win32\include' ..\win32\bin\libsbml.lib"
- Make OutputSBML:
- In Matlab, type "mex OutputSBML.c -I'..\win32\include' ..\win32\bin\libsbml.lib"
- copy libsbml.dll and xerces-c_2_8.dll from \Desktop\libsbml-2.3.5\win32\bin\
to C:\Program Files\MATLAB\R2008b\bin\win64\ .
- Test the .mex64 files you built in matlab:
- in Matlab, type "TranslateSBML". You should be asked to select a file. Use test.xml.
- type "ReadAndValidateSBML. Again, you should be asked to select a file. Use test.xml.
- type "OutputSBML". You should get a "USAGE" error.
At this point, if the tests go correctly, you've built the SBML Toolbox. Yay!
Now we can install and test the toolbox
- Move SBMLToolbox-2.0.2 from your desktop to your toolbox folder (mine is C:\Program Files\MATLAB\R2008b\toolbox)
- Since we didn't do debug builds for libsbml, and don't have the same version of xerces as SBMLToolbox was originally written with, edit C:\Program Files\MATLAB\R2008b\toolbox\SBMLToolbox-2.0.2\toolbox\install.m as follows:
- Change line 103 from "lib{4} = '..\win32\bin\xerces-c_2_5_0.dll';" to "lib{4} = '..\win32\bin\xerces-c_2_8.dll';"
- Change line 110 from "for i = 1:8" to "for i = 1:4"
- in Matlab, with C:\Program Files\MATLAB\R2008b\toolbox\SBMLToolbox-2.0.2\toolbox\ as your working directory, type "install"
Now you're ready to test the SBML Toolbox install.
- We'll use the SBML files Ec_iJR904_GlcMM.xml and Sc_iND750_GlcMM.xml to test the install. You can get them from the UCSD In Silico Organisms page, but you'll need to get a password from them to do so. I've put copies here for convenience: Sc_iND750_GlcMM.xml and Ec_iJR904_GlcMM.xml (You'll need to right click and "save the target as" on both links to get the files).
- Put the two .xml files in a working directory that you like (I use C:\Documents and Settings\userid\My Documents\MATLAB\SBML models )
- In Matlab, change to the working directory with the two models.
- Double check the PATH in Matlab by typing "which TranslateSBML" at the Matlab command prompt. You should get something like "C:\Program Files\MATLAB\R2008b\toolbox\sbmltoolbox-2.0.2\toolbox\TranslateSBML.mexw64"
- Type "Translate SBML", and select one of the .xml models you downloaded.
THIS IS WHERE I'M CURRENTLY GETTING THE SEGFAULT.
If you select "Attempt to Continue", then try "TranslateSBML" at the command prompt again, it works. After the first segfault, "M=ReadAndValidateSBML" also works (but causes a segfault if one hasn't happened before. However, even after the first segfault "OutputSBML(M)" causes a segfault the first time it is called.
OutputSBML may be a different issue, because it does not return a Stack Trace.
After we get this working, we'll be able to use the COBRA Toolbox (that install is easy!)....
Cobra toolbox download and installation
Luckily, the COBRA toolbox is all Matlab .m files, so the instructions from here on are the same as those for how we get it working on the 32 bit machines.
- Download the most recent version of the COBRA toolbox zip file from the UCSD site.
- Unzip the downloaded file, and copy the included COBRAToolbox folder to the toolboxes folder of your Matlab installation (for example, C:\Program Files\MATLAB\R2008b\toolbox).
- Add the COBRA toolbox folder to your Matlab path:
- In Matlab, go to "Set Path" from the "File" drop menu.
- In the "Set Path" dialog box that pops up, choose "Add with Subfolders"
- Navigate to the COBRAToolbox folder you added to your toolbox folder and click "ok"
- Click "Save" in the "Set Path" diaglog box, then "Close"
- Since we haven't installed the Tomlab solvers, you will get warnings if you try to initialize the Cobra Toolbox. To fix this, edit the initCobraToolbox.m file in the Cobra toolbox as follows:
- First, comment out lines 16, 17, 20, and 21.
- Next, change line 27 from "%CBTDIR = 'Z:\Pub\SOFTWARE\COBRAToolbox';" to "CBTDIR = 'C:\Program Files\MATLAB\R2008b\toolbox\COBRAToolbox-1.3.3';".
- Finally, comment out line 28. Save the changes.
- To test the Path, type "initCobraToolbox" at the Matlab command prompt. There shouldn't be any errors, and the CobraLBSolver should be set to glpk in your workspace.
Testing the COBRA toolbox
- Get the SBML files Ec_iJR904_GlcMM.xml and Sc_iND750_GlcMM. You can get them from the UCSD In Silico Organisms page, but you'll need to get a password from them to do so. I've put copies here for convenience: Sc_iND750_GlcMM.xml and Ec_iJR904_GlcMM.xml (You'll need to right click and "save the target as" on both links to get the files).
- Put the two .xml files in a working directory that you like (I use C:\Documents and Settings\userid\My Documents\MATLAB\SBML models )
- In Matlab, change to the working directory with the two models.
- At the Matlab command prompt, type "testCOBRAToolbox('glpk')". You may get some warnings that the "QP solver glpk not supported by COBRA Toolbox " and that "MEX-files with .dll extensions will not execute in a future version of MATLAB.", but that's okay. When it tests the "WriteSBML" module, you'll be asked to specify a filename to use. You'll be asked to "Press any key to continue" a few times, and then it will grind away at Flux variability analysis, and knockout analysis. The double knockout analysis may take a few hours, depending on your processing power.
- Save your workspace when you're done, and you can join me in the process of trying to understand the results you've got.
