def read_treemix_file2(filename_treeout,
                       filename_vertices,
                       filename_edges,
                       outgroup=None):
    if filename_treeout.endswith('.gz'):
        filename_treeout = unzip(filename_treeout)
    if filename_vertices.endswith('.gz'):
        filename_vertices = unzip(filename_vertices)
    if filename_edges.endswith('.gz'):
        filename_edges = unzip(filename_edges)
    with open(filename_treeout, 'r') as f:
        newick_tree = f.readline().rstrip()
        admixtures = parse_admixtures2(map(str.rstrip, f.readlines()))
    vertice_dictionary = read_vertices(filename_vertices)
    print vertice_dictionary
    edges = get_edge_lengths2(filename_edges)
    #print newick_tree
    tree = make_Rtree(edges, vertice_dictionary, admixtures)
    print pretty_string(tree)
    #tree=remove_children(tree)
    if outgroup is not None:
        tree = rearrange_root(tree, outgroup)
        print 'after rearrangement'
        print pretty_string(tree)
    return tree
def make_Rtree(edges, vertice_dictionary, admixtures):
    root_name = get_root(vertice_dictionary).name
    all_nodes = construct_pointers(edges)
    all_nodes, vertice_dictionary, edges = prune_tree_structure(
        all_nodes, vertice_dictionary, edges)
    print_all_nodes(all_nodes)
    leaf_nodes = get_leaf_nodes(all_nodes)
    rTree_structure = tree_structure_to_Rtree_structure(
        leaf_nodes, edges, root_name)
    #print pretty_string(rTree_structure)
    print 'Before inserting admixture proportions:', pretty_string(
        rTree_structure)
    tree = adjust_admixture_proportions(rTree_structure, vertice_dictionary,
                                        admixtures)
    print 'After admixture proportions, before double node pruning', pretty_string(
        tree)
    #tree=prune_double_nodes(tree)
    print 'After pruning, before renaming tree leaves', pretty_string(tree)
    tree = remap_leaves(tree, vertice_dictionary)
    print 'After remapping tree leaves', pretty_string(tree)
    return tree
 def __call__(self, tree, **not_needed):
     #print tree
     #print not_needed
     #print tree
     Rtree = identifier_to_tree_clean(
         tree, leaves=generate_predefined_list_string(deepcopy(self.nodes)))
     #print pretty_string(Rtree)
     if self.subnodes:  #DETTE TAGER IKKE ORDENTLIG HOJDE FOR KOVARIANSMATRICERNE SOM BLIVER FORKERTE
         try:
             Rtree = get_subtree(Rtree, self.subnodes)
         except AssertionError:
             print pretty_string(Rtree)
             from tree_plotting import plot_as_directed_graph
             plot_as_directed_graph(Rtree)
             print 'input_tree', tree
             print 'nodes', self.nodes
             print 'subnodes', self.subnodes
             assert False
     if self.remove_sadtrees and (not admixes_are_sadmixes(Rtree)):
         print 'returned true because adtrees are not sad'
         return {'Rtree': Rtree}, True
     return {'Rtree': Rtree}, False
    #print 'resimulated', old_length, 'to', new_length, 'on branch', str((key,branch)), 'and change in lattitude at', delta_L, 'backward',  backward
    return tree, 1.0, backward
    

