Commit fb7b8e7d authored by HaojiaWu's avatar HaojiaWu
Browse files

Add more methods

parent 9d6d6048
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -3,10 +3,12 @@
export(add_sliding_windows)
export(add_tract)
export(cell_order)
export(complex_vlnplot_single)
export(convert_geneid)
export(creat_cellphonedb_file)
export(create_pyscenic_file)
export(data_processing)
export(extract_gene_count)
export(get_metadata)
export(get_segment)
export(mk_color_table)
+15 −24
Original line number Diff line number Diff line
@@ -29,31 +29,20 @@ modified_dotplot <- function(
    'radius' = scale_radius,
    stop("'scale.by' must be either 'size' or 'radius'")
  )
  
  if(max(dataplot$Pct.Exp)>=20) {
  dotplot<-ggplot(dataplot, aes(x = features.plot, y = id, fill=Avg.Exp)) + 
    geom_tile(fill="white", color="white") +
    geom_point(aes( size =Pct.Exp), shape=21, color='grey80')  +  
    scale_fill_gradientn(colours  =  col_palette)+
      scale_size(range = c(0, 10))+
    theme(panel.background = element_rect(fill = "white", colour = "black"),
          axis.line = element_blank(),axis.text.x = element_text(angle = 45, hjust = 1), 
          legend.key = element_rect(colour = NA, fill = NA),
          axis.text = element_text(size = 12),axis.title=element_text(size=8),legend.text=element_text(size=8), 
          legend.title = element_text(size = 10),legend.position="right", legend.margin=margin())+ylab("")+xlab("")+
    guides(size = guide_legend(override.aes = list(color='black')))
  if(max(dataplot$Pct.Exp)>=20) {
    dotplot + scale_size(range = c(0, 10))
  } else {
    dotplot<-ggplot(dataplot, aes(x = features.plot, y = id, fill=Avg.Exp)) +  
      geom_tile(fill="white", color="white") +
      geom_point(aes( size =Pct.Exp), shape=21, color='grey80')  +  
      scale_fill_gradientn(colours  = col_palette)+
      scale.func(range = c(0, 10), limits = c(0, 20)) +
      theme(panel.background = element_rect(fill = "white", colour = "black"),
            axis.line = element_blank(),axis.text.x = element_text(angle = 45, hjust = 1), 
            legend.key = element_rect(colour = NA, fill = NA),
            axis.text = element_text(size = 12),axis.title=element_text(size=8),legend.text=element_text(size=8), 
            legend.title = element_text(size = 10),legend.position="right", legend.margin=margin())+ylab("")+xlab("")+
      guides(size = guide_legend(override.aes = list(color='black')))
    dotplot + scale.func(range = c(0, 10), limits = c(0, 20))
  }
  dotplot
}
@@ -95,3 +84,5 @@ modified_dimplot <- function(
  ylab('UMAP_2')+theme(axis.title.y = element_text(hjust = 0.05, angle = 90, size = 12))+
  ggtitle(title)+theme(plot.title = element_text(hjust = 0.5))
}

R/plot_violin.R

0 → 100644
+127 −0
Original line number Diff line number Diff line
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Functions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' Violin plot for a single gene across groups
#'
#' This function generates violin plot(s) to compare the expression of a single gene across 
#' different groups or cell types. It is designed for visualizing a complicated scenario: 
#' Gene expression on multiple cell types and multiple conditions.
#'
#' @param seu A complete Seurat object
#' @param feature Gene name. Only one gene is allowed.
#' @param cell.types Cell types of interest. By default, all cell types are included.
#' @param groups Groups to split the plot. Support multiple groups.
#' @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
#' @return A ggplot object
#' @export

