Example #1
0
def T_to_newick(T):
    """
    Get a newick string from an unweighted topology.
    @param T: topology
    @return: newick string
    """
    return R_to_newick(Ftree.T_to_R_canonical(T))
Example #2
0
def equal_arc_layout(T, B):
    """
    @param T: tree topology
    @param B: branch lengths
    @return: a map from vertex to location
    """
    # arbitrarily root the tree
    R = Ftree.T_to_R_canonical(T)
    r = Ftree.R_to_root(R)
    # map vertices to subtree tip count
    v_to_sinks = Ftree.R_to_v_to_sinks(R)
    v_to_count = {}
    for v in Ftree.R_to_postorder(R):
        sinks = v_to_sinks.get(v, [])
        if sinks:
            v_to_count[v] = sum(v_to_count[sink] for sink in sinks)
        else:
            v_to_count[v] = 1
    # create the equal arc angles
    v_to_theta = {}
    _force_equal_arcs(
            v_to_sinks, v_to_count, v_to_theta,
            r, -math.pi, math.pi)
    # convert angles to coordinates
    v_to_source = Ftree.R_to_v_to_source(R)
    v_to_location = {}
    _update_locations(
            R, B,
            v_to_source, v_to_sinks, v_to_theta, v_to_location,
            r, (0, 0), 0)
    return v_to_location
Example #3
0
def equal_daylight_layout(T, B, iteration_count):
    """
    @param T: topology
    @param B: branch lengths
    """
    R = Ftree.T_to_R_canonical(T)
    r = Ftree.R_to_root(R)
    # create the initial equal arc layout
    v_to_location = equal_arc_layout(T, B)
    # use sax-like events to create a parallel tree in the C extension
    v_to_sinks = Ftree.R_to_v_to_sinks(R)
    v_to_dtree_id = {}
    dtree = day.Day()
    count = _build_dtree(
            dtree, r, v_to_sinks, v_to_location, v_to_dtree_id, 0)
    # repeatedly reroot and equalize
    v_to_neighbors = Ftree.T_to_v_to_neighbors(T)
    for i in range(iteration_count):
        for v in Ftree.T_to_inside_out(T):
            neighbor_count = len(v_to_neighbors[v])
            if neighbor_count > 2:
                dtree.select_node(v_to_dtree_id[v])
                dtree.reroot()
                dtree.equalize()
    # extract the x and y coordinates from the dtree
    v_to_location = {}
    for v, dtree_id in v_to_dtree_id.items():
        dtree.select_node(dtree_id)
        x = dtree.get_x()
        y = dtree.get_y()
        v_to_location[v] = (x, y)
    return v_to_location
Example #4
0
def TB_to_newick(T, B):
    """
    Get a newick string from a weighted topology.
    @param T: topology
    @param B: branch lengths
    @return: newick string
    """
    return RB_to_newick(Ftree.T_to_R_canonical(T), B)
Example #5
0
def get_response_content(fs):
    # read the tree
    T, B, N = FtreeIO.newick_to_TBN(fs.tree)
    leaves = Ftree.T_to_leaves(T)
    internal = Ftree.T_to_internal_vertices(T)
    # root arbitrarily
    R = Ftree.T_to_R_canonical(T)
    # init some sampling parameters
    nsamples = 1000
    npillars = 10
    # Init the accumulators.
    # Accumulate the sum of squares of differences
    # and the sum of differences.
    # The differences are from the leaf mean.
    dsum = defaultdict(float)
    dsumsq = defaultdict(float)
    # Repeatedly sample using Brownian motion on the tree.
    for i in range(nsamples):
        # Sample using Brownian motion at vertices on the tree.
        v_to_sample = sample_brownian_motion(R, B)
        # Compute the mean at the leaves.
        mu = sum(v_to_sample[v] for v in leaves) / len(leaves)
        # Accumulate difference moments at vertices of the tree.
        for v, x in v_to_sample.items():
            dsum[(v, -1, -1)] += x - mu
            dsumsq[(v, -1, -1)] += (x - mu)**2
        # Sample using Brownian bridge on edges.
        for d_edge in R:
            u_edge = frozenset(d_edge)
            va, vb = d_edge
            a = v_to_sample[va]
            b = v_to_sample[vb]
            samples = bridge(a, b, npillars, B[u_edge])
            for i, x in enumerate(samples):
                dsum[(va, vb, i)] += x - mu
                dsumsq[(va, vb, i)] += (x - mu)**2
    quad = min((val, va, vb, i) for (va, vb, i), val in dsumsq.items())
    val, va, vb, i = quad
    # write the report
    out = StringIO()
    if i < 0:
        print >> out, 'min sum of squares was at vertex', N[va]
    else:
        print >> out, 'min sum of squares was at edge',
        print >> out, N[va], '--[', i, ']-->', N[vb]
    print >> out
    print >> out, 'the min sum of squares value was', val
    return out.getvalue()
Example #6
0
def get_response_content(fs):
    # read the tree
    T, B, N = FtreeIO.newick_to_TBN(fs.tree)
    leaves = Ftree.T_to_leaves(T)
    internal = Ftree.T_to_internal_vertices(T)
    # root arbitrarily
    R = Ftree.T_to_R_canonical(T)
    # init some sampling parameters
    npillars = 9
    # init some helper variables
    nleaves = len(leaves)
    r = get_new_vertex(T)
    vertices = internal + [r] + leaves
    combo = np.array([0] * len(internal) + [1] + [-1.0 / nleaves] * nleaves)
    # Map edge position triple to the quadratic form value.
    qform = {}
    for d_edge in R:
        a, b = d_edge
        u_edge = frozenset(d_edge)
        distance = B[u_edge]
        for i in range(npillars):
            # get the proportion of the distance along the branch
            t = (i + 1) / float(npillars + 1)
            T_new, B_new = add_vertex(T, B, d_edge, r, t)
            # create the new centered covariance matrix
            L = Ftree.TB_to_L_principal(T_new, B_new, vertices)
            S = np.linalg.pinv(L)
            qform[(a, b, t * distance)] = quadratic_form(S, combo)
            #shortcombo = np.array([1] + [-1.0/nleaves]*nleaves)
            #shortvert = [r] + leaves
            #L_schur = Ftree.TB_to_L_schur(T_new, B_new, shortvert)
            #S = np.linalg.pinv(L_schur)
            #qform[(a, b, t*distance)] = quadratic_form(S, shortcombo)
    wat = sorted((val, va, vb, d) for (va, vb, d), val in qform.items())
    # write the report
    out = StringIO()
    for val, va, vb, d in wat:
        print >> out, N[va], '--[', d, ']-->', N[vb], ':', val
        print >> out
    return out.getvalue()