if __name__=='__main__':
    
    test_tree={ 's3': ['x', None, None, 0.3, None, None, None],
                's2': ['a', None, None, 0.05, None, None, None],
                's1': ['d', None, None, 0.1, None, None, None],
                's4': ['b', None, None, 0.3, None, None, None],
                'a': ['b', 'c', 0.5, 0.2, 0.1, 's2', None],
                'c': ['x', 'd', 0.5, 0.083428092356333014, 0.1, 'a', None],
                'b': ['f', None, None, 0.05, None, 's4', 'a'],
                'd': ['r', None, None, 0.05, None, 's1', 'c'],
                'f': ['r', None, None, 0.02, None, 'b', 'x'],
                'x': ['f', None, None, 0.066571907643666994, None, 's3', 'c']}
    
    
    
    from Rcatalogue_of_trees import tree_good
    t=tree_good
    print pretty_string(t)
    letters=['n'+str(i) for i in xrange(100000)]
    for i in xrange(100):
        t,f,b= make_sliding_regraft(t, new_node=letters[i] , resimulate_moved_branch_length=True)
        print 'forward', f
        print 'backward',b
        print 'tree'
        print pretty_string(t)

    
        filename_vertices,
        filename_edges,
        outgroup='out',
        snodes=['s' + str(i) for i in range(1, 11)],
        prefix='sletmig' + os.sep,
        return_format='outgroup_rooted')
    #tree=read_treemix_file2('../../../../Dropbox/Bioinformatik/AdmixtureBayes/treemix_example3/new_one2.treeout',
    #                       '../../../../Dropbox/Bioinformatik/AdmixtureBayes/treemix_example3/new_one2.vertices',
    #                       '../../../../Dropbox/Bioinformatik/AdmixtureBayes/treemix_example3/new_one2.edges', outgroup='out')
    import tree_plotting
    tree_plotting.plot_as_directed_graph(tree)
    from tree_warner import check

    check(tree)

    print pretty_string(tree)
    import numpy as np
    print pretty_string(tree)
    from Rtree_to_covariance_matrix import make_covariance
    from reduce_covariance import reduce_covariance, Areduce
    cov = make_covariance(tree,
                          node_keys=['out'] +
                          ['s' + str(i) for i in range(1, 10)])
    print cov
    cov2 = np.loadtxt(
        '../../../../Dropbox/Bioinformatik/AdmixtureBayes/treemix_example3/anew.txt'
    )
    np.set_printoptions(precision=6, linewidth=200, suppress=True)
    print cov - cov2
    print reduce_covariance(cov - cov2, 0)
    print Areduce(cov - cov2)
Example #6
0
def addadmix(tree,
             new_node_names=None,
             pks={},
             fixed_sink_source=None,
             new_branch_length=None,
             new_to_root_length=None,
             check_opposite=False,
             preserve_root_distance=True):
    '''
    This proposal adds an admixture to the tree. There are a lot of free parameters but only 5 are in play here:
        c1: the branch length of the source population
        c2: the branch length of the population genes are migrating into. 
        u1: the position of the admixture source on the source population branch
        u2: the position of the admixture destination on the sink population branch
        w: the admixture proportion.
        The connecting function, h, (see Green & Hastie 2009) is
            h(c1,c2,u1,u2,w)=(c1*u1, c1*(1-u1), c2*u2, c2*(1-u2), 0.5*w)
    '''

    possible_nodes = _get_possible_starters(tree)

    no_admixtures = get_number_of_admixes(tree)
    new_tree = deepcopy(tree)
    #print possible_nodes
    sink_key, sink_branch = possible_nodes[choice(len(possible_nodes), 1)[0]]
    if fixed_sink_source is not None:
        sink_key, sink_branch, source_key, source_branch = fixed_sink_source
    children, other = get_all_branch_descendants_and_rest(
        tree, sink_key, sink_branch)
    candidates = other + [('r', 0)]
    ch = choice(len(candidates), 1)[0]
    if fixed_sink_source is None:
        source_key, source_branch = candidates[ch]

    pks['sink_key'] = sink_key
    pks['source_key'] = source_key
    pks['source_branch'] = source_branch
    pks['sink_branch'] = sink_branch
    #print 'children', children
    #print 'candidates', candidates
    #print 'sink', (sink_key, sink_branch)
    #print 'source', (source_key,source_branch)
    #print 'new_tree',new_tree
    if fixed_sink_source is not None:
        new_tree, forward_density, backward_density, multip = insert_admix(
            new_tree,
            source_key,
            source_branch,
            sink_key,
            sink_branch,
            pks=pks,
            new_branch_length=new_branch_length,
            new_to_root_length=new_to_root_length,
            preserve_root_distance=preserve_root_distance)
    elif new_node_names is None:
        new_tree, forward_density, backward_density, multip = insert_admix(
            new_tree,
            source_key,
            source_branch,
            sink_key,
            sink_branch,
            pks=pks,
            preserve_root_distance=preserve_root_distance)
    else:
        new_tree, forward_density, backward_density, multip = insert_admix(
            new_tree,
            source_key,
            source_branch,
            sink_key,
            sink_branch,
            pks=pks,
            source_name=new_node_names[0],
            sink_name=new_node_names[1],
            preserve_root_distance=preserve_root_distance)

    choices_forward = float(len(possible_nodes) * len(candidates)) * 2
    choices_backward = float(len(_get_removable_admixture_branches(new_tree)))

    pks['forward_density'] = forward_density
    pks['backward_density'] = backward_density
    pks['forward_choices'] = choices_forward
    pks['backward_choices'] = choices_backward

    if check_opposite:
        pks2 = {}
        t, f, b = deladmix(new_tree,
                           pks=pks2,
                           fixed_remove=(pks['sink_new_name'],
                                         pks['sink_new_branch']),
                           check_opposite=False,
                           preserve_root_distance=preserve_root_distance)
        if (float_equal(forward_density, pks2['backward_density'])
                and choices_forward == pks2['backward_choices']
                and float_equal(backward_density, pks2['forward_density'])
                and choices_backward == pks2['forward_choices']):
            print 'test passed'
        else:
            print 'TEST FAILED'
            print forward_density, "==", pks2[
                'backward_density'], ":", forward_density == pks2[
                    'backward_density']
            print backward_density, "==", pks2[
                'forward_density'], ":", backward_density == pks2[
                    'forward_density']
            print choices_forward, "==", pks2[
                'backward_choices'], ":", choices_forward == pks2[
                    'backward_choices']
            print choices_backward, "==", pks2[
                'forward_choices'], ":", choices_backward == pks2[
                    'forward_choices']
            print pretty_string(tree)
            print pretty_string(new_tree)
            print pretty_string(t)
            for key, val in pks.items():
                print key, ': ', val
            print "-----------"
            for key, val in pks2.items():
                print key, ': ', val
            assert False

    return new_tree, forward_density / choices_forward, backward_density / choices_backward * multip
