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
0 commit comments