Skip to content

Instantly share code, notes, and snippets.

@zkamvar
Last active Aug 29, 2015
Embed
What would you like to do?
A function for printing the genotypes in the MLG table from the package "poppr"
#==============================================================================#
# Function to print genotypes in the MLG table created from a genind or genclone
# object. This allows for quick identification for small numbers of loci.
#
# Requirements: the R package "poppr"
#
# usage: my.mlg.table(genind_object)
#
# arguments:
# ... arguments passed on to "mlg.table"
# asep a separator character for alleles within a locus
# lsep a separator character for loci
# transpose logical. If TRUE (default), the matrix will be transposed so that
# the MLGs are in the rows to make them easier to read.
#
# installation:
# devtools::source_gist("9c53a78094d5ecd9ca4c")
#==============================================================================#
my.mlg.table <- function(..., asep = "|", lsep = " ", transpose = TRUE){
mtab <- poppr::mlg.table(...)
args <- list(...)
# Step 1: clone correct and get the order of the MLGs
if (packageVersion("poppr") >= package_version("1.1.4.99.131")){
gid <- poppr::clonecorrect(args[[1]], strata = NA)
} else {
gid <- poppr::clonecorrect(args[[1]], hier = NA)
}
gid.mlg <- poppr::mlg.vector(gid)
mlg.order <- order(gid.mlg)
gid <- gid[mlg.order]
gid.mlg <- gid.mlg[mlg.order]
# Step 2: find out what MLGs exist in the table
if (!is.matrix(mtab)){
table.mlgs <- as.numeric(substr(names(mtab), 5, nchar(names(mtab))))
} else {
table.mlgs <- as.numeric(substr(colnames(mtab), 5, nchar(colnames(mtab))))
}
mlgs.in.table <- match(table.mlgs, gid.mlg)
# Step 3: convert genotypes into a human readable format.
gid <- gid[mlgs.in.table]
gid.df <- adegenet::genind2df(gid, sep = asep, usepop = FALSE)
# Step 4: paste them into the MLG table.
if (!is.matrix(mtab)){
names(mtab) <- paste(names(mtab), apply(gid.df, 1, paste, collapse = lsep))
} else {
colnames(mtab) <- paste(colnames(mtab), apply(gid.df, 1, paste, collapse = lsep))
}
if (transpose) mtab <- t(mtab)
# When using the poppr package, this forces zeroes to print as "."
class(mtab) <- c("locustable", "matrix")
return(mtab)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment