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 / getSnpBeta.R
Created August 1, 2014 19:34
To get the Beta values for the 65 SNP probes
# object must be an RGChannelSet object from the minfi package
getSnpBeta <- function(object){
require(minfi)
minfi:::.isRG(object)
manifest <- getManifest(object)
snpProbesII <- getProbeInfo(manifest, type = "SnpII")$Name
M.II <- getGreen(object)[getProbeInfo(object, type = "SnpII")$AddressA,]
@Jfortin1
Jfortin1 / makeAutosomal.R
Last active August 10, 2022 17:20
To remove sex chromosome probes from a 450k matrix
makeAutosomal <- function(matrix){
require(IlluminaHumanMethylation450kanno.ilmn12.hg19)
ann <- getAnnotation(IlluminaHumanMethylation450kanno.ilmn12.hg19)
good.probes <- rownames(ann)[-which(ann$chr=="chrX" | ann$chr=="chrY")]
matrix[rownames(matrix) %in% good.probes,]
}
makeX <- function(matrix){
require(IlluminaHumanMethylation450kanno.ilmn12.hg19)
@Jfortin1
Jfortin1 / getPCS.R
Created November 14, 2014 19:48
Fast eigenvalue decomposition for first 2 pcs
getPCS <- function(matrix, ncomp=2){
require(mixOmics)
nipals(matrix, ncomp = ncomp)$p
}
@Jfortin1
Jfortin1 / extractMetaGsm.R
Created November 16, 2014 17:13
To obtain metadata from GSM**** files
library(GEOquery)
extractMetaGsm <- function(GEO){
require(GEOquery)
GSElimits=NULL
AnnotGPL=FALSE
destdir <- tempdir()
meta <- list()
for (i in 1:length(GEO)){
@Jfortin1
Jfortin1 / extract.portion.R
Created November 20, 2014 22:54
Extract only a portion of a bigWig file
extract.portion <- function(file, chr, start, end, verbose = TRUE){
rl <- IRanges::RangesList(IRanges::IRanges(start=start, end=end))
names(rl) <- chr
portion <- import(file, as="Rle",format = "bw", selection = BigWigSelection(rl))
portion <- portion[chr]
portion <- as.numeric(portion[[chr]])
portion
}
@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))
})
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 / 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
}
@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 / 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)