escher icon indicating copy to clipboard operation
escher copied to clipboard

Plot FBA solutions in Escher with COBRAPy 0.6.1

Open rpelicae opened this issue 8 years ago • 3 comments

When following the Jupyter Notebook availabe on the Escher website, I noticed an error when trying to plot an FBA solution on the E coli core pathway map. Starting with: ecoli = cobra.test.create_test_model("textbook") solution = ecoli.optimize()

b = escher.Builder(map_name='e_coli_core.Core metabolism', reaction_data=solution.x_dict) b.display_in_browser() didn't work

import pandas b = Builder(map_name='e_coli_core.Core metabolism', reaction_data=solution.fluxes.to_dict()) b.display_in_browser() did work

as COBRAPy 0.6.1 solution.fluxes returns a pandas Series object

rpelicae avatar May 16 '17 13:05 rpelicae

That's curious. Did you get an error message with the first attempt, or just a map without data on it?

zakandrewking avatar May 16 '17 17:05 zakandrewking

I got an error message with the server not starting:

>>> import cobra
>>> import cobra.test
>>> import escher
>>> ecoli = cobra.test.create_test_model("textbook")
>>> solution = ecoli.optimize()
>>> b = escher.Builder(map_name='e_coli_core.Core metabolism',
...                    reaction_data=solution.x_dict,
...                    # change the default colors
...                    reaction_scale=[{'type': 'min', 'color': '#cccccc', 'size': 4},
...                                    {'type': 'mean', 'color': '#0000dd', 'size': 20},
...                                    {'type': 'max', 'color': '#ff0000', 'size': 40}],
...                    # only show the primary metabolites
...                    hide_secondary_metabolites=True)
>>> b.display_in_browser()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/escher/plots.py", line 725, in display_in_browser
    never_ask_before_quit=never_ask_before_quit)
  File "/usr/local/lib/python2.7/site-packages/escher/plots.py", line 606, in _get_html
    options_json=json_dump_and_escape(options),
  File "/usr/local/lib/python2.7/site-packages/escher/escape.py", line 17, in json_dump_and_escape
    return _escape_for_embedding(json.dumps(data))
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: ACALD                  0.000000
ACALDt                 0.000000
ACKr                   0.000000
ACONTa                 6.007250
ACONTb                 6.007250
ACt2r                  0.000000
ADK1                   0.000000
AKGDH                  5.064376
AKGt2r                 0.000000
ALCD2x                 0.000000
ATPM                   8.390000
ATPS4r                45.514010
Biomass_Ecoli_core     0.873922
CO2t                 -22.809833
CS                     6.007250
CYTBD                 43.598985
D_LACt2                0.000000
ENO                   14.716140
ETOHt2r                0.000000
EX_ac_e                0.000000
EX_acald_e             0.000000
EX_akg_e               0.000000
EX_co2_e              22.809833
EX_etoh_e              0.000000
EX_for_e               0.000000
EX_fru_e               0.000000
EX_fum_e               0.000000
EX_glc__D_e          -10.000000
EX_gln__L_e            0.000000
EX_glu__L_e            0.000000
                        ...    
ME2                    0.000000
NADH16                38.534610
NADTRHD                0.000000
NH4t                   4.765319
O2t                   21.799493
PDH                    9.282533
PFK                    7.477382
PFL                    0.000000
PGI                    4.860861
PGK                  -16.023526
PGL                    4.959985
PGM                  -14.716140
PIt2r                  3.214895
PPC                    2.504309
PPCK                   0.000000
PPS                    0.000000
PTAr                   0.000000
PYK                    1.758177
PYRt2                  0.000000
RPE                    2.678482
RPI                   -2.281503
SUCCt2_2               0.000000
SUCCt3                 0.000000
SUCDi                  5.064376
SUCOAS                -5.064376
TALA                   1.496984
THD2                   0.000000
TKT1                   1.496984
TKT2                   1.181498
TPI                    7.477382
Name: fluxes, Length: 95, dtype: float64 is not JSON serializable
>>> 

rpelicae avatar May 17 '17 08:05 rpelicae

For me this error disappears when I add a line forcing the fluxes to regular floats: for key in d.keys(): # also removes output like this: 1.653e-15 d[key] = round(float(d[key]),6)

ThierryMondeel avatar May 17 '17 09:05 ThierryMondeel