3D UMAP R分析代码

# LOAD LIBRARIES ####
# Restart Rstudio or R
# Run the following code once you have Seurat installed

library(ggplot2)
library(cowplot)
library(Matrix)
library(ggridges)
library(ggrepel)
library(dplyr)
library(Seurat)
library(monocle)
library(plotly)

# CONFIRM CORRECT INSTALL ####
# Confirm package version of Seurat and Monocle

packageVersion("Seurat")
packageVersion("monocle")
packageVersion("plotly")

# 3D PLOTTING ####
# Construct a dataframe using data from your pre-clustered Seurat v3.0.0 object
# Here ‘seurat_clusters’ is list of numeric cluster identities, you can find it here: yourseuratobject[[“seurat_cluster”]],
# or yourseuratobject$seurat_clusters, where ‘yourseuratobject’ is a Seurat object created with Seurat v3.0.0

yourseuratobject <- pancreas.integrated

# Re-run UMAP s that you have accurate calculations for all tSNE(s)

yourseuratobject <- RunUMAP(yourseuratobject,
dims = 1:10,
n.components = 3L)

# Extract UMAP information from Seurat Object

umap_1 <- yourseuratobject[["umap"]]@cell.embeddings[,1]
umap_2 <- yourseuratobject[["umap"]]@cell.embeddings[,2]
umap_3 <- yourseuratobject[["umap"]]@cell.embeddings[,3]

# Visualize what headings are called so that you can extract them to form a dataframe

Embeddings(object = yourseuratobject, reduction = "umap")

# Prepare a dataframe for cell plotting

plotting.data <- FetchData(object = yourseuratobject, vars = c("UMAP_1", "UMAP_2", "UMAP_3", "seurat_clusters"))

# Make a column of row name identities (these will be your cell/barcode names)

plotting.data$label <- paste(rownames(plotting.data))

# Plot your data, in this example my Seurat object had 21 clusters (0-20)

plot_ly(data = plotting.data,
x = ~UMAP_1, y = ~UMAP_2, z = ~UMAP_3,
color = ~seurat_clusters,
colors = c("darkgreen",
"red",
"sienna3",
"mediumseagreen",
"turquoise4",
"black",
"royalblue1",
"yellow4",
"gray30",
"darkred",
"orange2",
"darkgreen",
"darkmagenta",
"deeppink2"
),
type = "scatter3d",
mode = "markers",
marker = list(size = 3, width=2), # controls size of points
text=~label, #This is that extra column we made earlier for which we will use
hoverinfo="text") #When you visualize your plotly object, hovering your mouse pointer over a point shows cell names

# Say you wanto make a gene-expression 3D plot, where you can plot gene expression against a color scale
# Here using the same seurat object as above, we extract gene expression information for beta-actin ‘ACTB’
# In our paper, we concentrate on SCT normalized data, or log normalized RNA NOT raw counts.
# In addition if you want, you may look at normalised-RNA, SCT or integrated slots, to look at gene expression

DefaultAssay(object = yourseuratobject)
DefaultAssay(object = yourseuratobject) <- "SCT"
DefaultAssay(object = yourseuratobject) <- "RNA"
DefaultAssay(object = yourseuratobject) <- "integrated"

# create a dataframe

plotting.data <- FetchData(object = yourseuratobject, vars = c("UMAP_1", "UMAP_2", "UMAP_3", "THY1"), slot = 'data')

# Say you want change the scale, so that every cell having an expression >1 will be one color
# Basically, you are re-adjusting the scale here, so that any cell having a certain expression
# will light up on your 3D plot

# First make another column in your dataframe, where all values above 1 are re-assigned a value of 1
# This information is stored in the ‘changed’ column of your dataframe

plotting.data$Expr. <- ifelse(test = plotting.data$THY1 <2, yes = plotting.data$THY1, no = 2)

# Add the label column, so that now the column has ‘cellname-its expression value’

plotting.data$label <- paste(rownames(plotting.data)," - ", plotting.data$THY1, sep="")

# Plot your data, in this example my Seurat object had 21 clusters (0-20), and cells express a gene called ACTB

plot_ly(data = plotting.data,
x = ~UMAP_1, y = ~UMAP_2, z = ~UMAP_3,
color = ~Expr., # you can just run this against the column for the gene as well using ~ACTB
opacity = .5,
colors = c('darkgreen', 'red'),
type = "scatter3d",
mode = "markers",
marker = list(size = 3, width=2),
text=~label,
hoverinfo="text"
)

################## #
################## #
# CODE END ####

如若转载,请注明出处:https://www.ouq.net/3d-umap-r%e5%88%86%e6%9e%90%e4%bb%a3%e7%a0%81.html

(0)
打赏 微信打赏,为服务器增加50M流量 微信打赏,为服务器增加50M流量 支付宝打赏,为服务器增加50M流量 支付宝打赏,为服务器增加50M流量
上一篇 06/18/2021
下一篇 06/25/2021

相关推荐