Skip to content

Commit 659366e

Browse files
committed
updated miriam registry
1 parent b41e5ba commit 659366e

File tree

8 files changed

+1193
-663
lines changed

8 files changed

+1193
-663
lines changed

cbmpy-tools/miriam-tools/miriamids.py

Lines changed: 92 additions & 47 deletions
Large diffs are not rendered by default.

cbmpy-tools/miriam-tools/miriamresources.xml

Lines changed: 879 additions & 447 deletions
Large diffs are not rendered by default.

src/CBModel.py

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
Author: Brett G. Olivier
2121
Contact email: bgoli@users.sourceforge.net
22-
Last edit: $Author: bgoli $ ($Id: CBModel.py 505 2016-10-20 15:46:12Z bgoli $)
22+
Last edit: $Author: bgoli $ ($Id: CBModel.py 518 2016-11-07 17:40:44Z bgoli $)
2323
2424
"""
2525
## gets rid of "invalid variable name" info
@@ -37,9 +37,10 @@
3737
import numpy, re, time, weakref, copy, json, urllib2
3838

3939
try:
40-
import pickle
41-
except ImportError:
4240
import cPickle as pickle
41+
except ImportError:
42+
import pickle
43+
4344

4445
HAVE_SYMPY = False
4546
try:
@@ -99,6 +100,8 @@ def __getstate__(self):
99100
"""
100101
Internal method that should allow our weakrefs to be 'picklable'
101102
103+
# overloaded by Model
104+
102105
"""
103106

104107
if '__objref__' not in self.__dict__:
@@ -322,17 +325,21 @@ def serialize(self, protocol=0):
322325
- *protocol* [default=0] serialize to a string or binary if required,
323326
see pickle module documentation for details
324327
328+
# overloaded in Model
329+
325330
"""
326331
return pickle.dumps(self, protocol=protocol)
327332

328-
def serializeToDisk(self, filename, protocol=0):
333+
def serializeToDisk(self, filename, protocol=2):
329334
"""
330335
Serialize to disk using pickle protocol:
331336
332337
- *filename* the name of the output file
333-
- *protocol* [default=0] serialize to a string or binary if required,
338+
- *protocol* [default=2] serialize to a string or binary if required,
334339
see pickle module documentation for details
335340
341+
# overloaded in Model
342+
336343
"""
337344
F = file(filename, 'wb')
338345
pickle.dump(self, F, protocol=protocol)
@@ -555,6 +562,9 @@ def clone(self):
555562
cpy.__setModelSelf__()
556563
cpy.__setGlobalIdStore__()
557564
cpy.__populateGlobalIdStore__()
565+
# TODO: try make the global ID store work exclusively with __setState__ and __getState__ for both clone/serialise
566+
self.__setGlobalIdStore__()
567+
self.__populateGlobalIdStore__()
558568

559569
print('Model clone time: {}'.format(time.time()-tzero))
560570
return cpy
@@ -641,6 +651,21 @@ def __unsetModelSelf__(self):
641651
for p in self.parameters:
642652
p.__unsetObjRef__()
643653

654+
def __getstate__(self):
655+
"""
656+
Internal method that should allow our weakrefs to be 'picklable'
657+
658+
# overloaded by Model
659+
660+
"""
661+
662+
self.__global_id__ = None
663+
if '__objref__' not in self.__dict__:
664+
return self.__dict__
665+
else:
666+
cpy = self.__dict__.copy()
667+
cpy['__objref__'] = None
668+
return cpy
644669

645670
def __setstate__(self, dic):
646671
"""
@@ -649,6 +674,40 @@ def __setstate__(self, dic):
649674
"""
650675
self.__dict__ = dic
651676
self.__setModelSelf__()
677+
self.__setGlobalIdStore__()
678+
self.__populateGlobalIdStore__()
679+
680+
def serialize(self, protocol=0):
681+
"""
682+
Serialize object, returns a string by default
683+
684+
- *protocol* [default=0] serialize to a string or binary if required,
685+
see pickle module documentation for details
686+
687+
# overloaded in CBModel
688+
689+
"""
690+
s = pickle.dumps(self, protocol=protocol)
691+
self.__setGlobalIdStore__()
692+
self.__populateGlobalIdStore__()
693+
return s
694+
695+
def serializeToDisk(self, filename, protocol=2):
696+
"""
697+
Serialize to disk using pickle protocol:
698+
699+
- *filename* the name of the output file
700+
- *protocol* [default=2] serialize to a string or binary if required,
701+
see pickle module documentation for details
702+
703+
# overloaded in CBModel
704+
705+
"""
706+
F = file(filename, 'wb')
707+
pickle.dump(self, F, protocol=protocol)
708+
F.close()
709+
self.__setGlobalIdStore__()
710+
self.__populateGlobalIdStore__()
652711

653712
def addMIRIAMannotation(self, qual, entity, mid):
654713
"""

src/CBMultiCore.py

Lines changed: 49 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
Author: Brett G. Olivier
2121
Contact email: bgoli@users.sourceforge.net
22-
Last edit: $Author: bgoli $ ($Id: CBMultiCore.py 515 2016-11-07 14:20:11Z bgoli $)
22+
Last edit: $Author: bgoli $ ($Id: CBMultiCore.py 518 2016-11-07 17:40:44Z bgoli $)
2323
2424
"""
2525

@@ -41,14 +41,14 @@
4141
MULTIFVAFILE = __file__.replace('CBMultiCore', '_multicorefva')
4242
del _multicorefva
4343

44-
try:
45-
from . import _multicoreenvfva
46-
MULTIENVFVAFILE = __file__.replace('CBMultiCore','_multicoreenvfva')
47-
del _multicoreenvfva
48-
HAVE_MULTIENV = True
49-
except ImportError as ex:
50-
print(ex)
51-
HAVE_MULTIENV = False
44+
#try:
45+
#from . import _multicoreenvfva
46+
#MULTIENVFVAFILE = __file__.replace('CBMultiCore','_multicoreenvfva')
47+
#del _multicoreenvfva
48+
#HAVE_MULTIENV = True
49+
#except ImportError as ex:
50+
#print(ex)
51+
#HAVE_MULTIENV = False
5252

5353
from .CBConfig import __CBCONFIG__ as __CBCONFIG__
5454

