Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into main
  • Loading branch information
bianjh-cloud committed Dec 9, 2022
2 parents 53add2e + 940e610 commit 63fc3f6
Show file tree
Hide file tree
Showing 12 changed files with 1,181 additions and 0 deletions.
68 changes: 68 additions & 0 deletions R/3D_tSNE.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# This code comes from NIDAP 'Plot 3D TSNE from Seurat Object [scRNA-Seq][CCBR]' code template

#' @title Plot 3D TSNE from Seurat Object and saves as html file
#' @description Returns 3D tSNE plot for seurat object
#' @details This method provides visualization of 3D tSNE plot
#'
#' @param object Seurat-class object
#' @param color.variable Metadata column to use for color
#' @param label.variable Metadata column to use for label
#' @param fileName Filename for saving plot (default is "plot.html")
#' @param npcs Number of PC's (default is 15)
#' @param save.plot Save plot (default is FALSE)
#'
#' @import Seurat
#' @import ggplot2
#' @importFrom plotly as_widget plot_ly
#'
#' @export

tSNE3D <- function(object,
color.variable,
label.variable,
fileName = "plot.html",
save.plot = FALSE,
npcs = 15){

color.variable <- sub("orig_ident","orig.ident",color.variable)
label.variable <- sub("orig_ident","orig.ident",label.variable)
cols=c("blue","green","red","orange","purple4","darkcyan","magenta2",
"darkred","darkorange")

#Run TSNE again to get 3d coordinates:
object <- RunTSNE(object, assay="SCT",
dims=1:npcs,
dim.embed = 3,
seed.use=1)

tsne.coord <- as.data.frame(object@reductions$tsne@cell.embeddings)

tsne.df <- data.frame(TSNE1=tsne.coord$tSNE_1,
TSNE2=tsne.coord$tSNE_2,
TSNE3=tsne.coord$tSNE_3,
colorvar=as.factor(object@meta.data[[color.variable]]),
label = object@meta.data[[label.variable]])

fig <- plot_ly(tsne.df, x = ~TSNE1,
y = ~TSNE2,
z = ~TSNE3,
color = ~colorvar,
colors = cols,
type="scatter3d",
mode="markers",
hoverinfo = 'text',
text = ~label,
size= 4)

legend=TRUE
if(legend==FALSE){
fig <- hide_legend(fig)
}

if(save.plot == TRUE){
htmlwidgets::saveWidget(as_widget(fig), fileName, selfcontained=TRUE)
}

tsne.results <- list("plot" = fig, "data" = tsne.df)
return(tsne.results)
}
65 changes: 65 additions & 0 deletions R/Dotplot_by_Metadata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# This code comes from NIDAP 'Dotplot of Gene Expression by Metadata [scRNA-Seq][CCBR] (79573b27-8a93-4f22-9863-993be1a44fc1): v16' code template

#' @title Dotplot of Gene Expression by Metadata
#' @description This Dotplot plotter plots average gene expression values for a set of genes from an input table. Input table contains a single column for "Genes" and a single column for "Cell Type". The values in the "Cell Type" column should match the values provided in the metadata template (Metadata Category to Plot). The plot will order the genes (x-axis, left to right) and Cell Types (y-axis, top to bottom) in the order in which it appears in the input table. Based on the additional column selected (Sample Column where sample or any other metadata category), it will display a contingency table for the Metadata
#' @details This method provides a visualization plot showing the frequency of positively
#'
#' @param input.dataset
#' @param metadata.category.to.plot
#' @param sample Column that contains sample id
#' @param cell.type Column that contains the cell type information
#' @param genes.column Column that contains gene names
#' @param dot.color Dot color (default is "dark red")

#' @import tidyverse
#' @import Seurat
#' @import cowplot

#' @export
#'
#' @return Dotplot with markers and cell types.

DotplotMet <- function(object,
metadata,
sample.column,
cell.type,
markers,
dot.color = "darkred") {

#Make the metadata match:
metadata.df <- object@meta.data
#colnames(metadata.df) <- gsub("\\.","_",colnames(metadata.df))
Idents(object) <- metadata.df[[metadata]]

#Bring in input genes and custom names
markers <- gsub("[[:space:]]", "", markers)
markers <- markers[!duplicated(markers)]
celltype <- celltype[celltype != ""]
dp <- DotPlot(object, assay="SCT", features=markers,
dot.scale=4,
cols = c("lightgrey", dot.color))
dp
dp$data$id <- factor(dp$data$id, levels=rev(celltype))
dp$data$features.plot <- factor(dp$data$features.plot, levels=markers)

plot <- ggplot(data = dp$data, mapping = aes_string(x = "features.plot", y = "id")) +
geom_point(mapping = aes_string(size = "pct.exp", color = "avg.exp.scaled")) +
#scale_color_gradient2(midpoint=0, low="blue", mid="white",
# high="red", space ="Lab" ) +
theme_cowplot() +
theme(axis.title.x = element_blank(), axis.text.x = element_text(angle = 90)) +
labs(y=metadata)

# Generate Contingency Table for Annotated cell types
sample_column = sub("_",".",sample_column)
cluster_num <- as.data.frame.matrix(table(object@meta.data[[sample_column]],object@meta.data[[metadata]]))
cluster_num %>% rownames_to_column("Samples") -> cluster_num

result.list <- list("plot" = plot, "data" = cluster_num)

return(result.list)
}




Loading

0 comments on commit 63fc3f6

Please sign in to comment.