Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
explodecomputer / aries.R
Created November 21, 2015 12:34
aries data
load("aries_funnorm.randomeffect.pc10.160915.Robj")
load("samplesheet_aries.160915.Robj")
# Make sure that all the sample_names in samplesheet match the colnames in norm.beta.random
all(samplesheet$Sample_Name == colnames(norm.beta.random))
# This is not the case so we need to match things up
# Get the intersect
ids <- intersect(samplesheet$Sample_Name, colnames(norm.beta.random))
@explodecomputer
explodecomputer / rowcors.R
Created November 22, 2015 17:46
row correlations between two matrices
rowCors = function(x, y)
{
sqr = function(x) x*x
if(!is.matrix(x)||!is.matrix(y)||any(dim(x)!=dim(y)))
stop("Please supply two matrices of equal size.")
x = sweep(x, 1, rowMeans(x))
y = sweep(y, 1, rowMeans(y))
cor = rowSums(x*y) / sqrt(rowSums(sqr(x))*rowSums(sqr(y)))
return(cor)
}
@explodecomputer
explodecomputer / extract_bgen.sh
Created November 25, 2015 15:04
extract SNPs and IDs from bgen files
#!/bin/bash
while [[ $# > 1 ]]
do
key="$1"
case $key in
-g|--rootname)
genort="${2}"
shift
@explodecomputer
explodecomputer / missingness_pcs.R
Created December 4, 2015 12:37
missingness and PCs
impute <- function(X)
{
apply(X, 2, function(x) {
x[is.na(x)] <- mean(x, na.rn=T)
return(x)
})
}
# Original matrix
@explodecomputer
explodecomputer / snp_lookup.R
Last active December 7, 2015 17:39
SNP lookup with ld proxies
# In R:
load("/panfs/panasas01/shared/alspac/deprecated/gib/aries_renormalised/condanal_results.RData")
mprobes <- c("cg06500161", "cg11024682", "cg07094298", "cg04011474", "cg10192877", "cg17782974", "cg20496314", "cg07202479", "cg09494176", "cg17501210", "cg21139312", "cg26403843")
b <- subset(condres, CPG %in% mprobes)
write.table(unique(b$SNP), file="~/snplist.txt", row=F, col=F, qu=F)
@explodecomputer
explodecomputer / survival.Rmd
Created December 31, 2015 18:45
survival notes
---
title: Notes on survival analysis
author: Gibran Hemani
date: "`r Sys.Date()`"
output: pdf_document
bibliography: survival.bib
---
These notes are based on [http://data.princeton.edu/wws509/notes](http://data.princeton.edu/wws509/notes).
@explodecomputer
explodecomputer / simulate_correlated_snps.R
Created January 5, 2016 20:35
simulate correlated SNPs
simulate_two_correlated_snps <- function(n, p1, p2, r)
{
D <- r * sqrt((1-p1) * (1-p2) * p1 * p2)
p11 <- p1 * p2 + D
p12 <- p1 * (1 - p2) - D
p21 <- (1 - p1) * p2 - D
p22 <- (1-p1) * (1-p2) + D
if(any(c(p11, p12, p21, p22) < 0))
{
@explodecomputer
explodecomputer / server.R
Created February 26, 2016 10:28
shiny example 1
palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
"#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))
shinyServer(function(input, output, session) {
# Combine the selected variables into a new data frame
selectedData <- reactive({
iris[, c(input$xcol, input$ycol)]
})
@explodecomputer
explodecomputer / eff_from_z.R
Created March 10, 2016 17:05
calculate effect size and se from z scores
get_beta_se_from_p_z_n_vary <- function(z, n, vy, maf)
{
qval <- qchisq(pnorm(abs(z), low=FALSE)*2, 1, low=F) / (qchisq(pnorm(abs(z), low=FALSE)*2, n-2, low=F)/(n-2))
r <- sqrt(sum(qval / (n - qval)))
b <- sign(z) * sqrt(r^2 * vy / (2*maf*(1-maf)))
se <- b / z
return(list(b=b, se=se))
}
@explodecomputer
explodecomputer / server.R
Created March 19, 2016 20:20
R shiny google authentication example
library(shiny)
library(shinydashboard)
library(googleAuthR)
options("googleAuthR.scopes.selected" = c("https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"))
options("googleAuthR.webapp.client_id" = "906514199468-1jpkqgngur8emoqfg9j460s47fdo2euo.apps.googleusercontent.com")
options("googleAuthR.webapp.client_secret" = "I7Gqp83Ku4KJxL9zHWYxG_gD")
user_info <- function(){
f <- gar_api_generator("https://www.googleapis.com/oauth2/v1/userinfo",