使用interparse和xml文件的属性从xml文件中提取特定信息(Python)

2024-10-04 09:30:20 发布

您现在位置:Python中文网/ 问答频道 /正文

尊敬的Stack Overflow社区:

我的目标: 我已经下载了uniprot.xml文件文件(参见下面的示例行,示例文件:https://drive.google.com/open?id=1EI8jTvs4hzZ4XMINnzcsPVQlZLDvdFxPXug7FYJVG-U),我想提取特定信息(例如“dbReference^{id1})$

<.... >
< ....>
<entry>
  <accession>Q6GZX4</accession>
 <dbReference id="Q6GZX4" type="ProteinModelPortal"/>
 </dbReference>
  <dbReference id="GO:0006355" type="GO">
    <property value="P:regulation of transcription, DNA-templated" type="term"/>
  </dbReference>
</entry>
</uniprot>

我的步骤: 这个uniprot.xml文件是大的(6GB),因此我必须将元素增量加载到内存中,检查my是否对应于列表中的特定登录号,并提取特定属性,如GO Terms。我现在用interparse。你知道吗

我的问题(另请参阅下面的代码):

(a)我看不到我的注册号(返回一个空列表)-为什么这个元素是空的?你知道吗

(b)即使我使用了不同的标识符dbreference id=“Q6GZX4”,我也不知道如何只提取或解析特定于该标识符的元素,例如只提取dbreference^{id2}的GO术语$

我用“Q6GZX4”作为示例id(我的整个示例文件都托管在https://drive.google.com/open?id=1EI8jTvs4hzZ4XMINnzcsPVQlZLDvdFxPXug7FYJVG-U上)实现的目标

import xml.etree.cElementTree as etree
context = etree.iterparse('data_uniprot_short.xml', events = ("start", "end"))

for event, elem in context:

  # (a)
    print(elem.tag, elem.attrib)
    ## result: {http://uniprot.org/uniprot}accession {} ??
     ### here I see an empty accession number, and I cannot access this information like “Q6GZX4”?

   # (b)
   # ok, I use the dbreference id for "Q6GZX4", but how would I get all dbReference ids for type == "GO"?
          if event == "start" and elem.tag ==  "{http://uniprot.org/uniprot}dbReference":

        if elem.attrib["id"] =="Q6GZX4" and elem.attrib["type"]=="ProteinModelPortal":
            print(elem.tag, elem.attrib)
            # does not work!
            if elem.tag ==  "{http://uniprot.org/uniprot}dbReference" and elem.attrib["type"] == "GO":
                print(elem.tag, elem.attrib)

请注意,我想要类似于d={“QgGZX4”:GO:0006355….}的东西,并且我有许多不同uniprot id的条目。。。你知道吗

感谢您的帮助!你知道吗

瑞纳


Tags: and文件id元素go示例tagtype