Пример #1
0
    def build(self, cursor):
        switch_to_db(cursor, get_compara_name(cursor))
        for leaf in self.leafs:
            leaf.tax_id = species2taxid(cursor, leaf.name)
            leaf.is_leaf = True
            self.node[leaf.tax_id] = leaf

        # build the tree using ncbi_tax.nodes
        # fill in the names using ncbi_tax.names
        switch_to_db(cursor, get_ncbi_tax_name(cursor))
        for leaf in self.leafs:
            parent_id = taxid2parentid(cursor, leaf.tax_id)
            leaf.parent_id = parent_id
            current_id = leaf.tax_id
            # move to the root
            while current_id:
                current_node = self.node[current_id]
                parent_id = taxid2parentid(cursor, parent_id)
                if (not parent_id or current_id == parent_id):
                    current_node.is_root = True
                    self.root = self.node[current_id]
                    current_id = None

                else:

                    # does parent exist by any chance
                    if self.node.has_key(parent_id):
                        parent_node = self.node[parent_id]
                        parent_node.children.append(current_node)
                        # we are done here
                        current_id = None
                    else:  # make new node
                        parent_name = taxid2name(cursor, parent_id)
                        parent_node = Node(parent_name)
                        parent_node.tax_id = parent_id
                        # grampa:
                        parent_node.parent_id = taxid2parentid(
                            cursor, parent_id)
                        parent_node.children.append(current_node)
                        self.node[parent_id] = parent_node
                        # attach the current node to the parent
                        current_id = parent_id

        # shortcircuit nodes with a single child
        new_root = self.root.__cleanup__()
        if (new_root):
            new_root.is_root = True
            self.root = new_root

        del_ids = []
        for node_id, node in self.node.iteritems():
            if node.is_leaf:
                continue
            if (not node.children):
                del_ids.append(node_id)

        for node_id in del_ids:
            del self.node[node_id]

        self.__set_parent_ids__(self.root)
Пример #2
0
    def build (self, cursor):
        switch_to_db(cursor, get_compara_name(cursor))
        for leaf in self.leafs:
            leaf.tax_id = species2taxid (cursor, leaf.name)
            leaf.is_leaf = True
            self.node[leaf.tax_id] = leaf

        # build the tree using ncbi_tax.nodes
        # fill in the names using ncbi_tax.names
        switch_to_db(cursor, get_ncbi_tax_name(cursor))
        for leaf in self.leafs:
            parent_id = taxid2parentid (cursor, leaf.tax_id)
            leaf.parent_id = parent_id
            current_id     = leaf.tax_id
            # move to the root
            while current_id:
                current_node = self.node[current_id]
                parent_id    = taxid2parentid (cursor, parent_id)
                if (not parent_id or  current_id == parent_id):
                    current_node.is_root = True
                    self.root = self.node[current_id]
                    current_id = None

                else:

                    # does parent exist by any chance
                    if self.node.has_key(parent_id):
                        parent_node = self.node[parent_id]
                        parent_node.children.append(current_node)
                        # we are done here
                        current_id = None
                    else: # make new node
                        parent_name    = taxid2name(cursor, parent_id)
                        parent_node    = Node(parent_name)
                        parent_node.tax_id = parent_id
                        # grampa:
                        parent_node.parent_id = taxid2parentid (cursor, parent_id)
                        parent_node.children.append(current_node)
                        self.node[parent_id]  = parent_node
                        # attach the current node to the parent
                        current_id = parent_id
        
        # shortcircuit nodes with a single child
        new_root = self.root.__cleanup__()
        if (new_root):
            new_root.is_root = True
            self.root = new_root

        del_ids  = []
        for node_id, node in self.node.iteritems():
            if node.is_leaf:
                continue
            if (not node.children):
                del_ids.append(node_id)

        for node_id in del_ids:
            del self.node[node_id]
                              
        self.__set_parent_ids__ (self.root)
Пример #3
0
 def add(self, cursor, name):
     leaf = Node(name)
     # get tax ids from the gene_db table in compara database
     switch_to_db(cursor, get_compara_name(cursor))
     leaf.tax_id = species2taxid(cursor, leaf.name)
     leaf.is_leaf = True
     self.leafs.append(leaf)
     self.node[leaf.tax_id] = leaf
Пример #4
0
 def add(self, cursor, name):
     leaf = Node(name)
     # get tax ids from the gene_db table in compara database
     switch_to_db(cursor, get_compara_name(cursor))
     leaf.tax_id = species2taxid (cursor, leaf.name)
     leaf.is_leaf = True
     self.leafs.append(leaf)
     self.node[leaf.tax_id] = leaf
Пример #5
0
def get_common_name (cursor, species):
    switch_to_db(cursor, get_compara_name(cursor))
    tax_id = species2taxid (cursor, species)
    switch_to_db(cursor,get_ncbi_tax_name (cursor))
    qry   = "select name_txt from names where "
    qry  += "tax_id = %d and " % tax_id
    qry  += "name_class = 'genbank common name'"
    rows = search_db (cursor, qry)
    if rows:
        if ('ERROR' in rows[0]):
            search_db (cursor, qry, verbose = True)
            return ""
        else:
            return rows[0][0]

    return ""
Пример #6
0
def get_common_name(cursor, species):
    switch_to_db(cursor, get_compara_name(cursor))
    tax_id = species2taxid(cursor, species)
    switch_to_db(cursor, get_ncbi_tax_name(cursor))
    qry = "select name_txt from names where "
    qry += "tax_id = %d and " % tax_id
    qry += "name_class = 'genbank common name'"
    rows = search_db(cursor, qry)
    if rows:
        if ('ERROR' in rows[0]):
            search_db(cursor, qry, verbose=True)
            return ""
        else:
            return rows[0][0]

    return ""