Example #1
0
def test_unsupported_model():
    test_newick = """
        (a:1[&&momi:model=exponential:N=2.0:lineages=10],
         b:1[&&momi:model=constant:N=1.5:lineages=8]):3[&&momi:model=constant:N=10.0];
         """
    with pytest.raises(Exception):
        demo = Demography.from_newick(test_newick)
Example #2
0
def time_runs(args):
    ms_path, n_taxa, lineages_per_taxon, moranOnly = args

    print "# Starting job with n_per_deme=%d, n_demes=%d" % (lineages_per_taxon, n_taxa)

    # Get a random phylogeny
    newick_str = random_binary_tree(n_taxa)
    demo = Demography.from_newick(newick_str, lineages_per_taxon)

    n = n_taxa * lineages_per_taxon
    results = []
    snp_list = run_simulation(ms_path, demo, 100, lineages_per_taxon)
    for snp,state in enumerate(snp_list):
        #print(state)
        state_tuple = tuple([v['derived'] for k,v in sorted(state.iteritems())])
        rid = random.getrandbits(32)

        method_list = [("moran",False)]
        if not moranOnly:
            method_list += [("chen",True)]

        for name,use_chen_eqs in method_list:
            with Timer() as t:
                ret = demo.sfs(state, use_chen_eqs)
            results.append((name,n_taxa, lineages_per_taxon, snp, t.interval, ret, rid, str(state_tuple), newick_str))

    print "# Finished job with n_per_deme=%d, n_demes=%d" % (lineages_per_taxon, n_taxa)
    return results
Example #3
0
def do_job((ms_path, n_demes, n_per_pop, moranOnly, seed)):
    print "# Starting job with n_per_deme=%d, n_demes=%d" % (n_per_pop, n_demes)

    random.seed(seed)

    # Get a random phylogeny
    newick_str = random_binary_tree(n_demes)
    demo = Demography.from_newick(newick_str, n_per_pop)

    n = n_demes * n_per_pop
    snp_list = run_simulation(ms_path, demo, loci_per_rep, n_per_pop)
    snp_list = [{pop: counts['derived'] for pop,counts in snp.iteritems()}
                for snp in snp_list]

    # only keep unique snps
    snp_list = set([snp.iteritems() for snp in snp_list])
    snp_list = map(dict, snp_list)

    method_list = [("momi",False)]
    if not moranOnly:
        method_list += [("Chen",True)]

    results = {}
    for name,use_chen_eqs in method_list:
        with Timer() as t:
            demo.compute_sfs([snp_list[0]], use_chen_eqs)
        precompute_t = t.interval

        with Timer() as t:
            results[name] = {'sfs': demo.compute_sfs(snp_list, use_chen_eqs)}
        results[name].update({'timing': {"'Per SNP'" : t.interval / float(len(snp_list)), "'Precomputation'" : precompute_t}})

    print "# Finished job with n_per_deme=%d, n_demes=%d" % (n_per_pop, n_demes)
    return {'n': n_demes * n_per_pop, 'n_pops' : n_demes, 'results': results, 'seed' : seed}
Example #4
0
def test_unsupported_model():
    test_newick = """
        (a:1[&&momi:model=exponential:N=2.0:lineages=10],
         b:1[&&momi:model=constant:N=1.5:lineages=8]):3[&&momi:model=constant:N=10.0];
         """
    with pytest.raises(Exception):
        demo = Demography.from_newick(test_newick)
Example #5
0
## where ... contains:
##    lineages= # alleles (if population is leaf)
##    model= population history (default=constant)
##    N, N_top, N_bottom= parameters for constant/exponential size history
##    model_i= model for i-th epoch of piecewise history (either constant or exponential)
##    N_i, N_top_i, N_bottom_i, tau_i= parameters for i-th epoch of piecewise history
newick_str = """
((
a:.25[&&momi:lineages=10:model=constant:N=10.0],
b:.3[&&momi:lineages=5:model=exponential:N_top=1.0:N_bottom=10.0]
):.1[&&momi:model=constant:N=1.5],
c:.3[&&momi:lineages=8:model=piecewise:model_0=exponential:tau_0=.2:N_top_0=.1:N_bottom_0=1.0:model_1=constant:tau_1=.1:N_1=.3]
)[&&momi:N=3.0]
"""

demo3 = Demography.from_newick(newick_str)

# construct a [list] of SNPs to compute SFS for
# each SNP is represented as a {dict} giving the derived allele counts

derived_counts_list = [{'a': 0, 'b': 0, 'c':1}, # singleton SNP in population 'c'
                       {'a': 2, 'b':0, 'c':0}, # doubleton SNP in population 'a'
                       {'a': 10,'b':5, 'c':0}, # SNP with derived allele fixed in 'a','b', but not present in 'c'
                       ]

# compute the SFS
sfs_list = demo3.compute_sfs(derived_counts_list)

print "Printing a few SFS entries for a 3 population demography"

print "Derived_Counts","\t","SFS_Value"
Example #6
0
def demo():
    test_newick = """
        (a:1[&&momi:model=constant:N=2.0:lineages=10],
         b:1[&&momi:model=constant:N=1.5:lineages=8]):3[&&momi:model=constant:N=10.0];
         """
    return Demography.from_newick(test_newick)
Example #7
0
def test_requires_lineages():
    with pytest.raises(Exception):
        Demography.from_newick("(a:1,b:1)") 
Example #8
0
def demo():
    test_newick = """
        (a:1[&&momi:model=constant:N=2.0:lineages=10],
         b:1[&&momi:model=constant:N=1.5:lineages=8]):3[&&momi:model=constant:N=10.0];
         """
    return Demography.from_newick(test_newick)
Example #9
0
def test_requires_lineages():
    with pytest.raises(Exception):
        Demography.from_newick("(a:1,b:1)")
Example #10
0
## where ... contains:
##    lineages= # alleles (if population is leaf)
##    model= population history (default=constant)
##    N, N_top, N_bottom= parameters for constant/exponential size history
##    model_i= model for i-th epoch of piecewise history (either constant or exponential)
##    N_i, N_top_i, N_bottom_i, tau_i= parameters for i-th epoch of piecewise history
newick_str = """
((
a:.25[&&momi:lineages=10:model=constant:N=10.0],
b:.3[&&momi:lineages=5:model=exponential:N_top=1.0:N_bottom=10.0]
):.1[&&momi:model=constant:N=1.5],
c:.3[&&momi:lineages=8:model=piecewise:model_0=exponential:tau_0=.2:N_top_0=.1:N_bottom_0=1.0:model_1=constant:tau_1=.1:N_1=.3]
)[&&momi:N=3.0]
"""

demo3 = Demography.from_newick(newick_str)

# construct a [list] of SNPs to compute SFS for
# each SNP is represented as a {dict} giving the derived allele counts

derived_counts_list = [
    {
        'a': 0,
        'b': 0,
        'c': 1
    },  # singleton SNP in population 'c'
    {
        'a': 2,
        'b': 0,
        'c': 0
    },  # doubleton SNP in population 'a'