Commit bda08849 authored by HaojiaWu's avatar HaojiaWu
Browse files

updates

parent 0e1ba8b2
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
# Generated by roxygen2: do not edit by hand

export(Install.example)
export(add_sliding_windows)
export(add_track)
export(cell_order)
export(change_strip_background)
@@ -22,9 +21,6 @@ export(get_metadata)
export(get_segment)
export(mk_color_table)
export(mk_marker_ct)
export(modified_coverageplot)
export(modified_dimplot)
export(modified_dotplot)
export(order_gene_down)
export(order_gene_up)
export(plot_circlize)

R/modified_coverageplot.R

deleted100644 → 0
+0 −117
Original line number Diff line number Diff line
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Functions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' Modified CoveragePlot
#'
#' This function is a modified version of the CoveragePlot in Signac that allows gene name input and group splitting
#'
#' @param sig_obj A complete Signac object
#' @param gene Gene name
#' @param slide_windows_up Upper range of the slide window
#' @param slide_windows_down Lower range of the slide window
#' @param split_by Group ID in metadata to split peaks
#' @return A ggplot object
#' @export
modified_coverageplot <- function(
  sig_obj, 
  gene, 
  slide_windows_up, 
  slide_windows_down,
  split_by
){
  celltypes<-levels(sig_obj)
  DefaultAssay(sig_obj)<-'peaks'
  sig_obj@meta.data$disease<-sig_obj@meta.data[,split_by]
  sig_obj@meta.data$celltype<-sig_obj@active.ident
  sig_obj@meta.data$id2<-paste(sig_obj@meta.data$celltype,sig_obj@meta.data$disease,  sep = "_")
  sig_obj<- SetIdent(sig_obj, value = 'id2')
  p2 <- Signac::CoveragePlot(
    object = sig_obj,
    region = gene,
    annotation = T,
    peaks = T, 
    extend.upstream = slide_windows_up,
    extend.downstream = slide_windows_down
  )
  gene.ranges <- genes(EnsDb.Hsapiens.v86)
  seqlevelsStyle(gene.ranges) <- 'UCSC'
  gene.ranges <- keepStandardChromosomes(gene.ranges, pruning.mode = 'coarse')
  region2 <- GRangesToString(subset(gene.ranges, symbol==gene))
  if(region2=='--') {
    next
  }
  if(length(region2)>1){
    region2<-region2[1]
  }
  region2<-add_sliding_windows(region = region2, up_stream = slide_windows_up, down_stream = slide_windows_down)
  xlabel<-p2$patches$plots[1][[1]]$labels$x
  ylabel<-p2$patches$plots[1][[1]]$labels$y
  data_plot<-p2$patches$plots[1][[1]]$data
  data_plot$celltype<-gsub("_[^_]*$", "", data_plot$group )
  data_plot$disease<-gsub(".*_", "", data_plot$group )
  data_plot$celltype <- factor(data_plot$celltype, levels = celltypes)
  a1<-Signac:::PeakPlot(sig_obj,region = region2)
  a2<-p2$patches$plots[2][[1]]
  group_id<-names(table(data_plot$disease))
  a3 <- ggplot(
    data = data_plot[data_plot$disease==group_id[1],],
    mapping = aes(x = position, y = coverage, fill = celltype)
  ) +
    geom_area(stat = "identity") +
    geom_hline(yintercept = 0, size = 0.1) +
    xlab("")+ylab(as_string(ylabel))+ggtitle(group_id[1])+
    theme_browser(legend = FALSE) +
    facet_grid(celltype ~.)+
    theme(
      panel.spacing.y = unit(x = 0, units = "line"),
      plot.title = element_text(size = 14,hjust = 0.5, face = 'bold'),
      strip.text.y = element_text(angle = 0),
      axis.ticks.x = element_blank(),
      axis.text.x = element_blank(),
      axis.line.x = element_blank())
  
  a4 <- ggplot(
    data = data_plot[data_plot$disease==group_id[2],],
    mapping = aes(x = position, y = coverage, fill = celltype)
  ) +
    geom_area(stat = "identity") +
    geom_hline(yintercept = 0, size = 0.1) +
    xlab("")+
    ylab("")+
    ggtitle(group_id[2])+
    theme_browser(legend = FALSE) +
    facet_grid(celltype ~.)+
    theme(
      plot.title = element_text(size = 14,hjust = 0.5, face = 'bold'),
      strip.text.y = element_blank(),
      axis.ticks = element_blank(),
      axis.text = element_blank(),
      axis.line = element_blank())
  a5 <- a2 + ylab("")
  a6 <- a1 + ylab("")
  p3 <- a3 + a2 + a1  + plot_layout(heights = c(6,1,1))
  p4 <- a4 + a5 + a6  + plot_layout(heights = c(6,1,1))
  plot_grid(p3, p4, ncol = 2, rel_widths = c(2.4, 2))
}