complex_vlnplot_single <- function(
  seu,
  feature,
  cell.types=NULL,
  groups,
  add.dot = T,
  font.size=14,
  pt.size=0.1
  ){
  if(is.null(cell.types)){
    cell.types = levels(seu)
  }
  gene_count<-extract_gene_count(seu=seu, features = feature, cell.types = cell.types, meta.groups = groups)
  set.seed(seed = 42)
  noise <- rnorm(n = length(x = gene_count[,feature])) / 100000
  gene_count[, feature]<-gene_count[,feature]+noise
  if (length(groups)==1) {
      if(length(cell.types)==1){
      p<-ggplot(gene_count, aes_string(x=groups, y=feature, fill=groups))+
      geom_violin(scale = 'width', adjust = 1, trim = TRUE, size=0.3, alpha=0.5, color="pink")+
      xlab("") + ylab("") + ggtitle(feature) +
      theme(panel.background = element_rect(fill = "white",colour = "black"),
            axis.title = element_text(size = font.size),
            axis.text.x = element_text(size = font.size, angle = 45, hjust = 1, vjust = 1),
            axis.text.y = element_text(size=(font.size-2)),
            legend.title = element_blank(),
            legend.position = 'none',
            plot.title = element_text(size=(font.size+2), hjust = 0.5))
      if(add.dot){
        p = p + geom_quasirandom(size=pt.size, alpha=0.2)
      }
      p
    } else {
      plot_list<-list()
      for(i in 1:length(cell.types)){
        cell_count <- gene_count[gene_count$celltype==cell.types[i],]
        p<-ggplot(cell_count, aes_string(x=groups, y=feature, fill=groups))+
          geom_violin(scale = 'width', adjust = 1, trim = TRUE, size=0.3, alpha=0.5, color="pink")+
          theme(panel.background = element_rect(fill = "white",colour = "black"),
                legend.position = "none",
                axis.text.x = element_blank(),
                axis.ticks.x = element_blank(),
                axis.title.y = element_text(size = font.size, angle = 0),
                axis.text.y = element_text(size = (font.size-2)),
                plot.margin = unit(c(-0.5, 0, -0.5, 0), "cm") )
        if(add.dot){
          p = p + geom_quasirandom(size=pt.size, alpha=0.2)
        }
        plot_list[[i]]<-p
      }
      plot_list[[length(plot_list)]]<- plot_list[[length(plot_list)]] +
        theme(axis.text.x=element_text(angle = 45, hjust = 1, vjust = 1, size = font.size), axis.ticks.x = element_line())
      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 {
    if(length(cell.types)==1){
      gene_count<-melt(gene_count[,c(feature, groups)], measure.vars  = groups)
      p<-ggplot(gene_count, aes_string(x="value", y=feature, fill="value"))+
        geom_violin(scale = 'width', adjust = 1, trim = TRUE, size=0.3, alpha=0.5, color="pink")+
        xlab("") + ylab("") + ggtitle(feature) +
        theme(panel.background = element_rect(fill = "white",colour = "black"),
              axis.title = element_text(size = font.size),
              axis.text.x = element_text(size = font.size, angle = 45, hjust = 1, vjust = 1),
              axis.text.y = element_text(size=(font.size-2)),
              legend.title = element_blank(),
              legend.position = 'none',
              plot.title = element_text(size=(font.size+2), hjust = 0.5))+
        facet_wrap(~variable, scales = 'free_x')
      if(add.dot){
        p = p + geom_quasirandom(size=pt.size, alpha=0.2)
      }
      p
    } else {
      plot_list1<-list()
      plot_list2<-list()
      for(i in 1:length(groups)){
        group=groups[i]
        cell_count<-gene_count[,c(feature, group, "celltype")]
        for(j in 1:length(cell.types)){
          cell_count2 <- cell_count[cell_count$celltype==cell.types[j],]
          p<-ggplot(cell_count2, aes_string(x=group, y=feature, fill=group))+
            geom_violin(scale = 'width', adjust = 1, trim = TRUE, size=0.3, alpha=0.5, color="pink")+
            xlab("") + ylab(cell.types[j]) +
            theme(panel.background = element_rect(fill = "white",colour = "black"),
                  legend.position = "none",
                  axis.text.x = element_blank(),
                  axis.ticks.x = element_blank(),
                  axis.title.y = element_text(size = font.size, angle = 0),
                  axis.text.y = element_text(size = (font.size-2)),
                  plot.margin = unit(c(-0.5, 0, -0.5, 0), "cm") )
          if(add.dot){
            p = p + geom_quasirandom(size=pt.size, alpha=0.2)
          }
          plot_list1[[j]]<-p
        }
        plot_list1[[length(plot_list1)]]<- plot_list1[[length(plot_list1)]] +
          theme(axis.text.x=element_text(angle = 45, hjust = 1, vjust = 1, size = font.size), axis.ticks.x = element_line(), axis.title.x = element_text(size=font.size))+ xlab(group)
        p2<-patchwork::wrap_plots(plotlist = plot_list1, ncol = 1)
        plot_list2[[i]]<-p2
        plot_list1<-list()
      }
      p<-patchwork::wrap_plots(plotlist = plot_list2) + patchwork::plot_annotation(title = feature) & theme(plot.title = element_text(hjust = 0.5, size = (font.size+2)))
      p
    }
  }
}
+36 −0
Original line number Diff line number Diff line
@@ -327,3 +327,39 @@ data_processing <- function(
  Sys.time()
}


#' A function to extract gene counts for ploting
#'
#' This function is a modified Seurat::FetchData function to extract gene 
#' counts and the associated meta data for ploting. It returns a dataframe
#' with the requested information from the Seurat object.
#'
#' @param seu A finished Seurat Object with cell type annotation in the active.ident slot
#' @param features Gene names to extract expression data
#' @param cell.types The cell types to be inspected. By default, it will incorporate all cell types.
#' @param data.type The data slot to be accessed. By default, the "data" slot will be used.
#' @param meta.groups The colnames in the meta.data slot you want to include.
#' @return A data frame with the requested info.
#' @export
#' 
extract_gene_count <- function(
  seu, 
  features, 
  cell.types=NULL, 
  data.type="data", 
  meta.groups=NULL
 ){
  if(is.null(cell.types)){
    cell.types=levels(seu)
  }
  seu@meta.data$celltype<-as.character(seu@active.ident)
  if(is.null(meta.groups)){
    meta.groups=colnames(seu@meta.data)
  }
  new_seu<-subset(seu, idents=cell.types)
  feature_count<-Seurat::FetchData(new_seu, slot = data.type, vars = c(features,meta.groups,"celltype"))
  umap_data<-data.frame(new_seu[["umap"]]@cell.embeddings)
  feature_count$UMAP1<-umap_data$UMAP_1
  feature_count$UMAP2<-umap_data$UMAP_2
  feature_count
}
+39 −0
Original line number Diff line number Diff line
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plot_violin.R
\name{complex_vlnplot_single}
\alias{complex_vlnplot_single}
\title{Violin plot for a single gene across groups}
\usage{
complex_vlnplot_single(
  seu,
  feature,
  cell.types = NULL,
  groups,
  add.dot = T,
  font.size = 14,
  pt.size = 0.1
)
}
\arguments{
\item{seu}{A complete Seurat object}

\item{feature}{Gene name. Only one gene is allowed.}

\item{cell.types}{Cell types of interest. By default, all cell types are included.}

\item{groups}{Groups to split the plot. Support multiple groups.}

\item{add.dot}{Whether or not to add points on the violins.}

\item{font.size}{Font size for the labels.}

\item{pt.size}{Point size for the data points on the violin}
}
\value{
A ggplot object
}
\description{
This function generates violin plot(s) to compare the expression of a single gene across
different groups or cell types. It is designed for visualizing a complicated scenario:
Gene expression on multiple cell types and multiple conditions.
}
Loading