Skip to content

Commit b6781dd

Browse files
authored
Merge pull request KrasnitzLab#30 from adeschen/main
Adding fisherCategoricalVariable() function and unit tests
2 parents 1d807cb + bd248d4 commit b6781dd

11 files changed

+205
-404
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
S3method(is,DrugAUCQuantile)
44
S3method(is,DrugAUCQuantileNoReplicate)
5-
export(getClassNoReplicateOneDrug)
5+
export(fisherCategoricalVariable)
66
export(getClassOneDrug)
77
export(plotDrugAUCDensityCurve)
88
export(plotDrugAUCViolinPlot)

R/OrganoidsToolBox.R

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,10 @@ NULL
231231
#' \itemize{
232232
#' \item \code{\link{getClassOneDrug}} { for selecting the samples
233233
#' with sensitive and resistant behavior for a
234-
#' specific drug screening.}
235-
#' \item \code{\link{getClassNoReplicateOneDrug}} { for selecting
236-
#' the unrelated samples (only one sample per patient)
237-
#' with sensitive and resistant behavior for a
238-
#' specific drug screening.}
234+
#' specific drug screening dataset.}
235+
#' \item \code{\link{selectNoReplicateOrganoids}} { for selecting
236+
#' the unrelated samples (only one sample per patient) from a drug screening
237+
#' dataset.}
239238
#' }
240239
#'
241240
#' @usage data(drugScreening)
@@ -250,14 +249,12 @@ NULL
250249
#' ## Load patient information dataset for 1 drug
251250
#' data(patientInfo)
252251
#'
253-
#' ## Calculate the extreme organoids for the methotrexate drug screening
254-
#' ## using a quantile of 1/3
255-
#' results <- getClassNoReplicateOneDrug(drugScreening=drugScreening,
256-
#' drugName="Methotrexate", study="MEGA-TEST", screenType="TEST-01",
257-
#' patientInfo=patientInfo, doseType="Averaged", quantile=1/4)
252+
#' ## Select unrelated organoids in the drug screening
253+
#' results <- selectNoReplicateOrganoids(drugScreening=drugScreening,
254+
#' patientInfo=patientInfo)
258255
#'
259-
#' ## The information of the extreme organoids is found it the 'extreme' entry
260-
#' results$extreme
256+
#' ## The drug screen dataset has been filtered
257+
#' results
261258
#'
262259
#'
263260
NULL

R/drugScreening-internal.R

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -381,81 +381,6 @@ validateSelectOrgForOneDrug <- function(drugScreening, drugName, study,
381381
}
382382

383383

