Skip to content

Commit 910cdec

Browse files
committed
previous updates
1 parent 89ed3d2 commit 910cdec

File tree

2 files changed

+71
-52
lines changed

2 files changed

+71
-52
lines changed

modelseedpy/core/mstemplate.py

Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TemplateReactionType(Enum):
2929
GAPFILLING = 'gapfilling'
3030

3131

32-
class NewModelTemplateCompound:
32+
class MSTemplateMetabolite:
3333

3434
def __init__(self, cpd_id, formula=None, name='', default_charge=None,
3535
mass=None, delta_g=None, delta_g_error=None, is_cofactor=False,
@@ -51,7 +51,7 @@ def __init__(self, cpd_id, formula=None, name='', default_charge=None,
5151

5252
@staticmethod
5353
def from_dict(d):
54-
return NewModelTemplateCompound(
54+
return MSTemplateMetabolite(
5555
d['id'], d['formula'], d['name'],
5656
d['defaultCharge'], d['mass'],
5757
d['deltaG'], d['deltaGErr'],
@@ -184,7 +184,7 @@ class MSTemplateReaction(Reaction):
184184
def __init__(self, rxn_id: str, reference_id: str, name='', subsystem='', lower_bound=0.0, upper_bound=None,
185185
reaction_type=TemplateReactionType.CONDITIONAL, gapfill_direction='=',
186186
base_cost=1000, reverse_penalty=1000, forward_penalty=1000,
187-
status='OK', delta_g=0.0, delta_g_err=0.0, reference_reaction_id=None):
187+
status='OK', reference_reaction_id=None):
188188
"""
189189
190190
:param rxn_id:
@@ -199,8 +199,6 @@ def __init__(self, rxn_id: str, reference_id: str, name='', subsystem='', lower_
199199
:param reverse_penalty:
200200
:param forward_penalty:
201201
:param status:
202-
:param delta_g:
203-
:param delta_g_err:
204202
:param reference_reaction_id: DO NOT USE THIS duplicate of reference_id
205203
:param template:
206204
"""
@@ -212,9 +210,7 @@ def __init__(self, rxn_id: str, reference_id: str, name='', subsystem='', lower_
212210
self.forward_penalty = forward_penalty
213211
self.status = status
214212
self.type = reaction_type.value if type(reaction_type) == TemplateReactionType else reaction_type
215-
self.deltaG = delta_g
216-
self.deltaGErr = delta_g_err
217-
self.reference_reaction_id = reference_reaction_id #TODO: to be removed
213+
self.reference_reaction_id = reference_reaction_id # TODO: to be removed
218214
self.complexes = DictList()
219215
self.templateReactionReagents = {}
220216
self._template = None
@@ -273,8 +269,6 @@ def from_dict(d, template):
273269
d['type'], d['GapfillDirection'],
274270
d['base_cost'], d['reverse_penalty'], d['forward_penalty'],
275271
d['status'] if 'status' in d else None,
276-
d['deltaG'] if 'deltaG' in d else None,
277-
d['deltaGErr'] if 'deltaGErr' in d else None,
278272
d['reaction_ref'].split('/')[-1]
279273
)
280274
reaction.add_metabolites(metabolites)
@@ -336,8 +330,6 @@ def get_data(self):
336330
'base_cost': self.base_cost,
337331
'reverse_penalty': self.reverse_penalty,
338332
'forward_penalty': self.forward_penalty,
339-
'deltaG': self.deltaG,
340-
'deltaGErr': self.deltaGErr,
341333
'upper_bound': self.upper_bound,
342334
'lower_bound': self.lower_bound,
343335
'direction': get_direction_from_constraints(self.lower_bound, self.upper_bound),
@@ -347,7 +339,7 @@ def get_data(self):
347339
'templateReactionReagents': template_reaction_reagents,
348340
'templatecompartment_ref': '~/compartments/id/' + self.compartment,
349341
'templatecomplex_refs': list(map(lambda x: '~/complexes/id/' + x.id, self.complexes)),
350-
'status': self.status,
342+
# 'status': self.status,
351343
'type': self.type
352344
}
353345

@@ -523,16 +515,40 @@ def _repr_html_(self):
523515
self.roles.items()), 200))
524516

525517

518+
class MSTemplateCompartment:
519+
520+
def __init__(self, compartment_id: str, name: str, ph: float, hierarchy=0, aliases=None):
521+
self.id = compartment_id
522+
self.name = name
523+
self.ph = ph
524+
self.hierarchy = hierarchy
525+
self.aliases = [] if aliases is None else list(aliases)
526+
self._template = None
527+
528+
@staticmethod
529+
def from_dict(d):
530+
return MSTemplateCompartment(d['id'], d['name'], d['pH'], d['hierarchy'], d['aliases'])
531+
532+
def get_data(self):
533+
return {
534+
'id': self.id,
535+
'name': self.name,
536+
'pH': self.ph,
537+
'aliases': self.aliases,
538+
'hierarchy': self.hierarchy
539+
}
540+
541+
526542
class MSTemplate:
527543

528544
def __init__(self, template_id, name='', domain='', template_type='', version=1, info=None, args=None):
529545
self.id = template_id
530-
self.__VERSION__ = version
531546
self.name = name
532547
self.domain = domain
533-
self.biochemistry_ref = ''
534548
self.template_type = template_type
535-
self.compartments = {}
549+
self.__VERSION__ = version
550+
self.biochemistry_ref = ''
551+
self.compartments = DictList()
536552
self.biomasses = DictList()
537553
self.reactions = DictList()
538554
self.compounds = DictList()
@@ -541,7 +557,21 @@ def __init__(self, template_id, name='', domain='', template_type='', version=1,
541557
self.complexes = DictList()
542558
self.pathways = DictList()
543559
self.subsystems = DictList()
544-
#self.info = info if info else KBaseObjectInfo(object_type='KBaseFBA.NewModelTemplate')
560+
561+
def add_compartments(self, compartments: list):
562+
"""
563+
564+
:param compartments:
565+
:return:
566+
"""
567+
duplicates = list(filter(lambda x: x.id in self.compartments, compartments))
568+
if len(duplicates) > 0:
569+
logger.error("unable to add compartments [%s] already present in the template", duplicates)
570+
return None
571+
572+
for x in compartments:
573+
x._template = self
574+
self.compartments += compartments
545575

546576
def add_roles(self, roles: list):
547577
"""
@@ -693,32 +723,38 @@ def get_role(self, id):
693723
# return super()._to_object(key, data)
694724

695725
def get_data(self):
696-
compartments = []
726+
"""
727+
typedef structure {
728+
modeltemplate_id id;
729+
string name;
730+
string type;
731+
string domain;
732+
Biochemistry_ref biochemistry_ref;
733+
list < TemplateRole > roles;
734+
list < TemplateComplex > complexes;
735+
list < TemplateCompound > compounds;
736+
list < TemplateCompCompound > compcompounds;
737+
list < TemplateCompartment > compartments;
738+
list < NewTemplateReaction > reactions;
739+
list < NewTemplateBiomass > biomasses;
740+
list < TemplatePathway > pathways;
741+
} NewModelTemplate;
742+
"""
743+
697744
return {
698745
'__VERSION__': self.__VERSION__,
699746
'id': self.id,
700747
'name': self.name,
701748
'domain': self.domain,
702749
'biochemistry_ref': self.biochemistry_ref,
703750
'type': 'Test',
704-
'biomasses': [],
705-
'compartments': [{'aliases': [],
706-
'hierarchy': 3,
707-
'id': 'c',
708-
'index': '0',
709-
'name': 'Cytosol',
710-
'pH': 7},
711-
{'aliases': [],
712-
'hierarchy': 0,
713-
'id': 'e',
714-
'index': '1',
715-
'name': 'Extracellular',
716-
'pH': 7}],
751+
'compartments': list(map(lambda x: x.get_data(), self.compartments)),
717752
'compcompounds': list(map(lambda x: x.get_data(), self.compcompounds)),
718753
'compounds': list(map(lambda x: x.get_data(), self.compounds)),
719754
'roles': list(map(lambda x: x.get_data(), self.roles)),
720755
'complexes': list(map(lambda x: x.get_data(), self.complexes)),
721756
'reactions': list(map(lambda x: x.get_data(), self.reactions)),
757+
'biomasses': list(self.biomasses),
722758
'pathways': [],
723759
'subsystems': [],
724760
}
@@ -786,7 +822,7 @@ def __init__(self, template_id, name='', domain='', template_type='', version=1,
786822
self.biochemistry_ref = None
787823

788824
@staticmethod
789-
def from_dict(d):
825+
def from_dict(d, info=None, args=None):
790826
"""
791827
792828
:param d:
@@ -873,26 +909,9 @@ def with_compartment(self, cmp_id, name, ph=7, index='0'):
873909
return self
874910

875911
def build(self):
876-
base = {
877-
'__VERSION__': '',
878-
'biochemistry_ref': '',
879-
'biomasses': [],
880-
'compartments': self.compartments,
881-
'compcompounds': [],
882-
'complexes': [],
883-
'compounds': [],
884-
'domain': '',
885-
'id': '',
886-
'name': '',
887-
'pathways': [],
888-
'reactions': [],
889-
'roles': [],
890-
'subsystems': [],
891-
'type': ''
892-
}
893-
894912
template = MSTemplate(self.id, self.name, self.domain, self.template_type, self.version)
895-
template.add_compounds(list(map(lambda x: NewModelTemplateCompound.from_dict(x), self.compounds)))
913+
template.add_compartments(list(map(lambda x: MSTemplateCompartment.from_dict(x), self.compartments)))
914+
template.add_compounds(list(map(lambda x: MSTemplateMetabolite.from_dict(x), self.compounds)))
896915
template.add_comp_compounds(
897916
list(map(lambda x: MSTemplateSpecies.from_dict(x), self.compartment_compounds)))
898917
template.add_roles(list(map(lambda x: NewModelTemplateRole.from_dict(x), self.roles)))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"pyeda >= 0.28.0",
2727
"cobra >= 0.17.1",
2828
"scikit-learn == 0.23.2", # too support KBase pickle models
29-
"scipy >= 1.5.4"
29+
"scipy >= 1.5.4",
3030
"chemicals >= 1.0.13"
3131
]
3232
)

0 commit comments

Comments
 (0)