2016-01-11 59 views
0

R中使用XML2的SBML文件我是真正的新xml我試圖在R使用xml2包讀取sbml文件。閱讀使用XPath

演示sbml文件取自sbml主要page

我很困惑如何使用xpath搜索節點。

例如,我試圖

test <- read_xml("./scratch.xml") 
xml_children(test)[1] 
xml_attr(xml_children(test)[1], "name") 

的作品,給我"EnzymaticReaction"的答案。但是,我不希望訪問由索引節點,但名字 - 所以我試圖

xml_find_one(test, ".//model") 

這給了我錯誤

Error: No matches 

誰能幫助我爲我在做什麼錯打電話xpathsbml文件也粘貼在下面。

謝謝!

<?xml version="1.0" encoding="UTF-8"?> 
<sbml level="2" version="3" xmlns="http://www.sbml.org/sbml/level2/version3"> 
    <model name="EnzymaticReaction"> 
     <listOfUnitDefinitions> 
      <unitDefinition id="per_second"> 
       <listOfUnits> 
        <unit kind="second" exponent="-1"/> 
       </listOfUnits> 
      </unitDefinition> 
      <unitDefinition id="litre_per_mole_per_second"> 
       <listOfUnits> 
        <unit kind="mole" exponent="-1"/> 
        <unit kind="litre" exponent="1"/> 
        <unit kind="second" exponent="-1"/> 
       </listOfUnits> 
      </unitDefinition> 
     </listOfUnitDefinitions> 
     <listOfCompartments> 
      <compartment id="cytosol" size="1e-14"/> 
     </listOfCompartments> 
     <listOfSpecies> 
      <species compartment="cytosol" id="ES" initialAmount="0"  name="ES"/> 
      <species compartment="cytosol" id="P" initialAmount="0"  name="P"/> 
      <species compartment="cytosol" id="S" initialAmount="1e-20" name="S"/> 
      <species compartment="cytosol" id="E" initialAmount="5e-21" name="E"/> 
     </listOfSpecies> 
     <listOfReactions> 
      <reaction id="veq"> 
       <listOfReactants> 
        <speciesReference species="E"/> 
        <speciesReference species="S"/> 
       </listOfReactants> 
       <listOfProducts> 
        <speciesReference species="ES"/> 
       </listOfProducts> 
       <kineticLaw> 
        <math xmlns="http://www.w3.org/1998/Math/MathML"> 
         <apply> 
          <times/> 
          <ci>cytosol</ci> 
          <apply> 
           <minus/> 
           <apply> 
            <times/> 
            <ci>kon</ci> 
            <ci>E</ci> 
            <ci>S</ci> 
           </apply> 
           <apply> 
            <times/> 
            <ci>koff</ci> 
            <ci>ES</ci> 
           </apply> 
          </apply> 
         </apply> 
        </math> 
        <listOfParameters> 
         <parameter id="kon" value="1000000" units="litre_per_mole_per_second"/> 
         <parameter id="koff" value="0.2"  units="per_second"/> 
        </listOfParameters> 
       </kineticLaw> 
      </reaction> 
      <reaction id="vcat" reversible="false"> 
       <listOfReactants> 
        <speciesReference species="ES"/> 
       </listOfReactants> 
       <listOfProducts> 
        <speciesReference species="E"/> 
        <speciesReference species="P"/> 
       </listOfProducts> 
       <kineticLaw> 
        <math xmlns="http://www.w3.org/1998/Math/MathML"> 
         <apply> 
          <times/> 
          <ci>cytosol</ci> 
          <ci>kcat</ci> 
          <ci>ES</ci> 
         </apply> 
        </math> 
        <listOfParameters> 
         <parameter id="kcat" value="0.1" units="per_second"/> 
        </listOfParameters> 
       </kineticLaw> 
      </reaction> 
     </listOfReactions> 
    </model> 
</sbml> 
+0

您的XML文檔具有_default命名空間_。這意味着,在您的R代碼中,需要聲明該名稱空間URI,並且需要在XPath表達式中爲元素名稱添加前綴。 –

+0

感謝您的評論,您能否以示例的形式告訴我如何按名稱獲取'model'節點? – SN248

+0

我的解決方案是否回答您的問題? –

回答

2

在你的輸入文檔,有一個默認命名空間

<sbml xmlns="http://www.sbml.org/sbml/level2/version3"> 

適用於在默認情況下所有元素。像

//model 

XPath表達式意味着尋找一個元素沒有命名空間 - 但你的文檔中,沒有model節點是沒有命名空間。

我對R並不熟悉,所以我只能提出一些更多的解決方法而不是答案。解決方法是不直接提及元素的名稱,而是使用XPath表達式像

//*[local-name() = 'model'] 

但忽略了命名空間是不如直接在您的代碼不談。


同時,我讀過有關這here ...

真正的解決方案是使用一種方法來在R代碼裏面聲明空間URI從您的輸入文檔,並使用XPath表達式中的前綴。我認爲,正確的方式做這將是

ns <- xml_ns_rename(xml_ns(test), d1 = "sbml") 
xml_find_one(test, "/sbml:sbml/sbml:model", ns) 

改名並非絕對必要,但它是有幫助的。 XML文檔中的默認名稱空間被這個XML庫命名爲d1d2等等。