Skip to content

Instantly share code, notes, and snippets.

View dwinter's full-sized avatar
🐢
I may be slow to respond.

David Winter dwinter

🐢
I may be slow to respond.
View GitHub Profile
@dwinter
dwinter / rentrez.r
Created February 11, 2012 01:19
Dealing with entrez in R
entrez_search <- function(dbase, term, retmax=6,...){
base_url <- "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=%s&term=%s&retmax=%i"
search <- sprintf(base_url, dbase, term, retmax)
raw_result <- getURL(search)
ids <- unlist(getNodeSet(xmlParse(raw_result), "//Id", fun=xmlValue))
return(as.integer(ids))
}
entrez_fetch <- function(dbase, ids, format, ...){
@dwinter
dwinter / SummariseBeast.py
Created May 30, 2012 05:27
Summarise Beast output from the command line
"""
Summarise a variable in a BEAST logfile
arguments:
-h, --help show this help message and exit
-f or --file string whats the name of the BEAST file
-v or --variable string value you want to summarise
-b or --burnin int number of states to ignore
-g export graphs to summarise the sample?
-l export a logfile for just this variable
@dwinter
dwinter / Parse_multitree_paml.py
Created June 1, 2012 10:37
Parse results for a multi-tree PAML run
#
# Code to parse the interesting bits of a PAML results file that contains
# statistics for several trees. Note, this approach contains some hacks that
# are probably specific to multiple-tree CODEML files, Biopython has a module
# for handling other PAML outputs, and it probably a better starting point for
# 'normal' (single tree) input files.
#
# TODO - should clean up handlng of resifues - use @property decorator and
# ._functions to simplify writing/reading.
#
@dwinter
dwinter / gsi.R
Created July 24, 2012 23:36
calculate gsi in R
#calculate Cummings et al (2008) _gsi_ - a meaure of the exlusivity of a
#predefiend group of leaves in a phylogenetic tree
#example
#
# tr <- rtree(10)
# grp <- paste("t", 1:5, sep="")
# gsi(tr, grp)
library(ggplot2)
df0 <- data.frame(x=rnorm(100), y=rnorm(100), grp=factor(rep(letters[1:2], each=50)))
p <- ggplot(df0, aes(x,y, colour=grp, alpha=y))
p + geom_point()
library(RColorBrewer)
@dwinter
dwinter / na.r
Created March 18, 2013 04:48
'NA' is a gene symbol, which can cause problems
chars <- c("ABC", NA, "DEF")
(urls <- paste("web-api&id=", chars, sep=""))
## [1] "my-fav-web-api/id=ABC" "my-fav-web-api/id=NA" "my-fav-web-api/id=DEF"
@dwinter
dwinter / complete_cases.r
Last active December 15, 2015 22:39
complete_cases.r
df1 <- data.frame(x=sample(letters, 20), y=rnorm(20))
df2 <- data.frame(x=sample(letters, 20), z=rnorm(20))
new_df <- merge(df1, df2, all.x=TRUE)
matched <- merged[complete.cases(merged),]
unmatched <- merged[!complete.cases(merged),]
@dwinter
dwinter / MCMCglmm_diag.r
Created May 24, 2013 03:57
Quckly run a bunch of diagnostic tests on a list of MCMC runs
model_diagnostics <- function(modlist, plot=TRUE){
get_param_ranges <- function(x) {
res <- apply((sapply(x, posterior.mode)),1,range)
rownames(res) <- c("min", "max")
return(res)
}
n <- length(modlist)
on.exit(devAskNewPage(devAskNewPage())) #ie send it back the way it was
f <- function(x,y) sin(x)/cos(y)
x <- seq(-5,5,0.1)
y <- seq(-5,5,0.1)
df <- expand.grid(x,y) #all possible combinations of x and y
df$value <- apply(df, 1, function(x) f(x[1], x[2]))
p <- ggplot(df, aes(Var1,Var2,fill=res>0))
p + geom_raster()
import random
def shotgun(iterable, size=12):
""" """
start = random.choice(range(0, len(iterable)-size))
return(start, iterable[start:start+size])
def paired_end(iterable, read_size=10, gap_size=15):
""" """
start = random.choice(range(0, len(iterable)-(read_size+gap_size)))