1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| f_mkdir_s <- function(lc_path){ if (!file.exists(lc_path)){dir.create(lc_path)} setwd(lc_path) print(getwd()) }
f_mkdir_e <- function(){ setwd(output_path) print(getwd()) }
f_ggplot2_ti <- function(p, title){ (p + ggtitle(title) + theme(plot.title = element_text(hjust = 0.5))) } f_sc_DoHeatmap <- function(sObject, significant_markers, lc_groupN = 'ident', lc_listP){ if(nrow(significant_markers)<1){ print(significant_markers) return(lc_listP) } significant_markers %>% group_by(cluster) %>% top_n(n = 10, wt = avg_log2FC) -> topn if(nrow(topn)<1){ print(topn) return(lc_listP) }else{ pN <- paste0(sObject@project.name,'_',lc_groupN,"_significant_markers") img = DoHeatmap(sObject, group.by=lc_groupN, features=topn$gene, disp.min=-1, disp.max=1) %>% f_ggplot2_ti(sObject@project.name) lc_listP[[pN]] <- img } return(lc_listP) } f_sc_degs <- function(sObject, lc_groupN, lc_listR){ Idents(sObject) <- sObject[[lc_groupN]] sObject_markers <- FindAllMarkers(sObject, min.pct = 0.25, logfc.threshold = 0.25) if(nrow(sObject_markers)<1 !('p_val_adj' %in% colnames(sObject_markers))){ print(sObject_markers) return(lc_listR) } significant_markers <- subset(sObject_markers, subset = p_val_adj<0.05) SN <- paste0(sObject@project.name,'_',lc_groupN,"_sObject_markers") SN <- substr(SN, 1, 31) lc_listR$M[[SN]] <- sObject_markers SN <- paste0(sObject@project.name,'_',lc_groupN,"_significant_markers") SN <- substr(SN, 1, 31) lc_listR$SM[[SN]] <- significant_markers lc_listR$P <- f_sc_DoHeatmap(sObject,significant_markers,lc_groupN,lc_listR$P) return(lc_listR) }
f_sc_DoHeatmap_sp <- function(sObject, lc_labelN, lc_groupN, sampleN){ ss <- SplitObject(sObject, split.by = lc_labelN) lc_listR <- list() lc_listR$M <- list() lc_listR$SM <- list() lc_listR$P <- list() for (lc_N in names(ss)){ sso <- ss[[lc_N]] sso@project.name <- gsub('/', '_', lc_N) sso@project.name <- gsub(' ', '_', sso@project.name) sso@project.name <- gsub('\\.', '_', sso@project.name) lc_listR <- f_sc_degs(sso, lc_groupN, lc_listR) } sampleN <- gsub('/', '_', sampleN) sampleN <- gsub(' ', '_', sampleN) sampleN <- gsub('\\.', '_', sampleN) f_mkdir_s(sampleN) f_xlsx_output(paste0(sampleN, '_sObject_markers.xlsx'), lc_listR$M) f_xlsx_output(paste0(sampleN, '_significant_markers.xlsx'), lc_listR$SM) f_ppt_output(paste0(sampleN, '_Heatmap.pptx'), lc_listR$P) f_mkdir_e() return(lc_listR) }
|