xml2 in R: extract children attributes from parents (everything is named the same)

隐身守侯 提交于 2019-12-13 08:43:35

问题


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>

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.

Here is what I tried:

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" ...

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?

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

Thanks.


回答1:


Use relative XPaths:

'<?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

The actual code:

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"/>


来源:https://stackoverflow.com/questions/46229508/xml2-in-r-extract-children-attributes-from-parents-everything-is-named-the-sam

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!