Exemplo n.º 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)
Exemplo n.º 2
0
def find_mammals(cursor, trivial_name_list):
    
    mammals = []
    for trivial_name in trivial_name_list:
        switch_to_db(cursor, get_compara_name(cursor))
        tax_id = trivial2taxid (cursor, trivial_name)
        parent_id = taxid2parentid (cursor, tax_id)

        tax_id = parent_id
        is_mammal = False
        while tax_id:
            qry  = "select name_txt from names where tax_id= %d " % int(tax_id)
            qry += " and name_class = 'scientific name'";
            rows = search_db (cursor, qry)
            if rows and rows[0][0]:
                if 'mammal' in rows[0][0].lower():
                    is_mammal = True
                    break
                elif 'vertebrat' in  rows[0][0].lower():
                    # if the thing wasa mammal, we would have found it by now
                    is_mammal = False
                    break
               
            parent_id = taxid2parentid (cursor, tax_id)
            if parent_id and parent_id>1:
                tax_id = parent_id
            else:
                tax_id = None

        if is_mammal: 
            mammals.append(trivial_name)

            
    return mammals
Exemplo n.º 3
0
def find_mammals(cursor, trivial_name_list):

    mammals = []
    for trivial_name in trivial_name_list:
        switch_to_db(cursor, get_compara_name(cursor))
        tax_id = trivial2taxid(cursor, trivial_name)
        parent_id = taxid2parentid(cursor, tax_id)

        tax_id = parent_id
        is_mammal = False
        while tax_id:
            qry = "select name_txt from names where tax_id= %d " % int(tax_id)
            qry += " and name_class = 'scientific name'"
            rows = search_db(cursor, qry)
            if rows and rows[0][0]:
                if 'mammal' in rows[0][0].lower():
                    is_mammal = True
                    break
                elif 'vertebrat' in rows[0][0].lower():
                    # if the thing wasa mammal, we would have found it by now
                    is_mammal = False
                    break

            parent_id = taxid2parentid(cursor, tax_id)
            if parent_id and parent_id > 1:
                tax_id = parent_id
            else:
                tax_id = None

        if is_mammal:
            mammals.append(trivial_name)

    return mammals
Exemplo n.º 4
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)
Exemplo n.º 5
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
Exemplo n.º 6
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
Exemplo n.º 7
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 ""
Exemplo n.º 8
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 ""