コード例 #1
0
def addmix_with_correction(tree,
                           new_node_names=None,
                           pks={},
                           fixed_sink_source=None,
                           new_branch_length=None,
                           new_to_root_length=None):

    added_tree, forward, backward = addadmix(
        tree,
        new_node_names=new_node_names,
        pks=pks,
        fixed_sink_source=fixed_sink_source,
        new_branch_length=new_branch_length,
        new_to_root_length=new_to_root_length,
        check_opposite=False,
        preserve_root_distance=False)

    node_keys = sorted(get_leaf_keys(tree))

    A, _, bi1 = make_coefficient_matrix(tree, node_keys=node_keys)
    B, _, bi2 = make_coefficient_matrix(added_tree, node_keys=node_keys)

    x_A = get_specific_branch_lengths(tree, reverse_dic_to_list(bi1))
    x_B = get_specific_branch_lengths(added_tree, reverse_dic_to_list(bi2))

    Binverse = pinv(B)
    Ainverse = pinv(A)

    tilde_x_B = Binverse.dot(A.dot(x_A))
    #random_tilde_x_B=add_random_noise(tilde_x_B)
    tilde_x_A = Ainverse.dot(B.dot(tilde_x_B))
    #
    #     if all((float_equal(x,y) for x,y in zip(x_A, tilde_x_A))):
    #         added_tree=update_specific_branch_lengths(added_tree, bi2, tilde_x_B)
    #         if added_tree is None:
    #             return tree, 1,0
    #         else:
    #             return tree, forward, backward
    #     else:
    #         new_x_B=

    print array(x_B)
    print array(tilde_x_B)
    print array(x_A)
    print array(tilde_x_A)

    print B.dot(x_B)
    print A.dot(x_A)
    print B.dot(tilde_x_B)
    print A.dot(tilde_x_A)

    tilde2_x_A = Ainverse.dot(B.dot(x_B))
    tilde2_x_B = Binverse.dot(A.dot(tilde2_x_A))

    print B.dot(x_B)
    print A.dot(x_A)
    print B.dot(tilde2_x_B)
    print A.dot(tilde2_x_A)

    t = 5
コード例 #2
0
def getcorrection(old_tree, new_tree, sigma):

    node_keys = sorted(get_leaf_keys(old_tree))

    B, _, bi1 = make_coefficient_matrix(old_tree, node_keys=node_keys)
    A, _, bi2 = make_coefficient_matrix(new_tree, node_keys=node_keys)

    branches = reverse_dic_to_list(bi1)

    x_A = array(get_specific_branch_lengths(old_tree, branches))
    x_B = array(get_specific_branch_lengths(new_tree, branches))
    x_old = deepcopy(x_A)
    # print x_A
    #print x_B
    #print x_old

    upper = x_A.dot(B.T.dot(A) + identity(len(branches)))

    lower_first = A.T.dot(A) + identity(len(branches))

    mu_new = mm(U=upper, L=lower_first, initial_value=x_B)

    x_new = mu_new + norm.rvs(scale=sigma, size=len(mu_new))

    q_forward = sum(norm.logpdf(mu_new - x_new, scale=sigma))

    upper_reverse = x_new.dot((A.T.dot(B) + identity(len(branches))))
    lower_first_reverse = B.T.dot(B) + identity(len(branches))

    mu_reverse = mm(U=upper_reverse,
                    L=lower_first_reverse,
                    initial_value=array(x_new))

    #print 'matrix_rank , dimension (A)', matrix_rank(A), A.shape
    #print 'matrix_rank , dimension (B)', matrix_rank(B), B.shape
    #print 'x_reverse', reverse_mu_new

    q_backward = sum(norm.logpdf(mu_reverse - x_A, scale=sigma))

    #wear the new values
    #print branches

    new_tree = update_specific_branch_lengths(new_tree, branches, x_new)

    #print sum((A.dot(mu_new)-B.dot(x_old))**2)
    #print sum((A.dot(x_new)-B.dot(x_old))**2)
    #print sum((B.dot(x_old)-A.dot(x_old))**2)

    return new_tree, 1.0, exp(q_backward - q_forward)
コード例 #3
0
def getcorrection_adding(old_tree, new_tree, sigma, branches, U_matrix):

    node_keys = sorted(get_leaf_keys(old_tree))

    A, _, _ = make_coefficient_matrix(old_tree,
                                      node_keys=node_keys,
                                      branch_keys=branches[:-3])
    B, _, _ = make_coefficient_matrix(new_tree,
                                      node_keys=node_keys,
                                      branch_keys=branches)

    x_A = get_specific_branch_lengths(old_tree, branches[:-3])
    x_B = get_specific_branch_lengths(new_tree, branches)

    B2 = B.dot(U_matrix)

    lambd = pinv(B2.dot(B2.T)).dot(A - B2).dot(x_A)

    mu_new = (B2.T).dot(lambd) + x_A

    x_new_reduced = mu_new + norm.rvs(scale=sigma, size=len(mu_new))

    q_forward = reduce(mul, norm.pdf(mu_new - x_new_reduced, scale=sigma))

    x_new = U_matrix.dot(x_new_reduced)
    print 'x_A', x_A
    print 'x_B', x_B
    print 'mu_new', mu_new
    print 'x_new_reduced', x_new_reduced
    print 'x_new', x_new

    reverse_lambd = pinv(A.dot(A.T)).dot(B2 - A).dot(x_new_reduced)
    reverse_mu_new = (A.T).dot(reverse_lambd) + x_new_reduced

    print 'matrix_rank , dimension (A)', matrix_rank(A), A.shape
    print 'matrix_rank , dimension (B)', matrix_rank(B), B.shape
    print 'mu_reverse', reverse_mu_new

    q_backward = reduce(mul, norm.pdf(reverse_mu_new - x_A, scale=sigma))

    #wear the new values
    #print branches

    new_tree = update_specific_branch_lengths(new_tree, branches, x_new)

    return new_tree, q_forward, q_backward
