|
32 | 32 | #' |
33 | 33 | #' @examples |
34 | 34 | #' Block <- c(1, 1, 2, 3, 3, 4, 1, 1, 2, 3, 3, 4) |
35 | | -#' Treat <- factor(rep(c("ctrl", "drug"), each = 6)) |
| 35 | +#' Treat <- factor(rep(c('ctrl', 'drug'), each = 6)) |
36 | 36 | #' Time <- rep(rep(seq_len(2), each = 3), 2) |
37 | 37 | #' nbarcodes <- 50 |
38 | 38 | #' nsamples <- 12 |
39 | 39 | #' count <- abs(matrix(rnorm(nbarcodes * nsamples), nbarcodes, nsamples)) |
40 | | -#' rownames(count) <- paste0("Barcode", seq_len(nbarcodes)) |
| 40 | +#' rownames(count) <- paste0('Barcode', seq_len(nbarcodes)) |
41 | 41 | #' barbieQ <- createBarbieQ(count, data.frame(Treat = Treat, Time = Time)) |
42 | | -#' testBB <- testBarcodeBias(barbieQ, sampleGroups = "Treat") |
43 | | -#' plotBarcodeBiasScatterPlot(barbieQ = testBB, elementName = "diffProp_Treat") |
44 | | -plotBarcodeBiasScatterPlot <- function( |
45 | | - barbieQ, elementName = NULL, reorderRank = FALSE, |
| 42 | +#' testBB <- testBarcodeBias(barbieQ, sampleGroups = 'Treat') |
| 43 | +#' plotBarcodeBiasScatterPlot(barbieQ = testBB, elementName = 'diffProp_Treat') |
| 44 | +plotBarcodeBiasScatterPlot <- function(barbieQ, elementName = NULL, reorderRank = FALSE, |
46 | 45 | pValuesAdjusted = TRUE, xAxis = "avgRank") { |
47 | | - ## extract test resilts and information |
48 | | - if (is.null(elementName)) { |
49 | | - elementName <- names(barbieQ$testBarcodes)[length(names(barbieQ$testBarcodes))] |
50 | | - } |
51 | | - if (is.null(barbieQ$testBarcodes[[elementName]])) { |
52 | | - stop("test results not specified or not found") |
53 | | - } |
54 | | - testInfo <- barbieQ$testBarcodes[[elementName]] |
55 | | - statMat <- testInfo$results |
56 | | - methodLs <- testInfo$methods |
57 | | - ## define a custom color/shape palette for test results |
58 | | - customShape <- stats::setNames(c(21, 24, 23), c(methodLs$contrastLevels, "n.s.")) |
59 | | - customColor <- barbieQ$factorColors[[elementName]] |
60 | | - ## y axis will be p.value x axis will be optional: total occurrence, average |
61 | | - ## rank, average log CPM reorder Barcode rank within samples. |
62 | | - if (reorderRank) { |
63 | | - rank <- apply(barbieQ$rank, 2, rank) |
64 | | - } else { |
65 | | - rank <- barbieQ$rank |
66 | | - } |
67 | | - ## choose p.values |
68 | | - if (pValuesAdjusted) { |
69 | | - p.value <- statMat$adj.p.value |
70 | | - } else { |
71 | | - p.value <- statMat$p.value |
72 | | - } |
73 | | - ## check xAxis |
74 | | - xOptions <- c("avgRank", "totalOcc", "avgLogCPM", "avgProportion") |
75 | | - xAxis <- match.arg(xAxis, xOptions) |
76 | | - xTitle <- stats::setNames( |
77 | | - c( |
78 | | - "Average rank of Barcode across samples", "Number of samples in which Barcode occurs", |
79 | | - "Average Barcode Log2 CPM+1 across samples", "Average Barcode proportion across samples" |
80 | | - ), |
81 | | - xOptions |
82 | | - ) |
83 | | - ## data.frame for ggplot |
84 | | - mydata <- data.frame( |
85 | | - direction = statMat$direction, minusLogP = -log10(p.value), totalOcc = rowSums(barbieQ$occurrence), |
86 | | - avgRank = rowMeans(barbieQ$rank), BarcodeID = rownames(barbieQ$assay), avgLogCPM = log2(rowMeans((barbieQ$CPM + |
87 | | - 1))), avgProportion = rowMeans(barbieQ$proportion) |
88 | | - ) |
89 | | - ## visualize by ggplot |
90 | | - p <- ggplot(mydata, aes(x = mydata[, xAxis], y = minusLogP, text = BarcodeID)) + |
91 | | - geom_point(aes( |
92 | | - color = direction, |
93 | | - shape = direction, fill = direction |
94 | | - ), size = 4, stroke = 1) + |
95 | | - theme_classic() + |
96 | | - theme(aspect.ratio = 1) + |
97 | | - labs( |
98 | | - title = paste0(methodLs$aim, " : ", methodLs$contrastVector), |
99 | | - y = "-log10(p.value)", x = xTitle[xAxis] |
100 | | - ) + |
101 | | - geom_hline( |
102 | | - yintercept = -log10(0.05), |
103 | | - linetype = "dashed", color = "grey" |
104 | | - ) + |
105 | | - scale_color_manual(values = customColor) + |
106 | | - scale_shape_manual(values = customShape) + |
107 | | - scale_fill_manual(values = alpha( |
108 | | - customColor, |
109 | | - 0.2 |
110 | | - )) |
111 | | - ## reverse x scale if displaying Barcode rank |
112 | | - if (xAxis == "avgRank") { |
113 | | - p <- p + annotate("text", |
114 | | - x = min(mydata[, xAxis]) * 1.1, y = -log10(0.05), label = "p.value = 0.05", |
115 | | - vjust = 1.5, hjust = 1 |
116 | | - ) + scale_x_reverse() |
117 | | - } else { |
118 | | - p <- p + annotate("text", |
119 | | - x = max(mydata[, xAxis]) * 0.9, y = -log10(0.05), label = "p.value = 0.05", |
120 | | - vjust = 1.5, hjust = 1 |
121 | | - ) |
122 | | - } |
| 46 | + ## extract test resilts and information |
| 47 | + if (is.null(elementName)) { |
| 48 | + elementName <- names(barbieQ$testBarcodes)[length(names(barbieQ$testBarcodes))] |
| 49 | + } |
| 50 | + if (is.null(barbieQ$testBarcodes[[elementName]])) { |
| 51 | + stop("test results not specified or not found") |
| 52 | + } |
| 53 | + testInfo <- barbieQ$testBarcodes[[elementName]] |
| 54 | + statMat <- testInfo$results |
| 55 | + methodLs <- testInfo$methods |
| 56 | + ## define a custom color/shape palette for test results |
| 57 | + customShape <- stats::setNames(c(21, 24, 23), c(methodLs$contrastLevels, "n.s.")) |
| 58 | + customColor <- barbieQ$factorColors[[elementName]] |
| 59 | + ## y axis will be p.value x axis will be optional: total occurrence, average rank, |
| 60 | + ## average log CPM reorder Barcode rank within samples. |
| 61 | + if (reorderRank) { |
| 62 | + rank <- apply(barbieQ$rank, 2, rank) |
| 63 | + } else { |
| 64 | + rank <- barbieQ$rank |
| 65 | + } |
| 66 | + ## choose p.values |
| 67 | + if (pValuesAdjusted) { |
| 68 | + p.value <- statMat$adj.p.value |
| 69 | + } else { |
| 70 | + p.value <- statMat$p.value |
| 71 | + } |
| 72 | + ## check xAxis |
| 73 | + xOptions <- c("avgRank", "totalOcc", "avgLogCPM", "avgProportion") |
| 74 | + xAxis <- match.arg(xAxis, xOptions) |
| 75 | + xTitle <- stats::setNames(c("Average rank of Barcode across samples", "Number of samples in which Barcode occurs", |
| 76 | + "Average Barcode Log2 CPM+1 across samples", "Average Barcode proportion across samples"), |
| 77 | + xOptions) |
| 78 | + ## data.frame for ggplot |
| 79 | + mydata <- data.frame(direction = statMat$direction, minusLogP = -log10(p.value), totalOcc = rowSums(barbieQ$occurrence), |
| 80 | + avgRank = rowMeans(barbieQ$rank), BarcodeID = rownames(barbieQ$assay), avgLogCPM = log2(rowMeans((barbieQ$CPM + |
| 81 | + 1))), avgProportion = rowMeans(barbieQ$proportion)) |
| 82 | + ## visualize by ggplot |
| 83 | + p <- ggplot(mydata, aes(x = mydata[, xAxis], y = minusLogP, text = BarcodeID)) + geom_point(aes(color = direction, |
| 84 | + shape = direction, fill = direction), size = 4, stroke = 1) + theme_classic() + theme(aspect.ratio = 1) + |
| 85 | + labs(title = paste0(methodLs$aim, " : ", methodLs$contrastVector), y = "-log10(p.value)", |
| 86 | + x = xTitle[xAxis]) + geom_hline(yintercept = -log10(0.05), linetype = "dashed", |
| 87 | + color = "grey") + scale_color_manual(values = customColor) + scale_shape_manual(values = customShape) + |
| 88 | + scale_fill_manual(values = alpha(customColor, 0.2)) |
| 89 | + ## reverse x scale if displaying Barcode rank |
| 90 | + if (xAxis == "avgRank") { |
| 91 | + p <- p + annotate("text", x = min(mydata[, xAxis]) * 1.1, y = -log10(0.05), label = "p.value = 0.05", |
| 92 | + vjust = 1.5, hjust = 1) + scale_x_reverse() |
| 93 | + } else { |
| 94 | + p <- p + annotate("text", x = max(mydata[, xAxis]) * 0.9, y = -log10(0.05), label = "p.value = 0.05", |
| 95 | + vjust = 1.5, hjust = 1) |
| 96 | + } |
123 | 97 |
|
124 | | - return(p) |
| 98 | + return(p) |
125 | 99 | } |
126 | 100 |
|
127 | 101 |
|
@@ -155,47 +129,42 @@ plotBarcodeBiasScatterPlot <- function( |
155 | 129 | #' |
156 | 130 | #' @examples |
157 | 131 | #' Block <- c(1, 1, 2, 3, 3, 4, 1, 1, 2, 3, 3, 4) |
158 | | -#' Treat <- factor(rep(c("ctrl", "drug"), each = 6)) |
| 132 | +#' Treat <- factor(rep(c('ctrl', 'drug'), each = 6)) |
159 | 133 | #' Time <- rep(rep(seq_len(2), each = 3), 2) |
160 | 134 | #' nbarcodes <- 50 |
161 | 135 | #' nsamples <- 12 |
162 | 136 | #' count <- abs(matrix(rnorm(nbarcodes * nsamples), nbarcodes, nsamples)) |
163 | | -#' rownames(count) <- paste0("Barcode", seq_len(nbarcodes)) |
| 137 | +#' rownames(count) <- paste0('Barcode', seq_len(nbarcodes)) |
164 | 138 | #' barbieQ <- createBarbieQ(count, data.frame(Treat = Treat, Time = Time)) |
165 | | -#' testBB <- testBarcodeBias(barbieQ, sampleGroups = "Treat") |
166 | | -#' plotBarcodeBiasHeatmap(barbieQ = testBB, elementName = "diffProp_Treat") |
| 139 | +#' testBB <- testBarcodeBias(barbieQ, sampleGroups = 'Treat') |
| 140 | +#' plotBarcodeBiasHeatmap(barbieQ = testBB, elementName = 'diffProp_Treat') |
167 | 141 | plotBarcodeBiasHeatmap <- function(barbieQ, value = "CPM", elementName = NULL, sampleAnnotation = NULL) { |
168 | | - ## extract test resilts and information |
169 | | - if (is.null(elementName)) { |
170 | | - elementName <- names(barbieQ$testBarcodes)[length(names(barbieQ$testBarcodes))] |
171 | | - } |
172 | | - if (is.null(barbieQ$testBarcodes[[elementName]])) { |
173 | | - stop("test results not specified or not found") |
174 | | - } |
175 | | - testInfo <- barbieQ$testBarcodes[[elementName]] |
176 | | - statMat <- testInfo$results |
177 | | - methodLs <- testInfo$methods |
178 | | - modelTargets <- testInfo$targets |
179 | | - ## define a custom color/shape palette for test results |
180 | | - customShape <- stats::setNames(c(21, 24, 23), c(methodLs$contrastLevels, "n.s.")) |
181 | | - customColor <- barbieQ$factorColors[[elementName]] |
| 142 | + ## extract test resilts and information |
| 143 | + if (is.null(elementName)) { |
| 144 | + elementName <- names(barbieQ$testBarcodes)[length(names(barbieQ$testBarcodes))] |
| 145 | + } |
| 146 | + if (is.null(barbieQ$testBarcodes[[elementName]])) { |
| 147 | + stop("test results not specified or not found") |
| 148 | + } |
| 149 | + testInfo <- barbieQ$testBarcodes[[elementName]] |
| 150 | + statMat <- testInfo$results |
| 151 | + methodLs <- testInfo$methods |
| 152 | + modelTargets <- testInfo$targets |
| 153 | + ## define a custom color/shape palette for test results |
| 154 | + customShape <- stats::setNames(c(21, 24, 23), c(methodLs$contrastLevels, "n.s.")) |
| 155 | + customColor <- barbieQ$factorColors[[elementName]] |
182 | 156 |
|
183 | | - ## customize row annotation |
184 | | - barcodeAnnotation <- rowAnnotation( |
185 | | - Direction = statMat$direction, annotation_name_side = "top", |
186 | | - annotation_name_gp = grid::gpar(fontsize = 10), col = list(Direction = customColor), |
187 | | - show_legend = TRUE, show_annotation_name = TRUE |
188 | | - ) |
| 157 | + ## customize row annotation |
| 158 | + barcodeAnnotation <- rowAnnotation(Direction = statMat$direction, annotation_name_side = "top", |
| 159 | + annotation_name_gp = grid::gpar(fontsize = 10), col = list(Direction = customColor), |
| 160 | + show_legend = TRUE, show_annotation_name = TRUE) |
189 | 161 |
|
190 | | - ## adjust the order of slices based on contrast levels in the test |
191 | | - restLevels <- dplyr::setdiff(levels(modelTargets[, methodLs$contrastVector]), methodLs$contrastLevels) |
192 | | - levels(modelTargets[, methodLs$contrastVector]) <- c(methodLs$contrastLevels, restLevels) |
| 162 | + ## adjust the order of slices based on contrast levels in the test |
| 163 | + restLevels <- dplyr::setdiff(levels(modelTargets[, methodLs$contrastVector]), methodLs$contrastLevels) |
| 164 | + levels(modelTargets[, methodLs$contrastVector]) <- c(methodLs$contrastLevels, restLevels) |
193 | 165 |
|
194 | | - hp <- plotBarcodeHeatmap( |
195 | | - barbieQ = barbieQ, value = value, splitSamples = TRUE, targets = modelTargets, |
196 | | - sampleGroups = methodLs$contrastVector, barcodeAnnotation = barcodeAnnotation, |
197 | | - sampleAnnotation = sampleAnnotation |
198 | | - ) |
| 166 | + hp <- plotBarcodeHeatmap(barbieQ = barbieQ, value = value, splitSamples = TRUE, targets = modelTargets, |
| 167 | + sampleGroups = methodLs$contrastVector, barcodeAnnotation = barcodeAnnotation, sampleAnnotation = sampleAnnotation) |
199 | 168 |
|
200 | | - return(hp) |
| 169 | + return(hp) |
201 | 170 | } |
0 commit comments