Skip to content

Instantly share code, notes, and snippets.

@mkuhn
mkuhn / gist:9ad4da2e039e5c6741e2
Created March 10, 2015 16:00
Composite figures using ggplot2 and gtable
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")
library(dplyr)
a <- data.frame(foo = 1:10, bar = "bar")
b <- tbl_df(a)
a[,1]
b[,1]
paste0(a[,1], "!")
paste0(b[,1], "!")
# 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):
#!/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 January 5, 2011 08:21
a small utility for repeatedly (and interactively) running grep on the same file
#!/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 / gist:862551
Created March 9, 2011 17:04
Comparing two-dimensional distributions using ggplot2
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)
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 August 11, 2011 08:08
ggplot: Determining the order in which lines are drawn
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 September 30, 2015 09:52
Detecting correlation between initial parameters and run time in RStan
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
> library(purrr)
> ll <- list(list(a=1:3, b=4), list(a=5:7, b=8))
> ll %>% map(lift_dl(c)) %>% map_call(rbind)
a1 a2 a3 b
[1,] 1 2 3 4
[2,] 5 6 7 8
> Reduce(rbind, ll)
a b
init Integer,3 4
Integer,3 8