Skip to content

Instantly share code, notes, and snippets.

@DomBennett
DomBennett / grid_splitter.R
Last active December 14, 2015 18:00
Splitting Lat and Lon observations into grid cells
# R script for splitting grid space
# Variables:
# obs_lat = vector of observed lats
# obs_lon = vector of observed lons
# lats = vector of lists for the max and min values of each grid in lat-space
# lons = vector of lists for the max and min values of each grid in lon-space
# gird = matrix of indexes pointing to the lists in =lats and lons
# res = vector of indexes referring to indexes in grid
# Steps:
@DomBennett
DomBennett / with_phylo.R
Created May 4, 2016 17:05
Extract list of node IDs and descendant tips from a phylogeny in R
# Libraries
library(MoreTreeTools) # package in dev, install via github: https://github.com/DomBennett/MoreTreeTools
library(doMC) # use doSNOW if using Windows
# find out how many cores you have
n <- detectCores()
# set-up
registerDoMC(cores=n)
# example tree
tree <- rtree(1000)
# find all internal nodes in tree
@DomBennett
DomBennett / compare_get_groups.R
Last active May 6, 2016 13:58
Find bipartitions in trees
getGroups <- function(tree) {
getCnnctngEdges <- function(edge) {
nd <- tree$edge[edge, 1]
edge <- which(tree$edge[ ,2] == nd)
if(length(edge) > 0) {
edge <- c(edge, getCnnctngEdges(edge))
}
edge
}
.fillInEdgeTips <- function(i) {
@DomBennett
DomBennett / dplyr_predicts.R
Last active May 23, 2016 12:19
dplyr example with PREDICTS
# PREDICTS AND DPLYR
# D. Bennett & L. Graham
# BRIEF:
# PREDICTS is a large dataset of multiple ecological studies that
# have sampled (through counting, trapping, sighting... etc.) a given
# taxonomic group.
# Each study contains information regarding the number of samples taken,
# the taxonomy of the group, the study area etc.
# Here we will read in a small example dataset of PREDICTS and demonstrate
@DomBennett
DomBennett / get_edges.R
Created August 25, 2016 16:09
Extract edges and plot on a phylogenetic tree
# PLOTTING WITH HIGHLIGHTED TIPS
# LIB
library(ape)
# FUNCTIONS FROM MoreTreeTools
getEdges <- function (tree, node = NULL, tips = NULL, type = 1)
{
if (!is.null(node)) {
edges <- c()
@DomBennett
DomBennett / text_distance_example.R
Created February 11, 2017 16:25
Calculating text distances using string distance metrics and word frequencies
# EXAMPLE TEXT DISTANCES
# FUNCTIONS
justText <- function(txt) {
# converts text to its readable form by removing punctuation and numbers
gsub("[^a-zA-Z ]", "", txt)
}
calcStrDst <- function(txts) {
# calculate the distance between vector of texts
@DomBennett
DomBennett / getGroups.R
Created September 18, 2017 19:44
getGroups w/ edges
getGroups <- function(tree, get_edges=TRUE) {
.fillInRes <- function(i) {
res <- vector('list', length=2)
names(res) <- c('g1', 'g2')
res[['g1']] <- which(edge.tips[i, ] == 1)
res[['g2']] <- which(edge.tips[i, ] == 0)
res
}
.addEdges <- function(i) {
res[[i]][['edglngth']] <<- edglngths[[i]]
@DomBennett
DomBennett / over_a_list.R
Created January 25, 2018 13:15
Calculating trip dist in parallel
# LIB
library(treeman)
library(doMC)
library(foreach)
# FUNCTION
randTreeList <- function(n) {
res <- vector(mode='list', length=n)
for(i in 1:n) {
res[[i]] <- randTree(20, wndmtrx=TRUE)
@DomBennett
DomBennett / fasta_mover.R
Created February 6, 2018 10:42
Take sequences from fasta file and write as separate files
# Take sequences from fasta file and write as separate files
# FUNCTION
readSqs <- function(fl) {
all_data <- readLines(fl)
sqs <- list()
for(i in seq_along(all_data)) {
bit <- all_data[[i]]
if(grepl(pattern='^>', x=bit)) {
nm <- sub(pattern='^>', '', x=bit)
@DomBennett
DomBennett / parse_raxml_newick.R
Created February 12, 2018 14:53
Convert RAxML consensus tree | Readable in R | APE library
# LIB
library(ape)
# DATA
# tree string produced by `RAxML -J ` arg
trstr <- "(A,B,(C,((D,((E,F):1.0[80],((G,H):1.0[100],(I,J):1.0[100]):1.0[67]):1.0[58]):1.0[98],(K,(L,(M,N):1.0[89]):1.0[91]):1.0[100]):1.0[97]):1.0[97]);"
# FAIL
tree <- read.tree(text=trstr)
# node.label = NULL