How to get part of xml file as NumPy array

Viewed 28

I have xml file:

<STREME version="5.4.1" release="Sat Aug 21 19:23:23 2021 -0700">
<model>
<command_line>streme --verbosity 1 --oc . --alph RG_meme.txt --totallength 4000000 --time 14400 --minw 3 --maxw 3 --thresh 0.01 --align left --p balanced_2_ABC_10P.txt</command_line>
<train_positives count="33743" positions="387851" maxlen="44" file="balanced_2_ABC_10P.txt"/>
<train_negatives count="33743" positions="387763" from="shuffled"/>
<test_positives count="3749" positions="43215"/>
<test_negatives count="3749" positions="43303"/>
<alphabet name="RG">
<letter id="A" symbol="A" name="1a" colour="0000CC"/>
<letter id="B" symbol="B" name="1b" colour="CC0000"/>
<letter id="C" symbol="C" name="2" colour="008000"/>
<letter id="D" symbol="D" name="3a" colour="FF00FF"/>
<letter id="E" symbol="E" name="3b" colour="FFB300"/>
<letter id="F" symbol="F" name="4" colour="FFCCCC"/>
<letter id="G" symbol="G" name="5a" colour="FFFF00"/>
<letter id="H" symbol="H" name="5b" colour="33E6CC"/>
<letter id="I" symbol="I" name="5c" colour="7DAFCC"/>
<letter id="J" symbol="J" name="5d" colour="977DCC"/>
<letter id="x3F" symbol="?" equals="ABCDEFGHIJ"/>
</alphabet>
<strands>none</strands>
<sequence_db A="0.0991" B="0.0997" C="0.0993" D="0.101" E="0.0983" F="0.1" G="0.104" H="0.1" I="0.0985" J="0.1"/>
<background_frequencies source="--negatives--" order="0">
<alphabet_array>
<value letter_id="A">0.0991</value>
<value letter_id="B">0.0997</value>
<value letter_id="C">0.0993</value>
<value letter_id="D">0.101</value>
<value letter_id="E">0.0983</value>
<value letter_id="F">0.1</value>
<value letter_id="G">0.104</value>
<value letter_id="H">0.1</value>
<value letter_id="I">0.0985</value>
<value letter_id="J">0.1</value>
</alphabet_array>
</background_frequencies>
<stop thresh_type="pvalue" thresh="0.01" time="14400"/>
<objfun>Differential Enrichment</objfun>
<test>Binomial Test</test>
<minw>3</minw>
<maxw>3</maxw>
<kmer>1</kmer>
<hofract>0.1</hofract>
<neval>25</neval>
<nref>4</nref>
<niter>20</niter>
<patience>3</patience>
<seed>0</seed>
<useer>no</useer>
<minscore>0</minscore>
<ignore_depth>5</ignore_depth>
<nsubsets>1</nsubsets>
<min_pal_ratio>0.85</min_pal_ratio>
<max_pal_ed>5</max_pal_ed>
<cand>no</cand>
<experimental>no</experimental>
<totallength>4000000</totallength>
<align>left</align>
<host>noble-meme.grid.gs.washington.edu</host>
</model>
<motifs>
<motif id="1-IDE" alt="STREME-1" width="3" initial_width="3" seed="IDE" score_threshold="9.63185" train_pos_count="429" train_neg_count="286" train_log_pvalue="-7.29432" train_pvalue="5.1e-008" train_dtc="-1.0" train_bernoulli="0.500069" test_pos_count="61" test_neg_count="30" test_log_pvalue="-3.13765" test_pvalue="7.3e-004" test_log_evalue="-2.3595" test_evalue="4.4e-003" test_dtc="-1.0" test_bernoulli="0.500069" is_palindromic="n/a" elapsed_time="1.6" total_sites="429" site_distr=" 0 0 0 0 0 1 0 0 1 0 0 1 4 5 5 10 19 35 51 36 50 51 38 50 31 10 10 10 4 2 1 1 1 0 1 0 0 0 0 0 0" max_sites="2" site_hist=" 0 428 1">
<pos A="0.00900672" B="0.00906491" C="0.00902309" D="0.00915491" E="0.00893945" F="0.00909127" G="0.00944583" H="0.00911855" I="0.918046" J="0.00910946"/>
<pos A="0.00900672" B="0.00906491" C="0.00902309" D="0.918246" E="0.00893945" F="0.00909127" G="0.00944583" H="0.00911855" I="0.0089549" J="0.00910946"/>
<pos A="0.00900672" B="0.00906491" C="0.00902309" D="0.00915491" E="0.91803" F="0.00909127" G="0.00944583" H="0.00911855" I="0.0089549" J="0.00910946"/>
</motif>
<motif id="2-ABC" alt="STREME-2" width="3" initial_width="3" seed="ABC" score_threshold="9.62442" train_pos_count="476" train_neg_count="303" train_log_pvalue="-9.50389" train_pvalue="3.1e-010" train_dtc="-1.0" train_bernoulli="0.500069" test_pos_count="66" test_neg_count="34" test_log_pvalue="-3.06628" test_pvalue="8.6e-004" test_log_evalue="-2.28813" test_evalue="5.2e-003" test_dtc="-1.0" test_bernoulli="0.500069" is_palindromic="n/a" elapsed_time="1.1" total_sites="476" site_distr=" 0 0 0 0 0 0 0 0 1 2 3 2 3 7 12 20 29 38 46 51 51 42 35 60 19 20 13 10 5 3 2 0 1 0 0 0 0 0 0 0 0" max_sites="2" site_hist=" 0 475 1">
<pos A="0.918098" B="0.00906491" C="0.00902309" D="0.00915491" E="0.00893945" F="0.00909127" G="0.00944583" H="0.00911855" I="0.0089549" J="0.00910946"/>
<pos A="0.00900672" B="0.918156" C="0.00902309" D="0.00915491" E="0.00893945" F="0.00909127" G="0.00944583" H="0.00911855" I="0.0089549" J="0.00910946"/>
<pos A="0.00900672" B="0.00906491" C="0.918114" D="0.00915491" E="0.00893945" F="0.00909127" G="0.00944583" H="0.00911855" I="0.0089549" J="0.00910946"/>

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ I need to get this part of the file start with <pos A="0.918098" ...> (all 3 lines) and put it on NumPy array. I used "xml.etree.ElementTree" but I can not how to get to child that start with <pos...> Thank you for your help. This part of my code:

tree = ET.parse('streme.xml')
root = tree.getroot()
p_matrix1=[]
p_matrix2=[]
for x in root[1]:
    p_matrix1.append(x.tag)
    p_matrix2.append(x.attrib)

data that I need should be inside p_matrix2, which it is not!

1 Answers

Your <pos> is a child of <motif>, so you need a second loop to pull out the elements.

This works:

tree = ET.parse('streme.xml')
root = tree.getroot()
p_matrix1=[]
p_matrix2=[]
for x in root[1]:
    for child in x:
        if child.tag == "pos":
            p_matrix1.append(child.tag)
            p_matrix2.append(child.attrib)

So p_matrix2 has your positions:

p_matrix2

[{'A': '0.00900672',
  'B': '0.00906491',
  'C': '0.00902309',
  'D': '0.00915491',
  'E': '0.00893945',
  'F': '0.00909127',
  'G': '0.00944583',
  'H': '0.00911855',
  'I': '0.918046',
  'J': '0.00910946'},
 {'A': '0.00900672',
  'B': '0.00906491',
  'C': '0.00902309',
  'D': '0.918246',
  'E': '0.00893945',
  'F': '0.00909127',
  'G': '0.00944583',
  'H': '0.00911855',
  'I': '0.0089549',
  'J': '0.00910946'},
 {'A': '0.00900672',
  'B': '0.00906491',
  'C': '0.00902309',
  'D': '0.00915491',
  'E': '0.91803',
  'F': '0.00909127',
  'G': '0.00944583',
  'H': '0.00911855',
  'I': '0.0089549',
  'J': '0.00910946'},
 {'A': '0.918098',
  'B': '0.00906491',
  'C': '0.00902309',
  'D': '0.00915491',
  'E': '0.00893945',
  'F': '0.00909127',
  'G': '0.00944583',
  'H': '0.00911855',
  'I': '0.0089549',
  'J': '0.00910946'},
 {'A': '0.00900672',
  'B': '0.918156',
  'C': '0.00902309',
  'D': '0.00915491',
  'E': '0.00893945',
  'F': '0.00909127',
  'G': '0.00944583',
  'H': '0.00911855',
  'I': '0.0089549',
  'J': '0.00910946'},
 {'A': '0.00900672',
  'B': '0.00906491',
  'C': '0.918114',
  'D': '0.00915491',
  'E': '0.00893945',
  'F': '0.00909127',
  'G': '0.00944583',
  'H': '0.00911855',
  'I': '0.0089549',
  'J': '0.00910946'}]

Though p_matrix1 just has the pos tag - not sure what you were intending to use p_matrix1 for.

Related