JSON format

11 views
Skip to first unread message

Gui GuiGui

unread,
Jun 28, 2022, 10:26:00 AM6/28/22
to libsbml-development
Dear libsbml team,
I would like to know if you're planning to build a gateway between SBML and JSON format (import from JSON, export to JSON).
This feature will be very helpful to chunk a model in small functional parts and interoperate the model or the subsets with other tools.
It represents a big and long work, but I can participate to add this feature into libsbml.
What do you think ?
Best

Herbert M Sauro

unread,
Jun 28, 2022, 11:22:40 AM6/28/22
to libsbml-d...@googlegroups.com
There are json <-> xml that converters, would they be of any help to you? I don’t know how good they are however.



--
You received this message because you are subscribed to the Google Groups "libsbml-development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libsbml-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/libsbml-development/82d960c0-b295-48fd-ac49-81fe5ee0ff7fn%40googlegroups.com.
--
Herbert Sauro, Professor
University of Washington, Bioengineering
Mobile: 206-880-8093

Gui GuiGui

unread,
Jul 1, 2022, 11:06:13 AM7/1/22
to libsbml-development
I have tested converters with python as you suggested @Herbert Sauro :
- sbml to json: xmljson package, https://pypi.org/project/xmljson/
- json to sbml: json2xml package, https://pypi.org/project/json2xml/

Converting e_coli_core.xml to json format, I got this:

{
    "{http://www.sbml.org/sbml/level3/version1/core}sbml": {
        "@level": 3,
...
        "@{http://www.sbml.org/sbml/level3/version1/fbc/version2}required": false,
        "{http://www.sbml.org/sbml/level3/version1/core}model": {
            "@{http://www.sbml.org/sbml/level3/version1/fbc/version2}strict": true,
            "@id": "e_coli_core",
            "{http://www.sbml.org/sbml/level3/version1/core}listOfUnitDefinitions": {
                "{http://www.sbml.org/sbml/level3/version1/core}unitDefinition": {
                    "@id": "mmol_per_gDW_per_hr",
                    "{http://www.sbml.org/sbml/level3/version1/core}listOfUnits": {
                        "{http://www.sbml.org/sbml/level3/version1/core}unit": [
                            {
                                "@kind": "mole",
...
            "{http://www.sbml.org/sbml/level3/version1/core}listOfSpecies": {
                "{http://www.sbml.org/sbml/level3/version1/core}species": [
                    {
                        "@id": "M_glc__D_e",
...
                        "@{http://www.sbml.org/sbml/level3/version1/fbc/version2}charge": 0,

It seems to be good, no loss of data. But if I want to access to the species this is the command:


and loading data into dataframe:

df = pd.DataFrame.from_dict(species)

It's ugly, but it works.
But when I want to come back into xml, the new xml looks like this:

<?xml version="1.0" ?>
<all>
    <key name="{http://www.sbml.org/sbml/level3/version1/core}sbml">
        <key name="@level">3</key>
        <key name="@version">1</key>
        <key name="@sboTerm">SBO:0000624</key>
        <key name="@{http://www.sbml.org/sbml/level3/version1/fbc/version2}required">false</key>
        <key name="{http://www.sbml.org/sbml/level3/version1/core}model">

The format is lost.
Have you plan to include this feature into a roadmap ? Do you think it could be helpful ?

Herbert M Sauro

unread,
Jul 1, 2022, 12:58:18 PM7/1/22
to libsbml-d...@googlegroups.com
That doesn't look great. It might be easier to write your own python script to take an SBML XML and convert it to json. Both describe hierarchies so it shouldn't be too hard. One idea would be to create a class that represents an XML model, then use a JSON serializer to serialize it out to json. That might not be too difficult to do. You could also do the reverse. You could start with a minimal SBML model and go from there, eg initially just support species, parameters and reactions. 

Herbert Sauro

Frank T. Bergmann

unread,
Jul 1, 2022, 1:08:23 PM7/1/22
to libsbml-development
I agree, a custom object model in whatever language you are looking for might be best. I understand why the object structure from xml2json is what it is, but it is probably not what you want people to use. while xml namespaces are needed for identifying things, they are not nice to read. 

There have been other json like formats in the community for a while, there was the Escher converter by the BIGG community, that you might to want to have a look at. But i believe those also were basically one way converters from SBML to Json. 

Personally, I dont see the need to choose a different serialization format for SBML, xml is what all the tools that accept SBML understand. So i dont see reading / writing json as something that libSBML should do (it would only become yet more complicated than it already is). But it could be something that a library using libsbml can do. If you do want to go that route, i'm happy to give pointers. Sarah added a generic interface to all SBML objects to query properties and get / set attributes. So a generic way to walk over the libsbml objects, to generate json would be easily accomplished. The difficult part would be the way back, from json to xml. Since you have to keep all the vital information, while not sacrificing readability. 

Cheers
Frank

Gui GuiGui

unread,
Jul 1, 2022, 3:36:38 PM7/1/22
to libsbml-development
Many thanks for your advices
I'll get you in touch when I will begin the code,

Matthias König

unread,
Jul 4, 2022, 5:33:05 AM7/4/22
to libsbml-development
In the constrained based model community there were/are some efforts to convert SBML to JSON for serialization. I was part of some of them so if you are only interested in constrainted based models there are solutions already (i.e. SBML core level 3 + fbc package).
I can only highly recommend to not touch JSON serialization, but stick to XML/SBML. One argument is JSON serialization for the web, but you can just consume the XML with Javascript and you are good.
To give an example of all the problems you can have a look at the cobrapy issue tracker and basically half of the issues are related to incorrect/problematic JSON encoding.
Main issues coming up are invalid SIds, because JSON is not enforcing the ids of SBML (you see this problem a lot with BiGG models) and problems with NaN which cannot be encoded in JSON. As a consequence ids are replaced with prefixes which creates a ton of new issues.
Also validation becomes a big issue. You have very good validation for SBML, but would need to create a JSON schema and validator for your SBML files.

In summary, don't do JSON serialization of SBML if your life not absolutely depends on it. The long-term issue and maintenance are a nightmare.

Herbert M Sauro

unread,
Jul 4, 2022, 3:45:32 PM7/4/22
to libsbml-d...@googlegroups.com
Sounds like good advice.

Herbert

Reply all
Reply to author
Forward
0 new messages