Example #7
0
def deladmix(tree,
             pks={},
             fixed_remove=None,
             check_opposite=False,
             preserve_root_distance=True):
    '''
    Reversible Jump MCMC transition which removes a random admixture branch. This is the reverse of the other proposal in this file. 
    '''

    #making copy that we can erase branches from.
    cop = deepcopy(tree)

    candidates = _get_removable_admixture_branches(cop)
    #print candidates
    if len(candidates) == 0:
        return tree, 1, 1
    if fixed_remove is None:
        remove_key, remove_branch = candidates[choice(len(candidates), 1)[0]]
    else:
        remove_key, remove_branch = fixed_remove
    pks['remove_key'] = remove_key
    pks['remove_branch'] = remove_branch
    #get_keys_and_branches_from_children()
    #print 'remove', (remove_key, remove_branch)

    new_tree, (t1, t2, t3, t4, t5), alpha = remove_admix2(cop,
                                                          remove_key,
                                                          remove_branch,
                                                          pks=pks)
    #pks['sink_key']=sink_key
    #pks['sink_branch']=sink_branch
    #pks['removed_alpha']=alpha
    pks['t1'] = t1
    pks['t2'] = t2
    pks['t3'] = t3
    pks['t4'] = t4
    pks['t5'] = t5

    if preserve_root_distance:
        #print t1
        multip = (alpha**2 + (1.0 - alpha)**2)
        old_length = t2 + multip * t1
        t1 = old_length - t2
        #print old_length, t1,t2
        child_key, child_branch = get_keys_and_branches_from_children(
            tree, remove_key)[0]
        update_branch_length(new_tree, child_key, child_branch, old_length)
    else:
        multip = 1.0
    backward_density = get_backward_remove_density(t1, t2, t3, t4, t5, alpha)
    forward_density = 1.0

    forward_choices = float(len(candidates))
    backward_choices = float(
        get_possible_admixture_adds(new_tree, pks['orphanota_key'],
                                    pks['orphanota_branch'])) * 2
    pks['forward_choices'] = forward_choices
    pks['backward_choices'] = backward_choices
    pks['forward_density'] = forward_density
    pks['backward_density'] = backward_density

    if check_opposite:
        pks2 = {}
        if t4 is None:
            new_to_root_length = t3
        else:
            new_to_root_length = None
        t, f, b = addadmix(new_tree,
                           pks=pks2,
                           fixed_sink_source=(pks['orphanota_key'],
                                              pks['orphanota_branch'],
                                              pks['sorphanota_key'],
                                              pks['sorphanota_branch']),
                           new_branch_length=t5,
                           new_to_root_length=new_to_root_length,
                           check_opposite=False,
                           preserve_root_distance=preserve_root_distance)
        if (float_equal(forward_density, pks2['backward_density'])
                and forward_choices == pks2['backward_choices']
                and float_equal(backward_density, pks2['forward_density'])
                and backward_choices == pks2['forward_choices']):
            print 'test passed'
        else:
            print 'TEST FAILED'
            print forward_density, "==", pks2[
                'backward_density'], ":", forward_density == pks2[
                    'backward_density']
            print backward_density, "==", pks2[
                'forward_density'], ":", backward_density == pks2[
                    'forward_density']
            print forward_choices, "==", pks2[
                'backward_choices'], ":", forward_choices == pks2[
                    'backward_choices']
            print backward_choices, "==", pks2[
                'forward_choices'], ":", backward_choices == pks2[
                    'forward_choices']

            print pretty_string(tree)
            print pretty_string(new_tree)
            print pretty_string(t)
            for key, val in pks.items():
                print key, ': ', val
            print "----------------"
            for key, val in pks2.items():
                print key, ': ', val
            assert False

    return new_tree, forward_density / forward_choices, backward_density / backward_choices * multip

class rescale_constrained_class(object):
    new_nodes = 0
    proposal_name = 'rescale_constrained'
    adaption = True
    input = 'both'
    require_admixture = 0
    reverse_require_admixture = 0
    admixture_change = 0
    reverse = 'rescale_constrained'

    def __init__(self, **kwargs):
        self.kwargs = kwargs

    def __call__(self, *args, **kwargs):
        kwargs.update(self.kwargs)
        #print kwargs
        return rescale(*args, **kwargs)


if __name__ == '__main__':
    from tree_plotting import plot_graph
    from Rcatalogue_of_trees import tree_on_the_border2_with_children
    from Rtree_operations import pretty_string
    (new_tree, new_add), _, _ = rescale(
        (tree_on_the_border2_with_children, 0.1))

    print 'old_tree', pretty_string(tree_on_the_border2_with_children)
    print 'new_tree', pretty_string(new_tree)
Example #9
0
def identifier_to_tree(identifier, leaves=None, inner_nodes=None, branch_lengths=None, admixture_proportions=None):
    '''
    Transforms an identifier of the form qwert-uio-asdfg-jk into a dictionary tree using the generators of leaves, inner_nodes, branch_lengths and admixture_proportions.
    '''
    
    levels=identifier.split('-')
    n_leaves=len(levels[0].split('.'))
    
    #initiate leaves
    if leaves is None:
        leaf_values=sorted(get_trivial_nodes(n_leaves))
    else:
        leaf_values=[leaves() for _ in range(n_leaves)]
    tree={leaf:[None]*5 for leaf in leaf_values}
    trace_lineages=[(leaf,0) for leaf in leaf_values]
    
    #initiate generators
    if inner_nodes is None:
        inner_nodes=generate_numbered_nodes('n')
    if branch_lengths is None:
        def f(): 
            return 1.0
        branch_lengths= f
    if admixture_proportions is None:
        def g():
            return 0.4
        admixture_proportions=g
    for level in levels:
        identifier_lineages=level.split('.')
        assert len(trace_lineages)==len(identifier_lineages), 'the number of traced lineages did not match the number of lineages in the identifier '+\
                                                               '\n\n'+'trace_lineages:'+'\n'+str(trace_lineages)+\
                                                               '\n\n'+'identifier_lineages:'+'\n'+str(identifier_lineages)
        parent_index={}
        indexes_to_be_removed=[]
        for n,identifier_lineage in enumerate(identifier_lineages):
            if identifier_lineage=='c':
                ##there is a coalecence for the n'th lineage, and it should be replaced by a new lineage
                new_key=inner_nodes()
                old_key,old_branch=trace_lineages[n]
                new_branch_length=branch_lengths()
                tree=update_parent_and_branch_length(tree, old_key, old_branch, new_key, new_branch_length)
                tree[new_key]=[None]*5
                parent_index[n]=new_key
                trace_lineages[n]=(new_key,0)
            elif identifier_lineage=='w':
                pass
            elif identifier_lineage=='a':
                new_key=inner_nodes(admixture=True)
                old_key,old_branch=trace_lineages[n]
                new_branch_length=branch_lengths()
                tree=update_parent_and_branch_length(tree, old_key, old_branch, new_key, new_branch_length)
                new_admixture_proportion=admixture_proportions()
                tree[new_key]=[None,None,new_admixture_proportion,None,None]
                trace_lineages[n]=(new_key,0)
                trace_lineages.append((new_key,1))
            else:
                ##there is a coalescence but this lineage disappears
                try:
                    new_key=parent_index[int(identifier_lineage)]
                except KeyError as e:
                    print e
                    print 'new_key', new_key
                    print 'parent_index', parent_index
                    print 'identifier_lineage', identifier_lineage
                    print pretty_string(insert_children_in_tree(tree))
                
                old_key,old_branch=trace_lineages[n]
                new_branch_length=branch_lengths()
                tree=update_parent_and_branch_length(tree, old_key, old_branch, new_key, new_branch_length)
                indexes_to_be_removed.append(n)
        
        ##remove lineages
        trace_lineages=[trace_lineage for n,trace_lineage in enumerate(trace_lineages) if n not in indexes_to_be_removed]
    root_key=new_key
    del tree[root_key]
    tree=rename_root(tree, new_key)
    
    return insert_children_in_tree(tree)
