Skip to content

Instantly share code, notes, and snippets.

Avatar
🍄

Zhian N. Kamvar zkamvar

🍄
View GitHub Profile
@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 / subLoc.R
Last active Apr 10, 2021
Function for subsetting loci in a genind object by name
View subLoc.R
#' 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 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.
#
@zkamvar
zkamvar / gen2polysat.R
Last active Feb 12, 2018
Convert a genind object to a genambig object from polsay
View gen2polysat.R
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)){
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
@zkamvar
zkamvar / git_prompt.sh
Last active Jun 17, 2019
Command prompt functions to go in .bashrc or .bash_profile that will color the command line if there is a git repository.
View git_prompt.sh
#
#
#
#
#
#
#
#
# THIS IS NOW LOCATED AT https://raw.githubusercontent.com/zkamvar/config-files/master/.bash_prompt
#