Module dadi.Demes.Demes
Functions
def SFS(g,
sampled_demes,
sample_sizes,
pts,
sample_times=None,
Ne=None,
theta=1.0,
gamma=None,
h=None,
debug=False)-
Expand source code
def SFS( g, sampled_demes, sample_sizes, pts, sample_times=None, Ne=None, theta=1.0, gamma=None, h=None, debug=False, ): """ Takes a deme graph and computes the SFS. ``demes`` is a package for specifying demographic models in a user-friendly, human-readable YAML format. This function automatically parses the demographic description and returns a SFS for the specified populations and sample sizes. :param g: A ``demes`` DemeGraph from which to compute the SFS. :type g: :class:`demes.DemeGraph` :param sampled_demes: A list of deme IDs to take samples from. We can repeat demes, as long as the sampling of repeated deme IDs occurs at distinct times. :type sampled_demes: list of strings :param sample_sizes: A list of the same length as ``sampled_demes``, giving the sample sizes for each sampled deme. :type sample_sizes: list of ints :param sample_times: If None, assumes all sampling occurs at the end of the existence of the sampled deme. If there are ancient samples, ``sample_times`` must be a list of same length as ``sampled_demes``, giving the sampling times for each sampled deme. Sampling times are given in time units of the original deme graph, so might not necessarily be generations (e.g. if ``g.time_units`` is years) :type sample_times: list of floats, optional :param Ne: reference population size. If none is given, we use the initial size of the root deme. :type Ne: float, optional :return: A ``dadi`` site frequency spectrum, with dimension equal to the length of ``sampled_demes``, and shape equal to ``sample_sizes`` plus one in each dimension, indexing the allele frequency in each deme from 0 to n[i], where i is the deme index. :rtype: :class:`dadi.Spectrum` """ _check_demes_imported() if len(sampled_demes) != len(sample_sizes): raise ValueError("sampled_demes and sample_sizes must be same length") if sample_times is not None and len(sampled_demes) != len(sample_times): raise ValueError("sample_times must have same length as sampled_demes") for deme in sampled_demes: if deme not in g: raise ValueError(f"deme {deme} is not in demography") # Easy way to keep sample size properly ordered would be to make a dictionary? # pop_ns = {} # for pop in sampled_demes: pop_ns[pop] = sample_sizes[sampled_demes.index(pop)] # we need to copy these to new variable names # so they don't get updated during optimization sampled_pops = copy.copy(sampled_demes) deme_sample_times = copy.copy(sample_times) sampled_deme_end_times = [g[d].end_time for d in sampled_pops] if deme_sample_times is None: deme_sample_times = sampled_deme_end_times # # Redundant # for d, t in zip(sampled_pops, deme_sample_times): # if t < g[d].end_time or t >= g[d].start_time: # raise ValueError(f"sample time {t} is outside of deme {d}'s time span") if pts == None: raise ValueError("dadi requires defining pts (grid points)") # for any ancient samples, we need to add frozen branches # with this, all "sample times" are at time 0, and ancient sampled demes are frozen if np.any(np.array(deme_sample_times) != 0): g, sampled_pops, list_of_frozen_demes = _augment_with_ancient_samples( g, sampled_pops, deme_sample_times ) deme_sample_times = [0 for _ in deme_sample_times] else: list_of_frozen_demes = [] if g.time_units != "generations": g, deme_sample_times = _convert_to_generations(g, deme_sample_times) for d, n, t in zip(sampled_pops, sample_sizes, deme_sample_times): if t < g[d].end_time or t >= g[d].start_time: raise ValueError("sample time for {deme} must be within its time span") # check selection and dominance inputs if gamma is not None: if "_default" in g: raise ValueError( "Cannot use `_default` as a deme name when gamma is not None" ) if type(gamma) is dict: for k in gamma.keys(): if k != "_default" and k not in g: raise ValueError(f"Deme {k} in gamma, but {k} not in input graph") if h is not None: if type(h) is dict: for k in h.keys(): if k != "_default" and k not in g: raise ValueError(f"Deme {k} in h, but {k} not in input graph") # get the list of demographic events from demes, which is a dictionary with # lists of splits, admixtures, mergers, branches, and pulses demes_demo_events = g.discrete_demographic_events() # get the dict of events and event times that partition integration epochs, in # descending order. events include demographic events, such as splits and # mergers and admixtures, as well as changes in population sizes or migration # rates that require instantaneous changes in the size function or migration matrix. # get the list of demes present in each epoch, as a dictionary with non-overlapping # adjoint epoch time intervals demo_events, demes_present = _get_demographic_events( g, demes_demo_events, sampled_pops ) for epoch, epoch_demes in demes_present.items(): if len(epoch_demes) > 5: raise ValueError( f"dadi cannot integrate more than five demes at a time. " f"Epoch {epoch} has demes {epoch_demes}." ) # get the list of size functions, migration matrices, and frozen attributes from # the deme graph and event times, matching the integration times nu_funcs, migration_matrices, integration_times, frozen_demes = _get_integration_parameters( g, demes_present, list_of_frozen_demes, Ne=Ne ) # # get the sample sizes within each deme, given sample sizes # probably don't need since sample size doesn't matter untill converting phi to fs, unlike moments where each integration needs a ns # deme_sample_sizes = _get_deme_sample_sizes( # g, # demo_events, # sampled_pops, # sample_sizes, # demes_present, # unsampled_n=None, # ) # compute the SFS phi, xx, current_demes_order = _compute_sfs( demo_events, demes_present, sample_sizes, nu_funcs, migration_matrices, integration_times, frozen_demes, pts, theta, gamma, h, ) if debug: phi_bug = phi fs_bug = dadi.Spectrum.from_phi(phi, sample_sizes, [xx]*len(current_demes_order), pop_ids=current_demes_order) new_order = [current_demes_order.index(pop)+1 for pop in sampled_pops] phi_fix = dadi.PhiManip.reorder_pops(phi, new_order) fs_fix = dadi.Spectrum.from_phi(phi_fix, sample_sizes, [xx]*len(sampled_pops), pop_ids=sampled_pops) return fs_fix, fs_bug, phi_fix, phi_bug else: new_order = [current_demes_order.index(pop)+1 for pop in sampled_pops] phi = dadi.PhiManip.reorder_pops(phi, new_order) fs = dadi.Spectrum.from_phi(phi, sample_sizes, [xx]*len(sampled_pops), pop_ids=sampled_pops) return fsTakes a deme graph and computes the SFS.
demesis a package for specifying demographic models in a user-friendly, human-readable YAML format. This function automatically parses the demographic description and returns a SFS for the specified populations and sample sizes.:param g: A
demesDemeGraph from which to compute the SFS. :type g: :class:demes.DemeGraph:param sampled_demes: A list of deme IDs to take samples from. We can repeat demes, as long as the sampling of repeated deme IDs occurs at distinct times. :type sampled_demes: list of strings :param sample_sizes: A list of the same length assampled_demes, giving the sample sizes for each sampled deme. :type sample_sizes: list of ints :param sample_times: If None, assumes all sampling occurs at the end of the existence of the sampled deme. If there are ancient samples,sample_timesmust be a list of same length assampled_demes, giving the sampling times for each sampled deme. Sampling times are given in time units of the original deme graph, so might not necessarily be generations (e.g. ifg.time_unitsis years) :type sample_times: list of floats, optional :param Ne: reference population size. If none is given, we use the initial size of the root deme. :type Ne: float, optional :return: Adadisite frequency spectrum, with dimension equal to the length ofsampled_demes, and shape equal tosample_sizesplus one in each dimension, indexing the allele frequency in each deme from 0 to n[i], where i is the deme index. :rtype: :class:dadi.Spectrum