Commit 5379be3d authored by HaojiaWu's avatar HaojiaWu
Browse files

updates

parent 5181250a
Loading
Loading
Loading
Loading
+5 −7
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@ complex_dotplot_single <- function(
    groupby_level<-gsub("_","-",groupby_level)
    seu_obj@meta.data[,groupby] <-factor(seu_obj@meta.data[,groupby], levels = groupby_level)
  }
  levels(seu_obj)<-rev(levels(seu_obj))
  if(is.null(celltypes)){
    celltypes<-levels(seu_obj)
  }
@@ -47,24 +46,23 @@ complex_dotplot_single <- function(
  seu_obj<-SetIdent(seu_obj, value='celltype')
  levels(seu_obj)<-celltypes
  if(!is.null(splitby)){
    seu_obj@meta.data[,splitby]<-gsub("_","-",seu_obj@meta.data[,splitby])
    if (is.null(levels(seu_obj@meta.data[,splitby]))){
      seu_obj@meta.data[,splitby] <-factor(seu_obj@meta.data[,splitby], levels = names(table(seu_obj@meta.data[,splitby])))
    }
    splitby_level<-levels(seu_obj@meta.data[,splitby])
    count_df<-extract_gene_count(seu_obj, features = feature, meta.groups = c(groupby,splitby))
    count_df$new_group<-paste(count_df[,groupby], count_df[,"celltype"], count_df[,splitby],sep = "_")
    count_df$new_group<-paste(count_df[,groupby], count_df[,"celltype"], count_df[,splitby],sep = "___")
    exp_df<-aggregate(.~new_group, data=count_df[,c('new_group',feature)], FUN=function(x){mean(expm1(x))})
    pct_df<-aggregate(.~new_group, data=count_df[,c('new_group',feature)], FUN=function(x){length(x[x > 0]) / length(x)})
    colnames(exp_df)[2]<-"avg.exp"
    colnames(pct_df)[2]<-"pct.exp"
    data_plot<-merge(exp_df, pct_df, by='new_group')
    data_plot$groupby <- as.character(lapply(X=strsplit(data_plot$new_group, split = "_"),FUN = function(x){x[[1]]}))
    data_plot$celltype <- as.character(lapply(X=strsplit(data_plot$new_group, split = "_"),FUN = function(x){x[[2]]}))
    data_plot$splitby <- as.character(lapply(X=strsplit(data_plot$new_group, split = "_"),FUN = function(x){x[[3]]}))
    data_plot$groupby <- as.character(lapply(X=strsplit(data_plot$new_group, split = "___"),FUN = function(x){x[[1]]}))
    data_plot$celltype <- as.character(lapply(X=strsplit(data_plot$new_group, split = "___"),FUN = function(x){x[[2]]}))
    data_plot$splitby <- as.character(lapply(X=strsplit(data_plot$new_group, split = "___"),FUN = function(x){x[[3]]}))
    data_plot$groupby <- factor(data_plot$groupby, levels = groupby_level)
    data_plot$splitby <- factor(data_plot$splitby, levels = splitby_level)
    data_plot$celltype <- factor(data_plot$celltype, levels = celltypes)
    data_plot$celltype <- factor(data_plot$celltype, levels = rev(celltypes))
  } else {
  count_df<-extract_gene_count(seu_obj, features = feature, meta.groups = groupby)
  count_df$new_group<-paste(count_df[,groupby], count_df[,"celltype"],sep = "_")
+13 −13
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@
#' @param add.dot Whether or not to add points on the violins.
#' @param font.size Font size for the labels.
#' @param pt.size Point size for the data points on the violin
#' @param split.by Group to split the gene expression. Only works when length(groups)==1.
#' @param splitby Group to split the gene expression. Only works when length(groups)==1.
#' @return A ggplot object
#' @export

@@ -26,7 +26,7 @@ complex_vlnplot_single <- function(
  add.dot = T,
  font.size=14,
  pt.size=0.1,
  split.by=NULL
  splitby=NULL
){
  if(length(feature)>1){
    stop("Only one gene is allowed in this method. Please use complex_vlnplot_multiple if you want to plot multiple genes.")
@@ -34,8 +34,8 @@ complex_vlnplot_single <- function(
  if(is.null(celltypes)){
    celltypes = levels(seu_obj)
  } 
  gene_count<-extract_gene_count(seu_obj=seu_obj, features = feature, cell.types = celltypes, meta.groups = c(groups, split.by))
  allgroups<-c(groups,split.by )
  gene_count<-extract_gene_count(seu_obj=seu_obj, features = feature, cell.types = celltypes, meta.groups = c(groups, splitby))
  allgroups<-c(groups,splitby )
  for(i in 1:length(allgroups)){
    if (is.null(levels(seu_obj@meta.data[,allgroups[i]]))){
      seu_obj@meta.data[,allgroups[i]] <-factor(seu_obj@meta.data[,allgroups[i]], levels = names(table(seu_obj@meta.data[,allgroups[i]])))
@@ -65,13 +65,13 @@ complex_vlnplot_single <- function(
      if(add.dot){
        p = p + geom_quasirandom(size=pt.size, alpha=0.2)
      }
      if(!is.null(split.by)){
        p = p + facet_wrap(as.formula(paste("~", split.by)), scales = 'free_x')
      if(!is.null(splitby)){
        p = p + facet_wrap(as.formula(paste("~", splitby)), scales = 'free_x')
        g <- ggplot_gtable(ggplot_build(p))
        strip_t <- which(grepl('strip-t', g$layout$name))
        strip_r <- which(grepl('strip-r', g$layout$name))
        strip_both<-c( strip_r,strip_t)
        ncol <- length(celltypes) + length(names(table(gene_count[,split.by])))
        ncol <- length(celltypes) + length(names(table(gene_count[,splitby])))
        fills <- grDevices::colorRampPalette(RColorBrewer::brewer.pal(12, "Set3"))(ncol)
        k <- 1
        for (i in strip_both) {
@@ -84,7 +84,7 @@ complex_vlnplot_single <- function(
        p
      }
    } else {
      if(is.null(split.by)){
      if(is.null(splitby)){
        plot_list<-list()
        for(i in 1:length(celltypes)){
          cell_count <- gene_count[gene_count$celltype==celltypes[i],]
@@ -108,8 +108,8 @@ complex_vlnplot_single <- function(
        p<- patchwork::wrap_plots(plotlist = plot_list, ncol = 1)  + patchwork::plot_annotation(title = feature) & theme(plot.title = element_text(hjust = 0.5, size = (font.size +2)))
        p
      } else {
        p<-ggplot(gene_count, aes_string(x = "group", y = feature, fill = "group")) +
          facet_grid(as.formula(paste("celltype","~","group2")), scales = "free_x") +
        p<-ggplot(gene_count, aes_string(x = groups, y = feature, fill = groups)) +
          facet_grid(as.formula(paste("celltype","~", splitby)), scales = "free_x") +
          geom_violin(scale = 'width', adjust = 1, trim = TRUE, size=0.3, alpha=0.5, color="pink")+
          xlab("") +
          ylab(paste(feature,"expression")) +
@@ -127,7 +127,7 @@ complex_vlnplot_single <- function(
        strip_t <- which(grepl('strip-t', g$layout$name))
        strip_r <- which(grepl('strip-r', g$layout$name))
        strip_both<-c(strip_t, strip_r)
        ncol <- length(celltypes) + length(names(table(gene_count[,split.by])))
        ncol <- length(celltypes) + length(names(table(gene_count[,splitby])))
        fills <- grDevices::colorRampPalette(RColorBrewer::brewer.pal(12, "Set3"))(ncol)
        k <- 1
        for (i in strip_both) {
@@ -139,8 +139,8 @@ complex_vlnplot_single <- function(
      }
    }
  } else {
    if(!is.null(split.by)){
      stop("This function does not support spliting multiple groups. Plots will look too messy! Please select one group only in the 'groups' parameter if you want to use 'split.by'.")
    if(!is.null(splitby)){
      stop("This function does not support spliting multiple groups. Plots will look too messy! Please select one group only in the 'groups' parameter if you want to use 'splitby'.")
    }
    if(length(celltypes)==1){
      all_levels<-list()
+19 −3
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ rep_colors<-rand_color(length(names(table(iri.integrated$orig.ident))))

###plot and save figures
png(filename =  'circlize_plot.png', width = 6, height = 6,units = 'in', res = 300)
plot_circlize(circ_data,do.label = T, pt.size = 0.01, col.use = cluster_colors ,bg.color = 'white', kde2d.n = 200, repel = T)
plot_circlize(circ_data,do.label = T, pt.size = 0.01, col.use = cluster_colors ,bg.color = 'white', kde2d.n = 200, repel = T, label.cex = 0.6)
add_track(circ_data, group = "Group", colors = group_colors, track_num = 2) ## can change it to one of the columns in the meta data of your seurat object
add_track(circ_data, group = "orig.ident",colors = rep_colors, track_num = 3) ## can change it to one of the columns in the meta data of your seurat object
dev.off()
@@ -53,7 +53,17 @@ complex_dotplot_single(seu_obj = iri.integrated, feature = "Havcr1",groupby = "G
dev.off()
```
![alt text](https://github.com/HaojiaWu/Plot1cell/blob/master/data/dotplot_single.png) <br />
plot1cell allows visualization of multiple genes in dotplot format too. Here is an example.
If the group factor can be classified by another factor, complex_dotplot_single allows splitting the group factor by another group factor too. Here is an example for demo.
```
iri.integrated@meta.data$Phase<-plyr::mapvalues(iri.integrated@meta.data$Group, from = levels(iri.integrated@meta.data$Group), to = c(rep("Injury",4), rep("Recovery",2)))
iri.integrated@meta.data$Phase<-as.character(iri.integrated@meta.data$Phase)
png(filename =  'dotplot_single_split.png', width = 4, height = 6,units = 'in', res = 100)
complex_dotplot_single(iri.integrated, feature = "Havcr1",groupby = "Group",splitby = "Phase")
dev.off()
![alt text](https://github.com/HaojiaWu/Plot1cell/blob/master/data/dotplot_single_split.png) <br />

```
plot1cell also allows visualization of multiple genes in dotplot format. Here is an example.
```
png(filename =  'dotplot_multiple.png', width = 10, height = 4,units = 'in', res = 300)
complex_dotplot_multiple(seu_obj = iri.integrated, features = c("Slc34a1","Slc7a13","Havcr1","Krt20","Vcam1"),groupby = "Group", celltypes = c("PTS1" ,   "PTS2"  ,  "PTS3"  ,  "NewPT1" , "NewPT2"))
@@ -70,6 +80,13 @@ dev.off()
```
![alt text](https://github.com/HaojiaWu/Plot1cell/blob/master/data/vlnplot_single.png) <br />

Similar to complex_dotplot_single, the complex_vlnplot_single function also allows splitting the group factor by another factor with the argument "splitby".
```
png(filename =  'vlnplot_single_split.png', width = 4, height = 6,units = 'in', res = 100)
complex_vlnplot_single(iri.integrated, feature = "Havcr1", groups = "Group",celltypes   = c("PTS1" ,   "PTS2"  ,  "PTS3"  ,  "NewPT1" , "NewPT2"), splitby = "Phase")
dev.off()
```
![alt text](https://github.com/HaojiaWu/Plot1cell/blob/master/data/vlnplot_single_split.png) <br />
#### One gene/multiple group factors violin plot:
```
png(filename =  'vlnplot_multiple.png', width = 8, height = 6,units = 'in', res = 300)
@@ -90,7 +107,6 @@ dev.off()

#### Multiple genes/multiple group factors.
The violin plot will look too messy in this scenario so it is not included in plot1cell. It is highly recommended to use the complex_dot_plot instead. <br />
For scRNA-seq studies with higher complexity, the complex_vlnplot_single function also allows split the group by another group in the meta data with the argument "split.by". Since the demo dataset doesn't have this complexity, examples are not included here. Users can refer to our recent DKD dataset with multiple treatments/two timepoints (the PCT violin graph in <a href="https://humphreyslab.com/SingleCell/">K.I.T.</a>)

### 4. Umap geneplot across groups
```
−184 KiB (762 KiB)
Loading image diff...
+54.2 KiB
Loading image diff...
Loading