Skip to content

Instantly share code, notes, and snippets.

View sckott's full-sized avatar
😸

Scott Chamberlain sckott

😸
View GitHub Profile
@sckott
sckott / randmat.r
Created August 28, 2012 19:09
speed of random matrix algorithms
mm <- matrix(0, 10, 5)
m <- 10
n <- 5
> system.time( replicate(1000, apply(mm, c(1,2), function(x) sample(c(0,1),1)) ) ) # @recology_
user system elapsed
0.414 0.015 0.429
> system.time( replicate(1000, matrix(rbinom(10*5,1,.5),ncol=5,nrow=10) ) ) # @distribecology
user system elapsed
0.012 0.000 0.012
@sckott
sckott / comparfxn.r
Created September 2, 2012 15:35
Comparison of functions for comparative phylogenetics
> # Load
require(motmot); require(geiger); require(picante)
source("http://anolis.oeb.harvard.edu/~liam/R-phylogenetics/phylosig/v0.3/phylosig.R")
source("http://anolis.oeb.harvard.edu/~liam/R-phylogenetics/fastBM/v0.4/fastBM.R")
# Make tree
tree <- rcoal(10)
# Transform branch lengths
> system.time( replicate(1000, transformPhylo(tree, model = "lambda", lambda = 0.5)) ) # motmot
@sckott
sckott / gggraph.r
Created September 2, 2012 16:42
basic ggplot food web graphs
# ggplot network graphics functions
# g = an igraph graph object, any igraph graph object
# vplace = type of vertex placement assignment, one of rnorm, runif, etc.
gggraph <- function(g, vplace = rnorm) {
require(ggplot2)
g_ <- get.edgelist(g)
g_df <- as.data.frame(g_)
@sckott
sckott / binmat_comparison.r
Created September 5, 2012 16:44
Comparison of binary matrix algorithms (minus the Rcpparmadillo versions).
library(inline); library(compiler); library(rbenchmark)
n <- 500
k <- 100
scott <- function(N, K) {
mm <- matrix(0, N, K)
apply(mm, c(1, 2), function(x) sample(c(0, 1), 1))
}
scottComp <- cmpfun(scott)
@sckott
sckott / newphylomaticfxn.r
Created September 7, 2012 00:53
example of new phylomatic function - getting trees from R.
# Example of new phylomatic
install_github('taxize_', 'ropensci', branch="phylomatic_fix")
library(taxize)
dat_ <- c("Amylotheca_subumbellata","Impatiens_davidi","Gonocarpus_leptothecus","Calochortus_argillosus","Gagea_sarmentosa","Lilium_lankongense","Cephalanthera_rubra","Bicuiba_oleifera","Guatteria_dolichopoda","Iteadaphne_caudata")
tree <- get_phylomatic_tree(taxa = dat_)
> tree
Phylogenetic tree with 10 tips and 9 internal nodes.
@sckott
sckott / suppressnamespacewarning.r
Created September 12, 2012 17:45
gotta suppress the damn namespace warning in XML
library(httr); library(XML)
url <- "http://data.gbif.org/ws/rest/occurrence/list?scientificname=Accipiter%20erythronemius&coordinatestatus=TRUE&maxresults=100"
tt <- content(GET(url), as="text")
out <- xmlTreeParse(tt)$doc$children$gbifResponse
d> getNodeSet(out, "//to:TaxonOccurrence") # throws errors even though the output is what I want
Namespace prefix to on TaxonOccurrence is not defined
Namespace prefix to on catalogNumber is not defined
Namespace prefix to on country is not defined
@sckott
sckott / rgbif_occurrencelist_providerkey.r
Created September 25, 2012 19:52
Example of getting a provider key and running occurrence list with the key.
install_github('rgbif', 'schamberlain') # my branch is the newest
library(rgbif)
providers(name="UK National Biodiversity Network") # get numeric key for the NBN
> occurrencelist(sciname="Ursus", dataproviderkey=172, latlongdf=T) # should give you a data.frame like below
taxonName country decimalLatitude decimalLongitude catalogNumber earliestDateCollected latestDateCollected
1 Abietinaria abietina GB 59.01977 -2.9031110 13389886 1995-06-20 1995-06-20
2 Abietinaria abietina GB 60.84050 -0.9006389 13389540 1988-06-02 1988-06-02
3 Abietinaria abietina GB 56.55398 -5.4213445 13389523 1989-06-18 1989-06-18
4 Abra alba GB 49.91424 -6.3364116 13392162 1988-09-19 1988-09-19
@sckott
sckott / write_fasta.r
Created October 12, 2012 22:55
Write fasta file, which is ready to run in ClustalW for multiple sequence alignment.
# Clustal doesn't like when there are spaces in species names, so `write_fasta` fixes that in case you forget.
write_fasta <- function (sequences, file.out) {
outfile <- file(description = file.out, open = "w")
write.oneseq <- function(sequence, name) {
if(grepl("\\s", name)){name<-gsub("\\s", "_", name)} else{name<-name}
writeLines(paste(">", name, sep = ""), outfile)
writeLines(sequence[[1]], outfile)
}
sapply(seq_len(length(sequences)), function(x) write.oneseq(
sequence=as.character(sequences[[x]]), name=names(sequences[x])))
@sckott
sckott / downstream2.r
Created October 16, 2012 17:41
Retrieve all taxa names or TSNs (taxonomic serial numbers) downstream in hierarchy from given TSN.
install.packages("devtools") # need devtools to use install_github()
require(devtools)
install_github("ritis", "ropensci") # need for the function searchbyscientificname below
require(ritis)
install_github("taxize_", "ropensci", ref="downstream_fix") # Installs packages in development directly from Github
library(taxize)
tsn1 <- searchbyscientificname("Osteichthyes") # get TSN for Osteichthyes (bony fish)
@sckott
sckott / Rpumpkin.r
Created October 31, 2012 22:55
Run the code, then click three times to cut a piece out of your pumpkin
# From Jeff Joy
par(bg=1)
plot(c(0,0), cex=0, xlim=c(-1,1), ylim=c(-1,1))
X <- runif(100,-1,1)
Y <- runif(100,0,1)
M <- rchisq(100,1)/20
points(Y~X, cex=M, pch=19, col=colors()[1])
points(0,-0.5, pch=19, col=colors()[498],cex=20)
for (i in 1:4){polygon(locator(3),col=7)}