install from private guthub
#set config
usethis::use_git_config(user.name = "Rlhoove1", user.email = "rlhoove1@asu.edu")
#usethis::create_github_token()
credentials::set_github_pat('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')
library('devtools')
devtools::install_github('plaisier-lab/scRegNet/scRegNet', force = FALSE)
library(scRegNet)
library(igraph)
library(tidyverse)
library(ggraph)
library(dplyr)
## Set variables
species = 'mouse'
gene_id = 'ensembl'
## Load data
seurat_obj = readRDS('endocrinogenesis/pancreas_normalizedSeuratObj_allGenesEnsemblID_ccAFv2.rds')
beta_traj = read.csv('endocrinogenesis/pancreas_trajectoryOrder_Beta.csv', row.names=1, header=T)
traj = rownames(beta_traj)[beta_traj[,1]]
degs = read.csv('endocrinogenesis/pancreas_SCT_markerGenes_ensemblIDs.csv', row.names=1, header=T)
marker_genes = degs[degs[,'cluster']=='Beta' & degs[,'avg_log2FC']>=1.5 & degs[,'p_val_adj']<=0.05,'gene']
## Collect expression data
expressed_genes = row.names(seurat_obj@assays[['SCT']]@data)
m = seurat_obj@assays[['SCT']]@scale.data
exprs_matrix = m[, traj]
## Test mechanistic function
mech_tfs = scRegNet::mechanistic(marker_genes, expressed_genes, species, gene_id)
Loading up data for anlaysis...
Number of genes used for comparisons: 19697
Conduct TF target gene enrichment analysis...
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|=== | 3%
|
|=== | 4%
|
|==== | 4%
|
|==== | 5%
|
|===== | 5%
|
|===== | 6%
|
|====== | 6%
|
|====== | 7%
|
|======= | 7%
|
|======= | 8%
|
|======== | 8%
|
|======== | 9%
|
|========= | 9%
|
|========= | 10%
|
|========== | 10%
|
|========== | 11%
|
|========== | 12%
|
|=========== | 12%
|
|=========== | 13%
|
|============ | 13%
|
|============ | 14%
|
|============= | 14%
|
|============= | 15%
|
|============== | 15%
|
|============== | 16%
|
|=============== | 16%
|
|=============== | 17%
|
|================ | 17%
|
|================ | 18%
|
|================= | 18%
|
|================= | 19%
|
|================== | 19%
|
|================== | 20%
|
|=================== | 20%
|
|=================== | 21%
|
|==================== | 21%
|
|==================== | 22%
|
|===================== | 23%
|
|===================== | 24%
|
|====================== | 24%
|
|====================== | 25%
|
|======================= | 25%
|
|======================= | 26%
|
|======================== | 26%
|
|======================== | 27%
|
|========================= | 27%
|
|========================= | 28%
|
|========================== | 28%
|
|========================== | 29%
|
|=========================== | 29%
|
|=========================== | 30%
|
|============================ | 30%
|
|============================ | 31%
|
|============================= | 31%
|
|============================= | 32%
|
|============================== | 32%
|
|============================== | 33%
|
|=============================== | 34%
|
|=============================== | 35%
|
|================================ | 35%
|
|================================ | 36%
|
|================================= | 36%
|
|================================= | 37%
|
|================================== | 37%
|
|================================== | 38%
|
|=================================== | 38%
|
|=================================== | 39%
|
|==================================== | 39%
|
|==================================== | 40%
|
|===================================== | 40%
|
|===================================== | 41%
|
|====================================== | 41%
|
|====================================== | 42%
|
|======================================= | 42%
|
|======================================= | 43%
|
|======================================== | 43%
|
|======================================== | 44%
|
|========================================= | 45%
|
|========================================= | 46%
|
|========================================== | 46%
|
|========================================== | 47%
|
|=========================================== | 47%
|
|=========================================== | 48%
|
|============================================ | 48%
|
|============================================ | 49%
|
|============================================= | 49%
|
|============================================= | 50%
|
|============================================== | 50%
|
|============================================== | 51%
|
|=============================================== | 51%
|
|=============================================== | 52%
|
|================================================ | 52%
|
|================================================ | 53%
|
|================================================= | 53%
|
|================================================= | 54%
|
|================================================== | 54%
|
|================================================== | 55%
|
|=================================================== | 56%
|
|=================================================== | 57%
|
|==================================================== | 57%
|
|==================================================== | 58%
|
|===================================================== | 58%
|
|===================================================== | 59%
|
|====================================================== | 59%
|
|====================================================== | 60%
|
|======================================================= | 60%
|
|======================================================= | 61%
|
|======================================================== | 61%
|
|======================================================== | 62%
|
|========================================================= | 62%
|
|========================================================= | 63%
|
|========================================================== | 63%
|
|========================================================== | 64%
|
|=========================================================== | 64%
|
|=========================================================== | 65%
|
|============================================================ | 65%
|
|============================================================ | 66%
|
|============================================================= | 67%
|
|============================================================= | 68%
|
|============================================================== | 68%
|
|============================================================== | 69%
|
|=============================================================== | 69%
|
|=============================================================== | 70%
|
|================================================================ | 70%
|
|================================================================ | 71%
|
|================================================================= | 71%
|
|================================================================= | 72%
|
|================================================================== | 72%
|
|================================================================== | 73%
|
|=================================================================== | 73%
|
|=================================================================== | 74%
|
|==================================================================== | 74%
|
|==================================================================== | 75%
|
|===================================================================== | 75%
|
|===================================================================== | 76%
|
|====================================================================== | 76%
|
|====================================================================== | 77%
|
|======================================================================= | 78%
|
|======================================================================= | 79%
|
|======================================================================== | 79%
|
|======================================================================== | 80%
|
|========================================================================= | 80%
|
|========================================================================= | 81%
|
|========================================================================== | 81%
|
|========================================================================== | 82%
|
|=========================================================================== | 82%
|
|=========================================================================== | 83%
|
|============================================================================ | 83%
|
|============================================================================ | 84%
|
|============================================================================= | 84%
|
|============================================================================= | 85%
|
|============================================================================== | 85%
|
|============================================================================== | 86%
|
|=============================================================================== | 86%
|
|=============================================================================== | 87%
|
|================================================================================ | 87%
|
|================================================================================ | 88%
|
|================================================================================= | 88%
|
|================================================================================= | 89%
|
|================================================================================= | 90%
|
|================================================================================== | 90%
|
|================================================================================== | 91%
|
|=================================================================================== | 91%
|
|=================================================================================== | 92%
|
|==================================================================================== | 92%
|
|==================================================================================== | 93%
|
|===================================================================================== | 93%
|
|===================================================================================== | 94%
|
|====================================================================================== | 94%
|
|====================================================================================== | 95%
|
|======================================================================================= | 95%
|
|======================================================================================= | 96%
|
|======================================================================================== | 96%
|
|======================================================================================== | 97%
|
|========================================================================================= | 97%
|
|========================================================================================= | 98%
|
|========================================================================================== | 98%
|
|========================================================================================== | 99%
|
|===========================================================================================| 99%
|
|===========================================================================================| 100%
Adding multiple hypothesis correction...
Done.
write.csv(mech_tfs, 'enrichment_endocrinogenesis.csv')
#mech_tfs = read.csv('enrichment_endocrinogenesis_fixed.csv', header=T, row.names=1)
## Test associate_with_expression
associations = scRegNet::associate_with_expression(exprs_matrix, mech_tfs, species=species, gene_id=gene_id, method=c('pearson'))
Compare TF expression versus enriched marker eigengene...
Run mode: pearson
Error in txtProgressBar(min = 0, max = length(unlist(sig_mech_tfs)), initial = 0, :
must have 'max' > 'min'
library(Seurat)
DimPlot(seurat_obj, reduction = "umap", group.by = "clusters", label = TRUE)
# UMAP plot colored by velocity pseudotime
FeaturePlot(seurat_obj, features = "velocity_pseudotime", reduction = "umap")
library(Seurat)
FeaturePlot(seurat_obj, features = "ENSMUSG00000004366", reduction ="umap")
network graph practice
df <- data.frame(animal=c('koala', 'armadillo', 'sloth', 'sloth','panda','rori'),
country=c('Australia', 'USA', 'Peru', 'Peru', 'China', 'USA'),
fruit=c("leaves apples limes", "leaves arugala berries",
"grass roots apples","roots lemons limes",
"bamboo berries leaves","berries coffee arugala"))
df <- df %>%
separate_rows(fruit, sep = " ")
print(df)
edges <- data.frame(
from = c(df$country, df$animal),
to = c(df$animal, df$fruit)
)
nodes <- data.frame(
name = unique(c(edges$from, edges$to))
)
nodes$type <- ifelse(nodes$name %in% df$country, "country",
ifelse(nodes$name %in% df$animal, "animal", "fruit"))
graph <- graph_from_data_frame(d=edges, vertices=nodes, directed=TRUE)
ggraph(graph, layout="auto") +
geom_edge_link(aes(start_cap = label_rect(node1.name), end_cap = label_rect(node2.name)),
arrow = arrow(length = unit(4, 'mm')), end_cap = circle(3, 'mm')) +
geom_node_point(aes(color=type, size=type)) +
geom_node_text(aes(label=name), repel=TRUE, size=5) +
theme_void() +
scale_color_manual(values=c("country"="red", "animal"="blue", "fruit"="green")) +
scale_size_manual(values=c("country"=6, "animal"=5, "fruit"=4)) +
ggtitle("super sleepers")
Using "sugiyama" as default layout
df <- df[df$fruit == "berries", ]
edges <- data.frame(
from = c(df$country, df$animal),
to = c(df$animal, df$fruit))
nodes <- data.frame(
name = unique(c(edges$from, edges$to)))
nodes$type <- ifelse(nodes$name %in% df$country, "country",
ifelse(nodes$name %in% df$animal, "animal", "fruit"))
graph <- graph_from_data_frame(d=edges, vertices=nodes, directed=TRUE)
ggraph(graph, layout="auto") +
geom_edge_link(aes(start_cap = label_rect(node1.name), end_cap = label_rect(node2.name)),
arrow = arrow(length = unit(4, 'mm')), end_cap = circle(3, 'mm')) +
geom_node_point(aes(color=type, size=type)) +
geom_node_text(aes(label=name), repel=TRUE, size=5) +
theme_void() +
scale_color_manual(values=c("country"="red", "animal"="blue", "fruit"="green")) +
scale_size_manual(values=c("country"=6, "animal"=5, "fruit"=4)) +
ggtitle("who eats berries?")
Using "sugiyama" as default layout
#function to plot TFs acting on one gene
gene ="ENSMUSG00000022265"
gene_to_TF <- function(input, gene, ly="stress", rank=TRUE) {
df<- input
df <- input %>%
separate_rows(TF_targets, sep = " ")
df2 <- input %>%
separate_rows(TF_targets_symbol, sep = " ")
df$TF_targets_symbol <-df2$TF_targets_symbol
df$TF_symbol_Motif_Pair <- paste(df$TF_symbol, "(", df$Motif, ")", sep = "")
input<- df
df <- df[df$TF_targets_symbol == gene,]
if(length(df$Motif) == 0){
df<- input
df <- df[df$TF_targets == gene,]
}
edges <- data.frame(
from = gene,
to = df$TF_symbol_Motif_Pair)
n = unique(c(gene, edges$to))
nodes <- data.frame(name = n,
type = ifelse(n %in% df$TF_symbol_Motif_Pair, "TF Motif Pair", "gene"))
if (rank) {
max_of = max(df$PC1_var_exp)
nodes$size <- c(max_of, df$PC1_var_exp)
}
else {
nodes$size <- rep(1, length(nodes$name))
}
outs <- ggraph(graph_from_data_frame(d = edges, vertices = nodes, directed = TRUE), layout = ly) +
geom_edge_link(aes(start_cap = label_rect(node1.name), end_cap = label_rect(node2.name)),
arrow = arrow(length = unit(2, 'mm')), end_cap = circle(3, 'mm')) +
geom_node_point(aes(color = type, size = size)) +
geom_node_text(aes(label = c('', name[2:length(name)])), repel = TRUE, size = 3) +
theme_void() +
scale_color_manual(values = c("TF" = "red", "gene" = "blue")) +
ggtitle(paste0('TFS for ', nodes$name[1]))
show(outs)
return(df$TF_symbol_Motif_Pair)
}
its nice wwhen they look like this
gene_to_TF(associations, "Arxes1", ly="stress", rank=TRUE)
Error: object 'associations' not found
but most look like this, how can we fix this problem
gene_to_TF(associations,"ENSMUSG00000022265", ly="stress", rank=TRUE)
Error: object 'associations' not found
#function to plot motifs related to a TF
TF_to_Motif <- function(input, tf, ly="stress", rank=FALSE) {
df<- input
df <- df[df$TF_symbol == tf,]
if(length(df$Motif) == 0){
df<- input
df <- df[df$TF == tf,]
}
edges <- data.frame(
from = tf,
to = df$Motif)
n = c(tf, edges$to)
nodes <- data.frame(name = n,
type = ifelse(n %in% df$Motif, "Motif", "TF"))
if (rank) {
max_of = max(df$PC1_var_exp)
nodes$size <- c(max_of, df$PC1_var_exp)
}
else {
nodes$size <- rep(1, length(nodes$name))
}
outs <- ggraph(graph_from_data_frame(d = edges, vertices = nodes, directed = TRUE), layout = ly) +
geom_edge_link(aes(start_cap = label_rect(node1.name), end_cap = label_rect(node2.name)),
arrow = arrow(length = unit(2, 'mm')), end_cap = circle(3, 'mm')) +
geom_node_point(aes(color = type, size = size)) +
geom_node_text(aes(label = c('', name[2:length(name)])), repel = TRUE, size = 3) +
theme_void() +
scale_color_manual(values = c("Motif" = "red", "TF" = "blue")) +
ggtitle(paste0('Motifs for ', nodes$name[1]))
show(outs)
return(df$Motif)
}
TF_to_Motif( associations, "Bcl6b")
[1] "Bcl6b.2" "V_BCL6B_04_M02844"
plot_expression_worse <- function(seurat_obj, gene, assay="RNA"){
DefaultAssay(seurat_obj) <- assay
df <- FetchData(seurat_obj, vars = gene)
df$Cluster <- seurat_obj@meta.data$clusters
df$Pseudotime <- seurat_obj@meta.data$velocity_pseudotime
colnames(df)[colnames(df) == gene] <- "Expression"
ggplot(df, aes(x = Pseudotime, y = Expression, color = Cluster)) +
geom_point(alpha = 0.5) +
labs(title = paste("Expression of", gene, "over Pseudotime"),
x = "Pseudotime",
y = "Expression Level",
color = "Cluster") +
theme_minimal()
}
plot_expression_worse(seurat_obj, gene="ENSMUSG00000054667", assay="SCT")
library(dplyr)
df <- seurat_obj@meta.data %>%
select(clusters, velocity_pseudotime)
pseudotime_ranges <- df %>%
group_by(clusters) %>%
summarise(
min_pseudotime = min(velocity_pseudotime),
max_pseudotime = max(velocity_pseudotime),
mean_pseudotime = mean(velocity_pseudotime)
)
print(pseudotime_ranges)
ggplot(df, aes(x = velocity_pseudotime, y = as.factor(clusters))) +
geom_boxplot() +
labs(x = "Velocity Pseudotime", y = "Clusters", title = "Pseudotime Distribution by Cluster") +
theme_minimal()
plot_expression <- function(seurat_obj, gene, assay="RNA", celltype ){
DefaultAssay(seurat_obj) <- assay
df <- FetchData(seurat_obj, vars = gene)
df$Cluster <- seurat_obj@meta.data$clusters
df$Pseudotime <- seurat_obj@meta.data$velocity_pseudotime
colnames(df)[colnames(df) == gene] <- "Expression"
if (celltype == "Epsilon") {
df <- df[!df$Cluster %in% c("Beta", "Alpha", "Delta"), ]
} else if (celltype == "Beta") {
df <- df[!df$Cluster %in% c("Epsilon", "Alpha", "Delta"), ]
} else if (celltype == "Alpha") {
df <- df[!df$Cluster %in% c("Epsilon", "Beta", "Delta"), ]
} else if (celltype == "Delta") {
df <- df[!df$Cluster %in% c("Epsilon", "Alpha", "Beta"), ]
}
cluster_colors <- c(
"Alpha" = "darkmagenta",
"Beta" = "cyan2",
"Delta" = "coral2",
"Ductal" = "cornflowerblue",
"Epsilon" = "deeppink",
"Ngn3 high EP" = "darkgoldenrod2",
"Ngn3 low EP" = "chartreuse",
"Pre-endocrine" = "hotpink2"
)
ggplot(df, aes(x = Pseudotime, y = Expression, color = Cluster)) +
geom_point(alpha = 0.5) +
scale_color_manual(values = cluster_colors) +
labs(title = paste( gene ),
x = "Pseudotime",
y = "Expression Level",
color = "Cluster") +
theme_minimal()+
theme(plot.title = element_text(size = 10))
}
if you put in nonsense it still works but you cant put in nothing for celltype
plot_expression(seurat_obj, gene="ENSMUSG00000056758", celltype = "duck", assay="SCT") #Hmg2a
library(patchwork)
p1 <- plot_expression(seurat_obj, gene="ENSMUSG00000064177", celltype = "Epsilon", assay="SCT") #Ghrl
p2 <- plot_expression(seurat_obj, gene="ENSMUSG00000000215", celltype = "Beta", assay="SCT") #Ins2
p3 <- plot_expression(seurat_obj, gene="ENSMUSG00000004366", celltype = "Delta", assay="SCT") #Sst
p4 <- plot_expression(seurat_obj, gene="ENSMUSG00000000394", celltype = "Alpha", assay="SCT") #Gcg
(p1 | p2) /
(p3 | p4)