R中的xml2:从父项中提取子属性(一切都命名相同)

时间:2017-09-14 23:17:49

标签: r xml2

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

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

我希望根据signature提取entrygo-xreflocationshmmer3-match的属性。听起来很简单,我认为会是这样。

以下是我的尝试:

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" ...
从那以后,

似乎将比赛加倍:

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循环,

会发生同样的事情:

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`

我对此事的了解非常有限,我觉得我错过了一些相当简单的事情?

我正在寻找适用于任意数量的hmmer3-match节点go-xreflocations的一般解决方案。

感谢。

1 个答案:

答案 0 :(得分:1)

使用相对XPath:

67

实际代码:

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