From 37855e7e422fac82966d6451264fba987eb2cf7b Mon Sep 17 00:00:00 2001 From: Zewen Kelvin Tuong Date: Wed, 1 Nov 2023 19:36:41 +1000 Subject: [PATCH] Update utils.R --- R/utils.R | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/R/utils.R b/R/utils.R index 08d99ce0..b8c975c8 100644 --- a/R/utils.R +++ b/R/utils.R @@ -10,6 +10,22 @@ DEFAULT_CLASS_COL <- 13 DEFAULT_COL_START <- 12 +.prep_dimensions <- function(input, reference) { + requireNamespace("reshape2") + tmp_mat <- reference + tmp_mat <- (tmp_mat * 0) + 1 + melted_A <- reshape2::melt(as.matrix(tmp_mat)) + melted_B <- reshape2::melt(as.matrix(input)) + rownames(melted_A) <- paste0(melted_A$Var1, DEFAULT_SEP, melted_A$Var2) + rownames(melted_B) <- paste0(melted_B$Var1, DEFAULT_SEP, melted_B$Var2) + melted_A[row.names(melted_B), ] <- melted_B + tmp_mat <- reshape2::dcast(melted_A, Var1 ~ Var2, value.var = "value") + rownames(tmp_mat) <- tmp_mat$Var1 + tmp_mat <- tmp_mat[, -1] + tmp_mat <- tmp_mat[rownames(reference), colnames(reference)] + return(tmp_mat) +} + .prep_table <- function(data) { dat <- data rownames(dat) <- make.names(dat$interacting_pair, unique = TRUE) @@ -36,18 +52,18 @@ DEFAULT_COL_START <- 12 .prep_query_group <- function(data, genes = NULL, gene_family = NULL, custom_gene_family = NULL) { if (is.null(gene_family) & is.null(genes)) { query_group <- NULL - query_id <- grep("", data$interacting_pair) - query <- row.names(data)[query_id] + query_id <- grep("", data$interacting_pair, value = TRUE) + query <- row.names(data[data$interacting_pair %in% query_id, ]) } else if (!is.null(gene_family) & !is.null(genes)) { stop("Please specify either genes or gene_family, not both") } else if (!is.null(gene_family) & is.null(genes)) { - chemokines <- grep("^CXC|CCL|CCR|CX3|XCL|XCR", data$interacting_pair) - th1 <- grep("IL2|IL12|IL18|IL27|IFNG|IL10|TNF$|TNF |LTA|LTB|STAT1|CCR5|CXCR3|IL12RB1|IFNGR1|TBX21|STAT4", data$interacting_pair) - th2 <- grep("IL4|IL5|IL25|IL10|IL13|AREG|STAT6|GATA3|IL4R", data$interacting_pair) - th17 <- grep("IL21|IL22|IL24|IL26|IL17A|IL17A|IL17F|IL17RA|IL10|RORC|RORA|STAT3|CCR4|CCR6|IL23RA|TGFB", data$interacting_pair) - treg <- grep("IL35|IL10|FOXP3|IL2RA|TGFB", data$interacting_pair) - costimulatory <- grep("CD86|CD80|CD48|LILRB2|LILRB4|TNF|CD2|ICAM|SLAM|LT[AB]|NECTIN2|CD40|CD70|CD27|CD28|CD58|TSLP|PVR|CD44|CD55|CD[1-9]", data$interacting_pair) - coinhibitory <- grep("SIRP|CD47|ICOS|TIGIT|CTLA4|PDCD1|CD274|LAG3|HAVCR|VSIR", data$interacting_pair) + chemokines <- grep("^CXC|CCL|CCR|CX3|XCL|XCR", data$interacting_pair, value = TRUE) + th1 <- grep("IL2|IL12|IL18|IL27|IFNG|IL10|TNF$|TNF |LTA|LTB|STAT1|CCR5|CXCR3|IL12RB1|IFNGR1|TBX21|STAT4", data$interacting_pair, value = TRUE) + th2 <- grep("IL4|IL5|IL25|IL10|IL13|AREG|STAT6|GATA3|IL4R", data$interacting_pair, value = TRUE) + th17 <- grep("IL21|IL22|IL24|IL26|IL17A|IL17A|IL17F|IL17RA|IL10|RORC|RORA|STAT3|CCR4|CCR6|IL23RA|TGFB", data$interacting_pair, value = TRUE) + treg <- grep("IL35|IL10|FOXP3|IL2RA|TGFB", data$interacting_pair, value = TRUE) + costimulatory <- grep("CD86|CD80|CD48|LILRB2|LILRB4|TNF|CD2|ICAM|SLAM|LT[AB]|NECTIN2|CD40|CD70|CD27|CD28|CD58|TSLP|PVR|CD44|CD55|CD[1-9]", data$interacting_pair, value = TRUE) + coinhibitory <- grep("SIRP|CD47|ICOS|TIGIT|CTLA4|PDCD1|CD274|LAG3|HAVCR|VSIR", data$interacting_pair, value = TRUE) query_group <- list( chemokines = chemokines, chemokine = chemokines, @@ -60,11 +76,12 @@ DEFAULT_COL_START <- 12 costimulation = costimulatory, coinhibition = coinhibitory ) + query_group <- lapply(query_group, function(x) row.names(data[data$interacting_pair %in% x, ])) if (!is.null(custom_gene_family)) { cgf <- as.list(custom_gene_family) cgf <- lapply(cgf, function(x) { - q_id <- grep(paste(x, collapse = "|"), data$interacting_pair) - q <- row.names(data)[q_id] + q_id <- grep(paste(x, collapse = "|"), data$interacting_pair, value = TRUE) + q <- row.names(data[data$interacting_pair %in% q_id, ]) return(q) }) query_group <- c(query_group, cgf) @@ -72,8 +89,8 @@ DEFAULT_COL_START <- 12 query <- NULL } else if (is.null(gene_family) & !is.null(genes)) { query_group <- NULL - query_id <- grep(paste(genes, collapse = "|"), data$interacting_pair) - query <- row.names(data)[query_id] + query_id <- grep(paste(genes, collapse = "|"), data$interacting_pair, value = TRUE) + query <- row.names(data[data$interacting_pair %in% query_id, ]) } out <- list("query_group" = query_group, "query" = query) return(out)