PoonLab / sierra-local

Retrieve HIVdb algorithm as XML and apply locally to HIV sequences
GNU General Public License v3.0
6 stars 3 forks source link

AttributeError, when testing `parse_definitions`, `parse_drugs` and `parse_comments` #75

Closed SandeepThokala closed 1 year ago

SandeepThokala commented 1 year ago

Error Messages:

Traceback (most recent call last):
  File ".\sierra-local\sierralocal\hivdb.py", line 287, in parse_comments
    for element in root.getchildren():
AttributeError: 'ElementTree' object has no attribute 'getchildren'
======================================================================
Traceback (most recent call last):
  File ".\sierra-local\sierralocal\hivdb.py", line 127, in parse_definitions
    element_list = list(map(lambda x: xml.tostring(x).strip().decode("utf-8"), root.getchildren()))
AttributeError: 'ElementTree' object has no attribute 'getchildren'
======================================================================
Traceback (most recent call last):
  File ".\sierra-local\sierralocal\hivdb.py", line 127, in parse_definitions
    element_list = list(map(lambda x: xml.tostring(x).strip().decode("utf-8"), root.getchildren()))
AttributeError: 'ElementTree' object has no attribute 'getchildren'
SandeepThokala commented 1 year ago

Added the following changes to avoid the issue

@@ -125,18 +125,18 @@ class HIVdb():
             'comment': {}  # maps comments to id string
         }
         # Convert list of elements from class 'xml.etree.ElementTree.Element' to type 'str'
-        element_list = list(map(lambda x: xml.tostring(x).strip().decode("utf-8"), root.getchildren()))
+        element_list = list(map(lambda x: xml.tostring(x).strip().decode("utf-8"), root.findall('./')))
         # Find the index of element 'DEFINITIONS' so that it's children may be iterated over to parse definitions
         def_index = [i for i, item in enumerate(element_list) if re.search('<DEFINITIONS>', item)]
         def_ind = def_index[0]  # un-list the index of 'DEFINITIONS' element

-        definitions = root.getchildren()[def_ind]
-        comment_definitions = definitions.getchildren()[-1]  # TODO: swap out hard-coded index with variable
+        definitions = root.findall('./')[def_ind]
+        comment_definitions = definitions.findall('./')[-1]  # TODO: swap out hard-coded index with variable

         globalrange = definitions.find('GLOBALRANGE').text.split(',')
         default_grange = self.parse_globalrange(self.definitions['globalrange'], globalrange)

-        for element in definitions.getchildren():
+        for element in definitions.findall('./'):
             if element.tag == 'GENE_DEFINITION':
                 gene = element.find('NAME').text
                 drug_classes = element.find('DRUGCLASSLIST').text.split(',')
@@ -154,7 +154,7 @@ class HIVdb():
                 self.definitions['drugclass'].update({name: druglist})

             elif element.tag == 'COMMENT_DEFINITIONS':
-                for comment_str in comment_definitions.getchildren():
+                for comment_str in comment_definitions.findall('./'):
                     id = comment_str.attrib['id']
                     comment = comment_str.find('TEXT').text
                     sort_tag = comment_str.find('SORT_TAG').text
@@ -204,7 +204,7 @@ class HIVdb():
         """
         self.drugs = {}

-        for element in root.getchildren():
+        for element in root.findall('./'):
             if element.tag == 'DRUG':
                 drug = element.find('NAME').text                            # drug name
                 fullname = element.find('FULLNAME').text                    # drug full name
@@ -290,10 +290,10 @@ class HIVdb():
         """
         self.comments = {}

-        for element in root.getchildren():
+        for element in root.findall('./'):
             if element.tag == 'MUTATION_COMMENTS':

-                for gene in element.getchildren():
+                for gene in element.findall('.//GENE'):
                     name = gene.find('NAME').text
                     gene_dict = {}