Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt result table format from long to wide for FDR subset results #70

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,8 @@ setMethod("results", "FraserDataSet", function(object,
aggregate=aggregate, collapse=collapse, geneColumn=geneColumn,
subsetName=NULL, additionalColumns=additionalColumns,
BPPARAM=BPPARAM)
# to restore previous column order
prevColOrder <- colnames(as.data.table(res))
}

# add results for FDR_subsets if requested
Expand All @@ -1010,16 +1012,63 @@ setMethod("results", "FraserDataSet", function(object,
res <- unlist(GRangesList(unlist(list(res, resls_subsets))))
} else{
res <- unlist(GRangesList(unlist(resls_subsets)))
# restore previous column order
prevColOrder <- colnames(as.data.table(resls_subsets[[1]]))
}
}

# sort it if existing
if(length(res) > 0){

# dcast to have only one row per gene-sample combination
res <- as.data.table(res)
if(isTRUE(aggregate)){
dcastCols <- c("pValueGene", "padjustGene")
} else{
dcastCols <- "padjust"
}
res <- dcast(res, ... ~ FDR_set, value.var=dcastCols)

# sort it if existing
if(length(res) > 0){
res <- res[order(res$pValue)]
# rename column back to padjust for tw results after dcast
if(any(grepl("transcriptome-wide", colnames(res)))){
if(isTRUE(aggregate)){
setnames(res, "pValueGene_transcriptome-wide", "pValueGene")
setnames(res, "padjustGene_transcriptome-wide", "padjustGene")
} else{
setnames(res, "transcriptome-wide", "padjust")
}

} else{
res[, padjust := NA]
if(isTRUE(aggregate)){
res <- res[!is.na(res$pValueGene)]
res[, pValueGene := NA]
res[, padjustGene := NA]
}
}

# restore previous column order
setcolorder(res, prevColOrder[prevColOrder != "FDR_set"])

# rename padjust columns for other gene sets to padjust_setname
for(setName in FDRsets){
setnames(res, setName, paste0("padjust_", setName),
skip_absent=TRUE)
}

# revert back to GRanges
res <- makeGRangesFromDataFrame(res, keep.extra.columns=TRUE)

# order rows based on transcriptome-wide FDR
res <- res[order(res$pValue)]
if(isTRUE(aggregate) & isTRUE(returnTranscriptomewideResults)){
res <- res[!is.na(res$pValueGene)]
}


} else{
res$FDR_set <- NULL
}

return(res)
})

Expand Down
13 changes: 5 additions & 8 deletions R/plotMethods.R
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,13 @@
#'
#' # other ways to call these plotting functions
#' plotExpression(fds, idx=10, sampleID="sample1", type="jaccard")
#' plotExpression(fds, result=res[FDR_set == "testSet",][1],
#' subsetName="testSet")
#' plotExpression(fds, result=res[1], subsetName="testSet")
#' plotQQ(fds, idx=10, sampleID="sample1", type="jaccard")
#' plotQQ(fds, result=res[FDR_set == "testSet",][1], subsetName="testSet")
#' plotQQ(fds, result=res[1], subsetName="testSet")
#' plotExpectedVsObservedPsi(fds, idx=10, sampleID="sample1", type="jaccard")
#' plotExpectedVsObservedPsi(fds, result=res[FDR_set == "testSet",][1],
#' subsetName="testSet")
#' plotExpectedVsObservedPsi(fds, result=res[1], subsetName="testSet")
#' plotSpliceMetricRank(fds, idx=10, sampleID="sample1", type="jaccard")
#' plotSpliceMetricRank(fds, result=res[FDR_set == "testSet",][1],
#' subsetName="testSet")
#' plotSpliceMetricRank(fds, result=res[1], subsetName="testSet")
#'
#' # create manhattan plot of pvalues by genomic position
#' if(require(ggbio)){
Expand Down Expand Up @@ -819,7 +816,7 @@ plotExpectedVsObservedPsi <- function(fds, type=fitMetrics(fds),

if(is.null(colGroup)){
g <- g + scale_colour_manual(
values=c("FALSE"="gray70", "TRUE"="firebrick"))
values=c("gray70", "firebrick"))
}

plotBasePlot(g, basePlot)
Expand Down
11 changes: 4 additions & 7 deletions man/plotFunctions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/test_plotSampleResults.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ test_that("Results function", {
expect_equal(res_gene_signif[type == "psi3", .N], 2)
expect_equal(res_gene_signif[type == "theta", .N], 3)

# results on subset of genes during FDR
geneList <- list('sample1'=c("TIMMDC1"), 'sample2'=c("MCOLN1"))
fds <- calculatePadjValues(fds, type="jaccard",
subsets=list("exampleSubset"=geneList))
expect_equal(length(results(fds, all=TRUE, psiType="jaccard")),
prod(dim(fds)))
expect_error(results(fds, all=TRUE, psiType="psi5"))

})

test_that("Main plotting function", {
Expand Down
Loading