#' Add extended windows for coverage plot
#'
#' This function is to add an extended window for ploting
#'
#' @param region Gene region for plotting
#' @param up_stream Upper range of the slide window
#' @param down_stream Lower range of the slide window
#' @return Gene range for plot
#' @export
add_sliding_windows <- function(
  region,
  up_stream, 
  down_stream
){
  reg_list<-strsplit(region, split = "-")
  reg_list<-as.character(unlist(reg_list))
  new_reg<-paste(reg_list[1], as.integer(reg_list[2])-down_stream, as.integer(reg_list[3])+up_stream, sep = "-")
  new_reg
}
 No newline at end of file

R/modified_seurat_plot.R

deleted100644 → 0
+0 −90
Original line number Diff line number Diff line
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Functions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' Modified DotPlot
#'
#' This function is a modified version of the DotPlot function in Seurat
#'
#' @param seu_obj A complete Seurat object
#' @param features A vector of gene names.
#' @return A ggplot object
#' @export
modified_dotplot <- function(
  seu_obj, 
  features, 
  col_palette = NULL,
  scale.by='radius'
){
  levels(seu_obj) <- rev(levels(seu_obj))
  dataplot<-DotPlot(seu_obj, features = features)
  dataplot<-dataplot$data
  dataplot$avg.exp<-dataplot$avg.exp.scaled
  colnames(dataplot)[1:2]<-c('Avg.Exp', 'Pct.Exp')
  if(is.null(col_palette)){
    col_palette = colorRampPalette(c('grey80','lemonchiffon1','indianred1','darkred'))(255)
  }
  scale.func <- switch(
    EXPR = scale.by,
    'size' = scale_size,
    'radius' = scale_radius,
    stop("'scale.by' must be either 'size' or 'radius'")
  )
  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)+
    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 + scale.func(range = c(0, 10), limits = c(0, 20))
  }
  dotplot
}

#' Modified DimPlot
#'
#' This function is a modified version of the DimPlot function in Seurat
#'
#' @param seu_obj A complete Seurat object
#' @param colors Colors to label the clusters
#' @param pt.size Size of the data points
#' @param label.box Whether or not to label the cell type name
#' @param label.size Font size of the labels
#' @param do.reple Whether or not to repel the cluster annotation
#' @param title Main title of the plot
#' @return A ggplot object
#' @export
modified_dimplot <- function(
  seu_obj,
  colors = NULL,
  pt.size = 0.5,
  label.box = T,
  label.size = 6,
  do.repel=F,
  title = "UMAP plot"
){
  if(is.null(colors)){
    colors = scales::hue_pal()(length(levels(seu_obj)))
  }
  x0<-min(seu_obj@reductions$umap@cell.embeddings[,1])
  x1<-x0+(max(seu_obj@reductions$umap@cell.embeddings[,1])-min(seu_obj@reductions$umap@cell.embeddings[,1]))/8
  y0<-min(seu_obj@reductions$umap@cell.embeddings[,2])
  y1<-y0+(max(seu_obj@reductions$umap@cell.embeddings[,2])-min(seu_obj@reductions$umap@cell.embeddings[,2]))/8
  DimPlot(seu_obj, label = T, cols = colors,label.box = label.box, 
          label.size = label.size, pt.size = pt.size, raster = F, repel = do.repel)+NoLegend()+NoAxes()+
    geom_segment(aes(x=x0, xend = x1 , y=y0, yend = y0), size=0.8,
                 arrow = arrow(length = unit(0.2,"cm"))) +
    geom_segment(aes(x=x0, xend = x0 , y=y0, yend = y1), size=0.8,
                 arrow = arrow(length = unit(0.2,"cm"))) +
    xlab("UMAP_1")+theme(axis.title.x = element_text(hjust = 0.05, size = 12))+
    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))
}

+1 −1
Original line number Diff line number Diff line
# plot1cell: a package for advanced single cell data visualization

This package allows users to visualize the single cell data on the R objects or output files generated by the popular tools such as Seurat. It is currently under active development. 
This package allows users to visualize the single cell data on the R object or output files generated by Seurat. It is currently under active development. 

## Installation
plot1cell R package can be easily installed from Github using devtools. Please make sure you have installed <a href="https://satijalab.org/seurat/">Seurat 4.0</a>, <a href="https://github.com/jokergoo/circlize">circlize</a> and <a href="https://github.com/jokergoo/ComplexHeatmap">ComplexHeatmap</a> packages.

man/add_sliding_windows.Rd

deleted100644 → 0
+0 −21
Original line number Diff line number Diff line
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/modified_coverageplot.R
\name{add_sliding_windows}
\alias{add_sliding_windows}
\title{Add extended windows for coverage plot}
\usage{
add_sliding_windows(region, up_stream, down_stream)
}
\arguments{
\item{region}{Gene region for plotting}

\item{up_stream}{Upper range of the slide window}

\item{down_stream}{Lower range of the slide window}
}
\value{
Gene range for plot
}
\description{
This function is to add an extended window for ploting
}
Loading