Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python XML:回溯父元素_Python_Xml_Xml Parsing_Elementtree - Fatal编程技术网

Python XML:回溯父元素

Python XML:回溯父元素,python,xml,xml-parsing,elementtree,Python,Xml,Xml Parsing,Elementtree,我正在寻找python中与XML相关的问题的解决方案。虽然光谱不是根元素,但我们假设它是本例的根元素 <spectrum index="2" id="controller=0 scan=3" defaultArrayLength="485"> <cvParam cvRef="MS" accession="MS:1000511" name="ms level" value="2"/> <cvParam cvRef="MS" ac

我正在寻找python中与XML相关的问题的解决方案。虽然光谱不是根元素,但我们假设它是本例的根元素

<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>
到目前为止我所尝试的:

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
现在如何回溯到光谱元素/子元素以解析出上述相关信息?


如何才能成功?

XPath将允许您访问祖先:“祖先::光谱”将返回包含在其中的
元素。如果使用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

如果这仍然是当前的问题,您可以尝试使用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)

(披露:我是作者之一)

你为什么不走另一条路?i、 循环频谱元素,如果它有selectedIon元素,则输出。我试图只解析有selectedIon的频谱元素。换一种方式将加载所有可能未选择的spectrum元素。当然,如果是这种情况,您可以跳过该spectrum元素并转到下一个。嗯,xpath是基于一个的,
xpath('祖先::spectrum')[1]
。您还可以直接选择具有selectedIon子项的所有spectrum:
//spectrum[.//selectedIon]
我认为filesource=tree.findall('./'+NS+'selectedIon')创建列表,并且列表没有属性xpath@四十二://spectrum[.//selectedIon]表达式将选择所有selectedIon。但同样的问题是如何解析spectrum和其他spectrum元素的信息?@四十二:XPath是基于1的,但它返回的列表是Python列表,因此您需要在[0]处对其进行索引,以从中获取第一个(也是唯一一个)元素。@thchand:我添加了一个有效的代码段。您需要自己处理名称空间。
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)