plotSignatures_ads <- function(
        ads,
        contrast,
        dataName,
        effects_names = c(
            "Total mRNA Log2FC",
            "Polysome-associated mRNA Log2FC",
            "Buffering Log2FC",
            "Translation Log2FC"
        ),
        signatureList,
        generalName,
        signature_colours,
        xlim = c(-2, 2),
        scatterXY = NULL,
        tableCex = 1,
        pdfName = NULL
) {
    check_ads(ads)
    
    if (!check_number(contrast) ||
            !contrast %in% seq(1, ncol(ads@contrasts), 1)) {
        stop(
            "The input for 'contrast' should be a number corresponding ",
            "to the number of the anota2seq contrast for the selected ",
            "comparison. Please see the anota2seq vignette for ",
            "additional details on contrasts."
        )
    }
    
    if (is.null(dataName)) {
        stop(
            "Please provide an input for 'dataName' that will be ",
            "assigned to the data in the anota2seq object."
        )
    }
    
    if (length(effects_names) != 4) {
        stop(
            "There must be 4 effect names provided. By default, these ",
            "reflect the data in the anota2seq object (Total mRNA ",
            "Log2FC, Polysome-associated mRNA Log2FC, Translation ",
            "Log2FC, and Buffering Log2FC)."
        )
    }
    
    check_geneList(signatureList)
    signNames <- names(signatureList)
    
    if (is.null(generalName)) {
        stop(
            "Please provide an input for 'generalName' that will be ",
            "assigned to the gene signatures plotted."
        )
    }
    
    if (!is.character(signature_colours) ||
            !length(signature_colours) == length(signatureList)) {
        stop(
            "The input for the 'signature_colours' parameter should be ",
            "a character vector of the same length as number of ",
            "signatures in 'signatureList'. These colours will be used ",
            "for plotting."
        )
    }
    
    if (!check_number(tableCex)) {
        stop(
            "Please provide a numeric value for 'tableCex' to scale ",
            "the size of table containing the statistical results."
        )
    }
    
    if (!is.null(scatterXY) && !check_number(scatterXY)) {
        stop(
            "Please provide a numeric value for 'scatterXY' to indicate ",
            "range of the x and y axes of the fold change scatter plot."
        )
    }
    
    regData <- data.frame(geneSymb = rownames(ads@dataP))
    
    regData$totalApvEff <-
        ads@totalmRNA@apvStatsRvm[[contrast]][, "apvEff"]
    regData$polyApvEff <-
        ads@translatedmRNA@apvStatsRvm[[contrast]][, "apvEff"]
    regData$buffApvEff <-
        ads@buffering@apvStatsRvm[[contrast]][, "apvEff"]
    regData$translationApvEff <-
        ads@translation@apvStatsRvm[[contrast]][, "apvEff"]
    
    if (is.null(scatterXY)) {
        scatterXY <- roundNice(
            max(abs(c(
                range(regData$totalApvEff),
                range(regData$polyApvEff)
            ))),
            direction = "up"
        )
    }
    
    pdf(
        ifelse(
            is.null(pdfName),
            paste(
                "data_",
                dataName,
                "_signature_",
                generalName,
                ".pdf",
                sep = ""
            ),
            paste(
                pdfName,
                paste(
                    "data_",
                    dataName,
                    "_signature_",
                    generalName,
                    ".pdf",
                    sep = ""
                ),
                sep = "_"
            )
        ),
        width = 18,
        height = 4,
        useDingbats = FALSE
    )
    
    par(
        mfrow = c(1, 5),
        mar = c(5, 5, 6, 4),
        bty = "l",
        font = 2,
        font.axis = 2,
        font.lab = 2,
        cex.axis = 1.25,
        cex.main = 1.9,
        cex.lab = 2
    )
    
    plot(
        regData$totalApvEff,
        regData$polyApvEff,
        pch = 16,
        cex = 1.9,
        col = "grey75",
        ylab = effects_names[2],
        xlab = effects_names[1],
        main = paste(
            paste("Data: ", dataName, sep = ""),
            "\n",
            paste("Signature: ", generalName, sep = ""),
            sep = ""
        ),
        xlim = c(-scatterXY, scatterXY),
        ylim = c(-scatterXY, scatterXY)
    )
    
    abline(v = 0)
    abline(h = 0)
    
    if (any(duplicated(unlist(signatureList)))) {
        message(
            "There are some genes that overlap between signatures. ",
            "Separate backgrounds will be used for each gene signature."
        )
        
        for (i in seq_along(signatureList)) {
            regData[, 5 + i] <- "bkg"
            regData[, 5 + i][
                regData$geneSymb %in% signatureList[[i]]
            ] <- names(signatureList)[i]
            
            points(
                regData$totalApvEff[
                    regData[, 5 + i] == names(signatureList)[i]
                ],
                regData$polyApvEff[
                    regData[, 5 + i] == names(signatureList)[i]
                ],
                col = signature_colours[i],
                pch = 16,
                cex = 1.7
            )
        }
    } else {
        regData$signature <- "bkg"
        
        for (i in seq_along(signatureList)) {
            regData$signature[
                regData$geneSymb %in% signatureList[[i]]
            ] <- names(signatureList)[i]
            
            points(
                regData$totalApvEff[
                    regData$signature == names(signatureList)[i]
                ],
                regData$polyApvEff[
                    regData$signature == names(signatureList)[i]
                ],
                col = signature_colours[i],
                pch = 16,
                cex = 1.7
            )
        }
    }
    
    for (eff in 5:2) {
        tableOut <- matrix(NA, nrow = length(signNames), ncol = 5)
        colnames(tableOut) <- c(
            "signature",
            "Wilcox_pval",
            "q25",
            "q50",
            "q75"
        )
        tableOut[, 1] <- signNames
        
        if (any(duplicated(unlist(signatureList)))) {
            tmpBgOut <- list()
            
            for (i in seq_along(signatureList)) {
                tmpBg <- sort(as.numeric(
                    regData[regData[, 5 + i] == "bkg", ][, eff]
                ))
                ecdfBg <- seq_along(tmpBg) / length(tmpBg)
                bg_025 <- tmpBg[which(ecdfBg >= 0.25)[1]]
                bg_05 <- tmpBg[which(ecdfBg >= 0.5)[1]]
                bg_075 <- tmpBg[which(ecdfBg >= 0.75)[1]]
                
                tmpBgOut[[i]] <- tmpBg
                
                tableOut[i, 2] <- format(
                    as.numeric(
                        wilcox.test(
                            as.numeric(
                                regData[
                                    regData[, 5 + i] == signNames[i],
                                ][, eff]
                            ),
                            as.numeric(
                                regData[regData[, 5 + i] == "bkg", ][, eff]
                            ),
                            alternative = "two.sided"
                        )[3]
                    ),
                    scientific = TRUE,
                    digits = 2
                )
                
                tmpSign <- sort(as.numeric(
                    regData[regData[, 5 + i] == signNames[i], ][, eff]
                ))
                ecdfSign <- seq_along(tmpSign) / length(tmpSign)
                
                tableOut[i, 3] <- format(
                    tmpSign[which(ecdfSign >= 0.25)[1]] - bg_025,
                    digits = 2
                )
                tableOut[i, 4] <- format(
                    tmpSign[which(ecdfSign >= 0.5)[1]] - bg_05,
                    digits = 2
                )
                tableOut[i, 5] <- format(
                    tmpSign[which(ecdfSign >= 0.75)[1]] - bg_075,
                    digits = 2
                )
            }
            
            if (!is.null(xlim)) {
                xmin <- xlim[1]
                xmax <- xlim[2]
            } else {
                xmin <- ifelse(
                    as.numeric(quantile(as.numeric(unlist(tmpBgOut)), 0.01)) <
                        0,
                    -roundNice(
                        abs(as.numeric(
                            quantile(as.numeric(unlist(tmpBgOut)), 0.01)
                        )),
                        direction = "up"
                    ),
                    roundNice(
                        as.numeric(
                            quantile(as.numeric(unlist(tmpBgOut)), 0.01)
                        ),
                        direction = "up"
                    )
                )
                
                xmax <- ifelse(
                    as.numeric(quantile(as.numeric(unlist(tmpBgOut)), 0.99)) <
                        0,
                    -roundNice(
                        abs(as.numeric(
                            quantile(as.numeric(unlist(tmpBgOut)), 0.01)
                        )),
                        direction = "up"
                    ),
                    roundNice(
                        as.numeric(
                            quantile(as.numeric(unlist(tmpBgOut)), 0.99)
                        ),
                        direction = "up"
                    )
                )
            }
            
            plot(
                ecdf(as.numeric(regData[regData[, 5 + i] == "bkg", ][, eff])),
                col = "grey55",
                main = "",
                xlab = effects_names[eff - 1],
                verticals = TRUE,
                do.p = FALSE,
                lwd = 3,
                xlim = c(xmin, xmax)
            )
            
            legend(
                xmin,
                0.95,
                fill = "grey55",
                border = "grey55",
                "Background",
                bty = "n",
                cex = 1.3
            )
            
            for (i in seq_along(signatureList)) {
                lines(
                    ecdf(as.numeric(
                        regData[regData[, 5 + i] == signNames[i], ][, eff]
                    )),
                    col = signature_colours[i],
                    main = "",
                    xlab = "",
                    verticals = TRUE,
                    do.p = FALSE,
                    lwd = 3
                )
            }
            
            plotrix::addtable2plot(
                xmin - abs(xmin * 0.1),
                1.05,
                tableOut,
                bty = "n",
                display.rownames = FALSE,
                hlines = FALSE,
                vlines = TRUE,
                title = "",
                cex = tableCex,
                bg = signature_colours,
                xpad = 0.2,
                ypad = 1.4
            )
        } else {
            tmpBg <- sort(as.numeric(
                regData[regData$signature == "bkg", ][, eff]
            ))
            ecdfBg <- seq_along(tmpBg) / length(tmpBg)
            bg_025 <- tmpBg[which(ecdfBg >= 0.25)[1]]
            bg_05 <- tmpBg[which(ecdfBg >= 0.5)[1]]
            bg_075 <- tmpBg[which(ecdfBg >= 0.75)[1]]
            
            for (i in seq_along(signatureList)) {
                tableOut[i, 2] <- format(
                    as.numeric(
                        wilcox.test(
                            as.numeric(
                                regData[
                                    regData$signature == signNames[i],
                                ][, eff]
                            ),
                            as.numeric(
                                regData[regData$signature == "bkg", ][, eff]
                            ),
                            alternative = "two.sided"
                        )[3]
                    ),
                    scientific = TRUE,
                    digits = 2
                )
                
                tmpSign <- sort(as.numeric(
                    regData[regData$signature == signNames[i], ][, eff]
                ))
                ecdfSign <- seq_along(tmpSign) / length(tmpSign)
                
                tableOut[i, 3] <- format(
                    tmpSign[which(ecdfSign >= 0.25)[1]] - bg_025,
                    digits = 2
                )
                tableOut[i, 4] <- format(
                    tmpSign[which(ecdfSign >= 0.5)[1]] - bg_05,
                    digits = 2
                )
                tableOut[i, 5] <- format(
                    tmpSign[which(ecdfSign >= 0.75)[1]] - bg_075,
                    digits = 2
                )
            }
            
            if (!is.null(xlim)) {
                xmin <- xlim[1]
                xmax <- xlim[2]
            } else {
                xmin <- ifelse(
                    as.numeric(quantile(as.numeric(tmpBg), 0.01)) < 0,
                    -roundNice(
                        abs(as.numeric(quantile(as.numeric(tmpBg), 0.01))),
                        direction = "up"
                    ),
                    roundNice(
                        as.numeric(quantile(as.numeric(tmpBg), 0.01)),
                        direction = "up"
                    )
                )
                
                xmax <- ifelse(
                    as.numeric(quantile(as.numeric(tmpBg), 0.99)) < 0,
                    -roundNice(
                        abs(as.numeric(quantile(as.numeric(tmpBg), 0.01))),
                        direction = "up"
                    ),
                    roundNice(
                        as.numeric(quantile(as.numeric(tmpBg), 0.99)),
                        direction = "up"
                    )
                )
            }
            
            plot(
                ecdf(as.numeric(regData[regData$signature == "bkg", ][, eff])),
                col = "grey55",
                main = "",
                xlab = effects_names[eff - 1],
                verticals = TRUE,
                do.p = FALSE,
                lwd = 3,
                xlim = c(xmin, xmax)
            )
            
            legend(
                xmin,
                0.95,
                fill = "grey55",
                border = "grey55",
                "Background",
                bty = "n",
                cex = 1.3
            )
            
            for (i in seq_along(signatureList)) {
                lines(
                    ecdf(as.numeric(
                        regData[regData$signature == signNames[i], ][, eff]
                    )),
                    col = signature_colours[i],
                    main = "",
                    xlab = "",
                    verticals = TRUE,
                    do.p = FALSE,
                    lwd = 3
                )
            }
            
            plotrix::addtable2plot(
                xmin - abs(xmin * 0.1),
                1.05,
                tableOut,
                bty = "n",
                display.rownames = FALSE,
                hlines = FALSE,
                vlines = TRUE,
                title = "",
                cex = tableCex,
                bg = signature_colours,
                xpad = 0.2,
                ypad = 1.4
            )
        }
    }
    
    dev.off()
}
