本文介绍了R中的xml2:从父级提取子级属性(所有名称都相同)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下xml,其中节点可以具有相同的名称,但它们的属性可能不同.

I have the following xml, in which nodes can have the same names but their attributes may differ.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<protein-matches xmlns="http://www.ebi.ac.uk/interpro/resources/schemas/interproscan5">
    <protein>
        <sequence md5="6e7e4fcef214ab5cf97714e899af0b96">MATLRAMLKNAFILFLFTLTIMAKTVFSQQCGTTGCAANLCCSRYGYCGTTDAYCGTGCRSGPCSSSTTPIPPTPSGGAGGLNADPRDTIENVVTPAFFDGIMSKVGNGCPAKGFYTRQAFIAAAQSFDAYKGTVAKREIAAMLAQFSHESGSFCYKEEIARGKYCSPSTAYPCTPGKDYYGRGPIQITWNYNYGAAGKFLGLPLLTDPDMVARSPQVAFQCAMWFWNLNVRPVLDQGFGATTRKINGGECNGRRPAAVQSRVNYYLEFCRTLGITPGANLSC</sequence>
        <xref id="AT2G43620.1"/>
        <matches>
            <hmmer3-match evalue="1.5E-8" score="34.9">
                <signature ac="PF00187" desc="Chitin recognition protein" name="Chitin_bind_1">
                    <entry ac="IPR001002" desc="Chitin-binding, type 1" name="Chitin-bd_1" type="DOMAIN">
                        <go-xref category="MOLECULAR_FUNCTION" db="GO" id="GO:0008061" name="chitin binding"/>
                    </entry>
                    <models>
                        <model ac="PF00187" desc="Chitin recognition protein" name="Chitin_bind_1"/>
                    </models>
                    <signature-library-release library="PFAM" version="31.0"/>
                </signature>
                <locations>
                    <hmmer3-location env-end="64" env-start="31" score="34.9" evalue="1.5E-8" hmm-start="2" hmm-end="38" hmm-length="0" start="31" end="64"/>
                </locations>
            </hmmer3-match>
            <hmmer3-match evalue="1.4E-46" score="159.2">
                <signature ac="PF00182" desc="Chitinase class I" name="Glyco_hydro_19">
                    <entry ac="IPR000726" desc="Glycoside hydrolase, family 19, catalytic" name="Glyco_hydro_19_cat" type="DOMAIN">
                        <go-xref category="BIOLOGICAL_PROCESS" db="GO" id="GO:0016998" name="cell wall macromolecule catabolic process"/>
                        <go-xref category="MOLECULAR_FUNCTION" db="GO" id="GO:0004568" name="chitinase activity"/>
                        <go-xref category="BIOLOGICAL_PROCESS" db="GO" id="GO:0006032" name="chitin catabolic process"/>
                        <pathway-xref db="MetaCyc" id="PWY-7822" name="Chitin degradation III (Serratia)"/>
                        <pathway-xref db="MetaCyc" id="PWY-6855" name="Chitin degradation I (archaea)"/>
                        <pathway-xref db="MetaCyc" id="PWY-6902" name="Chitin degradation II (Vibrio)"/>
                        <pathway-xref db="KEGG" id="00520+3.2.1.14" name="Amino sugar and nucleotide sugar metabolism"/>
                    </entry>
                    <models>
                        <model ac="PF00182" desc="Chitinase class I" name="Glyco_hydro_19"/>
                    </models>
                    <signature-library-release library="PFAM" version="31.0"/>
                </signature>
                <locations>
                    <hmmer3-location env-end="228" env-start="94" score="131.4" evalue="4.3E-38" hmm-start="2" hmm-end="155" hmm-length="0" start="94" end="228"/>
                    <hmmer3-location env-end="283" env-start="237" score="28.0" evalue="1.7E-6" hmm-start="185" hmm-end="232" hmm-length="0" start="237" end="283"/>
                </locations>
            </hmmer3-match>
        </matches>
    </protein>
</protein-matches>

我想要的是为每个hmmer3-match提取signatureentrygo-xreflocations的属性.听起来很容易,我想应该是这样.

what I would like is to extract attributes for signature, entry, go-xref and locations per hmmer3-match. Sounds quite easy and I thought it would be so.

这是我尝试过的:

library(xml2)
res_xml <- xml2::read_xml(res)
hmmer3_match = xml_find_all(res_xml, "//*[name()='hmmer3-match']")

lapply(hmmer3_match, function(x) xml_find_all(x , "//*[name()='signature']"))
#output:
[[1]]
{xml_nodeset (2)}
[1] <signature ac="PF00187" desc="Chitin recognition protein" name="Chitin_bind_1">\n  <entry ac="IPR001002" desc="Chitin-binding, type 1" name="Chi" ...
[2] <signature ac="PF00182" desc="Chitinase class I" name="Glyco_hydro_19">\n  <entry ac="IPR000726" desc="Glycoside hydrolase, family 19, catalytic" ...

[[2]]
{xml_nodeset (2)}
[1] <signature ac="PF00187" desc="Chitin recognition protein" name="Chitin_bind_1">\n  <entry ac="IPR001002" desc="Chitin-binding, type 1" name="Chi" ...
[2] <signature ac="PF00182" desc="Chitinase class I" name="Glyco_hydro_19">\n  <entry ac="IPR000726" desc="Glycoside hydrolase, family 19, catalytic" ...

看起来它的比赛次数增加了一倍,因为:

looks like it doubled the matches since:

xml_find_all(hmmer3_match, "//*[name()='signature']")
#output
{xml_nodeset (2)}
[1] <signature ac="PF00187" desc="Chitin recognition protein" name="Chitin_bind_1">\n  <entry ac="IPR001002" desc="Chitin-binding, type 1" name="Chi" ...
[2] <signature ac="PF00182" desc="Chitinase class I" name="Glyco_hydro_19">\n  <entry ac="IPR000726" desc="Glycoside hydrolase, family 19, catalytic" ...

如果我尝试for循环,也会发生相同的事情:

same thing happens if I try a for loop:

hmmer3_match[1] #does select the first node
#but when I run:
signature = list()
for (i in 1: length(hmmer3_match)){
  signature[[i]] = xml_find_all(hmmer3_match[[i]] , "//*[name()='signature']")
}
#output is same as from `lapply`

我对此事的知识很有限,我觉得我缺少一些简单的东西吗?

My knowledge on the matter is quite limited and I feel I am missing something fairly simple?

我正在寻找一种通用解决方案,该解决方案可以在具有任意数量go-xreflocations的任意数量hmmer3-match节点上工作.

I am looking for a general solution that would work on any amount of hmmer3-match nodes having any amount of go-xref, locations.

谢谢.

推荐答案

使用相对XPath:

