Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update tutorial #85

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
606 changes: 387 additions & 219 deletions R/plot_cpdb.R

Large diffs are not rendered by default.

55 changes: 36 additions & 19 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ DEFAULT_V5_COL_START <- 14
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
Expand Down Expand Up @@ -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,
Expand All @@ -60,20 +76,21 @@ 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)
}
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)
Expand All @@ -99,7 +116,7 @@ DEFAULT_COL_START <- 12

.prep_data_querygroup_celltype1 <- function(.data, .query_group, .gene_family, .cell_type, .celltype, ...) {
dat <- suppressWarnings(tryCatch(
.data[.query_group[[tolower(.gene_family)]],
.data[rownames(.data) %in% .query_group[[tolower(.gene_family)]],
grep(.cell_type, colnames(.data), useBytes = TRUE, ...),
drop = FALSE,
],
Expand All @@ -110,7 +127,7 @@ DEFAULT_COL_START <- 12
)
})
colidx <- unique(do.call(c, colidx))
tmpm <- .data[.query_group[[tolower(.gene_family)]], colidx, drop = FALSE]
tmpm <- .data[rownames(.data) %in% .query_group[[tolower(.gene_family)]], colidx, drop = FALSE]
return(tmpm)
}
))
Expand All @@ -120,7 +137,7 @@ DEFAULT_COL_START <- 12

.prep_data_querygroup_celltype2 <- function(.data, .query_group, .gene_family, .cell_type, .celltype, ...) {
dat <- suppressWarnings(tryCatch(
.data[unlist(.query_group[c(tolower(.gene_family))], use.names = FALSE),
.data[rownames(.data) %in% unlist(.query_group[c(tolower(.gene_family))], use.names = FALSE),
grep(.cell_type, colnames(.data), useBytes = TRUE, ...),
drop = FALSE
],
Expand All @@ -131,7 +148,7 @@ DEFAULT_COL_START <- 12
)
})
colidx <- unique(do.call(c, colidx))
tmpm <- .data[unlist(.query_group[c(tolower(.gene_family))], use.names = FALSE), colidx, drop = FALSE]
tmpm <- .data[rownames(.data) %in% unlist(.query_group[c(tolower(.gene_family))], use.names = FALSE), colidx, drop = FALSE]
return(tmpm)
}
))
Expand All @@ -140,7 +157,7 @@ DEFAULT_COL_START <- 12
}

.prep_data_query_celltype <- function(.data, .query, .cell_type, .celltype, ...) {
dat <- suppressWarnings(tryCatch(.data[.query, grep(.cell_type, colnames(.data),
dat <- suppressWarnings(tryCatch(.data[rownames(.data) %in% .query, grep(.cell_type, colnames(.data),
useBytes = TRUE, ...
), drop = FALSE], error = function(e) {
colidx <- lapply(.celltype, function(z) {
Expand All @@ -150,7 +167,7 @@ DEFAULT_COL_START <- 12
)
})
colidx <- unique(do.call(c, colidx))
tmpm <- .data[.query, colidx, drop = FALSE]
tmpm <- .data[rownames(.data) %in% .query, colidx, drop = FALSE]
return(tmpm)
}))
dat <- dat[rowSums(is.na(dat)) == 0, ]
Expand Down
Binary file added data/cpdb_output_degs.RData
Binary file not shown.
Binary file added data/cpdb_output_stat.RData
Binary file not shown.
6 changes: 3 additions & 3 deletions man/plot_cpdb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading