compartments.py 2.59 KB
Newer Older
Peter Schubert's avatar
Peter Schubert committed
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
"""Implementation of Compartment components.

Peter Schubert, HHU Duesseldorf, October 2020
"""
import pandas as pd

import libsbml

from .sbase import SBase

# Explore_SBML_import_export_2020-10-05.ipynb

class ListOfCompartments(SBase):

    def __init__(self):
        self.compartments = []
        super().__init__()

    def import_sbml(self, sbml_model):
        sbml_lc = sbml_model.getListOfCompartments()
        for sbml_c in sbml_lc:
            c = Compartment()
            c.import_sbml(sbml_c)
            self.compartments.append(c)
        super().import_sbml(sbml_lc)

    def export_sbml(self, sbml_model):
        for c in self.compartments:
            c.export_sbml(sbml_model)
        super().export_sbml(sbml_model.getListOfCompartments())

    def to_df(self):
        return pd.DataFrame([c.to_df() for c in self.compartments])

    def from_df(self, lc_df):
        for idx, c_s in lc_df.iterrows():
            c = Compartment()
            c.from_df(c_s.dropna().to_dict())
            self.compartments.append(c)


class Compartment(SBase):

    def __init__(self):
        super().__init__()

    def import_sbml(self, sbml_c):
        if sbml_c.isSetSpatialDimensions():
            self.spatial_dim = sbml_c.getSpatialDimensionsAsDouble()
        if sbml_c.isSetSize():
            self.size = sbml_c.getSize()
        if sbml_c.isSetUnits():
            self.units = sbml_c.getUnits()
        self.constant = sbml_c.getConstant()
        super().import_sbml(sbml_c)

    def export_sbml(self, sbml_model):
        sbml_c = sbml_model.createCompartment()
        if hasattr(self, 'spatial_dim'):
            sbml_c.setSpatialDimensions(self.spatial_dim)
        if hasattr(self, 'size'):
            sbml_c.setSize(self.size)
        if hasattr(self, 'units'):
            sbml_c.setUnits(self.units)
        sbml_c.setConstant(self.constant)
        super().export_sbml(sbml_c)

    def to_df(self):
        c_dict = super().to_df()
        if hasattr(self, 'spatial_dim'):
            c_dict['spatialDimension'] = self.spatial_dim
        if hasattr(self, 'size'):
            c_dict['size'] = self.size
        if hasattr(self, 'units'):
            c_dict['units'] = self.units
        c_dict['constant'] = self.constant
        return c_dict

    def from_df(self, c_dict):
        if 'spatialDimension' in c_dict:
            self.spatial_dim = float(c_dict['spatialDimension'])
        if 'size' in c_dict:
            self.size = float(c_dict['size'])
        if 'units' in c_dict:
            self.units = c_dict['units']
        self.constant = (c_dict['constant']==str(True))
        super().from_df(c_dict)