Skip to content

Instantly share code, notes, and snippets.

View Jfortin1's full-sized avatar
🏠
Working from home

Jean-Philippe Fortin Jfortin1

🏠
Working from home
View GitHub Profile
@Jfortin1
Jfortin1 / splitByRow.R
Created April 9, 2018 21:46
Tired of slow split()
splitByRow <- function(Y, f){
require(tidyverse)
names <- dimnames(Y)
Y <- as.matrix(Y); dimnames(Y) <- names
#levels <- sort(unique(f)) %>% as.character
temp <- split(Y, f=f)
names <- names(temp)
ns <- table(f)[names]
temp <- lapply(1:length(temp), function(i){
x <- matrix(temp[[i]], ncol=ncol(Y), nrow=ns[[i]], byrow=FALSE)
@Jfortin1
Jfortin1 / limma.R
Created October 23, 2016 01:43
ordinary t from limma
tstat.ord <- fit$coef / fit$stdev.unscaled / fit$sigma
@Jfortin1
Jfortin1 / ComBat2.R
Created October 14, 2016 18:54
ComBat2.R
ComBat2 <- function(dat, batch, mod=NULL, par.prior=TRUE,prior.plots=FALSE,mean.only=FALSE,ref.batch=NULL, verbose=FALSE) {
# make batch a factor and make a set of indicators for batch
require(sva)
if(mean.only==TRUE){
cat("Using the 'mean only' version of ComBat\n")
}
if(length(dim(batch))>1){
stop("This version of ComBat only allows one batch variable")
} ## to be updated soon!
batch <- as.factor(batch)
getMNITemplatePath <- function(what=c("full", "brain", "brain_mask"), res=c("1mm", "2mm")){
require(fslr)
dir <- file.path(fsl_data_dir(), "standard")
what <- match.arg(what)
res <- match.arg(res)
if (what == "full") {
file <- file.path(dir, paste0("MNI152_T1_",res,".nii.gz"))
}
else if (what == "brain") {
file <- file.path(dir, paste0("MNI152_T1_",res,"_brain.nii.gz"))
@Jfortin1
Jfortin1 / getVashT.R
Last active April 14, 2016 19:32
Wrapper for vash
# Wrapper to calculate t-statistic from vash with one covariate
getVashT <- function(x, mod, df=NULL, ...){
require(vashr)
require(limma)
fit <- lmFit(x, mod)
if (is.null(df)){
df <- ncol(x)-1
}
temp <- vash(sehat = fit$sigma, df=df, ...)
temp <- fit$coef[,2] / temp$sd.post / fit$stdev.unscaled[,2]
Reduce(intersect, list(a,b,c,d))
@Jfortin1
Jfortin1 / getR2.R
Last active August 29, 2015 14:20
getR2.R
getR2 <- function(Y,X){
fitted <- t(solve(t(X)%*%X) %*% t(X) %*% t(Y)) %*% t(X)
res <- Y-fitted
Y.demeaned <- t(scale(t(Y), center=TRUE, scale=FALSE))
ssres <- rowSums(res^2)
sstot <- rowSums(Y.demeaned^2)
1-ssres/sstot
}
@Jfortin1
Jfortin1 / fastq.gz
Created April 23, 2015 23:30
SRA to zipped fastq
fastq-dump file.sra -Z | gzip > file.fastq.gz
@Jfortin1
Jfortin1 / darken.R
Last active September 2, 2023 20:42
Darken or lighten colors in R
darken <- function(color, factor=1.4){
col <- col2rgb(color)
col <- col/factor
col <- rgb(t(col), maxColorValue=255)
col
}
lighten <- function(color, factor=1.4){
col <- col2rgb(color)
@Jfortin1
Jfortin1 / liftOverFunctions.sh
Last active August 29, 2015 14:17
liftOver functions
#Here is how to convert a bigWig file from hg18 to hg19:
bigWigToBedGraph file18.bw hg18.chrom.sizes chfile18.bed
liftOver file18.bed hg18ToHg19.over.chain file19.bed unmapped
bedGraphToBigWig file19.bed hg19.chrom.sizes file19.bw