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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
| f_VlnBoxPlot_18 <- function(features){ tb <- data.frame(table(features)) tb <- tb[tb$Freq > 1,] return(tb) } f_VlnBoxPlot_19 <- function(tmp, features, groupN){ dupl <- f_VlnBoxPlot_18(features) if(nrow(dupl) < 1){ return(tmp) } for(lc_i in 1:nrow(dupl)){ dupfeature <- dupl[lc_i, 'features'] idx <- which(features == dupfeature) for (lc_j in 2:dupl[lc_i, 'Freq']){ newfeature <- paste0(dupfeature,'.',lc_j)
colnames(tmp)[[idx[lc_j]]] <- newfeature } }
return(tmp) } f_VlnBoxPlot_20 <- function(scRNAo, features, groupN){ tmp <- log1p(scRNAo[['RNA']]@counts)
tmp <- as.data.frame(t(as.matrix(tmp[features,]))) types <- scRNAo[[groupN]] types[[groupN]] <- factor(types[[groupN]]) newLevels <- levels(types[[groupN]]) newLevels <- newLevels[order(newLevels)] types[[groupN]] <- factor(types[[groupN]], levels=newLevels) tmp <- cbind(tmp, types) tmp <- f_VlnBoxPlot_19(tmp, features, groupN) return(tmp) } f_setRowName <- function(lc_df, lc_colName){ lc_df <- lc_df[order(lc_df[[lc_colName]]),] tp_index <- duplicated(lc_df[[lc_colName]]) lc_df <- lc_df[!tp_index,] rownames(lc_df) <- lc_df[[lc_colName]] lc_df } f_VlnBoxPlot_25 <- function(markers){ dupl <- f_VlnBoxPlot_18(markers$marker) if(nrow(dupl) >= 1){ for(lc_i in 1:nrow(dupl)){ dupfeature <- dupl[lc_i, 'features'] idx <- which(markers$marker == dupfeature) for (lc_j in 2:dupl[lc_i, 'Freq']){ newfeature <- paste0(dupfeature,'.',lc_j) markers[idx[lc_j], 'marker'] <- newfeature } } } markers <- f_setRowName(markers, 'marker') markers[['celltype']] <- factor(markers[['celltype']], ordered = T) col <- f_VlnBoxPlot_col(length(levels(markers[['celltype']]))) markers[['colour']] <- col[as.numeric(markers[['celltype']])] return(markers) } require(reshape2) f_VlnBoxPlot_21 <- function(scRNAo, markers, groupN){ data <- f_VlnBoxPlot_20(scRNAo, markers[['marker']], groupN) data <- melt(data, id.vars = groupN) markers <- f_VlnBoxPlot_25(markers) data[['facet_fill_color']] <- markers[as.character(data[['variable']]), 'colour'] newLevels <- data[['facet_fill_color']][!duplicated(data[['facet_fill_color']])] data[['facet_fill_color']] <- factor(data[['facet_fill_color']], levels=newLevels) return(data) } f_VlnBoxPlot_23 <- function(scRNAo, markers, groupN){ markers <- markers[order(markers$celltype),] theme_s <- VlnPlot(scRNAo, features = markers[1,'marker'], slot = "counts", log = TRUE, group.by = groupN) data <- f_VlnBoxPlot_21(scRNAo, markers, groupN) p <- ggplot(data, aes(x=!!sym(groupN), y=value, fill= !!sym(groupN))) p <- p + theme_s$theme + NoLegend() p <- p + geom_violin() p <- p + stat_ydensity(trim = TRUE, scale = 'width', adjust = 1) p <- p + geom_boxplot(width=0.618) p <- p + stat_summary(fun="mean",geom="point",color='white') p <- p + theme(plot.title=element_blank()) p <- p + labs(x=NULL, y=NULL) p <- p + scale_y_continuous('', breaks = floor(max(layer_scales(p)$y$range$range)))
p <- p + theme(plot.margin = unit(c(0,0,0,0), "cm")) p <- p + facet_grid(variable ~ .) nx <- length(unlist(unique(scRNAo[[groupN]]))) p <- p + scale_fill_manual(values=f_VlnBoxPlot_col(nx)) x_r <- layer_scales(p)$x$range$range p <- p + theme(axis.text.x=element_blank()) p <- f_VlnBoxPlot_26(data, p)
nr <- length(x_r) + 2 p <- p + scale_x_continuous( limits = c(1/nr/2,1-1/nr/2), breaks = seq(0,1,length.out=nr)[2:(nr-1)], label = x_r ) p <- p + theme(axis.text.x=element_text(hjust = 0.5, angle = 45)) return(p) } require(treeio) require(ggplot2) require(ggtree) require(tidytree)
f_VlnBoxPlot_24 <- function(markers){ markers <- markers[order(markers$celltype),]
feature_types <- unique(markers$celltype) feature_types <- as.data.frame(feature_types) feature_types[['root']] = ' ' feature_types <- feature_types[, c('root', 'feature_types')] colnames(feature_types) <- colnames(markers) y = as.phylo(rbind(feature_types, markers)) yy = as_tibble(y) %>% mutate(cat = ifelse(node %in% parent, 1, parent)) yy$cat[rootnode(y)] = 0 p <- ggtree(as.treedata(yy), ladderize=F, layout='roundrect') + geom_nodelab(aes(x=x*0.5, label=label, fill=factor(cat)), hjust=0.5, geom='text', angle=90) + geom_tiplab(aes(label=label, fill=factor(cat)), geom='text', angle=45, hjust=0.5) + scale_y_reverse() + hexpand(.2) + hexpand(.06, -1) + theme(legend.position = 'none')
p <- p + theme(plot.margin = unit(c(0,0,0,0), "cm")) return(p) } require(ggplotify) require(gtable) require(grid) gtable_select <- function (x, ...) { matches <- c(...) x$layout <- x$layout[matches, , drop = FALSE] x$grobs <- x$grobs[matches] x } gtable_stack <- function(g1, g2){ g1$grobs <- c(g1$grobs, g2$grobs) g1$layout <- rbind(g1$layout, g2$layout) g1 } f_VlnBoxPlot_26 <- function(dat, p){ p <- p + theme(strip.background=element_blank()) dummy <- p dummy$layers <- NULL dummy <- dummy + geom_rect(data=dat, xmin=-Inf, ymin=-Inf, xmax=Inf, ymax=Inf, aes(fill = facet_fill_color))
g1 <- ggplotGrob(p) g2 <- ggplotGrob(dummy) panels <- grepl(pattern="panel", g2$layout$name) strips <- grepl(pattern="strip-right", g2$layout$name) g2$grobs[strips] <- replicate(sum(strips), nullGrob(), simplify = FALSE) g2$layout$l[panels] <- g2$layout$l[panels] + 1 g2$layout$r[panels] <- g2$layout$r[panels] + 2 new_strips <- gtable_select(g2, panels strips)
new_plot <- gtable_stack(g1, new_strips) p <- as.ggplot(new_plot) return(p) } require(RColorBrewer) col_Paired <- colorRampPalette(brewer.pal(12, "Paired")) f_VlnBoxPlot_col <- function(nx){ if (nx <= 12){ return(brewer.pal(nx, "Paired")) }else{ return(col_Paired(nx)) } } f_VlnBoxPlot <- function(scRNAo, markers, groupN){ p2 <- f_VlnBoxPlot_24(markers) p <- f_VlnBoxPlot_23(scRNAo, markers , groupN) p2 + p + plot_layout(ncol = 2, widths = c(1, 5)) }
f_VlnBoxPlot_simple <- function(scRNAo, markers, groupN){ markers <- markers[order(markers$celltype),] theme_s <- VlnPlot(scRNAo, features = markers[1,'marker'], slot = "counts", log = TRUE, group.by = groupN) data <- f_VlnBoxPlot_21(scRNAo, markers, groupN) p <- ggplot(data, aes(x=!!sym(groupN), y=value, fill= !!sym(groupN))) p <- p + theme_s$theme + NoLegend() p <- p + geom_violin() p <- p + stat_ydensity(trim = TRUE, scale = 'width', adjust = 1) p <- p + geom_boxplot(width=0.618) p <- p + stat_summary(fun="mean",geom="point",color='white') p <- p + theme(plot.title=element_blank()) p <- p + labs(x=NULL, y=NULL) p <- p + scale_y_continuous('', breaks = floor(max(layer_scales(p)$y$range$range)))
p <- p + theme(plot.margin = unit(c(0,0,0,0), "cm")) p <- p + facet_grid(variable ~ .) nx <- length(unlist(unique(scRNAo[[groupN]]))) p <- p + scale_fill_manual(values=f_VlnBoxPlot_col(nx)) p <- f_VlnBoxPlot_26(data, p) return(p) }
|