Example #10
0
        's4':['n4',None,None,1.3,None,None,None],
        'n4':['r',None,None,0.3,None,'n3','s4'],
        's5':['r',None,None,1.2,None,None,None]}
 
 print unique_identifier_and_branch_lengths(tree1)
 
 from tree_warner import check
 from tree_plotting import pretty_string
 tree='c.c.w.w.a.0.1.w.w-w.a.w.c.3.w.w.w-c.w.w.0.w.w.c.9-c.w.w.w.w.0-c.w.0.w.w-c.w.0.w-c.0.w-c.0;0.012-0.038-0.05-0.062-0.029-0.068-0.068-0.097-0.005-0.048-0.083-0.024-0.123-0.057-0.024-0.047-0.058-0.144-0.079-0.04-0.035-0.032;0.086-0.219'
 tree2='c.c.w.w.a.0.1.w.w-w.a.w.c.3.w.w.w-c.w.w.0.w.w.c.6-c.w.w.w.w.0-c.w.0.w.w-c.w.0.w-c.0.w-c.0;0.012-0.038-0.05-0.062-0.029-0.068-0.068-0.097-0.005-0.048-0.083-0.024-0.123-0.057-0.024-0.047-0.058-0.144-0.079-0.04-0.035-0.032;0.586-0.219'
 tree3={'136': ['32', None, None, 0.000485164, None, 's8', 's6'], '172': ['16', None, None, 0.00101341, None, '244', '32'], '233': ['r', None, None, 0.000104723, None, 's7', None], 's9': ['32', None, None, 0.00224709, None, None, None], 's8': ['136', None, None, 0.00318685, None, None, None], 's3': ['244', None, None, 0.00230204, None, None, None], 's2': ['104', None, None, 0.00224093, None, None, None], 's1': ['103pruned', None, None, 0, None, None, None], 's7': ['233', None, None, 0.0, None, None, None], 's6': ['136', None, None, 0.0015962, None, None, None], 's5': ['52', None, None, 0.000780002, None, None, None], 's4': ['76', None, None, 0.0025597, None, None, None], '0': ['16', None, None, 0.000394219, None, 's10', '2'], '2': ['0', None, None, 0.000262294, None, 'out', '76'], '103pruned': ['104', '244', 0.875419, 0.00235895, 0, 's1', None], 'out': ['2', None, None, 0.00158848, None, None, None], 's10': ['0', None, None, 1.76418e-05, None, None, None], '244': ['172', None, None, 0.000340227, None, 's3', '103pruned'], '104': ['76', None, None, 0.000879448, None, 's2', '103pruned'], '76': ['2', None, None, 0.000144781, None, '104', 's4'], '32': ['172', None, None, 0.00148711, None, '136', 's9'], '16': ['52', None, None, 0.0041016, None, '172', '0'], '52': ['r', None, None, 0.000104723, None, '16', 's5']}
 tree4={'136': ['32', None, None, 0.000485164, None, 's8', 's6'], '172': ['16', None, None, 0.00101341, None, '244', '32'], '233': ['r', None, None, 0.000104723, None, 's7', None], 's9': ['32', None, None, 0.00224709, None, None, None], 's8': ['136', None, None, 0.00318685, None, None, None], 's3': ['244', None, None, 0.00230204, None, None, None], 's2': ['104', None, None, 0.00224093, None, None, None], 's1': ['103pruned', None, None, 0, None, None, None], 's7': ['233', None, None, 0.0, None, None, None], 's6': ['136', None, None, 0.0015962, None, None, None], 's5': ['52', None, None, 0.000780002, None, None, None], 's4': ['76', None, None, 0.0025597, None, None, None], '0': ['16', None, None, 0.000394219, None, 's10', '2'], '2': ['0', None, None, 0.000262294, None, 'out', '76'], '103pruned': ['104', '244', 0.875419, 0.00235895, 0, 's1', None], 'out': ['2', None, None, 0.00158848, None, None, None], 's10': ['0', None, None, 1.76418e-05, None, None, None], '244': ['172', None, None, 0.000340227, None, 's3', '103pruned'], '104': ['76', None, None, 0.000879448, None, 's2', '103pruned'], '76': ['2', None, None, 0.000144781, None, '104', 's4'], '32': ['172', None, None, 0.00148711, None, '136', 's9'], '16': ['52', None, None, 0.0041016, None, '172', '0'], '52': ['r', None, None, 0.000104723, None, '16', 's5']}
 ft=identifier_to_tree_clean(tree2)
 ft=tree3
 print check(ft)
 print pretty_string(ft)
 print unique_identifier_and_branch_lengths(ft)
 print tree_to_0ntree(ft)
 print tree_to_mode_ntree(ft)
 print tree_to_random_ntree(ft)
 print majority_tree(ft)
 
 from sys import exit
 exit()
 from Rcatalogue_of_trees import *
 
 from Rtree_operations import create_burled_leaved_tree
 
 print generation_counts(tree_good)
 print get_timing(tree_good)
 ad= unique_identifier_and_branch_lengths(tree_good)
