Commit 8fa58df6 authored by HaojiaWu's avatar HaojiaWu
Browse files

More methods

parent 41e072f4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ export(change_strip_background)
export(complex_dotplot_multiple)
export(complex_dotplot_single)
export(complex_featureplot)
export(complex_heatmap_group)
export(complex_vlnplot_single)
export(convert_geneid)
export(creat_cellphonedb_file)

R/plot_heatmap.R

0 → 100644
+122 −0
Original line number Diff line number Diff line
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Functions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' Plot gene expression across groups using ComplexHeatmap
#'
#' This function is for identifying the group-specific genes in a selected celltype
#' and plot the expression of those genes in heatmap.
#'
#' @param seu_obj A complete Seurat object
#' @param celltype Cell types selected for gene plot.
#' @param group The group to show on x axis. One of the column names in meta.data.
#' @param gene_highlight Gene names showing on the rows. Default: all genes
#' @param logfc Fold change to select the genes
#' @param return_marker If TRUE, a list of specific gene will be returned.
#' @param col_fun Heatmap color key.
#' @return A ComplexHeatmap object or/and a gene list
#' @export

complex_heatmap_group<-function(
  seu_obj,
  celltype,
  group,
  gene_highlight=NULL,
  logfc=0.5,
  return_marker=FALSE,
  col_fun = colorRamp2(c(-2, -1, 0, 1, 2), rev(c("#BF0080", "#CE6EAE", "#dddddd", "#6EAE6E", "#008000")))
){
cell1<-subset(seu_obj, idents=celltype)
cell1<-SetIdent(cell1, value = group)
group_levels<-levels(seu_obj@meta.data[,group])
cell1_avg<-AverageExpression(cell1, verbose = F, return.seurat = F, assays = "RNA")
cell1_avg<-cell1_avg$RNA
cell1_avg<-data.frame(cell1_avg)
all_markers<-FindAllMarkers(cell1, min.pct = 0.1, logfc.threshold = logfc,verbose = F)
all_markers1<-all_markers[all_markers$avg_log2FC>0,]
all_markers2<-all_markers[all_markers$avg_log2FC<0,]
unique_list1<-list()
for(i in 1:length(group_levels)){
  group1<-all_markers1[all_markers1$cluster==group_levels[i],]
  group2<-all_markers1[all_markers1$cluster!=group_levels[i],]
  group1_unique<-setdiff(group1$gene, intersect(group1$gene, group2$gene))
  unique_list1[[i]]<-group1_unique
}
unique_list2<-list()
for(i in 1:length(group_levels)){
  group1<-all_markers2[all_markers2$cluster==group_levels[i],]
  group2<-all_markers2[all_markers2$cluster!=group_levels[i],]
  group1_unique<-setdiff(group1$gene, intersect(group1$gene, group2$gene))
  unique_list2[[i]]<-group1_unique
}
unique_list<-c(unique_list1,unique_list2)
unique_genes<-as.character(unlist(unique_list))
data_plot<-cell1_avg[unique_genes,]
gene_num<-c()
for(i in 1:length(unique_list)){
  gene_num[i]<-length(unique_list[[i]])
}
unique_list<-unique_list[which(gene_num!=0)]
col_split<-group_levels
col_split<-factor(col_split, levels = group_levels)
gene_groups<-c(paste0(group_levels, "_up"),paste0(group_levels, "_down"))
gene_groups<-gene_groups[which(gene_num!=0)]
row_split<-list()
for(i in 1:length(gene_groups)){
  row_split[[i]]<-rep(gene_groups[i], length(unique_list[[i]]))
}
row_split<-as.character(unlist(row_split))
row_split<-factor(row_split, levels = gene_groups)
term = list()
for(i in 1:length(gene_groups)){
  txt1<-as.character(unlist(strsplit(gene_groups[i], split = "_")))
  term[[i]]<-data.frame(txt=txt1, index=c(1,2))
}
names(term) = gene_groups
data_plot<-t(scale(t(data_plot)))
label1<-gene_highlight
if(is.null(label1)){
  label1=rownames(data_plot)
}
ht_opt$message = FALSE
ht<- Heatmap(data_plot,name = "mat", cluster_rows = F, cluster_columns = F, 
             column_title = NULL,col = col_fun, row_title = NULL,
             cluster_row_slices = FALSE, cluster_column_slices = FALSE,
             column_split  = col_split, row_split = row_split,
             column_gap = unit(0, "mm"), row_gap = unit(0, "mm"), 
             heatmap_legend_param = list(direction = "horizontal",title = celltype)
) +
  rowAnnotation(link = anno_mark(at = match(label1,unique_genes), which = 'row',
                                 labels = label1, 
                                 labels_gp = gpar(fontsize = 10), padding = unit(1, "mm"))) +
  rowAnnotation(wc = anno_word_cloud(row_split, term, add_new_line = TRUE,
                                     value_range = c(1, 2), fontsize_range = c(12, 12)) ) 
ht=draw(ht,heatmap_legend_side = "top")
down_groups<-sum(grepl("down", gene_groups))
gaps<-c(1:length(group_levels), 1:length(group_levels))
gaps<-gaps[which(gene_num!=0)]
for(i in 1:length(gene_groups)){
  if(i<=down_groups){
    decorate_heatmap_body("mat", row_slice = i, column_slice = 1, code = {
      grid.rect(unit(gaps[i]-1, "npc"), unit(1, "npc"), 
                width = 1 * unit(1, "npc"),
                height = 1 * unit(1, "npc"),
                gp = gpar(lwd = 2, lty = 1, fill=NA, col='black'), just = c("left", "top") 
      )
    }
    )
  } else {
    decorate_heatmap_body("mat", row_slice = i, column_slice = 1, code = {
      grid.rect(unit(gaps[i]-1, "npc"), unit(1, "npc"), 
                width = 1 * unit(1, "npc"),
                height = 1 * unit(1, "npc"),
                gp = gpar(lwd = 2, lty = 1, fill=NA, col='blue'), just = c("left", "top") 
      )
    }
    )
  }
}
if(return_marker){
  names(unique_list)<-gene_groups
  return(unique_list)
}
}
+15 −6
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ This package allows users to visualize the single cell data on the R objects or
plot1cell R package can be easily installed from Github using devtools. Please make sure you have installed Seurat 4.0, circlize and ComplexHeatmap packages.
```
devtools::install_github("TheHumphreysLab/plot1cell")
## or devtools::install_github("HaojiaWu/plot1cell")
## or the development version, devtools::install_github("HaojiaWu/plot1cell")
```

