XML:返回跟踪父元素

时间:2011-09-25 16:29:28

标签: python xml xml-parsing elementtree

我正在寻找解决我在python中与XML相关的问题。虽然 spectrum 不是根元素,但我们假设它是针对此示例的。

<spectrum index="2" id="controller=0 scan=3" defaultArrayLength="485">
          <cvParam cvRef="MS" accession="MS:1000511" name="ms level" value="2"/>
          <cvParam cvRef="MS" accession="MS:1000580" name="MSn spectrum" value=""/>
          <cvParam cvRef="MS" accession="MS:1000127" name="centroid mass spectrum" value=""/>
          <precursorList count="1">
            <precursor spectrumRef="controller=0 scan=2">
              <isolationWindow>
                <cvParam cvRef="MS" accession="MS:1000040" name="m/z" value="810.78999999999996"/>
                <cvParam cvRef="MS" accession="MS:1000023" name="isolation width" value="2"/>
              </isolationWindow>
              <selectedIonList count="1">
                <selectedIon>
                  <cvParam cvRef="MS" accession="MS:1000040" name="m/z" value="810.78999999999996"/>
                </selectedIon>
              </selectedIonList>
              <activation>
                <cvParam cvRef="MS" accession="MS:1000133" name="collision-induced dissociation" value=""/>
                <cvParam cvRef="MS" accession="MS:1000045" name="collision energy" value="35"/>
              </activation>
            </precursor>
          </precursorList>
          <binaryDataArrayList count="2">
            <binaryDataArray encodedLength="5176">
              <cvParam cvRef="MS" accession="MS:1000523" name="64-bit float" value=""/>
              <cvParam cvRef="MS" accession="MS:1000576" name="no compression" value=""/>
              <cvParam cvRef="MS" accession="MS:1000514" name="m/z array" value="" unitCvRef="MS" unitAccession="MS:1000040" unitName="m/z"/>
              <binary>AAAAYHHsbEAAAADg3yptQAAAAECt7G1AAAAAAN8JbkAAAAAA.......hLJ==</binary>
            </binaryDataArray>
            <binaryDataArray encodedLength="2588">
              <cvParam cvRef="MS" accession="MS:1000521" name="32-bit float" value=""/>
              <cvParam cvRef="MS" accession="MS:1000576" name="no compression" value=""/>
              <cvParam cvRef="MS" accession="MS:1000515" name="intensity array" value=""/>
              <binary>ZFzUQWmVo0FH/o9BRfUyQg+xjUOzkZdC5k66QWk6HUSpqyZCsV1NQ......uH=</binary>
            </binaryDataArray>
          </binaryDataArrayList>
</spectrum>

我想要实现的是在树中找到所有 selectedIon 元素并回溯它的父元素谱。如果找到selectedIon元素,那么

  
    

SelectedIon信息:

         
         

质量:810.78999999999996

Spectra Info:
-------------
index=2
id=controller=0
scan=3
length=485

General Info
------------
ms level=2
Msn spectrum= -
centriod mass spectrum=-
.....................
And all the cvParam name and value as above. 

Binary
------
m/z array = AAAAYHHsbEAAAADg3yptQAAAAECt7G1AAAA.....== 

intensity array = ZFzUQWmVo0FH/o9BRfUyQg+xjUOzkZdC5k66Q....5C77=
  

到目前为止我尝试了什么:

import xml.etree.ElementTree as ET
tree=ET.parse('file.mzml')
NS="{http://psi.hupo.org/ms/mzml}"
filesource=tree.findall('.//'+NS+'selectedIon') # Will get all selectedIon element from the tree

现在我如何回溯到频谱元素/子元素以解析上述相关信息?

我怎么能成功?

2 个答案:

答案 0 :(得分:1)

XPath将允许您访问祖先:“ancestor :: spectrum”将返回您包含的<spectrum>元素。如果使用lxml,则可以使用完整的XPath语法来查找所需的元素。

from lxml import etree
tree = etree.XML('file.mzml')
NS = "{http://psi.hupo.org/ms/mzml}"
filesource = tree.findall('.//'+NS+'selectedIon')
spectrum = filesource.xpath('ancestor::spectrum')[0]

(我认为,未经测试......)

更新:实际运作的代码:

from lxml import etree

tree = etree.parse('foo.xml')
for el in tree.findall(".//selectedIon"):
    for top in el.xpath("ancestor::spectrum"):
        print top

答案 1 :(得分:0)

如果这仍然是当前问题,您可以尝试pymzML,一个mzML文件的python接口。

打印所有MS2光谱中的所有信息非常简单:

import pymzml
msrun = pymzml.run.Reader("your-file.mzML")
for spectrum in msrun:
    if spectrum['ms level'] == 2:
        # spectrum is a dict, so you can just print it        
        print(spectrum)

(披露:我是其中一位作者)