diff --git a/DESCRIPTION b/DESCRIPTION index 8401471..6485996 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: SPARKLE Type: Package Title: Single-cell Phenotype Association Research Kit for Large-scale Evaluation -Version: 0.2.3 +Version: 0.2.4 Authors@R: c( person("Chen", "Xi", ,email = "chenxi199506@stu.pku.edu.cn", role = c("aut", "cre")), person("Ding", "Yang", ,email = "dingy@mail.cbi.pku.edu.cn", role = "ctb"), diff --git a/R/f13.1.Moderation_Analysis_kernal.R b/R/f13.1.Moderation_Analysis_kernal.R index b39fb65..3e94c57 100644 --- a/R/f13.1.Moderation_Analysis_kernal.R +++ b/R/f13.1.Moderation_Analysis_kernal.R @@ -82,7 +82,7 @@ moderation_analysis_auto <- function(cwas.data,Best_model, X.cell=NULL,X.gene=NU names(Best_model[["Chosen_model"]]) <- gsub("-", "_", names(Best_model[["Chosen_model"]]) ) X.cell.fomula <- as.character(Best_model[["Chosen_model"]][[X.cell]]@call[["formula"]])[3] X.cell.fomula <- strsplit(X.cell.fomula, " \\+ ")[[1]] - #X.cell.fomula <-replace_double_pipe(X.cell.fomula ) + X.cell.fomula <-replace_double_pipe(X.cell.fomula ) X.cell.fomula.result <- formula_process_string_vector(X.cell.fomula,ratename = X.cell,clean = Cleaninfo) return(X.cell.fomula.result) diff --git a/R/f7.2.cwas_allmodel_heatmap.R b/R/f7.2.cwas_allmodel_heatmap.R index 0666739..0ce0cfb 100644 --- a/R/f7.2.cwas_allmodel_heatmap.R +++ b/R/f7.2.cwas_allmodel_heatmap.R @@ -1,28 +1,28 @@ #' Generate a heatmap of AIC values for multiple models -#' +#' #' This function takes a list of model dataframes and generates a heatmap #' to visualize the AIC values across different models. -#' -#' @param mdlist A list of model dataframes. Each element in the list corresponds +#' +#' @param mdlist A list of model dataframes. Each element in the list corresponds #' to a different cell type or model type. -#' @param method Character string specifying the method to use for ranking. +#' @param method Character string specifying the method to use for ranking. #' Currently supports "rank" (default) and "AIC". -#' +#' #' @details #' The function processes each model dataframe in the input list (\code{mdlist}), #' extracts relevant columns (formula, AIC, Pvalue, rank), and combines them into #' a single dataframe (\code{alldata}). It then cleans unnecessary columns, -#' orders the data, and prepares it for heatmap plotting. P-values are used to +#' orders the data, and prepares it for heatmap plotting. P-values are used to #' annotate the heatmap cells based on significance levels. -#' +#' #' @examples #' # Example usage: #' # cwas_allmodel_heatmap(list(model_data1, model_data2)) -#' +#' #' @import pheatmap #' @export -#' - +#' + cwas_allmodel_heatmap <- function (mdlist,method=c("rank")) { i = 1 celltype <- names(mdlist) @@ -39,7 +39,7 @@ cwas_allmodel_heatmap <- function (mdlist,method=c("rank")) { colnames(mydata) <- paste0(celltype[i], ".", colnames(mydata)) mydata$name <- mydata[, 1] alldata <- merge(alldata, mydata, by = "name", all = T) - print(i) + #print(i) } alldata_cleaned <- alldata[, !grepl("celltype", names(alldata))] alldata_cleaned <- alldata_cleaned[, !grepl("model", names(alldata_cleaned))] @@ -47,22 +47,22 @@ cwas_allmodel_heatmap <- function (mdlist,method=c("rank")) { alldata_cleaned <- alldata_cleaned[, !grepl("id", names(alldata_cleaned))] alldata_cleaned$formula <- alldata[, 2] alldata_cleaned$modelID <- alldata[, 3] - alldata_cleaned <- alldata_cleaned[order(alldata_cleaned$modelID), + alldata_cleaned <- alldata_cleaned[order(alldata_cleaned$modelID), ] alldata <- alldata[order(alldata[, 3]), ] allformula <- alldata$name - pvalue_columns <- grep("Pvalue", names(alldata_cleaned), + pvalue_columns <- grep("Pvalue", names(alldata_cleaned), value = TRUE) AICrank_columns <- grep(method, names(alldata_cleaned), value = TRUE) pvalue_data <- alldata_cleaned[c("name", pvalue_columns)] heatmap_data <- alldata_cleaned[c("name", AICrank_columns)] - + ## 绘制热图 - + heatmap_data2 <- heatmap_data heatmap_data2[, -1] <- lapply(heatmap_data2[, -1], as.numeric) max_value <- max(heatmap_data2[, -1], na.rm = TRUE) - + # 将 heatmap_data2 中的所有 NA 值替换成最大值 heatmap_data2[is.na(heatmap_data2)] <- max_value heatmap_data2$name <- gsub("binomial link function: ", "", heatmap_data2$name) @@ -70,23 +70,23 @@ cwas_allmodel_heatmap <- function (mdlist,method=c("rank")) { colnames(heatmap_data2) <- gsub(".rank", "", colnames(heatmap_data2)) colnames(heatmap_data2) <- gsub(".AIC", "", colnames(heatmap_data2)) heatmap_data2$name=NULL - + pvalue_matrix <- pvalue_data pvalue_matrix$name=NULL rownames(pvalue_matrix) <- rownames(heatmap_data2) colnames(pvalue_matrix) <- colnames(heatmap_data2) - + # 定义p值注释 annotation_matrix <- ifelse(pvalue_matrix < 0.001, "***", ifelse(pvalue_matrix < 0.01, "**", ifelse(pvalue_matrix < 0.05, "*", ""))) annotation_matrix[is.na(annotation_matrix)] <- "NA" - - - pheatmap::pheatmap(heatmap_data2, + + + pheatmap::pheatmap(heatmap_data2, display_numbers = annotation_matrix, color = colorRampPalette(c("red", "white"))(100), - cluster_rows = T, + cluster_rows = T, cluster_cols = T, angle_col = 90, fontsize_row = 10, @@ -94,6 +94,6 @@ cwas_allmodel_heatmap <- function (mdlist,method=c("rank")) { annotation_colors = list(display_numbers = c("black")), main = paste0("Heatmap of AIC Values for All Models","(",method,")") ) - + }