'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<protein-matches xmlns="http://www.ebi.ac.uk/interpro/resources/schemas/interproscan5">
    <protein>
        <sequence md5="6e7e4fcef214ab5cf97714e899af0b96">MATLRAMLKNAFILFLFTLTIMAKTVFSQQCGTTGCAANLCCSRYGYCGTTDAYCGTGCRSGPCSSSTTPIPPTPSGGAGGLNADPRDTIENVVTPAFFDGIMSKVGNGCPAKGFYTRQAFIAAAQSFDAYKGTVAKREIAAMLAQFSHESGSFCYKEEIARGKYCSPSTAYPCTPGKDYYGRGPIQITWNYNYGAAGKFLGLPLLTDPDMVARSPQVAFQCAMWFWNLNVRPVLDQGFGATTRKINGGECNGRRPAAVQSRVNYYLEFCRTLGITPGANLSC</sequence>
        <xref id="AT2G43620.1"/>
        <matches>
            <hmmer3-match evalue="1.5E-8" score="34.9">
                <signature ac="PF00187" desc="Chitin recognition protein" name="Chitin_bind_1">
                    <entry ac="IPR001002" desc="Chitin-binding, type 1" name="Chitin-bd_1" type="DOMAIN">
                        <go-xref category="MOLECULAR_FUNCTION" db="GO" id="GO:0008061" name="chitin binding"/>
                    </entry>
                    <models>
                        <model ac="PF00187" desc="Chitin recognition protein" name="Chitin_bind_1"/>
                    </models>
                    <signature-library-release library="PFAM" version="31.0"/>
                </signature>
                <locations>
                    <hmmer3-location env-end="64" env-start="31" score="34.9" evalue="1.5E-8" hmm-start="2" hmm-end="38" hmm-length="0" start="31" end="64"/>
                </locations>
            </hmmer3-match>
            <hmmer3-match evalue="1.4E-46" score="159.2">
                <signature ac="PF00182" desc="Chitinase class I" name="Glyco_hydro_19">
                    <entry ac="IPR000726" desc="Glycoside hydrolase, family 19, catalytic" name="Glyco_hydro_19_cat" type="DOMAIN">
                        <go-xref category="BIOLOGICAL_PROCESS" db="GO" id="GO:0016998" name="cell wall macromolecule catabolic process"/>
                        <go-xref category="MOLECULAR_FUNCTION" db="GO" id="GO:0004568" name="chitinase activity"/>
                        <go-xref category="BIOLOGICAL_PROCESS" db="GO" id="GO:0006032" name="chitin catabolic process"/>
                        <pathway-xref db="MetaCyc" id="PWY-7822" name="Chitin degradation III (Serratia)"/>
                        <pathway-xref db="MetaCyc" id="PWY-6855" name="Chitin degradation I (archaea)"/>
                        <pathway-xref db="MetaCyc" id="PWY-6902" name="Chitin degradation II (Vibrio)"/>
                        <pathway-xref db="KEGG" id="00520+3.2.1.14" name="Amino sugar and nucleotide sugar metabolism"/>
                    </entry>
                    <models>
                        <model ac="PF00182" desc="Chitinase class I" name="Glyco_hydro_19"/>
                    </models>
                    <signature-library-release library="PFAM" version="31.0"/>
                </signature>
                <locations>
                    <hmmer3-location env-end="228" env-start="94" score="131.4" evalue="4.3E-38" hmm-start="2" hmm-end="155" hmm-length="0" start="94" end="228"/>
                    <hmmer3-location env-end="283" env-start="237" score="28.0" evalue="1.7E-6" hmm-start="185" hmm-end="232" hmm-length="0" start="237" end="283"/>
                </locations>
            </hmmer3-match>
        </matches>
    </protein>
</protein-matches>' -> txt

实际代码:

library(purrr)

doc <- read_xml(txt)

xml_find_all(doc, ".//*[name()='hmmer3-match']") %>%
 map(xml_find_all, ".//*[name()='signature']") -> sig
sig
## [[1]]
## {xml_nodeset (1)}
## [1] <signature ac="PF00187" desc="Chitin recognition protein" name="Chit ...
##
## [[2]]
## {xml_nodeset (1)}
## [1] <signature ac="PF00182" desc="Chitinase class I" name="Glyco_hydro_1 ...

hmmer <- xml_find_all(doc, ".//*[name()='hmmer3-match']")
sig <- lapply(hmmer, xml_find_all, ".//*[name()='signature']")
sig
## [[1]]
## {xml_nodeset (1)}
## [1] <signature ac="PF00187" desc="Chitin recognition protein" name="Chit ...
##
## [[2]]
## {xml_nodeset (1)}
## [1] <signature ac="PF00182" desc="Chitinase class I" name="Glyco_hydro_1 ...

hmmer <- xml_find_all(doc, ".//*[name()='hmmer3-match']")
sig <- list()
for (i in 1:length(hmmer)) {
  sig_match <- xml_find_all(hmmer[[i]], ".//*[name()='signature']")
  sig <- c(sig, sig_match)
}
sig
## [[1]]
## {xml_node}
## <signature ac="PF00187" desc="Chitin recognition protein" name="Chitin_bind_1">
## [1] <entry ac="IPR001002" desc="Chitin-binding, type 1" name="Chitin-bd_ ...
## [2] <models>\n  <model ac="PF00187" desc="Chitin recognition protein" na ...
## [3] <signature-library-release library="PFAM" version="31.0"/>
##
## [[2]]
## {xml_node}
## <signature ac="PF00182" desc="Chitinase class I" name="Glyco_hydro_19">
## [1] <entry ac="IPR000726" desc="Glycoside hydrolase, family 19, catalyti ...
## [2] <models>\n  <model ac="PF00182" desc="Chitinase class I" name="Glyco ...
## [3] <signature-library-release library="PFAM" version="31.0"/>

这篇关于R中的xml2:从父级提取子级属性(所有名称都相同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:03