Python 如何从其他格式(如graphml)导出sbml文件?

Python 如何从其他格式(如graphml)导出sbml文件?,python,r,networkx,igraph,sbml,Python,R,Networkx,Igraph,Sbml,因此,如前所述,我正在尝试找到一种方法,将一个graphml文件(或其他格式,如xgmml、csv、edgelist)从networkx或igraph(python或R)转换成这种格式 我相信应该有一个简单的方法,但是…我找不到。有什么想法吗 编辑:有一些可以用来最终降落在SBML星球上,但我仍然不知道如何导出到其中任何一个 编辑II:我发布了一个与SBML和Cytoscape相关的问题,因此……可能对其他对该主题感兴趣的人有用。SBML主要是编码过程或基于反应的模型。此类模型对应的网络图是一个

因此,如前所述,我正在尝试找到一种方法,将一个graphml文件(或其他格式,如xgmml、csv、edgelist)从networkx或igraph(python或R)转换成这种格式

我相信应该有一个简单的方法,但是…我找不到。有什么想法吗

编辑:有一些可以用来最终降落在SBML星球上,但我仍然不知道如何导出到其中任何一个


编辑II:我发布了一个与SBML和Cytoscape相关的问题,因此……可能对其他对该主题感兴趣的人有用。

SBML主要是编码过程或基于反应的模型。此类模型对应的网络图是一个二部图,即图中有两类节点(反应和物种),物种和反应节点之间只有边,但物种或物种反应之间没有边。 SBML中的一个重要概念是反应中的化学计量,它基本上是一个边缘属性,定义了物种在相应反应中的发生方式

因此,对于可转换为SBML的图,它们必须遵循一定的结构,即它们必须是边上具有化学计量信息的反应节点和物种节点的二部有向图

例如,可以使用libsbml或JSBML(这两个库都用于操作SBML)轻松地将这些图转换为SBML。 我在下面附上了一个来自libsbml的python绑定示例

"""
Converts simple bipartite species-reaction graph to SBML
using python bindings in libsbml

requirements:
    pip install python-libsbml networkx
"""
import networkx as nx
import libsbml

'''
Create bipartite networkx graph consisting of species and reaction nodes.
Edges require stoichiometry (or set to 1 otherwise).
'''
G = nx.DiGraph()

# add species nodes
G.add_node("S1", ntype="specie")
G.add_node("S2", ntype="specie")
G.add_node("S3", ntype="specie")
G.add_node("S4", ntype="specie")
G.add_node("S5", ntype="specie")
G.add_node("S6", ntype="specie")

# add reaction nodes (and reaction edges)
G.add_node("r1", ntype="reaction")  # 2 S1 -> S2
G.add_edges_from([
    ("S1", "r1", {'stoichiometry': 2}),
    ("r1", "S2", {'stoichiometry': 1})])
G.add_node("r2", ntype="reaction")  # S2 -> S3
G.add_edges_from([
    ("S2", "r2", {'stoichiometry': 1}),
    ("r2", "S3", {'stoichiometry': 1})])
G.add_node("r3", ntype="reaction")  # S3 + S4 -> S5 + S6
G.add_edges_from([
    ("S3", "r3", {'stoichiometry': 1}),
    ("S4", "r3", {'stoichiometry': 1}),
    ("r3", "S5", {'stoichiometry': 1}),
    ("r3", "S6", {'stoichiometry': 1})
])

print(G)
for sid, n in G.nodes.items():
    print(sid, n)
for sid, e in G.edges.items():
    print(sid, e)

'''
Create SBML model from the graph
'''
doc = libsbml.SBMLDocument()  # type: libsbml.SBMLDocument
model = doc.createModel()  # type: libsbml.Model
model.setId("graph_model")
# create species
for sid, n in G.nodes.items():
    print(sid, n)
    if n['ntype'] == "specie":
        s = model.createSpecies()  # type: libsbml.Species
        s.setId(sid)

# create reactions
for sid, n in G.nodes.items():
    if n['ntype'] == "reaction":
        r = model.createReaction()  # type: libsbml.Reaction
        r.setId(sid)
        for reactant_id in G.predecessors(sid):

            stoichiometry = G.edges[reactant_id, sid]['stoichiometry']
            reactant = model.getSpecies(reactant_id)
            r.addReactant(reactant, stoichiometry)

        for product_id in G.successors(sid):
            product = model.getSpecies(product_id)
            stoichiometry = G.edges[sid, product_id]['stoichiometry']
            r.addProduct(product, stoichiometry)

