---
title: "PFCdev-web"
author: "Cao Lab"
server: shiny
format: 
  dashboard:
    theme: flatly
    logo: https://ZhengTiger.github.io/picx-images-hosting/PFCdev/Logo.3yemp7xp5k.webp
    nav-buttons:
      - icon: github
        href: https://github.com/ZhengTiger/PFC_develop
---
# Home

<p style="font-size: 50px; font-weight: bold; text-align: center;">Spatiotemporal molecular and cellular dynamics of intratelencephalic neurons in mouse prefrontal cortex during postnatal development</p>

<br>

<img src="https://ZhengTiger.github.io/picx-images-hosting/PFCdev/Figure1A.3yer620cdm.webp" style="width: 100%;">

<br>
<br>

<p style="font-size: 30px; font-weight: bold; text-align: left;">
Summary
</p>

<p style="font-size: 20px; text-align: justify;">
In early postnatal brain, the prefrontal cortex (PFC) remains immature and highly plastic, particularly for the intratelencephalic (IT) neurons. However, the spatiotemporal molecular and cellular dynamics of PFC during this period remain poorly characterized. Here, we performed spatiotemporal single-cell RNA analysis on mouse PFC during different postnatal time points and systematically delineated the molecular and cellular dynamics of mouse PFC during early postnatal development, among which IT neurons exhibit most dramatic alterations. Based on these comprehensive spatiotemporal atlases of PFC, we deciphered the time-specific molecular and cellular characteristics during the maturation process of IT neurons in PFC, particularly the dynamic expression programs of genes regulating axon development and synaptic formation, and the risk genes of neurological developmental diseases. Furthermore, we revealed the dynamic neuron-glia interaction patterns and the underlying signaling pathways during early postnatal period. Our study provided a comprehensive resource and important insights for PFC development and PFC-associated neurological diseases.
</p>


<br>

<p style="font-size: 30px; font-weight: bold; text-align: left;">
Interactively exploring our data
</p>

<p style="font-size: 20px; font-weight: bold; text-align: left;">
scRNAseq
</p>

<p style="font-size: 20px; text-align: justify;">
Our scRNAseq data sequenced the PFC of mice at four different stages (P1, P4, P10, Adult). Users can browse the following content through the scRNAseq page:
</p>

- Select dataset: Select datasets containing different cell types
- Select celltype: Select different resolutions to view cell clusters on UMAP
- Select gene: Select different genes to view their expression



<p style="font-size: 20px; font-weight: bold; text-align: left;">
Spatial data
</p>

<p style="font-size: 20px; text-align: justify;">
We collected the whole brain stereo-seq datasets of P1 and Adult mice from [(Han et al., Neuron, 2025)](https://doi.org/10.1016/j.neuron.2025.02.015), extracted and analyzed the PFC brain region. Users can browse the following content through the spatial page:
</p>

- Spatial Clustering: Select different cell subtypes to view their spatial distribution
- Spatial Gene Expression: Select different genes to view their spatial expression


<p style="font-size: 20px; font-weight: bold; text-align: left;">
Download
</p>

<p style="font-size: 20px; text-align: justify;">
Download the raw and processed data from this study.
</p>


```{r}
#| context: setup
#| warning: false
#| message: false

library(ggplot2)
library(Seurat)
library(shiny)
library(rgl)
library(ggdark)
library(viridis)
library(dplyr)

source("R/Palettes.R")

# scrnaseq
seu.downsample <- readRDS('data/seu.all.HVGs.rds')
seu.downsample$orig.ident[seu.downsample$orig.ident == "P0"] <- "P1"
seu.downsample$orig.ident <- factor(seu.downsample$orig.ident,
                                    levels = c("P1","P4","P10","Adult"))
seu.downsample$SubType <- seu.downsample$SubType_v4
seu.downsample$SubType <- factor(seu.downsample$SubType, 
                                 levels = names(col_cluster[["SubType"]]))
seu.downsample$MainType <- factor(seu.downsample$MainType,
                                  levels = names(col_cluster[["MainType"]]))

# spatial
column <- c("x_rotated","y_rotated","Im.L2.3.IT","Im.L4.5.IT","Im.L5.IT","Im.L6.IT","L2.3.IT","L4.5.IT","L5.IT","L6.IT","L5.PT","L5.NP","L6.CT","Lamp5","Pvalb","Sst","Vip","NPC","Astro","Endo","Microglia","Oligo","OPC")
P1_cell2loc <- read.csv("data/P1_cell2location.csv", row.names = 1)
P1_cell2loc <- P1_cell2loc[,column]
P1_cell2loc$Endo <- 0
P1_cell2loc$Oligo <- 0
colnames(P1_cell2loc) <- c("x_rotated","y_rotated",names(col_cluster[["SubType"]]))
P1_cell2loc$SubType <- as.character(apply(P1_cell2loc[,names(col_cluster[["SubType"]])[-18]], 1, function(x){
  names(which.max(x))
}))

P77_cell2loc <- read.csv("data/P77_cell2location.csv", row.names = 1)
P77_cell2loc <- P77_cell2loc[,column]
P77_cell2loc$Endo <- 0
colnames(P77_cell2loc) <- c("x_rotated","y_rotated",names(col_cluster[["SubType"]]))
P77_cell2loc$SubType <- as.character(apply(P77_cell2loc[,names(col_cluster[["SubType"]])[-18]], 1, function(x){
  names(which.max(x))
}))

sp_p1 <- readRDS("data/P1_bin50_PFC.rds")
sp_p77 <- readRDS("data/P77_bin50_PFC.rds")
```





# scRNAseq {scrolling="true"}

## {.sidebar}

```{r}
selectInput('dataset', 'Select dataset', c("All cells","Neurons"), selected = "Neurons")
```

```{r}
selectInput('celltype', 'Select celltype', c("MainType","SubType"), selected = "SubType")
```

```{r}
selectInput('gene', 'Select gene', rownames(seu.downsample), selected = "Cux2")
```



## Column

### Row

#### Column

```{r}
plotOutput('cluster_plot')
```


### Row

#### Column

```{r}
plotOutput('gene_plot')
```


### Row

#### Column

```{r}
plotOutput('vln_plot')
```


```{r}
#| context: server

seu <- reactive({
  if (input$dataset=="Neurons"){
    subset(seu.downsample, 
           cells = colnames(seu.downsample)[seu.downsample$MainType %in%
                                              c("Excitatory", "Inhibitory")])
  }else{
    seu.downsample
  }
})

output$cluster_plot <- renderPlot({
    DimPlot(
      seu(),
      reduction = 'umap',
      group.by = input$celltype,
      split.by = "orig.ident",
      ncol = 4,
      cols = col_cluster[[input$celltype]],
      pt.size = 0.5,
      label = F) +
      theme_bw(base_size = 15) +
      theme(panel.grid = element_blank(), legend.position = "right",
            strip.text = element_text(size = 20),
            strip.background = element_rect(color="white", fill="white",)) +
      coord_fixed() +
      labs(title = "") + 
      guides(color = guide_legend(ncol = 1, override.aes = list(size = 3)))
})


output$gene_plot <- renderPlot({
  FeaturePlot(
    seu(),
    features = input$gene,
    reduction = 'umap',
    split.by = "orig.ident",
    ncol=4,
    order = T,
    pt.size = 0.5
  ) &
    theme_bw(base_size = 15) &
    theme(panel.grid = element_blank(), plot.title = element_text(hjust = 0.5),
          strip.text = element_text(size = 20, face = "bold"),
          legend.position = c(0.1,0.25)) &
    coord_fixed() &
    scale_color_gradientn(colours = c("lightblue3", "lightblue", "white", "red", "red4"), limits=c(0,2), breaks=c(0,2), na.value = "red4")
})


output$vln_plot <- renderPlot({
  data <- data.frame(
    Gene = as.numeric(seu()[["RNA"]]@data[input$gene, ]),
    Cluster = seu()@meta.data[,input$celltype]
  )
  ggplot(data, aes(x=Cluster, y=Gene, fill=Cluster)) +
    geom_violin(scale = "width", adjust=1, trim=T) +
    geom_jitter(size=0.1) +
    scale_fill_manual(values = col_cluster[[input$celltype]]) +
    theme_classic(base_size = 15) +
    theme(legend.position = "none", 
          plot.title = element_text(hjust = 0.5, size = 20, face = "bold.italic"),
          axis.text.x = element_text(angle = 45, hjust = 1)) +
    labs(x="", y="Expression Level", title = input$gene)
})
```



# Spatial {scrolling="true"}

## {.sidebar}

```{r}
selectInput('sp_celltype', 'Select celltype', names(col_cluster[["SubType"]])[-18], selected = "Im L2/3 IT")
```

```{r}
selectInput('sp_gene', 'Select gene', intersect(rownames(sp_p1), rownames(sp_p77)), selected = "Cux2")
```



## Column

### Row

#### Column

```{r}
plotOutput('p1_cluster_plot')
```

#### Column

```{r}
plotOutput('p77_cluster_plot')
```


### Row

#### Column

```{r}
plotOutput('p1_subtype_plot')
```

#### Column

```{r}
plotOutput('p1_geneplot')
```

```{r}
#| context: server

output$p1_geneplot <- renderPlot({
  gene_i <- input$sp_gene
  gene_i_exp <- as.numeric(FetchData(object = sp_p1, vars = gene_i, slot = "counts")[,1])
  data <- data.frame(
    x = sp_p1$x_rotated, y = sp_p1$y_rotated,
    gene = scale(gene_i_exp)
    )
  data$gene[data$gene<0] <- 0
  data$gene[data$gene>3] <- 3
  color <- col_cluster[["gene"]]
  ggplot(data, aes(x=x, y=y, color=gene)) +
    geom_point(size=2) +
    scale_color_gradientn(colours = color, na.value = "lightgray",
                          limits = c(0,3), 
                          breaks = c(0,3)) +
    coord_fixed() +
    theme_void()+
    theme(legend.position = c(0.9,0.2), legend.title = element_blank(),
          plot.title = element_text(hjust = 0.5)) +
    labs(title=paste("P1", gene_i))
})
```


#### Column

```{r}
plotOutput('p77_subtype_plot')
```

#### Column

```{r}
plotOutput('p77_geneplot')
```

```{r}
#| context: server

output$p77_geneplot <- renderPlot({
  gene_i <- input$sp_gene
  gene_i_exp <- as.numeric(FetchData(object = sp_p77, vars = gene_i, slot = "counts")[,1])
  data <- data.frame(
    x = sp_p77$x_rotated, y = sp_p77$y_rotated,
    gene = scale(gene_i_exp)
    )
  data$gene[data$gene<0] <- 0
  data$gene[data$gene>3] <- 3
  color <- col_cluster[["gene"]]
  ggplot(data, aes(x=x, y=y, color=gene)) +
    geom_point(size=0.8) +
    scale_color_gradientn(colours = color, na.value = "lightgray",
                          limits = c(0,3), 
                          breaks = c(0,3)) +
    coord_fixed() +
    theme_void() +
    theme(legend.position = c(0.9,0.2), legend.title = element_blank(),
          plot.title = element_text(hjust = 0.5)) +
    labs(title=paste("P77", gene_i))
})
```



```{r}
#| context: server

output$p1_cluster_plot <- renderPlot({
  data <- P1_cell2loc
  ggplot(data, mapping = aes(x_rotated, y_rotated, color=SubType)) +
    geom_point(size = 2) +
    scale_color_manual(values = col_cluster[["SubType"]]) +
    coord_fixed() +
    #ggdark::dark_theme_void() +
    theme_void() +
    theme(legend.position = "right", plot.title = element_text(hjust = 0.5)) +
    guides(color = guide_legend(title="", ncol = 1, override.aes = list(size = 3))) +
    labs(title="P1 SubType")
})

output$p77_cluster_plot <- renderPlot({
  data <- P77_cell2loc
  ggplot(data, mapping = aes(x_rotated, y_rotated, color=SubType)) +
    geom_point(size = 0.8) +
    scale_color_manual(values = col_cluster[["SubType"]]) +
    coord_fixed() +
    #ggdark::dark_theme_void() +
    theme_void() +
    theme(legend.position = "right", plot.title = element_text(hjust = 0.5)) + 
    guides(color = guide_legend(title="", ncol = 1, override.aes = list(size = 3))) +
    labs(title="P77 SubType")
})

output$p1_subtype_plot <- renderPlot({
  data <- P1_cell2loc
  subtype_i <- input$sp_celltype
  df_i <- data.frame(
    x = data$x_rotated,
    y = data$y_rotated,
    col = data[,subtype_i]
  )
  if (subtype_i %in% names(col_cluster[["SubType"]])[c(16,17,18,19,21)]){
    df_i$col[!data$SubType==subtype_i] <- 0
  }
  df_i$col[df_i$col>1] <- 1
  df_i$col[df_i$col<0.05] <- 0
  low = "#f7f7f7"
  if (length(which(df_i$col>0))==0){
    high = "#f7f7f7"
  }else{
    high = col_cluster[["SubType"]][subtype_i]
  }
  ggplot(df_i, aes(x=x, y=y, color=col)) +
    geom_point(size=2) +
    scale_color_gradient(low = low, high=high,
                         na.value = "#f7f7f7", limits = c(0,max(df_i$col)),
                         breaks = c(0,max(df_i$col))) +
    coord_fixed() +
    theme_void() +
    theme(legend.position = "none",
          plot.title = element_text(hjust = 0.5)) +
    labs(title=paste("P1", subtype_i))
})


output$p77_subtype_plot <- renderPlot({
  data <- P77_cell2loc
  data$`L5 IT`[data$`L5 IT`<0.25] <- 0
  subtype_i <- input$sp_celltype
  df_i <- data.frame(
    x = data$x_rotated,
    y = data$y_rotated,
    col = data[,subtype_i]
  )
  if (subtype_i %in% names(col_cluster[["SubType"]])[c(16,17,18,20,21)]){
    df_i$col[!data$SubType==subtype_i] <- 0
  }
  df_i$col[df_i$col>1] <- 1
  df_i$col[df_i$col<0.05] <- 0
  ggplot(df_i, aes(x=x, y=y, color=col)) +
    geom_point(size=0.8) +
    scale_color_gradient(low = "#f7f7f7", high=col_cluster[["SubType"]][subtype_i],
                         na.value = "#f7f7f7", limits = c(0,max(df_i$col)),
                         breaks = c(0,max(df_i$col))) +
    coord_fixed() +
    theme_void() +
    theme(legend.position = "none",
          plot.title = element_text(hjust = 0.5)) +
    labs(title=paste("P77",subtype_i))
})
```




# Download

<p style="font-size: 20px; text-align: justify;">
The raw single cell RNA-seq data are available from GEO (<a href="https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE298260">GSE298260</a>). The raw stereo-seq data are available from GEO
</p>

<p style="font-size: 20px; text-align: justify;">
The processed data can be downloaded here:</p>

- All cells data: <a href="https://huggingface.co/TigerZheng/PFCdev-data/resolve/main/seu.harmony.rds?download=true">sc_seu.rds</a>
- Spatial data: <a href="https://huggingface.co/TigerZheng/PFCdev-data/resolve/main/P1_bin50_PFC.rds?download=true">p1_seu.rds</a>, <a href="https://huggingface.co/TigerZheng/PFCdev-data/resolve/main/P77_bin50_PFC.rds?download=true">p77_seu.rds</a>