Example #11
0
def get_all_pairs(nodes):
    all_nodes={}
    count=0
    for node_index in range(len(nodes)):
        for node_index2 in range(node_index, len(nodes)):
            node1=nodes[node_index]
            node2=nodes[node_index2]
            all_nodes[(node1, node2)]=count
            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
Example #12
0
        's10': ['n13', None, None, 0.017452689, None, None, None],
        'a1': ['a4', 'n12', 0.32, 0.008438453, 0.006708239, 's1', None],
        'a2': ['n5', 'n6', 0.135, 0.12066482, 0.001733599, 's5', None],
        'a4': ['a7', 'n9', 0.599, 0.035053964, 0.041518206, 'a1', None],
        'a7': ['n10', 'n11', 0.401, 0.029920208, 0.009904888, 'a4', None],
        'n8': ['n10', None, None, 0.009578977, None, 's3', 'n5'],
        'n9': ['n12', None, None, 0.033982401, None, 's7', 'a4'],
        'n3': ['r', None, None, 0.069890962, None, 's9', 's6'],
        'n5': ['n8', None, None, 0.008113672, None, 's4', 'a2'],
        'n6': ['n15', None, None, 0.002455554, None, 's8', 'a2']
    }

    #print plot_as_directed_graph(tree)
    sub_tree = get_subtree(tree, ['s1', 's2', 's3'])
    #print plot_as_directed_graph(sub_tree)
    print pretty_string(sub_tree)
    #plots=get_unique_plottable_tree(sub_tree)
    #print 'gotten unique_plottable'
    #print plots

    stree_difficult = 'a.w.w.c.c.w.c.4.3.6-c.w.0.w.c.w.w.4-c.w.w.w.w.0-c.w.w.w.0-c.0.w.w-c.0.w-c.0;0.014843959-0.003602704-0.002128203-0.027030132-0.008484730-0.067616899-0.021207056-0.027455759-0.011647297-0.009065170-0.053386961-0.001718477-0.009310923-0.010471979-0.036314546-0.004808845-0.055956235-0.004694887-0.003482668-0.039323330-0.014821628;1.000'
    from tree_statistics import (identifier_to_tree_clean,
                                 generate_predefined_list_string,
                                 identifier_file_to_tree_clean,
                                 unique_identifier_and_branch_lengths)
    from Rtree_to_covariance_matrix import make_covariance
    nodes = sorted(['s' + str(i + 1) for i in range(10)])
    tree_difficult = identifier_to_tree_clean(
        stree_difficult,
        leaves=generate_predefined_list_string(deepcopy(nodes)))
    cov1 = make_covariance(tree_difficult)