# serialization
sbml_str = libsbml.writeSBMLToString(doc)
print("-" * 80)
print(sbml_str)
libsbml.writeSBMLToFile(doc, "graph2sbml.xml")

与输出

S1 {'ntype': 'specie'}
S2 {'ntype': 'specie'}
S3 {'ntype': 'specie'}
S4 {'ntype': 'specie'}
S5 {'ntype': 'specie'}
S6 {'ntype': 'specie'}
r1 {'ntype': 'reaction'}
r2 {'ntype': 'reaction'}
r3 {'ntype': 'reaction'}
('S1', 'r1') {'stoichiometry': 2}
('S2', 'r2') {'stoichiometry': 1}
('S3', 'r3') {'stoichiometry': 1}
('S4', 'r3') {'stoichiometry': 1}
('r1', 'S2') {'stoichiometry': 1}
('r2', 'S3') {'stoichiometry': 1}
('r3', 'S5') {'stoichiometry': 1}
('r3', 'S6') {'stoichiometry': 1}
S1 {'ntype': 'specie'}
S2 {'ntype': 'specie'}
S3 {'ntype': 'specie'}
S4 {'ntype': 'specie'}
S5 {'ntype': 'specie'}
S6 {'ntype': 'specie'}
r1 {'ntype': 'reaction'}
r2 {'ntype': 'reaction'}
r3 {'ntype': 'reaction'}
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version2/core" level="3" version="2">
  <model id="graph_model">
    <listOfSpecies>
      <species id="S1"/>
      <species id="S2"/>
      <species id="S3"/>
      <species id="S4"/>
      <species id="S5"/>
      <species id="S6"/>
    </listOfSpecies>
    <listOfReactions>
      <reaction id="r1">
        <listOfReactants>
          <speciesReference species="S1" stoichiometry="2" constant="true"/>
        </listOfReactants>
        <listOfProducts>
          <speciesReference species="S2" stoichiometry="1" constant="true"/>
        </listOfProducts>
      </reaction>
      <reaction id="r2">
        <listOfReactants>
          <speciesReference species="S2" stoichiometry="1" constant="true"/>
        </listOfReactants>
        <listOfProducts>
          <speciesReference species="S3" stoichiometry="1" constant="true"/>
        </listOfProducts>
      </reaction>
      <reaction id="r3">
        <listOfReactants>
          <speciesReference species="S3" stoichiometry="1" constant="true"/>
          <speciesReference species="S4" stoichiometry="1" constant="true"/>
        </listOfReactants>
        <listOfProducts>
          <speciesReference species="S5" stoichiometry="1" constant="true"/>
          <speciesReference species="S6" stoichiometry="1" constant="true"/>
        </listOfProducts>
      </reaction>
    </listOfReactions>
  </model>
</sbml>
S1{'ntype':'specie'}
S2{'ntype':'specie'}
S3{'ntype':'specie'}
S4{'ntype':'specie'}
S5{'ntype':'specie'}
S6{'ntype':'specie'}
r1{'ntype':'reaction'}
r2{'ntype':'reaction'}
r3{'ntype':'reaction'}
('S1','r1'){'化学计量学]:2}
('S2','r2'){'化学计量学]:1}
('S3','r3'){'s化学计量比:1}
('S4','r3'){'s化学计量比:1}
('r1','S2'){'化学计量学]:1}
('r2','S3'){'化学计量学]:1}
('r3','S5'){'s化学计量比:1}
('r3','S6'){'化学计量学]:1}
S1{'ntype':'specie'}
S2{'ntype':'specie'}
S3{'ntype':'specie'}
S4{'ntype':'specie'}
S5{'ntype':'specie'}
S6{'ntype':'specie'}
r1{'ntype':'reaction'}
r2{'ntype':'reaction'}
r3{'ntype':'reaction'}
--------------------------------------------------------------------------------
然后可以使用Cytoscape中的cy3sbml等工具可视化SBML

可能help@TalhaJunaid嗯,我浏览了您建议的链接,但看起来您必须已经在smbl中拥有了网络。您需要使用
libsbml
中的实用程序从networkx创建sbml。好的,我知道了,但是任何指向smbl.org webiste的链接都不是很有用。你知道怎么做吗?不,还没有。我正在尝试其他一些可能性。还在弄清楚这个SBML是如何工作的!