dimanche 24 février 2019

Nested for loop with if condition in xml, Python

I want to return AA_code when user enters CHAIN_ID and RES_POSITION. CHAIN_ID is subelement of CHAIN tag and RES_POSITION and AA_CODE are subelements of RESIDUE tag. RESIDUE tag is also subelement of CHAIN tag. My code returns AA_CODE only for CHAIN_ID = "A" and until RES_POSITION = "370". However it doesn't return for other CHAIN_IDs but it should return. I couldn't understand the reason why, any help will be appreciated. PS:Im using Elementtree. XML sample:

<RESIDUE>
         <RES_POSITION>370</RES_POSITION>
         <AA_CODE>G</AA_CODE>
      </RESIDUE>
   </CHAIN>
   <CHAIN>
      <CHAIN_ID>B</CHAIN_ID>
      <RESIDUE>
         <RES_POSITION>371</RES_POSITION>
         <AA_CODE>S</AA_CODE>
      </RESIDUE>

My code which returns true result:

   chain = [seq for seq in SEQ.findall('CHAIN') if seq.findtext('CHAIN_ID') == "A"]
    print(chain)
    sequence = [res for res in SEQ.find('CHAIN') if res.findtext('RES_POSITION') == "370"]
    print(sequence)
    for seq in chain:
            for res in sequence:
                if res in seq:
                    print(res.findtext('AA_CODE'))

Returns:

[<Element 'CHAIN' at 0x0000019203C83138>]
        [<Element 'RESIDUE' at 0x00000192040E4C78>]
        G

My code for CHAIN_ID as B:

chain = [seq for seq in SEQ.findall('CHAIN') if seq.findtext('CHAIN_ID') == "B"]
print(chain)
sequence = [res for res in SEQ.find('CHAIN') if res.findtext('RES_POSITION') == "371"]
print(sequence)
for seq in chain:
        for res in sequence:
            if res in seq:
                print(res.findtext('AA_CODE'))

Returns:

[<Element 'CHAIN' at 0x000002EFB2254DB8>]
[]

Aucun commentaire:

Enregistrer un commentaire