目前,我正在设计一些糖生物学领域的格式转换工具。格式转换涉及从文本文件到该领域标准的 XML 文件。大多数时候,我们获得的数据包含如下纯文本文件中的感兴趣信息。实际文件在一行中包含所有这些内容。阅读和拆分此文本以获取信息是微不足道的(可能不直观),但 XML 才是问题所在。
[][b-D-GlcpNAc]
{[(4+1)][b-D-GlcpNAc]
{[(4+1)][b-D-Manp]
{[(3+1)][a-D-Manp]
{[(2+1)][a-D-Manp]{}
}
[(6+1)][a-D-Manp]
{[(3+1)][a-D-Manp]{}
[(6+1)][a-D-Manp]{}
}
}
}
如何解释:
- 所有 w-w-w+ 形式的东西都是与另一个糖相连的糖。链接由 curl 的 { 显示。
- 4+1、3+1 等表示一种糖上的哪些碳键与另一种糖结合。因此,前一个的第 4 个碳链接到后一个的第 1 个碳。
- {} 这表示没有额外的糖与该糖相关
- } curlies 只是关闭那一层。
您可能可以阅读 XML 并了解链接的工作原理。但是,如果你们想要更详细的解释,那就问吧。
XML 应该如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<GlydeII>
<molecule subtype="glycan" id="From_GlycoCT_Translation">
<residue subtype="base_type" partid="1" ref="http://www.monosaccharideDB.org/GLYDE-II.jsp?G=b-dglc-HEX-1:5" />
<residue subtype="substituent" partid="2" ref="http://www.monosaccharideDB.org/GLYDE-II.jsp?G=n-acetyl" />
<residue subtype="base_type" partid="3" ref="http://www.monosaccharideDB.org/GLYDE-II.jsp?G=b-dglc-HEX-1:5" />
<residue subtype="substituent" partid="4" ref="http://www.monosaccharideDB.org/GLYDE-II.jsp?G=n-acetyl" />
<residue subtype="base_type" partid="5" ref="http://www.monosaccharideDB.org/GLYDE-II.jsp?G=b-dman-HEX-1:5" />
<residue subtype="base_type" partid="6" ref="http://www.monosaccharideDB.org/GLYDE-II.jsp?G=a-dman-HEX-1:5" />
<residue subtype="base_type" partid="7" ref="http://www.monosaccharideDB.org/GLYDE-II.jsp?G=a-dman-HEX-1:5" />
<residue subtype="base_type" partid="8" ref="http://www.monosaccharideDB.org/GLYDE-II.jsp?G=a-dman-HEX-1:5" />
<residue subtype="base_type" partid="9" ref="http://www.monosaccharideDB.org/GLYDE-II.jsp?G=a-dman-HEX-1:5" />
<residue subtype="base_type" partid="10" ref="http://www.monosaccharideDB.org/GLYDE-II.jsp?G=a-dman-HEX-1:5" />
<residue_link from="2" to="1">
<atom_link from="N1H" to="C2" to_replace="O2" bond_order="1" />
</residue_link>
<residue_link from="3" to="1">
<atom_link from="C1" to="O4" from_replace="O1" bond_order="1" />
</residue_link>
<residue_link from="4" to="3">
<atom_link from="N1H" to="C2" to_replace="O2" bond_order="1" />
</residue_link>
<residue_link from="5" to="3">
<atom_link from="C1" to="O4" from_replace="O1" bond_order="1" />
</residue_link>
<residue_link from="6" to="5">
<atom_link from="C1" to="O3" from_replace="O1" bond_order="1" />
</residue_link>
<residue_link from="7" to="6">
<atom_link from="C1" to="O2" from_replace="O1" bond_order="1" />
</residue_link>
<residue_link from="8" to="5">
<atom_link from="C1" to="O6" from_replace="O1" bond_order="1" />
</residue_link>
<residue_link from="9" to="8">
<atom_link from="C1" to="O3" from_replace="O1" bond_order="1" />
</residue_link>
<residue_link from="10" to="8">
<atom_link from="C1" to="O6" from_replace="O1" bond_order="1" />
</residue_link>
</molecule>
</GlydeII>
到目前为止,我已经能够轻松获取所有剩余字段并将它们写入 XML。但是我什至在为 residue_link 字段编写伪代码时遇到了麻烦。即使我只能获得有关如何在 xml 中添加链接信息的帮助和想法,我也会很感激。
最佳答案
好的!很酷的问题,它以一种很好的方式伤害了我的大脑。
首先...为了我的理智,我将您的原始数据标记为一种有意义的方式:
[][b-D-GlcpNAc] {
[(4+1)][b-D-GlcpNAc] {
[(4+1)][b-D-Manp] {
[(3+1)][a-D-Manp] {
[(2+1)][a-D-Manp] { }
}
[(6+1)][a-D-Manp] {
[(3+1)][a-D-Manp] { }
[(6+1)][a-D-Manp] { }
}
}
}
我认为关键是弄清楚对是什么,并且您想以编程方式弄清楚您所处的级别。
伪代码:
hierarchy = 0
nextChar = getNextChar()
while (Parsing):
if (nextChar = "{"):
hierarchy += 1
elif (nextChar = "}"):
hierarchy -= 1
if (nextChar = "["):
storeSugar(hierarchy)
您还想跟踪哪个糖是以前的“父”糖。
关于python - 使用 DOM 从纯文本中提取信息并写入 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6616086/