384-
#' @title Validate input parameters for the
385-
#' getClassNoReplicateOneDrug() function
386-
#'
387-
#' @description The function validates the input parameters for the
388-
#' getClassNoReplicateOneDrug() function.
389-
#'
390-
#' @param drugScreening a single \code{character} string representing the path
391-
#' and name of the drug screening file that contains the information needed
392-
#' to run the organoid selection. Those columns are mandatory: 'organoid_id',
393-
#' 'timestamp', 'study', 'screen_type', 'dosage_type',
394-
#' 'drug_a', 'drug_b', 'drug_c', 'drug_background' and 'relative_auc'.
395-
#'
396-
#' @param drugName a single \code{character} string representing the name of
397-
#' the drug selected for the analyses.
398-
#'
399-
#' @param study a single \code{character} string representing the name of
400-
#' the study selected for the analyses.
401-
#'
402-
#' @param screenType a \code{vector} of \code{character} string representing
403-
#' the type of
404-
#' screening selected for the analyses.
405-
#'
406-
#' @param patientInfo a \code{data.frame} containing the meta-data information
407-
#' related to the organoids. The mandatory columns are: 'organoid_id'
408-
#' and 'patient_id'.
409-
#'
410-
#' @param doseType a single \code{character} string representing the type of
411-
#' dosage selected for the analyses.
412-
#'
413-
#' @param quantile a single positive \code{numeric} between 0 and 0.5
414-
#' indicating the quantile used to select the organoids. Default: \code{1/3}.
415-
#'
416-
#' @return the value \code{0L} when successful.
417-
#'
418-
#' @examples
419-
#'
420-
#' ## Load drug screen dataset for 1 drug
421-
#' data(drugScreening)
422-
#'
423-
#' ## Load patient information dataset
424-
#' data(patientInfo)
425-
#'
426-
#' ## Validation should return OL as all parameters are valids
427-
#' OrganoidsToolBox:::validateSelectOrgWithoutReplicateForOneDrug(
428-
#' drugScreening=drugScreening,
429-
#' drugName="Methotrexate", study="MEGA-TEST", screenType="TEST-01",
430-
#' patientInfo=patientInfo, doseType="Averaged", quantile=1/3)
431-
#'
432-
#' @author Astrid Deschênes, Pascal Belleau
433-
#' @importFrom S4Vectors isSingleNumber
434-
#' @encoding UTF-8
435-
#' @keywords internal
436-
validateSelectOrgWithoutReplicateForOneDrug <- function(drugScreening,
437-
drugName, study, screenType, patientInfo, doseType, quantile) {
438-
439-
## Validate all but patientInfo parameter
440-
validateSelectOrgForOneDrug(drugScreening=drugScreening, drugName=drugName,
441-
study=study, screenType=screenType, doseType=doseType,
442-
quantile=quantile)
443-
444-
if (!is.data.frame(patientInfo)) {
445-
stop("The \'patientInfo\' must be a data.frame.")
446-
}
447-
448-
## Check for mandatory columns in patientInfo
449-
if (!all(c('organoid_id', 'patient_id') %in% colnames(patientInfo))) {
450-
stop("Mandatory columns are missing from the patient info ",
451-
"dataset. The mandatory columns are: \'organoid_id\' and ",
452-
"\'patient_id\'.")
453-
}
454-
455-
return(0L)
456-
}
457-
458-
459384
#' @title Validate input parameters for the
460385
#' selectNoReplicateOrganoids() function
461386
#'

R/drugScreening.R

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -174,144 +174,6 @@ selectNoReplicateOrganoids <- function(drugScreening, patientInfo) {
174174
}
175175

176176

