Skip to content

Instantly share code, notes, and snippets.

View chr1swallace's full-sized avatar

Chris Wallace chr1swallace

View GitHub Profile
## I don't know how much R you know already. Start at the beginning,
## use online R tutorials, the native help system, and books, and work
## through the steps below. I've tried to write enough that if you
## already know R you won't be bored, so if you don't already know R,
## please don't assume you *have* to complete these tasks!
## install packages - only need to do this first time
install.packages("devtools")
library(devtools)
install_github("chr1swallace/coloc") # has the finemap.abf function
library(data.table)
library(parallel)
library(annotSnpStats)
library(snpStats)
library(GUESSFM)
##
inv.logit.fn <-function(x) return(exp(x)/(1+exp(x)))
##
## sharing parameter, largish, so we expect to see a big enough difference to check if working
S <- 100
prior.bin.fn <- function(nT1,s=100,mT1=3) {
dbinom(nT1,size=s,prob=mT1/s)/choose(s,nT1)
}
bothpp <- function(data) {
ss1 <- strsplit(data$t1.str,"%")
ss2 <- strsplit(data$t2.str,"%")
data$overlap <- sapply(1:nrow(data), function(i)
## sharing parameter, largish, so we expect to see a big enough difference to check if working
S <- 100
prior.bin.fn <- function(nT1,s=100,mT1=3) {
dbinom(nT1,size=s,prob=mT1/s)/choose(s,nT1)
}
bothpp <- function(data) {
ss1 <- strsplit(data$t1.str,"%")
ss2 <- strsplit(data$t2.str,"%")
data$overlap <- sapply(1:nrow(data), function(i)
@chr1swallace
chr1swallace / T1DManhattan.R
Created October 14, 2015 08:45
Create a Manhattan plot of T1D GWAS statistics downloaded from T1DBase
## download https://www.t1dbase.org/downloads/protected_data/GWAS_Data/hg19_gwas_t1d_barrett_4_18_0.gz
## you will need to create a login!
## Then...
x <- read.table("hg19_gwas_t1d_barrett_4_18_0.gz",sep="\t",skip=1,as.is=TRUE)
colnames(x) <- c("snp","Chr","Pos","PValue","OR","kk")
## libraries
library(ggplot2)
library(cowplot) # nicer ggplot theme
@chr1swallace
chr1swallace / orcidcloud.R
Last active February 8, 2021 08:49
make a word cloud from an orcid id
## install packages using the following
## install.packages("devtools")
## library(devtools)
## install_github("ropensci/rorcid")
## install.packages(c("tm","wordcloud"))
## load libraries
library(rorcid)
library(wordcloud)
@chr1swallace
chr1swallace / gantt.R
Created November 7, 2014 07:19
create a timeline
# replace "gantt.csv" with path to wherever your .csv file is stored
x <- read.table("gantt.csv",sep=",",as.is=TRUE, header=TRUE) # read data
## look at what we read, check it makes sense
head(x)
## make sure dates are recognised as such
x$Start <- as.Date(x$Start)
x$End <- as.Date(x$End)
@chr1swallace
chr1swallace / fit-normals.R
Created April 30, 2014 08:33
Fits a two Guassian mixture model, assuming Z ~ N(0,1) with prob pi0, N(0,1+sigma^2) with prob 1-pi0.
##' Fit a specific two Guassian mixture distribution
##'
##' Assumes Z ~ N(0,1) with prob pi0,
##' Z ~ N(0,1+sigma^2) with prob 1-pi0
##'
##' Aims to estimate sigma^2 and pi0.
##'
##' @title fit.em
##' @param Z numeric vector of observed data
##' @param s2 initial value for sigma^2
@chr1swallace
chr1swallace / cond.R
Created October 29, 2013 12:32
conditional testing of SNPs - reports number and which SNPs are "significant" in sequential conditional testing
library(snpStats)
best <- cond.best(X,Y, family=family)
while(length(newbest <- cond.best(X, Y, best, family=family)))
best <- c(best,newbest)
cond.best <- function(X,Y,best=NULL,p.thr=1e-6, ...) {
if(is.null(best)) {
cond <- snp.rhs.tests(Y ~ 1, snp.data=X, data=data.frame(Y=Y,
row.names=rownames(X)), ...)
p.thr <- 1
@chr1swallace
chr1swallace / covar.R
Created July 18, 2013 12:08
For Niko: linear transformations affect covariance but not correlation
library(MASS)
data<-mvrnorm(n=100, mu=c(1,2), Sigma=matrix(c(1,0.5,0.5,1),2))
colnames(data) <- c("x","y")
var(data)
cor(data)
data[,"x"] <- data[,"x"] + 3
var(data) # unchanged
cor(data) # unchanged