Learn R Programming

tidypopgen (version 0.4.3)

loci_pi: Estimate nucleotide diversity (pi) at each locus

Description

Estimate nucleotide diversity (pi) at each locus, accounting for missing values. This uses the formula: c_0 * c_1 / (n * (n-1) / 2)

Usage

loci_pi(.x, .col = "genotypes", n_cores, block_size, type, ...)

# S3 method for tbl_df loci_pi( .x, .col = "genotypes", n_cores = bigstatsr::nb_cores(), block_size = bigstatsr::block_size(nrow(.x), 1), ... )

# S3 method for vctrs_bigSNP loci_pi( .x, .col = "genotypes", n_cores = bigstatsr::nb_cores(), block_size = bigstatsr::block_size(length(.x), 1), ... )

# S3 method for grouped_df loci_pi( .x, .col = "genotypes", n_cores = bigstatsr::nb_cores(), block_size = bigstatsr::block_size(nrow(.x), 1), type = c("tidy", "list", "matrix"), ... )

Value

a vector of frequencies, one per locus

Arguments

.x

a vector of class vctrs_bigSNP (usually the genotypes column of a gen_tibble object), or a gen_tibble.

.col

the column to be used when a tibble (or grouped tibble is passed directly to the function). This defaults to "genotypes" and can only take that value. There is no need for the user to set it, but it is included to resolve certain tidyselect operations.

n_cores

number of cores to be used, it defaults to bigstatsr::nb_cores()

block_size

maximum number of loci read at once.

type

type of object to return, if using grouped method. One of "tidy", "list", or "matrix". Default is "tidy".

...

other arguments passed to specific methods, currently unused.

Examples

Run this code
if (FALSE) { # all(rlang::is_installed(c("RhpcBLASctl", "data.table")))
# \dontshow{
data.table::setDTthreads(2)
RhpcBLASctl::blas_set_num_threads(2)
RhpcBLASctl::omp_set_num_threads(2)
# }
example_gt <- load_example_gt("grouped_gen_tbl")

# For pi
example_gt %>% loci_pi()

# For pi per locus per population
example_gt %>%
  group_by(population) %>%
  loci_pi()
# alternatively, return a list of populations with their pi
example_gt %>%
  group_by(population) %>%
  loci_pi(type = "list")
# or a matrix with populations in columns and loci in rows
example_gt %>%
  group_by(population) %>%
  loci_pi(type = "matrix")
# or within reframe (not recommended, as it much less efficient
# than using it directly as shown above)
example_gt %>%
  group_by(population) %>%
  reframe(pi = loci_pi(genotypes))
}

Run the code above in your browser using DataLab