177-
#' @title Select the organoids with sensitive and resistant behavior for a
178-
#' specific drug screening
179-
#'
180-
#' @description The function selects the organoids within the low and upper
181-
#' quantile, as specified by user, for a specific drug screening. When more
182-
#' than one organoid is present for a patient, only one is retained for the
183-
#' analysis. The selection is done randomly. To obtain reproducible results,
184-
#' the seed should be set before running this function.
185-
#'
186-
#' @param drugScreening a \code{data.frame} that contains the drug screening
187-
#' results. The mandatory columns: 'organoid_id',
188-
#' 'timestamp', 'study', 'screen_type', 'dosage_type',
189-
#' 'drug_a', 'drug_b', 'drug_c', 'drug_background' and 'relative_auc'.
190-
#'
191-
#' @param drugName a single \code{character} string representing the name of
192-
#' the drug selected for the analyses. The drug must be present in the drug
193-
#' screening dataset. The drug name can be found in the 'drug_a' column of the
194-
#' drug screening dataset.
195-
#'
196-
#' @param study a single \code{character} string representing the name of
197-
#' the study selected for the analyses. The study must be present in the drug
198-
#' screening dataset. The study can be found in the 'study' column of the
199-
#' drug screening dataset.
200-
#'
201-
#' @param screenType a \code{vector} of \code{character} string representing
202-
#' the type of
203-
#' screening selected for the analyses. The type must be present in the drug
204-
#' screening dataset. The screen type can be found in the 'screen_type'
205-
#' column of the drug screening dataset.
206-
#'
207-
#' @param patientInfo a \code{data.frame} containing the meta-data information
208-
#' related to the organoids. The mandatory columns are: 'organoid_id'
209-
#' and 'patient_id'.
210-
#'
211-
#' @param doseType a single \code{character} string representing the type of
212-
#' dosage selected for the analyses. The type must be present in the drug
213-
#' screening dataset.
214-
#'
215-
#' @param quantile a single positive \code{numeric} between 0 and 0.5
216-
#' indicating the quantile used to select the organoids. Default: \code{1/3}.
217-
#'
218-
#' @return an object of class "\code{DrugAUCQuantile}" which contains the
219-
#' sensitive and resistant organoids for a specific drug. This object is a
220-
#' \code{list} with the following 3 components:
221-
#' \itemize{
222-
#' \item{quantile}{the \code{upper} and \code{lower} quantiles for the
223-
#' specified drug.}
224-
#' \item{dataset}{a \code{data.frame} containing the data used to
225-
#' calculate the quantiles. }
226-
#' \item{extreme}{a \code{data.frame} containing the sensitive and
227-
#' resistant organoids according to the specified quantiles.
228-
#' The \code{data.frame} also contains the relative AUC. }
229-
#' }
230-
#'
231-
#' @examples
232-
#'
233-
#' ## Load drug screen dataset for 1 drug
234-
#' data(drugScreening)
235-
#'
236-
#' ## Load patient information dataset for 1 drug
237-
#' data(patientInfo)
238-
#'
239-
#' ## Calculate the extreme organoids for the methotrexate drug screening
240-
#' ## using a quantile of 1/3
241-
#' results <- getClassNoReplicateOneDrug(drugScreening=drugScreening,
242-
#' drugName="Methotrexate", study="MEGA-TEST", screenType="TEST-01",
243-
#' patientInfo=patientInfo, doseType="Averaged", quantile=1/3)
244-
#'
245-
#' ## The classification of the organoids is in the 'extreme' entry
246-
#' results$extreme
247-
#'
248-
#' @author Astrid Deschênes, Pascal Belleau
249-
#' @importFrom S4Vectors isSingleNumber
250-
#' @encoding UTF-8
251-
#' @export
252-
getClassNoReplicateOneDrug <- function(drugScreening, drugName, study,
253-
screenType, patientInfo, doseType="Averaged", quantile=1/3) {
254-
255-
## Validate input types
256-
validateSelectOrgWithoutReplicateForOneDrug(drugScreening=drugScreening,
257-
drugName=drugName, study=study, screenType=screenType,
258-
patientInfo=patientInfo, doseType=doseType, quantile=quantile)
259-
260-
## The drug must be present in the drug dataset
261-
if (!(tolower(drugName) %in% tolower(unique(drugScreening$drug_a)))) {
262-
stop("The drug \'", drugName, "\' is not present in the drug ",
263-
"screening dataset.")
264-
}
265-
266-
## The study must be present in the drug dataset
267-
if (!(tolower(study) %in% tolower(unique(drugScreening$study)))) {
268-
stop("The study \'", study, "\' is not present in the drug ",
269-
"screening dataset.")
270-
}
271-
272-
## The study must be present in the drug dataset
273-
if (!any(tolower(screenType) %in%
274-
tolower(unique(drugScreening$screen_type)))) {
275-
stop("The screen type \'", screenType, "\' is not present in the ",
276-
"drug screening dataset.")
277-
}
278-
279-
## The doseType must be present in the drug dataset
280-
if (!(tolower(doseType) %in%
281-
tolower(unique(drugScreening$dosage_type)))) {
282-
stop("The dosage type \'", doseType, "\' is not present in the ",
283-
"drug screening dataset.")
284-
}
285-
286-
selectedDrugData <- filterDrugScreenOneDrug(drugData=drugScreening,
287-
drugName=drugName, study=study, screenType=screenType,
288-
doseType=doseType)
289-
290-
## All organoids should have an associated patient information
291-
if (!all(unique(selectedDrugData$organoid_id) %in%
292-
patientInfo$organoid_id)) {
293-
stop("Not all organoids have an associated patient information.")
294-
}
295-
296-
merged_data <- merge(selectedDrugData, patientInfo, by="organoid_id",
297-
all=FALSE)
298-
299-
shuffled_data <- merged_data[sample(1:nrow(merged_data),
300-
replace=FALSE), ]
301-
302-
noReplicate <- shuffled_data[!duplicated(shuffled_data$patient_id), ]
303-
304-
results <- findQuantileOneDrug(cleanDrugData=noReplicate, quantile=quantile)
305-
306-
results[["datasetWithReplicates"]] <- shuffled_data
307-
308-
# Return a list marked as an DrugAUCQuantileNoReplicate class
309-
class(results) <- "DrugAUCQuantileNoReplicate"
310-
311-
return(results)
312-
}
313-
314-
315177
#' @title Create a density plot using the relative AUC and the sensitivity
316178
#' classes (SENSITIVE, AVERAGE, RESISTANT) of the organoids for a specific
317179
#' drug.

