Skip to content

Commit a01d984

Browse files
committed
updated code to v7.20
1 parent 0e32a34 commit a01d984

File tree

5 files changed

+61
-11
lines changed

5 files changed

+61
-11
lines changed

src/CBRead.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
Author: Brett G. Olivier
2121
Contact email: [email protected]
22-
Last edit: $Author: bgoli $ ($Id: CBRead.py 604 2017-07-17 12:30:08Z bgoli $)
22+
Last edit: $Author: bgoli $ ($Id: CBRead.py 648 2018-05-23 20:01:54Z bgoli $)
2323
2424
"""
2525

@@ -91,6 +91,37 @@
9191
zfile.close()
9292
del zipfile, zfile
9393

94+
def loadModel(sbmlfile):
95+
"""
96+
Loads any SBML model in COBRA, FAME (SBML2FBA), SBML3FBCv1, SBML3FBCv2 format.
97+
98+
- *sbmlfile* an SBML model file
99+
100+
"""
101+
mod = res = None
102+
print('Attempting to load SBML file: {}'.format(sbmlfile))
103+
try:
104+
res = CBXML.sbml_fileFindVersion(sbmlfile)[0]
105+
except Exception as ex:
106+
print(ex)
107+
print('\nERROR: cannot determine SBML model type for file {}'.format(sbmlfile))
108+
return None
109+
110+
try:
111+
if res == 'L3V1FBC2':
112+
mod = readSBML3FBC(sbmlfile)
113+
elif res == 'L3V1FBC1':
114+
mod = readSBML3FBC(sbmlfile)
115+
elif res == 'COBRA':
116+
mod = readCOBRASBML(sbmlfile, delete_intermediate=True)
117+
elif res == 'L2FBA':
118+
mod = readSBML2FBA(sbmlfile)
119+
except Exception as ex:
120+
print(ex)
121+
print('\nERROR: model load failed for SBML file: {}'.format(sbmlfile))
122+
return None
123+
return mod
124+
94125

95126
def readSBML3FBC(fname, work_dir=None, return_sbml_model=False, xoptions={'validate' : False}, scan_notes_gpr=True):
96127
"""

src/CBWrite.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
Author: Brett G. Olivier
2121
Contact email: [email protected]
22-
Last edit: $Author: bgoli $ ($Id: CBWrite.py 629 2017-10-24 22:01:14Z bgoli $)
22+
Last edit: $Author: bgoli $ ($Id: CBWrite.py 648 2018-05-23 20:01:54Z bgoli $)
2323
2424
"""
2525

@@ -67,6 +67,21 @@
6767
except ImportError:
6868
print('\nINFO: No xlwt module available, Excel spreadsheet creation disabled')
6969

70+
71+
def saveModel(model, filename, compress=False):
72+
"""
73+
Saves the model to an SBML file using the lates SBML3 FBC version.
74+
75+
- *model* the CBMPy model
76+
- *filename* the filename to write
77+
78+
"""
79+
80+
writeSBML3FBCV2(model, filename, directory=None, gpr_from_annot=False, add_groups=True,\
81+
add_cbmpy_annot=True, add_cobra_annot=False, validate=False, compress_bounds=True,\
82+
zip_model=compress, return_model_string=False)
83+
84+
7085
def writeSBML3FBC(fba, fname, directory=None, gpr_from_annot=False,\
7186
add_groups=True, add_cbmpy_annot=True, add_cobra_annot=False,\
7287
xoptions={'fbc_version': 1, 'validate' : False, 'compress_bounds' : True}):

src/CBXML.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
Author: Brett G. Olivier
2121
Contact email: [email protected]
22-
Last edit: $Author: bgoli $ ($Id: CBXML.py 643 2018-03-08 23:17:53Z bgoli $)
22+
Last edit: $Author: bgoli $ ($Id: CBXML.py 648 2018-05-23 20:01:54Z bgoli $)
2323
2424
"""
2525
## gets rid of "invalid variable name" info
@@ -3002,13 +3002,13 @@ def sbml_fileFindVersion(f):
30023002

30033003
msg = ''
30043004
if output == 'L3V1FBC1':
3005-
msg = 'SBML Level 3 FBC version 1 model detected, load with cbmpy.readSBML3FBC()'
3005+
msg = 'SBML Level 3 FBC version 1 model detected, loading with cbmpy.readSBML3FBC()'
30063006
elif output == 'L3V1FBC2':
3007-
msg = 'SBML Level 3 FBC version 2 model detected, load with cbmpy.readSBML3FBC()'
3007+
msg = 'SBML Level 3 FBC version 2 model detected, loading with cbmpy.readSBML3FBC()'
30083008
elif output == 'L2FBA':
3009-
msg = 'SBML Level 2 FAME model detected, load with cbmpy.readSBML2FBA()'
3009+
msg = 'SBML Level 2 FAME model detected, loading with cbmpy.readSBML2FBA()'
30103010
elif output == 'COBRA':
3011-
msg = 'COBRA SBML L2 model detected, load with cbmpy.readCOBRASBML()'
3011+
msg = 'COBRA SBML L2 model detected, loading with cbmpy.readCOBRASBML()'
30123012
else:
30133013
msg = 'Unknown model type, contact the SBML community or developer for more details.'
30143014
del D

src/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
Author: Brett G. Olivier
2121
Contact email: [email protected]
22-
Last edit: $Author: bgoli $ ($Id: __init__.py 640 2017-12-18 18:13:40Z bgoli $)
22+
Last edit: $Author: bgoli $ ($Id: __init__.py 648 2018-05-23 20:01:54Z bgoli $)
2323
2424
"""
2525
#
@@ -58,8 +58,8 @@
5858
from .CBXML import _HAVE_SBML_
5959
if _HAVE_SBML_:
6060
from .CBXML import sbml_fileFindVersion, sbml_fileValidate
61-
from .CBRead import readSBML3FBC, readSBML2FBA, readCOBRASBML
62-
from .CBWrite import writeSBML3FBC, writeModelToExcel97, writeModelToCOMBINEarchive, writeCOBRASBML, writeSBML3FBCV2
61+
from .CBRead import loadModel, readSBML3FBC, readSBML2FBA, readCOBRASBML
62+
from .CBWrite import saveModel, writeSBML3FBC, writeModelToExcel97, writeModelToCOMBINEarchive, writeCOBRASBML, writeSBML3FBCV2
6363
else:
6464
print('\nWARNING: No SBML support, top-level SBML read/write functions disabled.\n')
6565

src/nosetests/test_loadSBML.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,18 @@ def setup_class(klass):
6262
"""This method is run once for each class before any tests are run"""
6363
klass.m = {}
6464
for m in DATA:
65+
cmod = None
6566
if m.startswith('L2CBR_'):
6667
cmod = cbmpy.readCOBRASBML(m, work_dir=MDIR,\
6768
output_dir=self.CDIR, delete_intermediate=True)
6869
elif m.startswith('L2FBA_'):
6970
cmod = cbmpy.readSBML2FBA(os.path.join(MDIR, m))
7071
elif m.startswith('L3FBCV1_') or m.startswith('L3FBCV2_'):
7172
cmod = cbmpy.readSBML3FBC(os.path.join(MDIR, m))
72-
klass.m[m] = cmod
73+
else:
74+
cmod = None
75+
if cmod is not None:
76+
klass.m[m] = cmod
7377
if 'HOME' in os.environ:
7478
klass.CDIR = os.environ['HOME']
7579
else:

0 commit comments

Comments
 (0)