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.
Use relative XPaths:
The actual code: