Skip to content

Commit

Permalink
Update R code after running 'lintr::lint()' which detects style, synt…
Browse files Browse the repository at this point in the history
…ax and semantic issues
  • Loading branch information
aursiber committed Aug 21, 2024
1 parent 38a838d commit d604940
Show file tree
Hide file tree
Showing 24 changed files with 2,490 additions and 2,936 deletions.
84 changes: 35 additions & 49 deletions R/PCAdataplot.R
Original file line number Diff line number Diff line change
@@ -1,83 +1,69 @@
# Plot of trend repartition per group of items,
# (e.g. from biological annotation),
# and optionally per molecular level
# Plot of trend repartition per group of items,
# (e.g. from biological annotation),
# and optionally per molecular level
# (or per another additional grouping level )
PCAdataplot <- function(omicdata, batch, label)
{
if (!(inherits(omicdata, "microarraydata") |
inherits(omicdata, "RNAseqdata") |
PCAdataplot <- function(omicdata, batch, label) {
if (!(inherits(omicdata, "microarraydata") ||
inherits(omicdata, "RNAseqdata") ||
inherits(omicdata, "continuousomicdata")))
stop("Use only with 'microarraydata', 'RNAseqdata' or 'continuousomicdata'
objects, respectively created with functions 'microarraydata()', 'RNAseqdata()'
or 'continuousomicdata()'.")

dosef <- as.factor(omicdata$dose)
if (!missing(batch))
{
if (!missing(batch)) {
if (length(batch) != length(dosef))
stop("Argument batch must be a factor of length the number of samples in
your omic data set.")
datacondition <- data.frame(batch = as.factor(batch),
dose = dosef)

} else
{
datacondition <- data.frame(dose = dosef)

}
datacondition <- data.frame(batch = as.factor(batch), dose = dosef)
} else {
datacondition <- data.frame(dose = dosef)
}

pseudologdata <- as.data.frame(omicdata$data) # data after log 2 scale
if (missing(label))
{
pseudologdata <- as.data.frame(omicdata$data) # data after log 2 scale
if (missing(label)) {
label <- FALSE
}
if (length(label) > 1)
{
}
if (length(label) > 1) {
add.label <- TRUE
if (length(label) != length(dosef))
{
if (length(label) != length(dosef)) {
stop("Argument label must be TRUE, FALSE of a character vector
giving the name the samples, so of length
the number of samples in your omic data set.")
} else
{
} else {
colnames(pseudologdata) <- as.character(label)

}
} else
{
if (label) {add.label <- TRUE} else {add.label <- FALSE}
} else {
if (label) {
add.label <- TRUE
} else {
add.label <- FALSE
}
}

# colnames(pseudologdata) <- 1:ncol(pseudologdata)
# prcomp{stats}
# autoplot du package ggfortify qui est un ajout de ggplot2
# https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_pca.html
pca.info <- stats::prcomp(t(pseudologdata), scale. = FALSE)
if (add.label)
{
pca.plot <- autoplot(pca.info,
data = datacondition,
if (add.label) {
pca.plot <- autoplot(pca.info,
data = datacondition,
colour = "dose",
shape = FALSE,
label = TRUE)

} else
{
if (missing(batch))
{
pca.plot <- autoplot(pca.info,
data = datacondition,
} else {
if (missing(batch)) {
pca.plot <- autoplot(pca.info,
data = datacondition,
colour = "dose")

} else
{
pca.plot <- autoplot(pca.info,
data = datacondition,
shape = "batch",
} else {
pca.plot <- autoplot(pca.info,
data = datacondition,
shape = "batch",
colour = "dose")
}

}
return(pca.plot)
}
148 changes: 61 additions & 87 deletions R/RNAseqdata.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
### import, check normalize and transform RNAseq data

RNAseqdata <- function(file, backgrounddose, check = TRUE,
transfo.method,
transfo.blind = TRUE, round.counts = FALSE)
{
if (is.data.frame(file))
{
RNAseqdata <- function(file, backgrounddose, check = TRUE,
transfo.method,
transfo.blind = TRUE, round.counts = FALSE) {

if (is.data.frame(file)) {
d <- file
} else
{
if (check)
{
} else {
if (check) {
# check argument file
if (!is.character(file))
stop("The argument file must be a character string.")
Expand All @@ -20,109 +17,93 @@ RNAseqdata <- function(file, backgrounddose, check = TRUE,
stop("The argument file must be a character string ending by .txt.")
}
d <- utils::read.table(file, header = FALSE)
colnames(d) <- c("item", paste0("S", 1:(ncol(d)-1)))

colnames(d) <- c("item", paste0("S", 1:(ncol(d) - 1)))
}

nrowd <- nrow(d)
ncold <- ncol(d)
data <- as.matrix(d[2:nrowd, 2:ncold])
data <- as.matrix(d[2:nrowd, 2:ncold])
nrowdata <- nrowd - 1
ncoldata <- ncold - 1

if(!all(stats::complete.cases(data)))
if (!all(stats::complete.cases(data)))
stop("RNAseqdata() should not be used with data including NA values.")

if (round.counts)
{
if (round.counts) {
data <- round(data)
} else
{
} else {
subdata4check <- as.numeric(data[1:min(nrowdata, 10), ])
subdata4checkT <- trunc(subdata4check)
if (!identical(subdata4check, subdata4checkT))
stop("Your data contain non integer values. Make sure that your RNAseq data are imported in raw counts.
If your counts come from Kallisto or Salmon put the argument round.counts of RNAseqdata at TRUE to round them.\n")
If your counts come from Kallisto or Salmon put the argument round.counts of RNAseqdata at TRUE to round them.\n")
}

if (check)
{
if (check) {
if (nrowdata < 100)
warning(strwrap(prefix = "\n", initial = "\n",
"Your dataset contains less than 100 lines. Are you sure you really
work on RNAseq data ? This function should
not be used with another type of data."))

# check that doses and responses are numeric
if (!is.numeric(as.matrix(d[,2:ncold])))
if (!is.numeric(as.matrix(d[, 2:ncold])))
stop("All the columns except the first one must be numeric with the numeric dose in the firt line
and the read counts (integer values corresponding to raw counts) of each item in the other lines.")
}

# Normalization and count data transformation using DESeq2
if (missing(transfo.method))
{
if (ncoldata > 30) {transfo.method <- "vst"} else {transfo.method <- "rlog"}
} else
{
if (missing(transfo.method)) {
if (ncoldata > 30) {
transfo.method <- "vst"
} else {
transfo.method <- "rlog"
}
} else {
transfo.method <- match.arg(transfo.method, c("rlog", "vst"))
}
if(transfo.method == "rlog")
if (transfo.method == "rlog")
cat(strwrap(paste0("Just wait, the transformation using regularized logarithm (rlog) may take a few minutes.\n")), fill = TRUE)

raw.counts <- data
(dose <- as.vector(unlist(d[1, 2:ncold])))

if(!transfo.blind)
{
if (!transfo.blind) {
coldata <- data.frame(fdose = fdose)
rownames(coldata) <- colnames(data)
dds <- DESeqDataSetFromMatrix(
countData = raw.counts,
colData = coldata,
design = ~ fdose)
}

if (transfo.method == "rlog")
{
if (transfo.blind)
{

if (transfo.method == "rlog") {
if (transfo.blind) {
data <- rlog(raw.counts)
} else
{
data <- assay(rlog(dds, blind = FALSE))
} else {
data <- assay(rlog(dds, blind = FALSE))
}
} else # transfo.method == "vst"
{
} else { # transfo.method == "vst"
nsub <- 1000 # parameter of vst to speed computation
if (transfo.blind)
{
if (nrowdata < nsub)
{
if (transfo.blind) {
if (nrowdata < nsub) {
data <- varianceStabilizingTransformation(raw.counts)
} else
{
} else {
tryvst <- try(vst(raw.counts, nsub = nsub), silent = TRUE)
if (!inherits(tryvst, "try-error"))
{
if (!inherits(tryvst, "try-error")) {
data <- tryvst
} else
{
} else {
data <- varianceStabilizingTransformation(raw.counts)
}
}
} else # VST is not blind to the experimental design
{
if (nrowdata < nsub)
{
} else { # VST is not blind to the experimental design
if (nrowdata < nsub) {
data <- assay(varianceStabilizingTransformation(dds, blind = FALSE))
} else
{
} else {
tryvst <- try(vst(dds, nsub = nsub, blind = FALSE), silent = TRUE)
if (!inherits(tryvst, "try-error"))
{
if (!inherits(tryvst, "try-error")) {
data <- assay(tryvst)
} else
{
} else {
data <- assay(varianceStabilizingTransformation(dds, blind = FALSE))
}
}
Expand All @@ -133,8 +114,7 @@ RNAseqdata <- function(file, backgrounddose, check = TRUE,
row.names(data) <- item <- as.character(d[2:nrowd, 1])
(nitems <- nrow(data))

if (!missing(backgrounddose))
{
if (!missing(backgrounddose)) {
dose <- dose * (dose > backgrounddose)
}

Expand All @@ -150,7 +130,7 @@ RNAseqdata <- function(file, backgrounddose, check = TRUE,
design <- table(dose, dnn = "")
nbdoses <- length(design)
nbpts <- sum(design)
if ((nbdoses < 4)| (nbpts < 8))
if ((nbdoses < 4) || (nbpts < 8))
stop("Dromics cannot be used with a dose-response design
with less than four tested doses/concentrations or less than eight data points
per dose-response curve.")
Expand All @@ -162,32 +142,29 @@ RNAseqdata <- function(file, backgrounddose, check = TRUE,
# calculation of the means per dose
fdose <- as.factor(dose)
tdata <- t(data)
calcmean <- function(i)
{
calcmean <- function(i) {
tapply(tdata[, i], fdose, mean)
}
s <- sapply(1:(nrowd - 1), calcmean)
data.mean <- as.matrix(t(s))

calcsd <- function(i)
{

calcsd <- function(i) {
tapply(tdata[, i], fdose, sd)
}
s <- sapply(1:(nrowd - 1), calcsd)
data.sd <- as.matrix(t(s))

reslist <- list(data = data, dose = dose, item = item,
design = design, data.mean = data.mean,
reslist <- list(data = data, dose = dose, item = item,
design = design, data.mean = data.mean,
data.sd = data.sd,
transfo.method = transfo.method, raw.counts = raw.counts,
containsNA = FALSE)
containsNA = FALSE)

return(structure(reslist, class = "RNAseqdata"))
}


print.RNAseqdata <- function(x, ...)
{
print.RNAseqdata <- function(x, ...) {
if (!inherits(x, "RNAseqdata"))
stop("Use only with 'RNAseqdata' objects.")

Expand All @@ -196,35 +173,32 @@ print.RNAseqdata <- function(x, ...)
print(x$design)
cat("Number of items:", length(x$item), "\n")

if (length(x$item) > 20)
{
if (length(x$item) > 20) {
cat("Identifiers of the first 20 items:\n")
print(x$item[1:20])
} else
{
} else {
cat("Identifiers of the items:\n")
print(x$item)
}
cat(strwrap(paste0("Data were normalized with respect to library size
and tranformed using the following method: ", x$transfo.method)), fill = TRUE)
}

plot.RNAseqdata <- function(x, range4boxplot = 0, ...)
{
plot.RNAseqdata <- function(x, range4boxplot = 0, ...) {
if (!inherits(x, "RNAseqdata"))
stop("Use only with 'RNAseqdata' objects.")

def.par <- graphics::par(no.readonly = TRUE)
ymin.rc <- min(x$raw.counts)
ymax.rc <- max(x$raw.counts)
graphics::par(mfrow = c(1,2), xaxt = "n")
graphics::boxplot(x$raw.counts, xlab = "Samples", ylab = "Raw counts",
main = "Raw data",
ylim = c(ymin.rc, ymax.rc), range = range4boxplot, ...)
graphics::par(mfrow = c(1, 2), xaxt = "n")
graphics::boxplot(x$raw.counts, xlab = "Samples", ylab = "Raw counts",
main = "Raw data",
ylim = c(ymin.rc, ymax.rc), range = range4boxplot, ...)
ymin.log <- min(x$data)
ymax.log <- max(x$data)
graphics::boxplot(x$data, xlab = "Samples", ylab = "Signal",
main = "Normalized and transformed data",
ylim = c(ymin.log, ymax.log), range = range4boxplot, ...)
graphics::par(def.par)
graphics::boxplot(x$data, xlab = "Samples", ylab = "Signal",
main = "Normalized and transformed data",
ylim = c(ymin.log, ymax.log), range = range4boxplot, ...)
graphics::par(def.par)
}
Loading

0 comments on commit d604940

Please sign in to comment.