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

时间:2017-12-11 14:34:41

标签: python xml attributes

亲爱的Stack Overflow社区,

我的目标: 我从uniprot服务器下载了uniprot.xml文件(参见下面的示例行,示例文件:https://drive.google.com/open?id=1EI8jTvs4hzZ4XMINnzcsPVQlZLDvdFxPXug7FYJVG-U),我想提取具体信息(例如“dbReference id =”GO:0006355“type =”GO “”)列表中的特定登录号(例如Q6GZX4)。

<.... >
< ....>
<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),因此我必须逐步将元素加载到内存中,检查我是否对应于列表中的特定登录号并提取GO-Terms等特定属性。我目前使用interparse。

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

(a)我看不到我的入藏号码(返回一个空列表) - 为什么这个元素是空的?

(b)即使我使用不同的标识符dbreference id =“Q6GZX4”,我也不知道如何仅提取或解析特定于该标识符的元素,例如只有dbReference id =“Q6GZX4”的GO-Terms,而不是我整个.xml文件中的以下条目。

我用“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 ......

感谢任何帮助!

Rhaenna

0 个答案:

没有答案