Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Kuhn mkuhn

View GitHub Profile
@mkuhn
mkuhn / gist:9ad4da2e039e5c6741e2
Created Mar 10, 2015
Composite figures using ggplot2 and gtable
View gist:9ad4da2e039e5c6741e2
library(ggplot2)
library(gtable)
# create example data
set.seed(42)
dataset_names <- c("Human", "Mouse", "Fly", "Worm")
datasets <- data.frame(name = factor(dataset_names, levels=dataset_names), parity = factor(c(0, 0, 1, 0)), v50 = runif(4, max=0.5), y=1:4)
data <- data.frame( dataset1 = rep(datasets$name, 4), dataset2 = rep(datasets$name, each = 4), z = runif(16,min = 0, max = 0.5) )
pal <- c("#dddddd", "#aaaaaa")
View code.R
library(dplyr)
a <- data.frame(foo = 1:10, bar = "bar")
b <- tbl_df(a)
a[,1]
b[,1]
paste0(a[,1], "!")
paste0(b[,1], "!")
View DecoratorUseCase.py
# with the help of decorators, keep track of the functions
# we would like to use for fitting
fit_functions = []
def fit_func(f):
fit_functions.append(f)
return f
class C(object):
View stockholm2fasta.pl
#!/usr/bin/perl -w
my $columns = 50;
my $gapped = 0;
my $progname = $0;
$progname =~ s/^.*?([^\/]+)$/$1/;
my $usage = "Usage: $progname [<Stockholm file(s)>]\n";
$usage .= " [-h] print this help message\n";
@mkuhn
mkuhn / igrep.py
Created Jan 5, 2011
a small utility for repeatedly (and interactively) running grep on the same file
View igrep.py
#!/usr/bin/env python
import readline
import os
import sys
import re
if len(sys.argv) == 1:
print >> sys.stderr, "Usage: igrep[.py] file1 [file2 ...]"
@mkuhn
mkuhn / uncompressed.js
Last active Sep 25, 2015
ShortDOI Bookmarklet
View uncompressed.js
javascript:var%20metas=document.getElementsByTagName('meta');var%20a='';for(i=0;i<metas.length;i++){if(metas[i].getAttribute('name')=='citation_doi'){a=metas[i].getAttribute('content')}}if(a){location.href='http://shortdoi.org/'+a}else{a=document.documentElement.innerHTML.match(/doi%20*[:=]%20*10[-/0-9a-z.]\/[-/0-9a-z.]+/mig)||document.documentElement.innerHTML.match(/dx.doi.org\/10[-/0-9a-z.]+/mig);if(a==null){alert('Could%20not%20find%20DOI!')}else{a=a.map(function(x){return%20x.match('10.*')[0]});var%20d=a[0];if(a.length>1){var%20c=new%20Array();for(i%20in%20a){if(c[a[i]]){c[a[i]]++}else{c[a[i]]=1}}var%20m=0;d=null;for(i%20in%20c){if(c[i]>m){m=c[i];d=i}else%20if(c[i]==m){d=null}}}if(d==null){alert('More%20than%20one%20DOI%20found!')}else{location.href='http://shortdoi.org/'+d}}}
@mkuhn
mkuhn / gist:862551
Created Mar 9, 2011
Comparing two-dimensional distributions using ggplot2
View gist:862551
p <- ggplot(d,aes(x+0.05,y+0.05))+geom_tile(aes(fill=enrichment)) + scale_fill_gradient(low="white", high="steelblue", limits=c(0,120))
p <- p + xlab("x") + ylab("y")
p <- p + geom_point(aes(x=x+0.05, y=y+0.05, colour=pred),size=20) + scale_colour_gradient(low="white", high="steelblue", limits=c(0,120))
print(p)
View gist:863745
pred <- ddply(data.frame(x=c(5:65)*0.01), .(x), function(t) data.frame(x = t$x, y = c(5:85)*0.01) )
pred$enrichment <- predict(sigmoid, pred)
max_pred <- max(pred$enrichment, d$enrichment)
p <- ggplot(pred,aes(x,y))+geom_tile(aes(fill=enrichment))+scale_fill_gradient(low = "white", high = "steelblue",limits=c(0,max_pred))
p <- p + xlab("x") + ylab("y")
p <- p + geom_point(data=d,aes(x=x, y=y, colour=enrichment),size=20) +scale_colour_gradient(low = "white", high = "steelblue",limits=c(0,max_pred))
print(p)
@mkuhn
mkuhn / ggplot2_order.R
Created Aug 11, 2011
ggplot: Determining the order in which lines are drawn
View ggplot2_order.R
library(ggplot2)
df <- data.frame( n=c("a","a","b","b","c","c"), x = rep(c(1,2), 3), y = rep(c(1), 6), l = as.factor(c(1,1,0,0,0,0)))
# Contents of df:
# n x y l
# 1 a 1 1 1
# 2 a 2 1 1
# 3 b 1 1 0
# 4 b 2 1 0
@mkuhn
mkuhn / initial_param_vs_runtime.R
Created Sep 30, 2015
Detecting correlation between initial parameters and run time in RStan
View initial_param_vs_runtime.R
library(purrr)
runtimes <- get_elapsed_time(fit)[,2]
inits <- get_inits(fit)
## traditional conversion to a matrix
# m.init <- do.call(rbind, lapply(inits, function(l) do.call(c, l)))
## using purrr