コード例 #4
0
            count+=1
    return all_nodes

if __name__=='__main__':
    from Rcatalogue_of_trees import *
    from Rtree_operations import pretty_string, create_trivial_tree, get_specific_branch_lengths, update_specific_branch_lengths
    print pretty_string(tree_good)
    coef, ni, bi= make_coefficient_matrix(tree_good)
    nodes_determined = [None]*len(ni)
    branches_determined=[None]*len(bi)
    for n,i in ni.items():
        nodes_determined[i]=n
        
    for b,i in bi.items():
        branches_determined[i]=b
    branch_lengths=get_specific_branch_lengths(tree_good, branches_determined)
    
    from numpy import array
    print coef
    print coef.dot(array(branch_lengths))
    from Rtree_to_covariance_matrix import make_covariance
    from numpy.random import normal
    print make_covariance(tree_good, node_keys= nodes_determined)
    from numpy import set_printoptions
    set_printoptions(precision=2)
    org, bi,_= get_orthogonal_branch_space(tree_good, add_one_column=True)
    branches_determined=[None]*len(bi) 
    for b,i in bi.items():
        branches_determined[i]=b
    
    updates=org.dot(normal(scale=0.01, size=org.shape[1]))
コード例 #5
0
def insert_admix(tree,
                 source_key,
                 source_branch,
                 sink_key,
                 sink_branch,
                 source_name=None,
                 sink_name=None,
                 pks={},
                 new_branch_length=None,
                 new_to_root_length=None,
                 preserve_root_distance=False):
    #print "new branch", new_branch_length
    branches = get_all_branches(tree)
    new_branches = deepcopy(branches)
    branch_indices = {branch: n for n, branch in enumerate(branches)}
    no_branches = len(branches)
    U_matrix = identity(no_branches)
    branch_lengths = get_specific_branch_lengths(tree, branches)
    total_length = sum(branch_lengths)
    if sink_name is None:
        sink_name = str(getrandbits(128)).strip()
    if source_name is None:
        source_name = str(getrandbits(128)).strip()
    if source_key == 'r':
        u1, q1 = get_root_branch_length()
        if new_to_root_length is not None:
            u1, q1 = new_to_root_length, get_root_branch_length(
                new_to_root_length)
        U_matrix = insert(U_matrix,
                          U_matrix.shape[0],
                          [u1 / total_length] * U_matrix.shape[1],
                          axis=0)
    else:
        u1, q1 = get_insertion_spot(
            length=get_branch_length(tree, source_key, source_branch))
        index = branch_indices[(source_key, source_branch)]
        insertion = [0] * U_matrix.shape[1]
        insertion[index] = 1.0 - u1
        U_matrix[index, index] = u1
        U_matrix = insert(U_matrix, U_matrix.shape[0], insertion, axis=0)
    branches.append((source_name, 0))

    u2, q2 = get_insertion_spot(
        length=get_branch_length(tree, sink_key, sink_branch))
    index = branch_indices[(sink_key, sink_branch)]
    insertion = [0] * U_matrix.shape[1]
    insertion[index] = 1.0 - u2
    U_matrix[index, index] = u2
    #print U_matrix
    U_matrix = insert(U_matrix, U_matrix.shape[0], insertion, axis=0)
    branches.append((sink_name, 0))

    if new_branch_length is not None:
        t4, q4 = new_branch_length, get_admixture_branch_length(
            new_branch_length)
    else:
        t4, q4 = get_admixture_branch_length()
    u3, q3 = get_admixture_proportion()
    U_matrix = insert(U_matrix,
                      U_matrix.shape[0],
                      [t4 / total_length] * U_matrix.shape[1],
                      axis=0)
    branches.append((sink_name, 1))

    tree = insert_admixture_node_halfly(tree,
                                        sink_key,
                                        sink_branch,
                                        u2,
                                        admix_b_length=t4,
                                        new_node_name=sink_name,
                                        admixture_proportion=u3)
    #print 'tree after inserting admixture', tree
    tree = graft(tree,
                 sink_name,
                 source_key,
                 u1,
                 source_name,
                 source_branch,
                 remove_branch=1)

    #print 'old_t1', tree[sink_name][3]
    if preserve_root_distance:
        tree[sink_name], multip = readjust_length(tree[sink_name])
    else:
        multip = 1.0
    #print 'new_t1', tree[sink_name][3]

    new_branch = 1
    if random() < 0.5:
        new_branch = 0
        tree[sink_name] = change_admixture(tree[sink_name])
        branches[-2:] = list(reversed(branches[-2:]))
    pks['t5'] = t4
    pks['t1'] = u1
    pks['sink_new_name'] = sink_name
    pks['sink_new_branch'] = new_branch
    #print 'tree after grafting', tree
    return tree, q1 * q2 * q3 * q4, 1, multip, U_matrix, branches