Skip to content

Instantly share code, notes, and snippets.

Avatar
🐈‍⬛

Zhian N. Kamvar zkamvar

🐈‍⬛
View GitHub Profile
View purr_mc.R
# This is from @drob on twitter:
#
# https://twitter.com/drob/status/915378838078722048
#
library("purrr")
transition_mc <- function(steps, start, mat){
i <- seq_len(nrow(mat))
transition <- ~ sample(i, prob = (i == .) %*% mat)
accumulate(seq_len(steps), transition, .init = start)
}
@zkamvar
zkamvar / pastena.R
Last active Sep 14, 2017
Paste function that replaces NA characters with blanks
View pastena.R
#' paste function that replaces NA characters with blanks
#'
#' All arguments are passed to paste.
#'
#' Usage:
#' pastena(letters, sample(c(1:10, NA), 26, replace = TRUE))
pastena <- function(..., collapse = NULL, sep = " ") {
dots <- list(...)
dots <- lapply(dots, function(i) ifelse(is.na(i), "", i))
res <- do.call("paste", c(dots, list(collapse = collapse, sep = sep)))
@zkamvar
zkamvar / gvcf2csv.sh
Created Sep 8, 2017
create csv from gvcf
View gvcf2csv.sh
#!/usr/bin/env bash
# This ridiculous script will take in a file as input and spit out comma-separated text for easier parsing.
# Usage:
# $ gvcf2csv.sh GVCF.err | column -t -s,
grep ProgressMeter $1 | \
awk -F" - " '{print $2}' | \
tail -n +2 | \
sed -E 's/[ ]\|[ ]/,/g' | \
sed -E "s/([0-9hmsw%])[ ]{3,}([0-9])/\1,\2/g"
@zkamvar
zkamvar / Makefile
Last active Mar 18, 2020
Example SLURM script that records memory, time, and job id
View Makefile
FILES := file1.out \
file2.out \
file3.out \
file4.out \
file5.out \
file6.out \
file7.out \
file8.out \
file9.out
@zkamvar
zkamvar / doi2coi-list.R
Created Jun 12, 2017
Conflict of Interest List
View doi2coi-list.R
# This will take a list of dois and convert them
# to a table containing the names of coauthers to
# populate a conflict of interest list.
#
# It's not perfect, but it's better than nothing.
#
library("rcrossref")
library("tidyverse")
#
# Change these to YOUR dois.
@zkamvar
zkamvar / update_cran_packages.R
Created Apr 25, 2017
Script for updating CRAN packages
View update_cran_packages.R
# Note: this assumes that you have a custom library at the top of your .libPaths()
fields <- c("Package", "LibPath", "Version", "Priority", "Depends", "Imports", "LinkingTo", "Suggests", "Enhances", "OS_type", "Built", "RemoteSha", "Packaged", "biocViews")
x <- installed.packages(.libPaths()[1], fields = fields)
needs_update <- x[x[, "Built"] != "3.4.0", ]
bicond <- !is.na(needs_update[, "biocViews"])
ghub <- !is.na(needs_update[, "RemoteSha"])
remote <- !is.na(needs_update[, "Packaged"])
bioconductor <- needs_update[bicond, ]
cran <- needs_update[!bicond & !ghub & remote, ]
install.packages(rownames(cran))
View pattern.txt
Name this damn pattern.
\binom{N}{0}, \binom{N}{1}, \binom{N}{2}, ..., \binom{N}{floor(N/2)}, ... N!
@zkamvar
zkamvar / mll.representative.R
Last active Jan 11, 2017
Replacing multilocus lineages with one representative sample
View mll.representative.R
# Replacing multilocus lineages with one representative sample
library(poppr)
data(monpop)
mlg.filter(monpop) <- 0 + .Machine$double.eps
mondf <- genind2df(monpop, usepop = FALSE)
mid <- mlg.id(monpop)
orders <- unlist(mid, use.names = FALSE)
monpop <- monpop[orders, ]
mondif <- mondf[orders, ]
n <- vapply(mid, length, integer(1))
@zkamvar
zkamvar / mod_poppr_plot_phylo.R
Created Jan 10, 2017
Modify default aboot plotting function in R
View mod_poppr_plot_phylo.R
# poppr.plot.phylo definition:
# https://github.com/grunwaldlab/poppr/blob/9170398f6552f27ab69b294172c97c8ee7a97ce0/R/internal.r#L1529-L1545
# You can also get it by simply typing poppr:::poppr.plot.phylo in your R console
library("ape")
library("phangorn")
poppr.plot.phylo <- function(tree, type = "nj", root = FALSE){
barlen <- min(median(tree$edge.length), 0.1)
if (barlen < 0.1) barlen <- 0.01
if (!root && type != "upgma"){
tree <- ladderize(tree)
@zkamvar
zkamvar / znk_dissertation_wordcloud.R
Last active Dec 31, 2016
Generate a wordcloud from my dissertation
View znk_dissertation_wordcloud.R
library('rjson')
library('wordcloud')
library('viridis')
library('magrittr')
library('purrr')
library('svglite')
# Download JSON data ------------------------------------------------------