This repository was archived by the owner on Oct 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconditions.py
376 lines (275 loc) · 11.3 KB
/
conditions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# -*- coding: utf-8 -*-
import logging
from util import constants
import numpy
class _BasePhase(object):
def PhaseName(self):
return NotImplementedError
def Name(self):
return NotImplementedError
def Subscript(self):
return NotImplementedError
def Units(self):
return NotImplementedError
def Value(self):
# the value in the given units (i.e. the number shown to the user)
return 1
def PhysiologicalValue(self):
# the value in the given units (i.e. the number shown to the user)
return 1
def IsPhysiological(self):
"""
Checks if the condition matches standard physiological conditions
i.e. 1 mM for aqueous and 1 bar for liquid, gas and solid
"""
return self.Value() == self.PhysiologicalValue()
def ValueString(self):
return '%.2g' % self.Value()
def IsConstant(self):
return True
def SetValue(self, v):
return NotImplementedError
def HumanValueAndUnits(self):
"""
convert the value to human readable numbers by changing
the units (adding milli, micro modifiers)
"""
return (self.Value()*1e3, 1e-3, 'm' + self.Units())
# make an exception for standard values (e.g. instead of 1 M the
# default value will be 1000 mM). This is more convenient for users.
# if self.Value() == 1:
# return (1e3, 1e-3, 'x10<sup>-3</sup> %s' % self.Units())
logv = numpy.log10(self.Value())
exp = numpy.floor(logv / 3.0)*3
prefactor = 10**(logv - exp)
if exp != 0:
return (prefactor, 10**(exp),
'x10<sup>%d</sup> %s' % (exp, self.Units()))
else:
# avoid writing 10^0 as a prefix for the units
return (self.Value(), 1, self.Units())
def HumanValueAndUnits_letters(self):
"""
convert the value to human readable numbers by changing
the units (adding milli, micro modifiers)
"""
# if self.Value() > 1e-2:
# return (self.Value(), self.Units())
if self.Value() >= 1e-4:
return (self.Value() * 1e3, 1e-3, 'm' + self.Units())
if self.Value() >= 1e-7:
return (self.Value() * 1e6, 1e-6, 'μ' + self.Units())
return (self.Value() * 1e9, 1e-9, 'n' + self.Units())
def __str__(self):
return 'phase = %s (%g %s)' % (self.PhaseName(),
self.Value(), self.Units())
class StandardAqueousPhase(_BasePhase):
def PhaseName(self):
return constants.AQUEOUS_PHASE_NAME
def Name(self):
return constants.STANDARD_AQUEOUS_PHASE_NAME
def Subscript(self):
return '(aq)'
def Units(self):
return 'M'
class StandardGasPhase(_BasePhase):
def PhaseName(self):
return constants.GAS_PHASE_NAME
def Name(self):
return constants.STANDARD_GAS_PHASE_NAME
def Subscript(self):
return '(g)'
def Units(self):
return 'bar'
class StandardLiquidPhase(_BasePhase):
def PhaseName(self):
return constants.LIQUID_PHASE_NAME
def Name(self):
return constants.STANDARD_LIQUID_PHASE_NAME
def Subscript(self):
return '(l)'
def Units(self):
return 'bar'
def IsPhysiological(self):
return True
class StandardSolidPhase(_BasePhase):
def PhaseName(self):
return constants.SOLID_PHASE_NAME
def Name(self):
return constants.STANDARD_SOLID_PHASE_NAME
def Subscript(self):
return '(s)'
def Units(self):
return 'bar'
class CustomAqueousPhase(StandardAqueousPhase):
def __init__(self, concentration=None): # in units of M
self.SetValue(concentration)
def Name(self):
return constants.CUSTOM_AQUEOUS_PHASE_NAME
def IsConstant(self):
return False
def SetValue(self, v=None):
self._concentration = v or self.PhysiologicalValue()
def Value(self):
return self._concentration
def PhysiologicalValue(self):
return constants.PHYS_AQUEOUS_VALUE
class CustomGasPhase(StandardGasPhase):
def __init__(self, partial_pressure=1.0):
self.SetValue(partial_pressure)
def Name(self):
return constants.CUSTOM_GAS_PHASE_NAME
def IsConstant(self):
return False
def SetValue(self, v=None):
self._partial_pressure = v or self.PhysiologicalValue()
def Value(self):
return self._partial_pressure
def PhysiologicalValue(self):
return constants.PHYS_GAS_VALUE
###############################################################################
class _BaseConditions(object):
def __init__(self,
pH=constants.DEFAULT_PH,
pMg=constants.DEFAULT_PMG,
ionic_strength=constants.DEFAULT_IONIC_STRENGTH,
temperature=constants.DEFAULT_TEMP,
e_reduction_potential=constants.DEFAULT_ELECTRON_REDUCTION_POTENTIAL):
self.pH = pH
self.pMg = pMg
self.ionic_strength = ionic_strength
self.temperature = temperature
self.e_reduction_potential = e_reduction_potential
self._phases = {}
@staticmethod
def _GeneratePhase(phase, value):
if phase == constants.AQUEOUS_PHASE_NAME:
return CustomAqueousPhase(value)
if phase == constants.GAS_PHASE_NAME:
return CustomGasPhase(value)
if phase == constants.LIQUID_PHASE_NAME:
return StandardLiquidPhase()
if phase == constants.SOLID_PHASE_NAME:
return StandardSolidPhase()
raise NotImplementedError
def SetPhasesAndRatios(self, all_ids, all_phases, all_ratios):
for kegg_id, phase, ratio in zip(all_ids, all_phases, all_ratios):
self.SetPhase(kegg_id, phase, ratio)
def SetPhase(self, kegg_id, phase, ratio=1):
raise NotImplementedError
def GetPhase(self, kegg_id):
if kegg_id not in self._phases:
logging.debug('Condition requested for unknown id: %s', kegg_id)
return None
return self._phases[kegg_id]
def _GetUrlParams(self, kegg_id):
phase = self.GetPhase(kegg_id)
return ['conditions=%s' % self.__str__(),
'reactantsPhase=%s' % phase.PhaseName(),
'reactantsConcentration=%s' % phase.Value()]
class StandardConditions(_BaseConditions):
def __str__(self):
return constants.STANDARD_CONDITION_STRING
def SetPhase(self, kegg_id, phase, ratio=1):
self._phases[kegg_id] = CustomConditions._GeneratePhase(phase, 1)
class MillimolarConditions(_BaseConditions):
def __str__(self):
return constants.MILLIMOLAR_CONDITION_STRING
def SetPhase(self, kegg_id, phase, ratio=1):
self._phases[kegg_id] = CustomConditions._GeneratePhase(phase, 1e-3)
class CustomConditions(_BaseConditions):
def __str__(self):
return constants.CUSTOM_CONDITION_STRING
def SetPhase(self, kegg_id, phase, ratio=1):
logging.debug('For %s, setting phase to %s and ratio to %g' %
(kegg_id, phase, ratio))
self._phases[kegg_id] = CustomConditions._GeneratePhase(phase, ratio)
###############################################################################
def CreateConditions(name, all_ids=None, all_phases=None, all_ratios=None):
if name == constants.STANDARD_CONDITION_STRING:
return StandardConditions()
if name == constants.MILLIMOLAR_CONDITION_STRING:
return MillimolarConditions()
if name == constants.CUSTOM_CONDITION_STRING:
assert all_ids and all_phases and all_ratios
cc = CustomConditions()
cc.SetPhasesAndRatios(all_ids, all_phases, all_ratios)
return cc
logging.warning('unrecognized condition name: ' + name)
return None
###############################################################################
class AqueousParams(object):
def __init__(self,
pH=constants.DEFAULT_PH,
pMg=constants.DEFAULT_PMG,
ionic_strength=constants.DEFAULT_IONIC_STRENGTH,
e_reduction_potential=constants.DEFAULT_ELECTRON_REDUCTION_POTENTIAL,
max_priority=0,
mode=''):
self.pH = pH
self.pMg = pMg
self.ionic_strength = ionic_strength
self.e_reduction_potential = e_reduction_potential
self.max_priority = max_priority
self.mode = mode or 'BA'
@staticmethod
def FromForm(form, cookies=None):
if cookies is None:
cookies = {}
pH = form.cleaned_ph
if pH is None:
pH = float(cookies.get('pH', constants.DEFAULT_PH))
pMg = form.cleaned_pmg
if pMg is None:
pMg = float(cookies.get('pMg', constants.DEFAULT_PMG))
ionic_strength = form.cleaned_ionic_strength
if ionic_strength is None:
ionic_strength = float(cookies.get(
'ionic_strength',
constants.DEFAULT_IONIC_STRENGTH))
e_reduction_potential = form.cleaned_e_reduction_potential
if e_reduction_potential is None:
e_reduction_potential = float(cookies.get(
'e_reduction_potential',
constants.DEFAULT_ELECTRON_REDUCTION_POTENTIAL))
max_priority = form.cleaned_max_priority
if max_priority is None:
max_priority = int(cookies.get('max_priority', 1))
mode = form.cleaned_mode
if not mode:
mode = cookies.get('mode', '')
return AqueousParams(pH, pMg, ionic_strength, e_reduction_potential,
max_priority, mode)
def Clone(self):
return AqueousParams(self.pH, self.pMg, self.ionic_strength,
self.e_reduction_potential, self.max_priority,
self.mode)
def __str__(self):
return 'pH = %.2g, pMg = %.2g, I = %.2g M, Ered = %g, MaxPriority=%d' % \
(self.pH, self.pMg, self.ionic_strength,
self.e_reduction_potential, self.max_priority)
def SetCookies(self, response):
response.set_cookie('pH', str(self.pH))
response.set_cookie('pMg', str(self.pMg))
response.set_cookie('ionic_strength', str(self.ionic_strength))
response.set_cookie('e_reduction_potential', str(self.e_reduction_potential))
response.set_cookie('max_priority', str(self.max_priority))
response.set_cookie('mode', str(self.mode))
def GetTemplateData(self):
logging.debug('max_priorty = %d' % self.max_priority)
return {'ph': self.pH, 'pmg': self.pMg,
'ionic_strength': self.ionic_strength,
'e_reduction_potential': self.e_reduction_potential,
'max_priority': self.max_priority,
'advanced_mode': (self.mode == 'AD')}
def _GetUrlParams(self):
"""
Get the URL params for this reaction.
"""
url_params = ['ph=%f' % self.pH,
'pmg=%f' % self.pMg,
'ionic_strength=%f' % self.ionic_strength,
'e_reduction_potential=%f' % self.e_reduction_potential,
'max_priority=%d' % self.max_priority,
'mode=%s' % self.mode]
return url_params