예제 #1
0
    def update_hierarchy(self):
        """
            TODO!!! requires the logging methods!!!

            this method accepts only the final taxon list
            where all we need to check if the consecutive
            taxa has a child-parent relationship. if not
            set the relationshp.

        """
        
        if sorted([tax.level for tax in self.lineage.taxon_list]) != sorted(self.lineage.default_levels):
            raise Exception("Required taxonomic levels are not satisfied yet.")


        for i in range(len(self.lineage.taxon_list)-1):
            child_tax= self.lineage.taxon_list[i]
            parent_tax= self.lineage.taxon_list[i+1]
             

            #try:
            hier= self.lineage.biodb_selector.store.find(Hierarchy, Hierarchy.childID == child_tax.taxon.id ).one()
            
            #except:
            #    continue

            if hier.parent.id != parent_tax.taxon.id:
                new_hier= Hierarchy()
                new_hier.parentID= parent_tax.taxon.id
                new_hier.childID= child_tax.taxon.id

                self.lineage.biodb_selector.store.remove(hier) 
                self.lineage.biodb_selector.store.add(new_hier)

                child_id= child_tax.taxon.id
                old_parent_id= hier.parentID
                new_parent_id= parent_tax.taxon.id
                #self.logger.error("child id: %d, old parent id: %d, new parent id: %d" %(child_id, old_parent_id, new_parent_id))
                #self.logger.error("child name: %s, old parent name: %s, new parent name: %s" %(child_tax.taxon.name, hier.parent.name, parent_tax.taxon.name))
                ### DONT FORGET TO COMMIT

        self.lineage.biodb_selector.store.commit()