## Usage
@@ -66,7 +66,7 @@ dev.off()
```
![alt text](https://github.com/HaojiaWu/Plot1cell/blob/master/data/vlnplot_single.png) <br />

#### One genes/multiple groups violin plot:
#### One gene/multiple groups violin plot:
```
png(filename =  'vlnplot_multiple.png', width = 8, height = 6,units = 'in', res = 300)
complex_vlnplot_single(iri.integrated, feature = "Havcr1", groups = c("Group","Replicates"),celltypes   = c("PTS1" ,   "PTS2"  ,  "PTS3"  ,  "NewPT1" , "NewPT2"))
@@ -88,8 +88,17 @@ dev.off()
```
![alt text](https://github.com/HaojiaWu/Plot1cell/blob/master/data/geneplot_umap.png) <br />

### 5. ComplexHeatmap to show unique genes across groups
plot1cell can directly identify the condition specific genes in a selected cell type and plot those genes using ComplexHeatmap. An example is shown below:
```
iri.integrated$Group2<-mapvalues(iri.integrated$Group, from = c("Control", "4hours",  "12hours", "2days",   "14days" , "6weeks" ),
to = c("Ctrl","Hr4","Hr12","Day2", "Day14","Wk6"))
iri.integrated$Group2<-factor(iri.integrated$Group2, levels = c("Ctrl","Hr4","Hr12","Day2", "Day14","Wk6"))
png(filename =  'heatmap_group.png', width = 4, height = 8,units = 'in', res = 100)
complex_heatmap_group(seu_obj = iri.integrated, celltype = "NewPT2", group = "Group2",gene_highlight = c("Slc22a28","Vcam1","Krt20","Havcr1"))
dev.off()
```
![alt text](https://github.com/HaojiaWu/Plot1cell/blob/master/data/heatmap_group.png) <br />




### 6. Other ploting functions
There are other functions for plotting/data processing in plot1cell. Many more functions will be added in the future package development. For questions, please raise an issue in this github page or contact <a href="https://humphreyslab.com">TheHumphreysLab</a>. 

data/heatmap_group.png

0 → 100644
+66.9 KiB
Loading image diff...
+39 −0
Original line number Diff line number Diff line
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plot_heatmap.R
\name{complex_heatmap_group}
\alias{complex_heatmap_group}
\title{Plot gene expression across groups using ComplexHeatmap}
\usage{
complex_heatmap_group(
  seu_obj,
  celltype,
  group,
  gene_highlight = NULL,
  logfc = 0.5,
  return_marker = FALSE,
  col_fun = colorRamp2(c(-2, -1, 0, 1, 2), rev(c("#BF0080", "#CE6EAE", "#dddddd",
    "#6EAE6E", "#008000")))
)
}
\arguments{
\item{seu_obj}{A complete Seurat object}

\item{celltype}{Cell types selected for gene plot.}

\item{group}{The group to show on x axis. One of the column names in meta.data.}

\item{gene_highlight}{Gene names showing on the rows. Default: all genes}

\item{logfc}{Fold change to select the genes}

\item{return_marker}{If TRUE, a list of specific gene will be returned.}

\item{col_fun}{Heatmap color key.}
}
\value{
A ComplexHeatmap object or/and a gene list
}
\description{
This function is for identifying the group-specific genes in a selected celltype
and plot the expression of those genes in heatmap.
}