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 / getCorrelation.R
Created March 24, 2015 21:50
Before I forget
D <- split(as.data.frame(M), A)
correlations <- llapply(D,D,getCorrelation)
llapply <- function(list1,list2, fun){
temp <- unlist(lapply(list1, function(x){
unlist(lapply(list2, function(y){
do.call(fun, list(x,y))
}))
}))
@Jfortin1
Jfortin1 / llapply.R
Last active August 29, 2015 14:17
Useful apply functions
pair.lapply <- function(list1, list2, fun, list3=NULL){
results <- list()
for (i in 1:length(list1)){
if (is.null(list3)){
results[[i]] <- do.call(fun, list(list1[[i]],list2[[i]]))
} else {
results[[i]] <- do.call(fun, list(list1[[i]][list3[[i]]],list2[[i]][list3[[i]]]))
}
}
@Jfortin1
Jfortin1 / tar.sh
Created February 5, 2015 16:54
Stupid tar command I always forget
tar -zxvf
@Jfortin1
Jfortin1 / getLocations.R
Created January 5, 2015 18:23
To get the locations of the 450k probes
library(IlluminaHumanMethylation450kanno.ilmn12.hg19)
locations <- getLocations(IlluminaHumanMethylation450kanno.ilmn12.hg19)
@Jfortin1
Jfortin1 / killscreens.sh
Created December 6, 2014 03:19
To kill all your screen sessions
killscreens () {
screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill
}
@Jfortin1
Jfortin1 / commands.sh
Last active August 29, 2015 14:10
Commands that I always forget
# To ask for more writing memory:
-l h_fsize=40G
# Tar
tar -xvfz
# To remove all deleted files from git repo:
git rm $(git ls-files --deleted)
@Jfortin1
Jfortin1 / BetaToM.R
Last active August 29, 2015 14:10
Beta to M
BetaToM <- function(beta){
matrix <- log2(beta) - log2(1-beta)
impute.matrix(matrix)
}
MToBeta <- function(mmatrix){
matrix <- 2^mmatrix/(2^mmatrix+1)
impute.matrix(matrix)
}
@Jfortin1
Jfortin1 / impute.matrix.R
Last active August 29, 2015 14:10
Simple way to impute missing values in matrix
impute.matrix <- function (matrix) {
missing <- which(is.na(matrix) | !is.finite(matrix), arr.ind=TRUE)
if (length(missing)!=0){
for (j in 1:nrow(missing)){
mean <- mean(matrix[missing[j,1],][is.finite(matrix[missing[j,1],])], na.rm=TRUE)
matrix[missing[j,1],missing[j,2]] <- mean
}
}
matrix
}
library(IlluminaHumanMethylation450kanno.ilmn12.hg19)
ann <- getAnnotation(IlluminaHumanMethylation450kanno.ilmn12.hg19)
island.status <- gsub("N_|S_","",ann$Relation_to_Island)
counts <- table(island.status)
percs <- counts/nrow(ann)*100
@Jfortin1
Jfortin1 / extract.block.R
Created November 20, 2014 22:54
Extract a block from bigWig files
extract.block <- function(files, chr, start, end, verbose = TRUE){
rl <- IRanges::RangesList(IRanges::IRanges(start=start, end=end))
names(rl) <- chr
cat("[extract.block] Reading in the files \n")
rles <- lapply(files, function(xx) {
#if (verbose){
# cat("Reading", xx, "\n")
#}
import(xx, as = "Rle", format = "bw", selection = BigWigSelection(rl))
})