@@ -101,110 +101,47 @@ def runMultiCoreFVA(fba, selected_reactions=None, pre_opt=True, tol=None, objF2c
101101
REAC.reduced_costs = fva[R][1]
102102
return fva, fvan
103103

104-
if HAVE_MULTIENV:
105-
def runMultiCoreMultiEnvFVA(lp, selected_reactions=None, tol=None, rhs_sense='lower', optPercentage=100.0, work_dir=None, debug=False, procs=2):
106-
"""
107-
Run a multicore FVA where:
108-
109-
- *lp* is a multienvironment lp model instance
110-
- *procs* [default=2] number of processing threads (optimum seems to be about the number of physical cores)
111-
112-
"""
113-
fN = os.path.join(work_dir, str(time.time()).split('.')[0])
114-
lp.write(fN+'.lp', filetype='lp')
115-
MEargs = [fN+'.lp', selected_reactions, tol, rhs_sense, optPercentage, work_dir, debug]
116-
print(MEargs)
117-
print(fN)
118-
F = file(fN, 'wb')
119-
pickle.dump(MEargs, F, protocol=-1)
120-
F.close()
121-
subprocess.call(['python', MULTIENVFVAFILE, str(procs), fN])
122-
F = file(fN, 'rb')
123-
res = pickle.load(F)
124-
F.close()
125-
os.remove(fN)
126-
127-
fva = res[0]
128-
fvan = res[1]
129-
130-
if len(fva) == 1:
131-
fva = fva[0]
132-
fvan = fvan[0]
133-
elif len(fva) > 1:
134-
fva = numpy.vstack(fva)
135-
fvan2 = []
136-
for n_ in fvan:
137-
fvan2 += n_
138-
fvan = fvan2
139-
140-
return fva, fvan
141-
else:
142-
def runMultiCoreMultiEnvFVA(lp, selected_reactions=None, tol=None, rhs_sense='lower', optPercentage=100.0, work_dir=None, debug=False, procs=2):
143-
raise RuntimeError('\nMultiCore module not present')
144-
145-
146-
147-
148-
# look at using if __name__ != '__main__'
149-
150-
"""
151-
import os, time, math, pickle
152-
import multiprocessing
153-
import CBSolver, CBMultiCore
154-
155-
def funcFVA(x,r):
156-
try:
157-
fva, fvan = CBSolver.FluxVariabilityAnalysis(x, selected_reactions=r, pre_opt=True, quiet=True, oldlpgen=False)
158-
except Exception as ex:
159-
print ex
160-
fva = fvan = None
161-
raise RuntimeError(ex)
162-
return fva, fvan
163-
164-
def multiCoreFVA(fvamod, procs=2, timeout=None):
165-
#assert procs >= 2, '\nMinimum 2 processes required'
166-
rid = fvamod.getReactionIds()
167-
PRs = [d_[0] for d_ in list(CBMultiCore.grouper(int(math.ceil(len(rid)/float(procs))), range(len(rid))))]
168-
print procs, len(PRs), PRs
169-
170-
s2time = time.time()
171-
TP = multiprocessing.Pool(processes=procs)
172-
results = []
173-
if procs == 1:
174-
results.append(TP.apply_async(funcFVA, (fvamod.clone(), rid)))
175-
else:
176-
for p_ in range(len(PRs)-1):
177-
print '%s -> %s' % (PRs[p_], PRs[p_+1])
178-
results.append(TP.apply_async(funcFVA, (fvamod.clone(), rid[PRs[p_]:PRs[p_+1]])))
179-
results.append(TP.apply_async(funcFVA, (fvamod.clone(), rid[PRs[-1]:])))
180-
fva = []
181-
fvan = []
182-
for r_ in results:
183-
out = r_.get(timeout)
184-
fva.append(out[0])
185-
fvan.append(out[1])
186-
e2time = time.time()
187-
del results
188-
189-
print '\nMulticore FVA took: %.2f min (%s processes)' % ((e2time-s2time)/60., procs)
190-
return fva, fvan
191-
192-
193-
if __name__ == '__main__':
194-
print os.sys.argv
104+
#if HAVE_MULTIENV:
105+
#def runMultiCoreMultiEnvFVA(lp, selected_reactions=None, tol=None, rhs_sense='lower', optPercentage=100.0, work_dir=None, debug=False, procs=2):
106+
#"""
107+
#Run a multicore FVA where:
108+
109+
#- *lp* is a multienvironment lp model instance
110+
#- *procs* [default=2] number of processing threads (optimum seems to be about the number of physical cores)
111+
112+
#"""
113+
#fN = os.path.join(work_dir, str(time.time()).split('.')[0])
114+
#lp.write(fN+'.lp', filetype='lp')
115+
#MEargs = [fN+'.lp', selected_reactions, tol, rhs_sense, optPercentage, work_dir, debug]
116+
#print(MEargs)
117+
#print(fN)
118+
#F = file(fN, 'wb')
119+
#pickle.dump(MEargs, F, protocol=-1)
120+
#F.close()
121+
#subprocess.call(['python', MULTIENVFVAFILE, str(procs), fN])
122+
#F = file(fN, 'rb')
123+
#res = pickle.load(F)
124+
#F.close()
125+
#os.remove(fN)
126+
127+
#fva = res[0]
128+
#fvan = res[1]
129+
130+
#if len(fva) == 1:
131+
#fva = fva[0]
132+
#fvan = fvan[0]
133+
#elif len(fva) > 1:
134+
#fva = numpy.vstack(fva)
135+
#fvan2 = []
136+
#for n_ in fvan:
137+
#fvan2 += n_
138+
#fvan = fvan2
139+
140+
#return fva, fvan
141+
#else:
142+
#def runMultiCoreMultiEnvFVA(lp, selected_reactions=None, tol=None, rhs_sense='lower', optPercentage=100.0, work_dir=None, debug=False, procs=2):
143+
#raise RuntimeError('\nMultiCore module not present')
195144

196-
cores = int(os.sys.argv[1])
197-
F = file(os.sys.argv[2],'rb')
198-
cmod = pickle.load(F)
199-
F.close()
200145

201146

202-
CBSolver.analyzeModel(cmod, oldlpgen=False)
203-
res = multiCoreFVA(cmod, procs=cores)
204-
205-
F = file(os.sys.argv[2], 'wb')
206-
pickle.dump(res, F, protocol=-1)
207-
F.flush()
208-
F.close()
209147

210-
"""

src/CBTools.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
Author: Brett G. Olivier
2121
Contact email: bgoli@users.sourceforge.net
22-
Last edit: $Author: bgoli $ ($Id: CBTools.py 515 2016-11-07 14:20:11Z bgoli $)
22+
Last edit: $Author: bgoli $ ($Id: CBTools.py 518 2016-11-07 17:40:44Z bgoli $)
2323
2424
"""
2525
## gets rid of "invalid variable name" info
@@ -98,6 +98,17 @@ def deSerialize(s):
9898
"""
9999
return pickle.loads(s)
100100

101+
def deSerializeFromDisk(filename):
102+
"""
103+
Loads a serialised Python pickle from *filename* returns the Python object(s)
104+
105+
"""
106+
assert os.path.exists(filename), '\nFile \"{}\" does not exist'.format(filename)
107+
F = file(filename, 'rb')
108+
obj = pickle.load(F)
109+
F.close()
110+
return obj
111+
101112
def addStoichToFBAModel(fm):
102113
"""
103114
Build stoichiometry: this method has been refactored into the model class - cmod.buildStoichMatrix()

src/CBXML.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: bgoli@users.sourceforge.net
22-
Last edit: $Author: bgoli $ ($Id: CBXML.py 515 2016-11-07 14:20:11Z bgoli $)
22+
Last edit: $Author: bgoli $ ($Id: CBXML.py 520 2016-11-07 21:10:45Z bgoli $)
2323
2424
"""
2525
## gets rid of "invalid variable name" info
@@ -3726,15 +3726,15 @@ def sbml_readSBML3FBC(fname, work_dir=None, return_sbml_model=False, xoptions={}
37263726
else:
37273727
print('Gene {} is not part of a GPR association. Will create anyway!'.format(g_))
37283728
non_gpr_genes.append(g_)
3729-
# TODO: this is a corner case I'll deal with it better later, non GPR associated genes are added as inactive gene objects
3729+
37303730
for ng_ in non_gpr_genes:
37313731
G = CBModel.Gene(ng_, label=GENE_D[ng_]['label'], active=False)
37323732
G.annotation = GENE_D[ng_]['annotation']
37333733
G.__sbo_term__ = GENE_D[ng_]['sbo']
37343734
G.miriam = GENE_D[ng_]['miriam']
37353735
G.setNotes(GENE_D[ng_]['notes'])
37363736
print('WARNING: Non-gpr gene detected.', G.getId(), G.getLabel(), fm.getId())
3737-
raise RuntimeError()
3737+
#raise RuntimeError()
37383738

37393739
if DEBUG: print('GPR build: {}'.format(round(time.time() - time0, 3)))
37403740
time0 = time.time()

src/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
Author: Brett G. Olivier
2121
Contact email: bgoli@users.sourceforge.net
22-
Last edit: $Author: bgoli $ ($Id: __init__.py 515 2016-11-07 14:20:11Z bgoli $)
22+
Last edit: $Author: bgoli $ ($Id: __init__.py 518 2016-11-07 17:40:44Z bgoli $)
2323
2424
"""
2525
##
@@ -87,6 +87,7 @@
8787
NAN = float('nan')
8888

8989
from . import CBModel, CBDataStruct, CBModelTools, CBRead, CBReadtxt, CBTools, CBVersion, CBWrite, CBXML, CBNetDB, CBPlot
90+
from .CBTools import deSerialize, deSerializeFromDisk
9091
from . import PyscesSED
9192
SED = PyscesSED.SEDCBMPY
9293

0 commit comments

Comments
 (0)