Commit cd16e491 authored by Leon-Charles Tranchevent's avatar Leon-Charles Tranchevent
Browse files

New parameter for limma analysis: possibility to include gender as a co-factor.

parent ebc03074
......@@ -32,4 +32,4 @@ Imports:
methods,
illuminaHumanv3.db,
topconfects
RoxygenNote: 7.0.2
RoxygenNote: 7.1.0
......@@ -13,6 +13,9 @@
#' @param correct_for_batch A boolean indicating whether we should correct for a poential batch
#' effect in the limma design matrix. This expects that the actual batch information exists in
#' the object pheno_data (as field Batch). Default to FALSE.
#' @param correct_for_gender A boolean indicating whether we should correct for sample gender
#' in the limma design matrix. This expects that a clinical factor termed Gender exists. Default
#' to FALSE.
#' @param cofactor_name A string indicating a potential cofactor (included in the phenotypic data).
#' The default is NULL, and no cofactor is considered. This will replace the age correction if
#' both were deemed necessary.
......@@ -22,6 +25,7 @@
create_design <- function(pheno_data, limma_parameters,
correct_for_age = FALSE,
correct_for_batch = FALSE,
correct_for_gender = FALSE,
cofactor_name = NULL,
verbose = FALSE) {
......@@ -40,7 +44,9 @@ create_design <- function(pheno_data, limma_parameters,
if (has_block == FALSE) {
if (correct_for_age == FALSE) {
if (correct_for_batch == FALSE) {
# We do not have a block, we do not include age, nor batch.
if (correct_for_gender == FALSE) {
# We do not have a block, we do not include age, nor batch, nor gender.
design <- stats::model.matrix(~ 0 + clinical)
colnames(design) <- levels(clinical)
......@@ -49,8 +55,24 @@ create_design <- function(pheno_data, limma_parameters,
message(paste0("[", Sys.time(), "] Design matrix created."))
}
} else {
# We do not have a block, we do not include age, nor batch, but we include gender.
genders <- factor(pheno_data[["Gender"]])
design <- stats::model.matrix(~ 0 + clinical + genders)
colnames(design) <- c(levels(clinical), levels(genders)[2:length(levels(genders))])
# We log information.
rm(genders)
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] Design matrix created (genders)."))
}
}
} else {
# Here we have correct_for_batch = TRUE
# We do not have a block, we do not include age but we do include batch.
if (correct_for_gender == FALSE) {
# We do not have a block, we do not include age, nor gender but we do include batch.
batches <- factor(pheno_data[["Batch"]])
design <- stats::model.matrix(~ 0 + clinical + batches)
colnames(design) <- c(levels(clinical), levels(batches)[2:length(levels(batches))])
......@@ -60,11 +82,29 @@ create_design <- function(pheno_data, limma_parameters,
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] Design matrix created (batches)."))
}
} else {
# We do not have a block, we do not include age but we do include batch and gender.
batches <- factor(pheno_data[["Batch"]])
genders <- factor(pheno_data[["Gender"]])
design <- stats::model.matrix(~ 0 + clinical + batches + genders)
colnames(design) <- c(levels(clinical), levels(batches)[2:length(levels(batches))],
levels(genders)[2:length(levels(genders))])
# We log information.
rm(batches, genders)
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] Design matrix created (batches, genders)."))
}
}
}
} else {
# Here we have correct_for_age = TRUE
if (correct_for_batch == FALSE) {
# We do not have a block, we do include age, but not batch.
if (correct_for_gender == FALSE) {
# We do not have a block, we do include age, but not batch, nor gender.
ages <- as.numeric(pheno_data[["Age"]])
design <- stats::model.matrix(~ 0 + clinical + ages)
colnames(design) <- c(levels(clinical), "Age")
......@@ -75,8 +115,25 @@ create_design <- function(pheno_data, limma_parameters,
message(paste0("[", Sys.time(), "] Design matrix created (ages)."))
}
} else {
# We do not have a block, we do include age and gender but not batch.
ages <- as.numeric(pheno_data[["Age"]])
genders <- factor(pheno_data[["Gender"]])
design <- stats::model.matrix(~ 0 + clinical + ages + genders)
colnames(design) <- c(levels(clinical), "Age", levels(genders)[2:length(levels(genders))])
# We log information.
rm(ages, genders)
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] Design matrix created (ages, genders)."))
}
}
} else {
# Here we have correct_for_batch = TRUE
# We do not have a block, we do include age and batch.
if (correct_for_gender == FALSE) {
# We do not have a block, we do include age and batch but not gender.
ages <- as.numeric(pheno_data[["Age"]])
batches <- factor(pheno_data[["Batch"]])
design <- stats::model.matrix(~ 0 + clinical + ages + batches)
......@@ -87,13 +144,32 @@ create_design <- function(pheno_data, limma_parameters,
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] Design matrix created (ages, batches)."))
}
} else {
# We do not have a block, we do include age, batch and gender.
ages <- as.numeric(pheno_data[["Age"]])
batches <- factor(pheno_data[["Batch"]])
genders <- factor(pheno_data[["Gender"]])
design <- stats::model.matrix(~ 0 + clinical + ages + batches + genders)
colnames(design) <- c(levels(clinical), "Age", levels(batches)[2:length(levels(batches))],
levels(genders)[2:length(levels(genders))])
# We log information.
rm(ages, batches, genders)
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] Design matrix created (ages, batches, genders)."))
}
}
}
}
} else {
# Here we have has_block = TRUE
if (correct_for_age == FALSE) {
if (correct_for_batch == FALSE) {
# We do have a block, we do not include age, nor batch.
if (correct_for_gender == FALSE) {
# We do have a block, we do not include age, nor batch, nor gender.
clinical_cofactor <- factor(pheno_data[[cofactor_name]]) # nolint
design <- stats::model.matrix(~ 0 + clinical + clinical_cofactor)
colnames(design) <- c(levels(clinical), cofactor_name)
......@@ -104,8 +180,26 @@ create_design <- function(pheno_data, limma_parameters,
message(paste0("[", Sys.time(), "] Design matrix created (pairs)."))
}
} else {
# We do have a block, we do not include age, nor batch, but we include gender.
clinical_cofactor <- factor(pheno_data[[cofactor_name]]) # nolint
genders <- factor(pheno_data[["Gender"]])
design <- stats::model.matrix(~ 0 + clinical + genders + clinical_cofactor)
colnames(design) <- c(levels(clinical), levels(genders)[2:length(levels(genders))],
cofactor_name)
# We log information.
rm(clinical_cofactor, genders)
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] Design matrix created (pairs, genders)."))
}
}
} else {
# Here we have correct_for_batch = TRUE
# We do have a block, we do not include age but we do include batch.
if (correct_for_gender == FALSE) {
# We do have a block, we do not include age, nor gender but we do include batch.
clinical_cofactor <- factor(pheno_data[[cofactor_name]]) # nolint
batches <- factor(pheno_data[["Batch"]])
design <- stats::model.matrix(~ 0 + clinical + batches + clinical_cofactor)
......@@ -118,11 +212,32 @@ create_design <- function(pheno_data, limma_parameters,
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] Design matrix created (pairs, batches)."))
}
} else {
# We do have a block, we do not include age, nor gender but we do include batch.
clinical_cofactor <- factor(pheno_data[[cofactor_name]]) # nolint
batches <- factor(pheno_data[["Batch"]])
genders <- factor(pheno_data[["Gender"]])
design <- stats::model.matrix(~ 0 + clinical + batches + genders + clinical_cofactor)
colnames(design) <- c(levels(clinical),
levels(batches)[2:length(levels(batches))],
levels(genders)[2:length(levels(genders))],
cofactor_name)
# We log information.
rm(clinical_cofactor, batches, genders)
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] Design matrix created (pairs, batches, genders)."))
}
}
}
} else {
# Here we have correct_for_age = TRUE
if (correct_for_batch == FALSE) {
# We do have a block, we do include age, but not batch.
if (correct_for_gender == FALSE) {
# We do have a block, we do include age, but not batch, nor gender.
clinical_cofactor <- factor(pheno_data[[cofactor_name]]) # nolint
ages <- as.numeric(pheno_data[["Age"]])
design <- stats::model.matrix(~ 0 + clinical + ages + clinical_cofactor)
......@@ -134,8 +249,27 @@ create_design <- function(pheno_data, limma_parameters,
message(paste0("[", Sys.time(), "] Design matrix created (pairs, ages)."))
}
} else {
# We do have a block, we do include age and gender but not batch.
clinical_cofactor <- factor(pheno_data[[cofactor_name]]) # nolint
ages <- as.numeric(pheno_data[["Age"]])
genders <- factor(pheno_data[["Gender"]])
design <- stats::model.matrix(~ 0 + clinical + ages + genders + clinical_cofactor)
colnames(design) <- c(levels(clinical), "Age",
levels(genders)[2:length(levels(genders))], cofactor_name)
# We log information.
rm(clinical_cofactor, ages, genders)
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] Design matrix created (pairs, ages, genders)."))
}
}
} else {
# Here we have correct_for_batch = TRUE
# We do have a block, we do include age and batch.
if (correct_for_gender == FALSE) {
# We do have a block, we do include age and batch but not gender.
clinical_cofactor <- factor(pheno_data[[cofactor_name]]) # nolint
ages <- as.numeric(pheno_data[["Age"]])
batches <- factor(pheno_data[["Batch"]])
......@@ -151,6 +285,27 @@ create_design <- function(pheno_data, limma_parameters,
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] Design matrix created (pairs, ages, batches)."))
}
} else {
# We do have a block, we do include age, batch and gender.
clinical_cofactor <- factor(pheno_data[[cofactor_name]]) # nolint
ages <- as.numeric(pheno_data[["Age"]])
batches <- factor(pheno_data[["Batch"]])
genders <- factor(pheno_data[["Gender"]])
design <- stats::model.matrix(~ 0 + clinical + ages + batches + genders +
clinical_cofactor)
colnames(design) <- c(levels(clinical),
"Age",
levels(batches)[2:length(levels(batches))],
levels(genders)[2:length(levels(genders))],
cofactor_name)
# We log information.
rm(clinical_cofactor, ages, batches, genders)
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] Design matrix created (pairs, ages, batches, genders)."))
}
}
}
}
}
......
......@@ -14,9 +14,12 @@
#' ignored for the time being.
#' @param correct_for_age A boolean indicating whether we should correct for age in the limma
#' design matrix. This expects that a clinical factor termed Age exists. Default to FALSE.
#' @param correct_for_batch A boolean indicating whether we should correct for a poential batch
#' @param correct_for_batch A boolean indicating whether we should correct for a potential batch
#' effect in the limma design matrix. This expects that the actual batch information exists in
#' the object pheno_data (as field Batch). Default to FALSE.
#' @param correct_for_gender A boolean indicating whether we should correct for sample gender
#' in the limma design matrix. This expects that a clinical factor termed Gender exists. Default
#' to FALSE.
#' @param cofactor_name A string indicating a potential cofactor (included in the phenotypic data).
#' The default is NULL, and no cofactor is considered. This will replace the age correction if
#' both were deemed necessary.
......@@ -38,6 +41,7 @@
run_limma <- function(pheno_data, eset, limma_parameters,
correct_for_age = FALSE,
correct_for_batch = FALSE,
correct_for_gender = FALSE,
cofactor_name = NULL,
block_key = NULL,
run_topconfects = FALSE,
......@@ -70,6 +74,7 @@ run_limma <- function(pheno_data, eset, limma_parameters,
limma_parameters = limma_parameters,
correct_for_age = correct_for_age,
correct_for_batch = correct_for_batch,
correct_for_gender = correct_for_gender,
cofactor_name = cofactor_name,
verbose = verbose)
......@@ -124,6 +129,7 @@ run_limma <- function(pheno_data, eset, limma_parameters,
# We now finish the TopConfects analysis if necesary.
if (run_topconfects) {
# We first run TopConfects on the dedicated lmfit object.
confects <- topconfects::limma_confects(lmfit_topconfects, coef = 1, fdr = 0.05)
......@@ -170,6 +176,7 @@ run_limma <- function(pheno_data, eset, limma_parameters,
# a rank rank plot.
limma_top <- limma::topTable(fit, coef = 1, p.value = 0.05, n = Inf)
if (nrow(limma_top) > 0) {
# Log info.
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] [TopConfects] ", nrow(limma_top),
......@@ -199,12 +206,14 @@ run_limma <- function(pheno_data, eset, limma_parameters,
grDevices::dev.off()
rm(p, mdplot_fn)
} else {
# Log info.
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] [TopConfects] no gene identified with Limma."))
}
} # End of the plotting (if there are Limma genes).
} else {
# Log info.
if (verbose == TRUE) {
message(paste0("[", Sys.time(), "] [TopConfects] no gene identified with TopConfects."))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment