Skip to content

Instantly share code, notes, and snippets.

View joelnitta's full-sized avatar

Joel Nitta joelnitta

View GitHub Profile
@joelnitta
joelnitta / download_all_files_in_folder.R
Last active November 6, 2018 08:20
R function to download all non-folder files in a google drive folder
#' Download all the files in a google drive folder.
#'
#' \code{dribble_data} should be obtained using \code{\link[googledrive]{drive_ls}}.
#'
#' @param dribble_data Data frame of class \code{dribble}.
#' @param folder Path to local folder to download data.
#' @param pattern Optional grep pattern; only file names matching this pattern
#' will be included.
#' @param negate Logical; should only file names that don't match the pattern
#' be included instead?
@joelnitta
joelnitta / list_proj_pkgs.R
Created January 5, 2019 08:54
List all functions and packages used by R scripts in a project
# List all functions and packages used by R scripts in a project.
library(NCmisc)
# IMPORTANT: Also load any libraries used by the project
# Make list of all functions by package
funcs <-
list.files(here::here(), pattern ="\\.R$", recursive = TRUE, full.names = TRUE) %>%
map(list.functions.in.file) %>%
flatten
@joelnitta
joelnitta / DNAbin_to_DNAstringset.R
Created June 4, 2019 13:25
convert DNAbin to DNAstring set
# Convert ape::DNAbin format to Biostrings::DNAStringSet format,
# optionally removing gaps
DNAbin_to_DNAstringset <- function (seqs, remove_gaps = TRUE) {
if(isTRUE(remove_gaps)) {
seqs %>% as.list() %>% as.character %>%
lapply(.,paste0,collapse="") %>%
lapply( function (x) gsub("-", "", x)) %>%
unlist %>% Biostrings::DNAStringSet()
} else {
seqs %>% as.list() %>% as.character %>%
@joelnitta
joelnitta / concatenate_genes.R
Created June 4, 2019 13:54
Concatenate a list of aligned genes
#' Concatenate a list of aligned genes
#'
#' @param dna_list List of matrices of class DNAbin
#'
#' @return Matrix of class DNAbin
#'
#' @examples
#' data(woodmouse)
#' gene_1 <- woodmouse[,1:100]
#' gene_2 <- woodmouse[,101:200]
@joelnitta
joelnitta / iq_tree.R
Created June 4, 2019 14:06
Wrapper to run IQ-TREE
#' Run IQ-TREE
#'
#' For details, see http://www.iqtree.org/doc/
#'
#' @param alignment DNA alignment to use for phylogenetic analysis. Must be matrix
#' (i.e., aligned sequences) of class DNAbin
#' @param wd Path to working directory. The alignment and IQ-TREE intermediate files
#' and results will be written here.
#' @param bb Optional; number of ultrafast bootstrap replicates to run.
#' @param nt Optional; number of cores to use. Set to "AUTO" to determine automatically.
@joelnitta
joelnitta / preview_pal.R
Created June 5, 2019 14:07
preview a color palette
preview_pal <- function(colors) {
labels <- if(is.null(names(colors))) {colors} else {names(colors)}
pie(
rep(1, length(colors)),
labels = labels,
col = colors)
}
@joelnitta
joelnitta / compare_folders.R
Last active July 8, 2019 00:56
Compare the contents of two folders
#' Compare the contents of two folders
#'
#' Comparison is done by checking file names and the MD5 hash of each
#' file. No recursion is used, so files in subfolders will not be checked.
#'
#' @param x The absolute path to the first folder.
#' @param y The absolut path the second folder.
#'
#' @return A confirmation if all files in the two folders are equal.
#' Otherwise, a list of two tibbles. Each tibble contains a column for
@joelnitta
joelnitta / center_palette.R
Created September 4, 2019 17:27
Scale a color palette centered on zero
# Scale a color palette centered on zero
#' Get the lower, upper, or absolute maximum value
#' of a variable in a dataframe
#'
#' For setting plotting limits manually
#'
#' @param data Dataframe
#' @param var Name of variable (column) in dataframe
#' @param digits Number of digits desired in output
@joelnitta
joelnitta / generic_hex_sticker.R
Created September 22, 2019 22:25
Make a generic hex sticker
library(hexSticker)
library(ggplot2)
# Make a blank plot because hexSticker::sticker() expects something.
p <- ggplot(mtcars, aes(wt, mpg)) + geom_blank() + theme_void() + theme_transparent()
sticker(p,
package="generic",
p_size=8, s_x=1, s_y=.8, s_width=1.2, s_height=1,
h_color = "grey",
country
Afghanistan
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antarctica
Antigua and Barbuda