Skip to content

Commit 2c7dbbf

Browse files
author
Liyang Fei
committed
rename parameters
1 parent b9d7b4b commit 2c7dbbf

File tree

4 files changed

+54
-51
lines changed

4 files changed

+54
-51
lines changed

R/BarcodePairCorrelation.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#' @export
2929
#'
3030
#' @import ggplot2
31-
#' @importFrom magrittr %>%
31+
#' @importFrom magrittr %>%
3232
#' @import data.table
3333
#'
3434
#' @examples

R/createBarbieQ.R

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,32 @@
55
#' which is designed to process Barcode count data gained from
66
#' cell clonal tracking experiments.
77
#'
8-
#' @param object A numeric matrix of Barcode counts, with Barcodes in rows
8+
#' @param object A numeric `matrix` of Barcode counts, with Barcodes in rows
99
#' and samples in columns;
1010
#' Alternatively, you can pass an existing `barbieQ` object, from which
11-
#' barcode counts, sample conditions, and color palettes will be inherited.
12-
#' @param target A `matrix` or `data.frame` of sample conditions,
13-
#' with each factor in a separate column.
14-
#' If a `barbieQ` object is passed to `object`, the sample conditions
15-
#' in `barbieQ` will be updated by `target`.
11+
#' barcode counts, sample conditions, and the mapping colors will be inherited.
12+
#' @param target A `matrix` or `data.frame` where rows represent samples
13+
#' and columns represent factors, each containing the specific condition assigned to a sample.
1614
#' If not specified, all samples are assigned the same condition.
17-
#' @param factorColors A `list` of colour palettes corresponding to
18-
#' the factors in `target`.
19-
#' If a `barbieQ` object is passed to `object`, the color palettes
20-
#' in `barbieQ`will be updated by `factorColors`.
21-
#' If not specified, defaults to NULL.
15+
#' If a `barbieQ` object is passed to `object`, the sample conditions
16+
#' will be updated by specifying `target`.
17+
#' @param factorColors A `list` mapping factors in `target` to color palettes,
18+
#' where each element is a named `vector` assigning colors to specific sample conditions.
19+
#' If not specified, defaults to `NULL`.
20+
#' If a `barbieQ` object is passed to `object`, the mapping colors
21+
#' will be updated by specifying `factorColors`.
2222
#'
2323
#' @return A `barbieQ` object, including several components:
24-
#' * `assay`: a `data.frame` containing Barcode counts across samples.
25-
#' * `metadata`: a `data.frame` representing sample conditions,
26-
#' organized by various experimental factors.
27-
#' * `factorColors`: a `list` of color palettes corresponding to
28-
#' the sample conditions.
24+
#' * `assay`: a `data.frame` of Barcode counts, with Barcodes in rows and samples in columns.
25+
#' * `metadata`: a `data.frame` of sample conditions, with samples in rows and
26+
#' experimental factors in columns.
27+
#' * `factorColors`: A `list` mapping factors in `target` to color palettes,
28+
#' where each element is a named `vector` assigning colors to specific sample conditions.
2929
#' * Matrices of `proportion`, `CPM`, `occurrence`, and `rank` representing
30-
#' the proportion, Counts Per Million (CPM), presence or absence, and ranking
31-
#' of Barcodes in each sample, based on the Barcode counts in `assay`.
32-
#' * `isTop`: a list containing a vector `vec` and a matrix `mat`, tagging
33-
#' each Barcode as being part of the major contributors or not,
34-
#' by calling the function [tagTopBarcodes].
30+
#' Barcode proportion, Counts Per Million (CPM), presence/absence, and ranking
31+
#' in each sample, based on the Barcode counts in `assay`.
32+
#' * `isTop`: a `list` containing a `vector` (`vec`) and a `matrix` (`mat`),
33+
#' indicating whether each Barcode is a major contributor, determined by calling [tagTopBarcodes].
3534
#' * Additional processed information for further analysis.
3635
#'
3736
#' @export
@@ -113,30 +112,28 @@ createBarbieQ <- function(object, target = NULL, factorColors = NULL) {
113112
## extract metadata from `target`
114113
## if `target` not specified, inherit metadata from the passed `barbieQ` object
115114
if (is.null(target)) target <- barbieQ$metadata
116-
## if 'barbieQ$metadata' is NULL either, assume a homogeneous setting in the metadata.
115+
## if 'barbieQ$metadata' is NULL either, default a homogeneous setting in the metadata.
117116
if (is.null(target)) {
118117
target <- as.data.frame(matrix(1, ncol(barbieQ$assay), 1))
119-
message("no 'target' provided.
120-
now creating a pseudo uni-group with a homogeneous setting in 'barbieQ$metadata'.")
118+
warning("no valid 'target' provided. Assigning a homogeneous condition to all samples.")
121119
} else {
122-
## now target is provided by either @param target or barbieQ$metadata, check target format
123-
if (is.vector(target) || is.factor(target)) {
124-
target <- matrix(target, ncol = 1)
120+
## with valid `target` provided by either @param `target` or `object$metadata`, check the structure
121+
if ((!(is.list(target)) && is.vector(target)) || is.factor(target)) {
122+
target <- data.frame(target)
125123
} else if (!(inherits(target, "data.frame") || inherits(target, "matrix"))) {
126-
stop("'target' should be a vector, matrix or data.frame describing sample conditions.")
124+
stop("'target' must be a vector, matrix or data.frame describing sample conditions.")
127125
}
128126
}
129127

130-
## check if the sample sizes match between 'barbieQ$metadata' and 'barbieQ$assay'
128+
## check if sample size match between 'barbieQ$metadata' and 'barbieQ$assay'
131129
if (identical(ncol(barbieQ$assay), nrow(target))) {
132130
barbieQ$metadata <- as.data.frame(target)
133131
} else {
134-
## if sample sizes don't match, create a homogeneous setting in metadata.
132+
## if sample size not matching, set a homogeneous setting in metadata.
135133
barbieQ$metadata <- as.data.frame(matrix("1", ncol(barbieQ$assay), 1))
136-
warning("sample size of 'target' (or 'barbieQ$metadata') doesn't match sample size of Barcode count extracted in 'object'!
137-
now creating a pseudo uni-group with a homogeneous setting in 'barbieQ$metadata'.")
134+
warning("sample size mismatch between conditions and barcode counts! Assigning a homogeneous condition to all samples.")
138135
}
139-
if (anyNA(barbieQ$metadata)) warning("barbieQ$metadata includes NAs.")
136+
if (anyNA(barbieQ$metadata)) warning("sample conditions includes NAs.")
140137

141138
## extract factor color list from @param 'factorColors'
142139
## if @param factorColors is not provided, inherit factorColors from the 'barbieQ' object created.
@@ -150,15 +147,20 @@ now creating a pseudo uni-group with a homogeneous setting in 'barbieQ$metadata'
150147

151148
## now 'barbieQ$assay' should be a matrix already, check if it's numeric apart from NAs.
152149
## find NAs in the 'barbieQ$assay'
153-
ColumnHasNA <- vapply(as.data.frame(barbieQ$assay), function(x) any(is.na(x)), FUN.VALUE = logical(1L))
150+
ColumnHasNA <- vapply(
151+
as.data.frame(barbieQ$assay),
152+
function(x) any(is.na(x)),
153+
FUN.VALUE = logical(1L)
154+
)
154155
WhichColumnHasNA <- which(ColumnHasNA)
155-
RowHasNA <- vapply(seq_len(nrow(barbieQ$assay)),
156+
RowHasNA <- vapply(
157+
seq_len(nrow(barbieQ$assay)),
156158
function(i) any(is.na(barbieQ$assay[i, ])),
157159
FUN.VALUE = logical(1L)
158160
)
159161
WhichRowHasNA <- which(RowHasNA)
160162
if (any(ColumnHasNA)) {
161-
warning("In Barcode count matrix (barbieQ$assay), ", length(WhichRowHasNA), " Barcodes (rows) show NAs across ", length(WhichColumnHasNA), " samples (columns).")
163+
warning("Barcode count matrix has NA, across ", length(WhichRowHasNA), " Barcodes (rows), and ", length(WhichColumnHasNA), " samples (columns).")
162164
## display a menu for the user to choose how to handle NAs.
163165
choice <- utils::menu(c("yes, continue with zeros", "no, keep NA and continue", "stop"), title = "Do you want to convert all NA into zero and continue?")
164166
if (choice == 1) {
@@ -167,10 +169,9 @@ now creating a pseudo uni-group with a homogeneous setting in 'barbieQ$metadata'
167169
message("all NAs in Barcode count table are converted into zero.")
168170
} else if (choice == 2) {
169171
## proceed without preprocessing calculations
170-
message("NAs are retained. barbieQ object is created without preprocessing.
171-
note that NAs may cause issues in subsequent analyses.")
172+
message("NAs are retained. barbieQ object is created without auto-preprocessing. Note that NAs may cause issues in subsequent analyses.")
172173
} else {
173-
stop("please address the NAs in the 'object' parameter first.")
174+
stop("please address the NAs in the barcode count matrix first.")
174175
}
175176
}
176177

R/tagTopBarcodes.R

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
#' Tag each Barcode as being part of the major contributors or not
22
#'
3-
#' `tagTopBarcodes()` is designed for better filtering out the background noise,
4-
#' i.e., Barcodes that consistently have low contributions across samples.
5-
#' Each Barcode is tagged as one of the major contributing Barcodes
6-
#' (\emph{top} Barcodes) or not in each sample, based on whether their
3+
#' `tagTopBarcodes()` tags \emph{top} Barcodes that collectively contribute to
4+
#' the majority of counts across the dataset.
5+
#' It is designed for subsequently filtering out the background noise,
6+
#' i.e., filtering out Barcodes that consistently have low contributions across samples.
7+
#' In each sample, Barcodes are tagged as major contributing Barcodes
8+
#' (\emph{top} Barcodes) or not, based on whether their
79
#' combined proportion passes a defined threshold in the sample.
810
#' Across the entire dataset, a Barcode is considered \emph{top}
911
#' if it is tagged as \emph{top} in a number of samples,
1012
#' meeting a specified appearance threshold across all selected samples.
1113
#'
1214
#' @param barbieQ An object created by the [createBarbieQ] function.
13-
#' @param activeSamples a logical vector indicating which samples (columns)
15+
#' @param activeSamples A logical vector indicating which samples (columns)
1416
#' to consider when determining the \emph{top} Barcodes across the entire
1517
#' dataset. Default to considering all samples in the `barbieQ` object.
1618
#' @param proportionThreshold A numeric value ranging from 0 to 1,
1719
#' used as a threshold for determining \emph{top} Barcodes in each sample
1820
#' based on their combined proportion in theat sample. Default to 0.99.
19-
#' @param minTopFrequency An integer specifying the minimum number of times
21+
#' @param nSampleThreshold An integer specifying the minimum number of times
2022
#' a Barcode must be tagged as \emph{top} across the selected samples
2123
#' (specified by `activeSamples`) in order to be considered \emph{top}
2224
#' for the entire dataset. Default to 1.
@@ -52,7 +54,7 @@
5254
#' ## tag top Barcodes
5355
#' tagTopBarcodes(myBarbieQ)
5456
tagTopBarcodes <- function(barbieQ, activeSamples = NULL,
55-
proportionThreshold = 0.99, minTopFrequency = 1) {
57+
proportionThreshold = 0.99, nSampleThreshold = 1) {
5658
mat <- barbieQ$assay
5759
## dispatch 'returnNumMat' function to ensure the object is a numeric matrix apart from NAs.
5860
if (inherits(mat, "data.frame") || inherits(mat, "matrix") || is.vector(mat)) {
@@ -69,7 +71,7 @@ tagTopBarcodes <- function(barbieQ, activeSamples = NULL,
6971
## select the active columns used to determine 'top Barcodes' out of the samples.
7072
subTopMat <- topsInMat[, activeSamples, drop = FALSE]
7173
## determine the 'top Barcodes' based on the numbers of being true out of all 'active' samples.
72-
topOverall <- rowSums(subTopMat, na.rm = TRUE) >= minTopFrequency
74+
topOverall <- rowSums(subTopMat, na.rm = TRUE) >= nSampleThreshold
7375
## store
7476
updatedObject <- list(
7577
## retain other components
@@ -101,7 +103,7 @@ tagTopBarcodes <- function(barbieQ, activeSamples = NULL,
101103

102104
#' Tag \emph{top} Barcodes in each sample.
103105
#'
104-
#' @param tempCol a numeric vector of Barcode counts in a sample,
106+
#' @param tempCol A numeric vector of Barcode counts in a sample,
105107
#' usually being a column in a count matrix
106108
#' @param proportionThreshold A numeric value ranging from 0 to 1,
107109
#' used as a threshold for determining \emph{top} Barcodes in each sample

tests/testthat/test-tagTopBarcodes.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ test_that("tagging top Barcodes across samples works", {
3535
proportionThreshold = 0.5
3636
)
3737
expect_equal(object4$isTop$vec, rep(c(TRUE, FALSE), each = 25))
38-
## check `minTopFrequency` setting up correct minimum frequency of *top*
38+
## check `nSampleThreshold` setting up correct minimum frequency of *top*
3939
object5 <- tagTopBarcodes(
4040
barbieQ = object1, activeSamples = rep(c(TRUE, FALSE), each = 6),
41-
proportionThreshold = 0.5, minTopFrequency = 2
41+
proportionThreshold = 0.5, nSampleThreshold = 2
4242
)
4343
expect_equal(object5$isTop$vec, rep(FALSE, 50))
4444
})

0 commit comments

Comments
 (0)