Skip to content

Instantly share code, notes, and snippets.

View zkamvar's full-sized avatar
🍄
On Sabbatical

Zhian N. Kamvar zkamvar

🍄
On Sabbatical
View GitHub Profile
@zkamvar
zkamvar / ape_nex_mod.R
Last active August 29, 2015 14:09
Add tip colors and round node labels for nexus files produced via APE for use in FigTree
#==============================================================================#
#' INSTALLATION
#'
#' devtools::source_gist("3c3f12926de648431937")
#==============================================================================#
#
#
#==============================================================================#
#' Adds tip colors to tiplabels of an object of class "phylo"
#'
@zkamvar
zkamvar / subLoc.R
Last active April 10, 2021 19:40
Function for subsetting loci in a genind object by name
#' Subset loci in a genind object by name
#'
#' @param dat a genind object
#' @param ... names of the loci in character format OR numeric indices of the loci.
#'
#' @return the appropriate names to use for subsetting in a genind object
#' @examples
#' library(adegenet)
#' data(nancycats)
#' # Old way:
@zkamvar
zkamvar / poppr_patches.R
Last active August 29, 2015 14:10
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.
#==============================================================================#
# 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 August 29, 2015 14:11
Visualize poppr population hierarchies with treemap
#==============================================================================#
# 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 December 21, 2014 00:58
Recursive function to quickly search for the parameter that maximizes a likelihood function
# ============================================================================ #
# 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.
#
@zkamvar
zkamvar / gen2polysat.R
Last active February 12, 2018 20:49
Convert a genind object to a genambig object from polsay
gen2polysat <- function(gen, newploidy = gen@ploidy){
if (!require(polysat)){
stop("User needs polysat installed")
}
gen <- recode_polyploids(gen, newploidy)
gendf <- genind2df(gen, sep = "/", usepop = FALSE)
gendf <- lapply(gendf, strsplit, "/")
gendf <- lapply(gendf, lapply, as.numeric)
ambig <- new("genambig", samples = indNames(gen), loci = locNames(gen))
for (i in names(gendf)){
@zkamvar
zkamvar / poppr_plot_phylo.R
Created February 17, 2015 21:28
plot_phylo
# 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 August 29, 2015 14:17
Replace data within an R folder when updating a package.
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 August 29, 2015 14:18
Polyploid data generator
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
@zkamvar
zkamvar / git_prompt.sh
Last active June 17, 2019 09:42
Command prompt functions to go in .bashrc or .bash_profile that will color the command line if there is a git repository.
#
#
#
#
#
#
#
#
# THIS IS NOW LOCATED AT https://raw.githubusercontent.com/zkamvar/config-files/master/.bash_prompt
#