Skip to content

Commit a082e8c

Browse files
committed
Adapt to wide format of results table for results on gene subsets
1 parent 59ddb1e commit a082e8c

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

R/AllGenerics.R

+53-4
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,8 @@ setMethod("results", "FraserDataSet", function(object,
992992
aggregate=aggregate, collapse=collapse, geneColumn=geneColumn,
993993
subsetName=NULL, additionalColumns=additionalColumns,
994994
BPPARAM=BPPARAM)
995+
# to restore previous column order
996+
prevColOrder <- colnames(as.data.table(res))
995997
}
996998

997999
# add results for FDR_subsets if requested
@@ -1010,16 +1012,63 @@ setMethod("results", "FraserDataSet", function(object,
10101012
res <- unlist(GRangesList(unlist(list(res, resls_subsets))))
10111013
} else{
10121014
res <- unlist(GRangesList(unlist(resls_subsets)))
1015+
# restore previous column order
1016+
prevColOrder <- colnames(as.data.table(resls_subsets[[1]]))
10131017
}
1018+
}
1019+
1020+
# sort it if existing
1021+
if(length(res) > 0){
1022+
1023+
# dcast to have only one row per gene-sample combination
1024+
res <- as.data.table(res)
1025+
if(isTRUE(aggregate)){
1026+
dcastCols <- c("pValueGene", "padjustGene")
1027+
} else{
1028+
dcastCols <- "padjust"
1029+
}
1030+
res <- dcast(res, ... ~ FDR_set, value.var=dcastCols)
10141031

1015-
# sort it if existing
1016-
if(length(res) > 0){
1017-
res <- res[order(res$pValue)]
1032+
# rename column back to padjust for tw results after dcast
1033+
if(any(grepl("transcriptome-wide", colnames(res)))){
1034+
if(isTRUE(aggregate)){
1035+
setnames(res, "pValueGene_transcriptome-wide", "pValueGene")
1036+
setnames(res, "padjustGene_transcriptome-wide", "padjustGene")
1037+
} else{
1038+
setnames(res, "transcriptome-wide", "padjust")
1039+
}
1040+
1041+
} else{
1042+
res[, padjust := NA]
10181043
if(isTRUE(aggregate)){
1019-
res <- res[!is.na(res$pValueGene)]
1044+
res[, pValueGene := NA]
1045+
res[, padjustGene := NA]
10201046
}
10211047
}
1048+
1049+
# restore previous column order
1050+
setcolorder(res, prevColOrder[prevColOrder != "FDR_set"])
1051+
1052+
# rename padjust columns for other gene sets to padjust_setname
1053+
for(setName in FDRsets){
1054+
setnames(res, setName, paste0("padjust_", setName),
1055+
skip_absent=TRUE)
1056+
}
1057+
1058+
# revert back to GRanges
1059+
res <- makeGRangesFromDataFrame(res, keep.extra.columns=TRUE)
1060+
1061+
# order rows based on transcriptome-wide FDR
1062+
res <- res[order(res$pValue)]
1063+
if(isTRUE(aggregate) & isTRUE(returnTranscriptomewideResults)){
1064+
res <- res[!is.na(res$pValueGene)]
1065+
}
1066+
1067+
1068+
} else{
1069+
res$FDR_set <- NULL
10221070
}
1071+
10231072
return(res)
10241073
})
10251074

tests/testthat/test_plotSampleResults.R

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ test_that("Results function", {
3535
expect_equal(res_gene_signif[type == "psi3", .N], 2)
3636
expect_equal(res_gene_signif[type == "theta", .N], 3)
3737

38+
# results on subset of genes during FDR
39+
geneList <- list('sample1'=c("TIMMDC1"), 'sample2'=c("MCOLN1"))
40+
fds <- calculatePadjValues(fds, type="jaccard",
41+
subsets=list("exampleSubset"=geneList))
42+
expect_equal(length(results(fds, all=TRUE, psiType="jaccard")),
43+
prod(dim(fds)))
44+
expect_error(results(fds, all=TRUE, psiType="psi5"))
45+
3846
})
3947

4048
test_that("Main plotting function", {

0 commit comments

Comments
 (0)