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 / columncombiner.r
Created March 28, 2013 23:36
A function that will print out a function for OpenOffice spreadsheet or excel which can be used to concatenate cells with a common separator
################################################################################
# A by-column concatenator for excel through R.
#
# Arguments:
# row - indicate the row you wish to concatenate.
# cell - the first cell containing the information you want to concatenate.
# lastcell - the last cell containing the information you want to concatenate.
#
# Usage: combine_excel(row = 1, cell = "A", lastcell = "D")
#
@zkamvar
zkamvar / getfile.r
Last active December 16, 2015 11:29
Function from the package [poppr](https://github.com/poppr/poppr) that serves as a wrapper for `file.choose()`, `file.path()`, and `list.files()`. It will allow the user to grab multiple files for import into R with their gui.
#==============================================================================#
# Usage:
# x <- getfile() # for a single file.
# x <- getfile(multi = TRUE) # for multiple files
# x <- getfile(multi = TRUE, pattern = "^.+?csv$") # grabs all *.csv files.
# setwd(x$path)
# read.csv(x$files)
#==============================================================================#
getfile <- function(multi=FALSE, pattern=NULL, combine=TRUE){
@zkamvar
zkamvar / poppr_stats.r
Last active December 18, 2015 11:49
Statistics for poppr installations.
if (!require('devtools')) install.packages('devtools'); require('devtools')
if (!require('data.table')) install.packages('data.table'); require('data.table')
install_github('installr', 'talgalili') # get the latest installr R package
# or run the code from here:
# https://github.com/talgalili/installr/blob/master/R/RStudio_CRAN_data.r
require(installr)
if(!require(ISOcodes)) install.packages("ISOcodes", repos = "http://cran.at.r-project.org"); require(ISOcodes)
@zkamvar
zkamvar / hapmap2df.r
Created October 16, 2013 21:48
A function for converting hapmap formatted files to data frames for the purpose of importing them into adegenet's genlight object.
#==============================================================================#
# hapmap2df.r Convert hapmap files to a data frame.
#
# Author: Zhian N. Kamvar
# License: GPLv3
# Year: 2013
# Waranty: NONE
#
# This function will read in hapmap formatted files and convert them to a data
# frame for the purposes of importing them to genlight objects. Example:
@zkamvar
zkamvar / pic.r
Last active December 27, 2015 15:19
Polymorphic Information Content
pic <- function(gid){
if (gid@type != "PA"){
gid_loc <- seploc(gid)
return(vapply(gid_loc, pic_loc, 1))
}
else{
return(vapply(1:nLoc(gid), function(x) pic_loc(gid[, x]), 1))
}
}
@zkamvar
zkamvar / insert_sep.r
Created April 27, 2014 06:08
Insert a separator into a character vector containing two pieces of information where one piece has a constant width.
#==============================================================================#
# 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 / lazierLoad.R
Last active September 11, 2015 16:22
Does a lazy load across all files when provided a knitr cache directory.
#' Performs lazy load on a directory
#'
#' @param path a filepath containing the necessary files for lazy loading
#' @return NULL
#'
#' @details This function will go into a directory, search for all the files
#' that seem like they can be lazily loaded and attempt to load them.
#'
lazierLoad <- function(path){
files <- dir(path)
#!/usr/bin/perl
#
# This script was created because I had a problem with inkscape not recognizing
# fonts generated by R's pdf generator. This will go through and make these fonts
# scaleable.
#
# Workflow: Generate PDF > open in inkscape > save as svg > run this script
#
# Usage: perl -i inkscape_pdf_font.pl <my_file.svg>
#
@zkamvar
zkamvar / my_MLG_table.R
Last active August 29, 2015 14:08
A function for printing the genotypes in the MLG table from the package "poppr"
#==============================================================================#
# 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 August 29, 2015 14:08
A set of functions to detect potential issues with repeat length specifications for use with Bruvo's distance in poppr
#' 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