Skip to content

Instantly share code, notes, and snippets.

View kbroman's full-sized avatar

Karl Broman kbroman

View GitHub Profile
@kbroman
kbroman / sprintf_fix.R
Created March 21, 2013 23:04
R function to deal with sprintf("%.2f", ...) returning -0.00
# a function to deal with sprintf("%.2f", ...) returning -0.00
# see https://twitter.com/hspter/status/314858331598626816
f <- function(..., dig=2) {
g <- sprintf(paste0("%.", dig, "f"), ...)
z <- paste0("0.", paste(rep("0", dig), collapse=""))
g[g==paste0("-",z)] <- z
g
}
@kbroman
kbroman / cistrans.coffee
Last active December 16, 2015 15:08
Cis-trans plot using the old version of d3-tip.
# cistrans_coffee
#
# Interactive cis-trans eQTL plot
#
# In top figure, x-axis corresponds to marker location, y-axis is
# genomic position of probes on a gene expression microarray Each
# plotted point is an inferred eQTL with LOD > 10; opacity corresponds
# to LOD score, though all LOD > 25 are made fully opaque.
#
# Hover over a point to see probe ID and LOD score; also highlighted
@kbroman
kbroman / cistrans_rev.coffee
Last active December 16, 2015 15:09
Cis-trans plot using the revised version of d3-tip.
# cistrans_coffee
#
# Interactive cis-trans eQTL plot
#
# In top figure, x-axis corresponds to marker location, y-axis is
# genomic position of probes on a gene expression microarray Each
# plotted point is an inferred eQTL with LOD > 10; opacity corresponds
# to LOD score, though all LOD > 25 are made fully opaque.
#
# Hover over a point to see probe ID and LOD score; also highlighted
@kbroman
kbroman / chutes_and_ladders.R
Last active September 23, 2023 05:13
R simulation of chutes and ladders
# function to simulate chutes and ladders game
chutesNladders <-
function(nplayers=1)
{
transitions <- rbind(
c(1, 38),
c(4, 14),
c(9, 31),
c(16, 6),
c(21, 42),
@kbroman
kbroman / qtleff_riself.R
Last active August 29, 2015 13:58
Assess estimated QTL effects by fitqtl
library(qtl)
# simulate data
set.seed(57173190)
data(map10)
map10 <- map10[1:2]
x <- sim.cross(map10, n.ind=128, type="riself")
g1 <- pull.geno(x)[,"D1M6"]
g2 <- pull.geno(x)[,"D2M6"]
x$pheno[,1] <- rnorm(nind(x)) + 0.5*(g1==2) + 1.5*(g2==2)
@kbroman
kbroman / gives_error.csv
Last active July 2, 2018 21:43
Weird problem with read.csv in R
We can make this file beautiful and searchable if this error is corrected: It looks like row 4 should actually have 4 columns, instead of 7 in line 3.
id,chrom,cM,bp
1_444,1,0,44
MSAT1.1,1,162,263
5-727,5,34,727,,,
5-784,5,35,784,,,
5_472,5,24,472
@kbroman
kbroman / cleanGeno.R
Last active August 29, 2015 13:58
cleanGeno modified to work with riself, risib, dh, and haploids, rather than just backcross
######################################################################
#
# cleanGeno: omit genotypes that are possibly in error, as indicated
# by apparent double-crossovers separated by a distance of
# no more than maxdist and having no more than maxmark
# interior typed markers
#
######################################################################
cleanGeno <-
@kbroman
kbroman / backslashes.Rmd
Last active August 29, 2015 13:59
markdown package turns double-backslashes into backslashes
$$latex
\begin{bmatrix}
a & z \\
d & e \\
g & h
\end{bmatrix}
$$
```{r}
cat(RJSONIO::toJSON(list(x=1, y=character(0))))
sessionInfo()
```
mu <- 3
sigma <- 5
ran <- rnorm(1e5, mu, sigma)
differentiate <-
function(x, y)
{
diffOfX <- diff(x)
data.frame(
x = x[-length(x)] + (diffOfX / 2),