Example #1
0
 def __init__(self, databaseFile):
     self._taxonomy = TaxonomyNcbi(databaseFile)
     self._ncbidToNcbidParent = dict()
     self._closed = False
Example #2
0
 def __init__(self, databaseFile):
     self._taxonomy = TaxonomyNcbi(databaseFile)
     self._ncbidToNcbidParent = dict()
     self._closed = False
Example #3
0
class _TaxonomyWrapper():
    """
        Wraps the taxonomy to buffer (speed up) taxonomy calls.
    """
    def __init__(self, databaseFile):
        self._taxonomy = TaxonomyNcbi(databaseFile)
        self._ncbidToNcbidParent = dict()
        self._closed = False

    def getParent(self, ncbid):
        """
            Gets direct parent ncbi taxon id.
        """
        if ncbid in self._ncbidToNcbidParent:
            return self._ncbidToNcbidParent[ncbid]
        else:
            parent = self._taxonomy.getParentNcbid(ncbid)
            self._ncbidToNcbidParent[ncbid] = parent
            return parent

    def getDist(self, ncbid, ncbidSet):
        """
            Gets the distance between ncbid and the closest clade in the ncbidSet which
            represents a path from the root to a clade.
        """
        current = ncbid
        dist = 0
        while (current not in ncbidSet):
            if current is None:
                sys.stderr.write('Consistency:_TaxonomyWrapper:getDist: current is "None" '
                                 + str(ncbid) + ' ' + str(ncbidSet) + '\n')
                break
            current = self.getParent(current)
            dist += 1
        return dist

    def getDistantParent(self, ncbid, intDist):
        """
            Gets parent of "ncbid" that is in distance "intDist".
        """
        parentNcbid = ncbid
        for i in range(int(intDist)):
            parentNcbid = self.getParent(parentNcbid)
        return parentNcbid

    def getDistTowardsRoot(self, ncbid, parentNcbid):
        """
            Gets the distance from ncbid to parentNcbid that is its parent and lies on the same path to the root.
        """
        current = ncbid
        dist = 0
        while (current != parentNcbid) and (current != 1):
            if current is None:
                sys.stderr.write('Consistency:_TaxonomyWrapper:getDistTowardsRoot: current is "None" '
                                 + str(ncbid) + ' ' + str(parentNcbid) + '\n')
                break
            current = self.getParent(current)
            dist += 1
        return float(dist)

    def getScientificName(self, ncbid):
        return self._taxonomy.getScientificName(ncbid)

    def close(self):
        """ To free resources. """
        self._taxonomy.close()
        self._closed = True

    def isClosed(self):
        return self._closed
Example #4
0
class _TaxonomyWrapper():
    """
        Wraps the taxonomy to buffer (speed up) taxonomy calls.
    """
    def __init__(self, databaseFile):
        self._taxonomy = TaxonomyNcbi(databaseFile)
        self._ncbidToNcbidParent = dict()
        self._closed = False

    def getParent(self, ncbid):
        """
            Gets direct parent ncbi taxon id.
        """
        if ncbid in self._ncbidToNcbidParent:
            return self._ncbidToNcbidParent[ncbid]
        else:
            parent = self._taxonomy.getParentNcbid(ncbid)
            self._ncbidToNcbidParent[ncbid] = parent
            return parent

    def getDist(self, ncbid, ncbidSet):
        """
            Gets the distance between ncbid and the closest clade in the ncbidSet which
            represents a path from the root to a clade.
        """
        current = ncbid
        dist = 0
        while (current not in ncbidSet):
            if current is None:
                sys.stderr.write(
                    'Consistency:_TaxonomyWrapper:getDist: current is "None" '
                    + str(ncbid) + ' ' + str(ncbidSet) + '\n')
                break
            current = self.getParent(current)
            dist += 1
        return dist

    def getDistantParent(self, ncbid, intDist):
        """
            Gets parent of "ncbid" that is in distance "intDist".
        """
        parentNcbid = ncbid
        for i in range(int(intDist)):
            parentNcbid = self.getParent(parentNcbid)
        return parentNcbid

    def getDistTowardsRoot(self, ncbid, parentNcbid):
        """
            Gets the distance from ncbid to parentNcbid that is its parent and lies on the same path to the root.
        """
        current = ncbid
        dist = 0
        while (current != parentNcbid) and (current != 1):
            if current is None:
                sys.stderr.write(
                    'Consistency:_TaxonomyWrapper:getDistTowardsRoot: current is "None" '
                    + str(ncbid) + ' ' + str(parentNcbid) + '\n')
                break
            current = self.getParent(current)
            dist += 1
        return float(dist)

    def getScientificName(self, ncbid):
        return self._taxonomy.getScientificName(ncbid)

    def close(self):
        """ To free resources. """
        self._taxonomy.close()
        self._closed = True

    def isClosed(self):
        return self._closed