Example #13
0
def get_true_posterior(wishart_file='tmp_covariance_and_multiplier.txt',
                       true_tree_file='tmp_scaled_true_tree.txt',
                       p=0.5,
                       use_skewed_distr=False,
                       wishart_df_file='tmp_wishart_DF.txt',
                       outgroup='out'):
    true_tree, nodes = read_tree_file(true_tree_file)
    set_printoptions(precision=2)
    multiplier = 8.57292960745
    print make_covariance(scale_tree_copy(true_tree, multiplier),
                          node_keys=nodes)
    print reduce_covariance(
        make_covariance(scale_tree_copy(true_tree, multiplier),
                        node_keys=nodes),
        len(nodes) - 1)
    print true_tree
    print nodes
    print outgroup
    print pretty_string(true_tree)
    x = get_pruned_tree_and_add(true_tree, outgroup)
    print pretty_string(x[0])
    print x[1]

    print nodes
    nodes.remove(outgroup)
    covariance, multiplier = read_wishart_file(wishart_file, nodes)
    print 'multiplier', multiplier
    #multiplier=1.0
    print covariance
    #print make_covariance(scale_tree_copy(x[0], multiplier))
    t_covariance, t_multiplier = rescale_empirical_covariance(
        make_covariance(x[0]) + x[1])
    print t_covariance
    print t_covariance - covariance
    print(t_covariance) / covariance
    print(t_covariance / t_multiplier) - covariance / multiplier
    print(t_covariance / t_multiplier) / (covariance / multiplier)
    avg_scale = mean((make_covariance(scale_tree_copy(x[0], 1.0)) + x[1]) *
                     multiplier / covariance)
    avg_root = get_average_distance_to_root(x[0])
    max_root = get_max_distance_to_root(x[0])
    min_root = get_min_distance_to_root(x[0])
    wishart_df = read_wishart_df_file(wishart_df_file)
    posterior = posterior_class(covariance,
                                M=wishart_df,
                                p=p,
                                use_skewed_distr=use_skewed_distr,
                                multiplier=multiplier,
                                nodes=nodes)
    pks = {}
    a = posterior(x, pks)
    print a
    prior_val = pks['prior']
    lik_vals = [
        posterior.get_likelihood_from_matrix(t_covariance * c)
        for c in linspace(0.1, 2.5, 1500)
    ]
    print lik_vals
    n = sorted([(v, e) for e, v in enumerate(lik_vals)])[-1][1]
    print n, linspace(0.1, 2.5, 1500)[n]
    print lik_vals[n]
    print linspace(0.1, 2.5, 1500)[n] * t_covariance
    print covariance
    print t_covariance
    print posterior.get_likelihood_from_matrix(t_covariance)
    print sum((covariance - t_covariance)**2)
    print sum((covariance - t_covariance * linspace(0.1, 2.5, 1500)[n])**2)
    t_cov2 = deepcopy(t_covariance)
    t_cov2[3, 5] = t_cov2[5, 3] = 1.6
    t_cov2[1, 2] = t_cov2[2, 1] = 2.45
    print t_cov2
    print posterior.get_likelihood_from_matrix(t_cov2)
    print posterior.get_likelihood_from_matrix(covariance)
    print posterior.get_likelihood_from_matrix(
        covariance * (wishart_df / (wishart_df - covariance.shape[0] - 1)))
    return prior_val + max(lik_vals)
Example #14
0
 
 #print reduce_covariance(identity(10), 5)
 
 #print file_to_emp_cov('out_stem.cov',4)
 
 
 
 
 from sys import exit
 
 exit()
 from generate_prior_trees import generate_phylogeny
 from Rcatalogue_of_trees import *
 from Rtree_operations import create_trivial_tree, scale_tree
 tree2=scale_tree(generate_phylogeny(5,1),0.05)
 print pretty_string(tree2)
 print pretty_string(identifier_to_tree_clean(unique_identifier_and_branch_lengths(tree2)))
 print supplementary_text_ms_string()
 tree_good=generate_phylogeny(7)
 a=tree_to_ms_command(tree_good, 50,20)
 #print call_ms_string(a, 'supp.txt')
 b=time_adjusted_tree_to_ms_command(tree_good,50,20)
 #print call_ms_string(b, 'supp2.txt')
 #print call_ms_string(tree_to_ms_command(tree2, 50,20), 'tmp.txt')
 #cov= ms_to_treemix2('supp.txt', 20, 20,400)
 #cov= ms_to_treemix2('tmp.txt', 50, 5,20)
 #cov2=calculate_covariance_matrix('tmp.txt', 50, 5,20)
 #print cov
 #print cov2
 #print make_covariance(tree2)
 #print reduce_covariance(cov, 0)