Пример #1
0
def nextbacktree(t, i, q=None, lim=0, maxdepth=None):
    """
    create a tree of ivy.tree.Nodes from table t, with
    records having fields next, back, parent, and depth
    """
    db = t._db
    r = t[int(i)]
    limits = dict(orderby=t.next)
    if lim: limits["limitby"] = (0, lim)
    q2 = (t.next > r.next) & (t.back < r.back)
    if maxdepth:
        q2 &= (t.depth <= maxdepth)
    recs = db(q)(q2).select(**limits)

    #Node = ivy.tree.Node
    root = Node()
    root.isroot = True
    root.rec = r
    root.id = r.id

    i2n = {}
    i2n[r.id] = root
    for r in recs:
        n = Node()
        n.rec = r
        n.id = r.id
        n.next = r.next
        n.back = r.back
        if (((not maxdepth) and (r.next == r.back - 1))
                or (maxdepth and (n.depth == maxdepth))):
            n.isleaf = True
        i2n[r.id] = n
        i2n[r.parent].add_child(n)
    return root
Пример #2
0
def getSTree( db, treeId ):
   
    root = None

    parentStack = []

    for snodeRow in db( db.snode.tree == treeId ).select( db.snode.ALL, db.taxon.ALL, left = db.taxon.on( db.taxon.id == db.snode.taxon ), orderby = db.snode.next ).as_list():

        node = Node()

        node.id = snodeRow['snode']['id']; node.next = snodeRow['snode']['next']; node.back = snodeRow['snode']['back']; node.length = snodeRow['snode']['length']   
        node.label = snodeRow['snode']['label']; node.taxon = snodeRow['taxon']['name']; node.depth = len( parentStack )

        if( node.next - node.back == 1 ):
            node.isleaf = True

        if( node.next == 1 ):
            node.isroot = True
            root = node
            parentStack.append( node )
            continue

        while( node.back > parentStack[ -1 ].back ):
            parentStack.pop() 

        parentStack[ -1 ].add_child( node )
        parentStack.append( node )

    return root
Пример #3
0
def nextbacktree(t, i, q=None, lim=0, maxdepth=None):
    """
    create a tree of ivy.tree.Nodes from table t, with
    records having fields next, back, parent, and depth
    """
    db = t._db
    r = t[int(i)]
    limits = dict(orderby=t.next)
    limits['orderby'] = t.next
    if lim: limits["limitby"] = (0,lim)
    q2 = (t.next>r.next)&(t.back<r.back)
    if maxdepth:
        q2 &= (t.depth <= maxdepth)
    recs = db(q)(q2).select(**limits)
    
    root = Node(); root.isroot = True
    root.rec = r; root.id = r.id; root.next = r.next; root.back = r.back;

    i2n = {}; i2n[r.id] = root
    for r in recs:
        n = Node()
        n.rec = r; n.id = r.id; n.next = r.next; n.back = r.back; n.parent = i2n[r.parent];
        if (((not maxdepth) and (r.next==r.back-1)) or
            (maxdepth and (n.depth == maxdepth))):
            n.isleaf = True
        i2n[r.id] = n
        i2n[r.parent].add_child(n)
    return root
Пример #4
0
def getGTree( db, treeId ):
   
    root = None

    parentStack = []

    for gnodeRow in db( ( db.gnode.tree == treeId ) &
                        ( db.gnode.pruned == False ) ).select( db.gnode.ALL, orderby = db.gnode.next ).as_list():

        node = Node()

        node.id = gnodeRow['id']; node.next = gnodeRow['next']; node.back = gnodeRow['back']
        node.label = gnodeRow['label']; node.depth = len( parentStack )

        if( node.next - node.back == 1 ):
            node.isleaf = True

        if( node.next == 1 ):
            node.isroot = True
            root = node
            parentStack.append( node )
            continue

        while( node.back > parentStack[ -1 ].back ):
            parentStack.pop() 

        parentStack[ -1 ].add_child( node )
        parentStack.append( node )

    return root
Пример #5
0
def buildtree(t, otu_id2data):
    from ivy.tree import Node
    if t.has_key('nodeById'):
        # newer Nexson 1.2.1
        node_id2data = t['nodeById']
    else:
        # older Nexson 1.0.0
        node_id2data = {} 
        for n in t['node']:
            node_id2data[ n['@id'] ] = n
    root = None
    for i, d in node_id2data.iteritems():
        n = Node()
        n.snode_id = i
        n.taxid = None
        if d.get('@root'):
            n.isroot = True
            root = n
        oid = d.get('@otu')
        if oid:
            n.isleaf = True
            try:
                n.otu = otu_id2data[oid]
                n.label = (n.otu.get('^ot:ottTaxonName') or
                           n.otu.get('^ot:originalLabel'))
                n.taxid = n.otu.get('^ot:ottId')
            except KeyError:
                pass
                ## print t['nexson_file'], 'missing', oid
        n.nexson_id = i
        d['node'] = n
    if t.has_key('edgeBySourceId'):
        # newer Nexson 1.2.1
        edges_by_source_id = t['edgeBySourceId']
    else:
        # older Nexson 1.0.0
        edges_by_source_id = {} 
        for e in t['edge']:
            if not edges_by_source_id.has_key( e['@source'] ):
                edges_by_source_id[ e['@source'] ] = {}
            edges_by_source_id[ e['@source'] ][ e['@id'] ] = e
    for nid, ed in edges_by_source_id.iteritems():
        n = node_id2data[nid]
        for e in ed.itervalues():
            cd = node_id2data[e['@target']]
            c = cd['node']
            c.length = e.get('@length')
            n['node'].add_child(c)
    root.tree_nexson = t
    root.stree = t['treeid']
    root.ladderize()
    ivy.tree.index(root)
    return root