Skip to content

Instantly share code, notes, and snippets.

@darmitage
darmitage / PseudomonasSharedCode
Last active March 23, 2016 23:08
Data and code for EcoLetts submission
require(AICcmodavg)
require(mgcv)
require(boot)
require(car)
require(MASS)
require(repmis)
require(pscl)
#Read in data from my public dropbox folder
dropURL <- "https://dl.dropboxusercontent.com/u/26280366/Papers/EcoLetts/AreaNrgPseudoFinal.csv"
@darmitage
darmitage / batcall_classify
Created September 18, 2012 17:49
Classification of zero-crossings bat calls using machine learning
require(kernlab)
require(caret)
require(e1071)
require(nnet)
require(randomForest)
set.seed(34873458)
#Open call library (needs name (eg. MYLU), and parameters)
setwd = ("Dropbox/Anabat files/Dave's Florida Bat Calls/")
@darmitage
darmitage / NaiiveD
Created July 25, 2012 23:49
Naiive Diversity Profiles + Rarefaction
require(picante)
require(gtools)
require(reshape)
###########################################################
### For calculation of Naiive (Z = I) diversity profiles ###
###########################################################
@darmitage
darmitage / PhyloClean
Created June 1, 2012 03:37
Cleaning up OTU tables and Phylogenies
phy = read.tree("mytree.tre")
samp = read.table("OTUtable.txt", head = T, sep = "")
#lists taxa in OTU table that are not in the phylogeny, and removes them
samp = (samp[rowSums(samp) != 0, , drop=FALSE])
samp = t(samp)
phy = prune.sample(samp, phy)
labs <- phy$tip.label
length(labs)
ncol(samp)
@darmitage
darmitage / ChaoPD
Created May 15, 2012 04:52
Chao's phylogenetic diversity
require(picante)
require(gtools)
require(reshape)
###########################################################
### Utility functions ###
###########################################################
internal2tips.self = function (phy, int.node){
@darmitage
darmitage / Phylo.Z.code
Created May 9, 2012 23:33
Phylogenetic Hill Numbers
############Here's a function that calculates Hill number diversity using a phylogeny from the Leinster and Cobbold (2012) paper
#Requires a phylogeny with tip labels and a community matrix (rows = samples, columns = species)
Phylo.Z <- function(phy,samp){
#packages required for this to work:
require(reshape)
require(picante)
require(ape)
require(gtools)
@darmitage
darmitage / WorkingZCode
Created May 9, 2012 05:28
Working similarity matrix code sensu Leinster & Cobbold 2012
require(reshape)
require(picante)
require(gtools)
#Function that lists all terminal tip labels descended from ancestral node
internal2tips.self = function (phy, int.node){
#require(picante); require(ape)
Ntaxa = length(phy$tip.label)
Nnode = phy$Nnode
if ((Ntaxa + Nnode - 1) != nrow(phy$edge)) {
@darmitage
darmitage / nodes2root
Created May 8, 2012 00:35
List nodes falling between specified node and root of phylogeny
nodes2root <-
function(phy,int.node) {
Ntaxa = length(phy$tip.label) + phy$Nnode
Nnode = phy$Nnode
tips = c()
nodes = int.node
repeat {
nodes = phy$edge[which(phy$edge[,2]%in%nodes),1]
if (length(nodes)==0) break
tips = c(tips,nodes)
@darmitage
darmitage / node_collapse
Created April 24, 2012 22:59
Collapse a phylogenetic clade by support values
#Code to collapse a phylogeny by a tree's support values, when nodes have support value names
#tree = phylogenetic tree, cutoff = support value cutoff (can be changed or made into a vector)
require(ape)
h <- which(tree$node.label < cutoff)
nodes <- matrix(h + Ntip(tree))
nodes <- nodes[-1]
k <- matrix(tree$edge[,2])
j <- matrix(nrow = length(i), ncol = 1)
@darmitage
darmitage / phylosim
Created December 12, 2011 22:22
histograms of phylogenetic similarity
require(picante)
#function that maps dissimilarity to similarity & scales between 0 and 1
range01 <- function(x){exp((-1/max(x))*x)}
#Calculate branch lengths bewteen taxa
dist <- cophenetic(tree)
#calculate similarity between taxa from 0 to 1
Z <- range01(dist)