R/enrichment.R

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#' @title Run Fisher tests in the sensitive and resistant groups
2+
#' using a categorical value
3+
#'
4+
#' @description The function run Fisher test in the sensitive and
5+
#' resistant groups using a categorical value. TODO
6+
#'
7+
#' @param drugQuantile an object of class "\code{DrugAUCQuantile}" which
8+
#' contains the sensitive and resistant organoids for a specific drug.
9+
#'
10+
#' @param patientInfo a \code{data.frame} containing the meta-data information
11+
#' related to the organoids. The mandatory columns are: 'organoid_id'
12+
#' and 'patient_id'.
13+
#'
14+
#' @param category a single \code{character} string representing the column
15+
#' from the 'patientInfo' dataset that should be used as the
16+
#' categorical variable.
17+
#'
18+
#' @return a \code{list} containing 2 components:
19+
#' \itemize{
20+
#' \item{Fisher}{ TODO}
21+
#' \item{table}{a \code{data.frame} TODO }
22+
#' }
23+
#'
24+
#' @examples
25+
#'
26+
#' ## Load drug screen dataset for 1 drug
27+
#' data(drugScreening)
28+
#'
29+
#' ## Calculate the extreme organoids for the methotrexate drug screening
30+
#' ## using a quantile of 1/3
31+
#' results <- getClassOneDrug(drugScreening=drugScreening,
32+
#' drugName="Methotrexate", study="MEGA-TEST", screenType="TEST-01",
33+
#' doseType="Averaged", quantile=1/3)
34+
#'
35+
#' ## The classification of the organoids is in the 'extreme' entry
36+
#' results$extreme
37+
#'
38+
#' @author Astrid Deschênes, Pascal Belleau
39+
#' @importFrom stats fisher.test
40+
#' @encoding UTF-8
41+
#' @export
42+
fisherCategoricalVariable <- function(drugQuantile, patientInfo, category) {
43+
44+
## Validate that the drugQuantile parameter is a DrugAUCQuantile object
45+
if (!(is.DrugAUCQuantile(drugQuantile) ||
46+
is.DrugAUCQuantileNoReplicate(drugQuantile))) {
47+
stop("The \'drugQuantile\' parameter must be a DrugAUCQuantile ",
48+
"object.")
49+
}
50+
51+
if (!is.data.frame(patientInfo)) {
52+
stop("The \'patientInfo\' parameter must be a data.frame.")
53+
}
54+
55+
if (!is.character(category)) {
56+
stop("The \'category\' parameter must be a character string.")
57+
}
58+
59+
## The drug must be present in the drug dataset
60+
if (!(category %in% colnames(patientInfo))) {
61+
stop("The category \'", category, "\' must be one of the columns in ",
62+
"the \'patientInfo\' dataset.")
63+
}
64+
65+
results <- drugQuantile$extreme
66+
67+
resultsD <- merge(results, patientInfo[, c("organoid_id", category)],
68+
by="organoid_id", all.x = TRUE)
69+
70+
sensitive <- resultsD[resultsD$group == "SENSITIVE",]
71+
resistant <- resultsD[resultsD$group == "RESISTANT",]
72+
73+
categoryAll <- unique(resultsD[[category]])
74+
75+
all <- list()
76+
for (i in categoryAll) {
77+
all[[i]] <- data.frame(V1=c(sum(sensitive[[category]] == i),
78+
sum(resistant[[category]] == i)))
79+
colnames(all[[i]]) <- i
80+
rownames(all[[i]]) <- c("SENSITIVE", "RESISTANT")
81+
}
82+
83+
allTable <- do.call(cbind, all)
84+
85+
fisherResult <- fisher.test(allTable)
86+
87+
result <- list()
88+
result[["Fisher"]] <- fisherResult
89+
result[["table"]] <- allTable
90+
91+
return(result)
92+
}

0 commit comments

Comments
 (0)