Commit d86c3382 authored by HaojiaWu's avatar HaojiaWu
Browse files

bug fixes

parent b332352b
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ Imports:
    reshape2,
    ggbeeswarm,
    purrr,
    UpSetR,
    ComplexUpset,
    matrixStats,
    DoubletFinder,
    methods,
@@ -57,7 +57,7 @@ Depends:
    reshape2,
    ggbeeswarm,
    purrr,
    UpSetR,
    ComplexUpset,
    matrixStats,
    DoubletFinder,
    methods,
+2 −2
Original line number Diff line number Diff line
@@ -8,7 +8,8 @@ export(change_strip_background)
export(complex_dotplot_multiple)
export(complex_dotplot_single)
export(complex_featureplot)
export(complex_heatmap_group)
export(complex_heatmap_unique)
export(complex_upset_plot)
export(complex_vlnplot_multiple)
export(complex_vlnplot_single)
export(convert_geneid)
@@ -28,7 +29,6 @@ export(order_gene_down)
export(order_gene_up)
export(plot_circlize)
export(plot_qpcr)
export(plot_upset)
export(prepare_circlize_data)
export(run_correlation)
export(transform_coordinates)
+1 −2
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@
#' @return A ComplexHeatmap object or/and a gene list
#' @export

complex_heatmap_group<-function(
complex_heatmap_unique<-function(
  seu_obj,
  celltype,
  group,
@@ -28,7 +28,6 @@ complex_heatmap_group<-function(
cell1<-subset(seu_obj, idents=celltype)
cell1<-SetIdent(cell1, value = group)
group_levels<-levels(seu_obj@meta.data[,group])
seu_obj@meta.data[,group]<-gsub("_","-",seu_obj@meta.data[,group])
if (is.null(group_levels)){
  seu_obj@meta.data[,group] <-factor(seu_obj@meta.data[,group], levels = names(table(seu_obj@meta.data[,group])))
}
+90 −58
Original line number Diff line number Diff line
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Functions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' UpSet plot for the cell markers
#' UpSet plot visualize the number of unique and shared DEGs across group 
#'
#' This function takes the marker gene table as input and visualize the genes that 
#' are unique to a particular cell type or shared by multiple cell types. 
#' @param deg_table The dataframe from Seurat::FindAllMarkers function.
#' @param celltypes The cell types of interest
#' @param textscale Scale of the text font size
#' @param pointsize The size of the dots in the matrix
#' @param nintersects The number of intersections showing in the main bar
#' @param alpha The transparency of the bars
#' This function takes Seurat object as input and visualize the genes that 
#' are unique to a particular group or shared by multiple group. 
#' @param seu_obj A complete Seurat object.
#' @param celltype The cell type to analyze.
#' @param group Group factor in meta data.
#' @param logfc Log fold change to select the DEGs
#' @param min_size Minimal number of observations in an intersection for it to be included
#' @return An UpSet plot
#' @export
plot_upset<-function(
  deg_table, 
  celltypes = NULL, 
  nintersects = NULL,
  textscale = 1.5,
  pointsize = 2,
  alpha = 0.3

complex_upset_plot<-function(
  seu_obj, 
  celltype, 
  group,
  logfc = 0.5,
  min_size = 1
){
    fullcelltypes<-as.character(unique(deg_table$cluster))
    if(is.null(celltypes)){
      celltypes = fullcelltypes
  cell1<-subset(seu_obj, idents=celltype)
  cell1<-SetIdent(cell1, value = group)
  group_levels<-levels(seu_obj@meta.data[,group])
  if (is.null(group_levels)){
    seu_obj@meta.data[,group] <-factor(seu_obj@meta.data[,group], levels = names(table(seu_obj@meta.data[,group])))
    group_levels<-levels(seu_obj@meta.data[,group])
  }
    gene_list<-list()
    for(i in 1:length(fullcelltypes)){
      cluster_marker <- deg_table[deg_table$cluster == fullcelltypes[i],]$gene
  levels(cell1)<-group_levels
  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,]
  gene_list1<-list()
  for(i in 1:length(group_levels)){
    cluster_marker <- all_markers1[all_markers1$cluster == group_levels[i],]$gene
    cluster_marker <- data.frame("gene" = cluster_marker)
    cluster_marker$cell1 <- 1
      colnames(cluster_marker)[2] <- fullcelltypes[i]
      gene_list[[i]] <- cluster_marker
    colnames(cluster_marker)[2] <- group_levels[i]
    gene_list1[[i]] <- cluster_marker
  }
  combined_data <- reduce(gene_list, full_join)
  combined_data[is.na(combined_data)] <- 0
  group.color<-scales::hue_pal()(length(celltypes))
  names(group.color)<-fullcelltypes
  group.color<-group.color[celltypes]
  metadata<-data.frame(sets=celltypes)
  metadata$group<-metadata$sets
  combined_data<-combined_data[,celltypes]
  sum_col<-colSums(combined_data)
  sum_col<-sum_col[order(sum_col)]
  group.color2<-rev(as.character(group.color[names(sum_col)]))
  if(is.null(nintersects)){
    nintersects = 2^ncol(combined_data)-1
  combined_data1 <- purrr::reduce(gene_list1, full_join)
  combined_data1[is.na(combined_data1)] <- 0
  gene_list2<-list()
  for(i in 1:length(group_levels)){
    cluster_marker <- all_markers2[all_markers2$cluster == group_levels[i],]$gene
    cluster_marker <- data.frame("gene" = cluster_marker)
    cluster_marker$cell1 <- 1
    colnames(cluster_marker)[2] <- group_levels[i]
    gene_list2[[i]] <- cluster_marker
  }
  upset(combined_data,
        set.metadata = list(data = metadata, 
                            plots = list(list(type = "matrix_rows", 
                                              column = "group", 
                                              colors = group.color, 
                                              alpha = alpha))),
        sets.bar.color = group.color2, 
        point.size = pointsize,
        nsets = length(celltypes),
        nintersects = nintersects,
        text.scale = textscale, 
        main.bar.color=colorRampPalette(RColorBrewer::brewer.pal(9, "Set1"))(nintersects),
        matrix.color= "black",
        mainbar.y.label="Unique or shared DEG",
        sets.x.label="Total DEG"
  combined_data2 <- purrr::reduce(gene_list2, full_join)
  combined_data2[is.na(combined_data2)] <- 0
  combined_data1$Direction<-"Upregulated"
  combined_data2$Direction<-"Downregulated"
  combined_data<-rbind(combined_data1, combined_data2)
  all_genes<-combined_data$gene
  gene_count<-data.frame(table(all_markers$gene))
  colnames(gene_count)[1]<-"gene"
  combined_data<-merge(combined_data, gene_count, by='gene')
  combined_data$type<-ifelse(combined_data$Freq==1, "Unique","Shared")
  main_bar_color<-hue_pal()(length(group_levels))
  metadata<-data.frame(set=group_levels)
  metadata$color_col<-metadata$set
  upset(combined_data, group_levels, 
        base_annotations=list(
          "Unique or shared DEG"=intersection_size(
            counts=T,
            mapping=aes(fill=Direction),
            width=0.7,
            alpha=0.4
          ) + scale_fill_manual(values= c("blue", "orange"))+
            theme_void()+
            theme(legend.position = "top", legend.title = element_blank())
        ),
        set_sizes=(
          upset_set_size(
            geom=geom_bar(
              aes(fill=type, x=group),
              width=0.7
            ),
            position='right'
          )+ scale_fill_manual(values= c("hotpink",'green'))+theme_void()+
            theme(axis.line.x = element_line(colour = "black"),
                  axis.ticks.x =element_line(size = 0.5, color="black") ,
                  axis.ticks.length = unit(.05, "cm"),
                  axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1, size = 8))
        ),
        width_ratio=0.1,
        stripes=upset_stripes(
          geom = geom_point(size=0.1),
          mapping=aes(color=color_col),
          data=metadata
        ),
        name = celltype,
        min_size = min_size
  )
}
 No newline at end of file
+11 −2
Original line number Diff line number Diff line
@@ -105,12 +105,21 @@ iri.integrated$Group2<-plyr::mapvalues(iri.integrated$Group, from = c("Control",
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"))
complex_heatmap_unique(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
### 6. Upset plot to show the unique and shared DEGs across groups.

```
png(filename =  'upset_plot.png', width = 8, height = 4,units = 'in', res = 300)
complex_upset_plot(iri.integrated, celltype = "NewPT2", group = "Group", min_size = 10, logfc=0.5)
dev.off()
```
![alt text](https://github.com/HaojiaWu/Plot1cell/blob/master/data/upset_plot.png) <br />

### 7. Other ploting functions
There are other functions for plotting/data processing in plot1cell. 
```
help(package = plot1cell)
Loading