Skip to content

Instantly share code, notes, and snippets.

Avatar
🐈‍⬛

Zhian N. Kamvar zkamvar

🐈‍⬛
View GitHub Profile
@zkamvar
zkamvar / insert_sep.r
Created Apr 27, 2014
Insert a separator into a character vector containing two pieces of information where one piece has a constant width.
View insert_sep.r
#==============================================================================#
# insert_sep.r Insert a separator into a character vector containing two pieces
# of information where one piece has a constant width.
#
# Author: Zhian N. Kamvar
# License: GPLv3
# Year: 2014
# Waranty: NONE
#
# Arguments:
@zkamvar
zkamvar / my_MLG_table.R
Last active Aug 29, 2015
A function for printing the genotypes in the MLG table from the package "poppr"
View my_MLG_table.R
#==============================================================================#
# 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"
@zkamvar
zkamvar / fix_replen.R
Last active Aug 29, 2015
A set of functions to detect potential issues with repeat length specifications for use with Bruvo's distance in poppr
View fix_replen.R
#' Test repeat length consistency.
#'
#' This function will test for consistency in the sense that all alleles are
#' able to be represented as discrete units after division and rounding.
#' @param gid a genind object
#' @param replen a numeric vector of repeat motif lengths.
#' @return a logical vector indicating whether or not the repeat motif length is
#' consistent.
#'
#' @details This function is modified from the version used in
@zkamvar
zkamvar / ape_nex_mod.R
Last active Aug 29, 2015
Add tip colors and round node labels for nexus files produced via APE for use in FigTree
View ape_nex_mod.R
#==============================================================================#
#' INSTALLATION
#'
#' devtools::source_gist("3c3f12926de648431937")
#==============================================================================#
#
#
#==============================================================================#
#' Adds tip colors to tiplabels of an object of class "phylo"
#'
@zkamvar
zkamvar / poppr_patches.R
Last active Aug 29, 2015
Script to fix any bugs that occur in the R code in poppr. This unfortunately does not fix bugs that occur in the C code.
View poppr_patches.R
#==============================================================================#
# UPDATED: 2015-07-17
#
# USAGE/INSTALLATION:
# with devtools - devtools::source_gist("b64078a0d04d2452c905")
# downloaded - source("poppr_patches.R")
#
# UPDATES:
# Fix read.genalex ( see: https://github.com/grunwaldlab/poppr/issues/58 )
#
@zkamvar
zkamvar / poppr_treemap.R
Last active Aug 29, 2015
Visualize poppr population hierarchies with treemap
View poppr_treemap.R
#==============================================================================#
# Visualize the population hierarchy of a genclone object.
#
# Packages needed (install beforehand):
library(poppr)
library(treemap)
#==============================================================================#
# Main function
#
# Inputs:
@zkamvar
zkamvar / maximum_likelihood.R
Created Dec 21, 2014
Recursive function to quickly search for the parameter that maximizes a likelihood function
View maximum_likelihood.R
# ============================================================================ #
# Quickly find the parameter that maximizes a likelihood function.
#
# This recursive function will take a range of n parameters within an interval
# and re-adjust the interval to border the maximum value and recursively call
# the function. Once the difference between the maximum value found and the
# previous maximum value is less than the error term, the parameter value is
# returned. This is faster than the brute force method as the range is reduced
# every time the function is recursed.
#
View poppr_plot_phylo.R
# Two functions to plot phylo objects from ape.
#
# Both of these functions plot their respective trees. The ... gives the user
# the ability to adjust things like color or font size.
#
nj_plot_phylo <- function(tree, ...){
barlen <- min(median(tree$edge.length), 0.1)
if (barlen < 0.1) barlen <- 0.01
tree <- ladderize(tree)
plot.phylo(tree, font = 2, adj = 0, xpd = TRUE,
@zkamvar
zkamvar / gather_and_replace_data.R
Last active Aug 29, 2015
Replace data within an R folder when updating a package.
View gather_and_replace_data.R
datasets <- vapply(strsplit(dir("data"), "\\."), "[", character(1), 1)
data_replacer <- function(x){
# manipulate your data here
y <- old2new_genind(x, new(class(x)))
if ("genclone" %in% class(x)){
y@mlg <- x@mlg
}
return(y)
}
@zkamvar
zkamvar / polygen.R
Last active Aug 29, 2015
Polyploid data generator
View polygen.R
library("stringr")
library("adegenet")
stopifnot(packageVersion("adegenet") >= 2)
generate_polyploid_data <- function(nind = 10, nloc = 2, maxploid = 4, sep = "/", genind = TRUE){
locnames <- paste("locus", seq(nloc), sep = "_")
indnames <- paste("sample", seq(nind), sep = "_")
res <- lapply(seq(nloc), generate_locus, nind, maxploid, sep)
names(res) <- locnames