---
title: "TP73_neuron_Notebook"
output: html_notebook
---

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. 

 

```{r}

library(Seurat)

library(ggplot2)
```

```{r}
##Load the Object with all salamander cells from Deryckere et al 2025
all.dev <- readRDS("all.dev.230525_SCT_allcells_labelsLTadult.rds")
all.dev

```

```{r, fig.height=10}

all.dev <- FindClusters(all.dev, resolution = 4)

sdp<-DotPlot(all.dev, features = c("TP73"))
pw_dot_mx<-sdp$data



```


```{r}
### Take all clusters that have avg.exp.scaled above .1 and above 5% cells expressing TP73 

#pw_dot_mx[order(pw_dot_mx$avg.exp.scaled, decreasing = T),]$id

### Put the clusters in order of TP73 expression for visualization 
all.dev@meta.data$seurat_clusters <-factor(all.dev@meta.data$seurat_clusters, levels = rev(pw_dot_mx[order(pw_dot_mx$avg.exp.scaled, decreasing = T),]$id))


Idents(all.dev)<-"seurat_clusters"
```


```{r, fig.height=20, fig.width=4}
DotPlot(all.dev, features = c("TP73"))    
```


```{r, fig.width=10, fig.height=10}
DimPlot(all.dev, label = T, raster = F, label.size = 3)+NoLegend()+NoAxes()
```
```{r}
### Make subset of clusters that match filtering criteria 
#look at pw_dot_mx for values 

TP73_dev_pos<-subset(all.dev, idents = c("142","62","86","80","74","64") )
#avg.exp.scaled above .1 and above 5% cells expressing TP73 

```


```{r}
all.dev@meta.data$TP73_clusters<-ifelse(rownames(all.dev@meta.data)%in%rownames(TP73_dev_pos@meta.data), as.character(all.dev@meta.data$seurat_clusters), "NA")
```


```{r}
DimPlot(all.dev,shuffle = T,pt.size = .5, group.by = "TP73_clusters", raster = F, cols = c("deeppink2","firebrick","blue3","forestgreen","seagreen2","gold2","grey"))+NoAxes()+ggtitle("")
```

Now take Adult neurons that express Tp73 

```{r}

### Adult neuron object from Woych et al 2022
neurons_final6 <- readRDS("neurons_final6.rds")
```

```{r}
#DimPlot(neurons_final6, label = T)+NoLegend()

#FeaturePlot(neurons_final6, features = c("TP73","NFIX","ASIC4","STK32A"))




sdp<-DotPlot(neurons_final6, features = c("TP73"))



pw_dot_mx<-sdp$data


#View(pw_dot_mx)

```


```{r}


neurons_final6@meta.data$cluster_label <-factor(neurons_final6@meta.data$cluster_label, levels = rev(pw_dot_mx[order(pw_dot_mx$avg.exp.scaled, decreasing = T),]$id)) 


Idents(neurons_final6)<-"cluster_label"

```


```{r, fig.height=25, fig.width=4}

### Take all clusters that have avg.exp.scaled above .1 and above 5% cells expressing TP73 
### Look at pw_dot_mx for values 

DotPlot(neurons_final6, features = "TP73")    

```

```{r}

adult_TP73_neurons<-subset(neurons_final6, idents = c("TEGLU31","DIMEGLU6"))

neurons_final6@meta.data$TP73_clusters<-ifelse(rownames(neurons_final6@meta.data)%in%rownames(adult_TP73_neurons@meta.data), as.character(neurons_final6@meta.data$cluster_label), "NA")


```



```{r}
DimPlot(neurons_final6, group.by ="TP73_clusters", cols = c("red3","grey","blue2") )+NoAxes()+ggtitle("")
```
```{r}
FeaturePlot(neurons_final6, features = "TP73") +NoLegend()+NoAxes()+ggtitle("")
```
```{r}


TP73_dev_pos@meta.data$old_cell_ID<-rownames(TP73_dev_pos@meta.data)
adult_TP73_neurons@meta.data$old_cell_ID<-rownames(adult_TP73_neurons@meta.data)


d_data<-TP73_dev_pos[["RNA"]]$counts


a_data<-adult_TP73_neurons[["RNA"]]$counts



d.meta<-TP73_dev_pos@meta.data
a.meta<-adult_TP73_neurons@meta.data


dev.1<-CreateSeuratObject(counts = d_data, meta.data = d.meta)


adult.1<-CreateSeuratObject(counts = a_data, meta.data = a.meta)




TP73_all<-merge(x=dev.1, y= adult.1)
TP73_all<- JoinLayers(TP73_all,  assay = "RNA")


```



```{r}

TP73_all <- SCTransform(TP73_all, variable.features.n = 6000 ) 
TP73_all <- RunPCA(TP73_all)


nDims=50

TP73_all <- FindNeighbors(TP73_all, dims = 1:nDims )


TP73_all <- FindClusters(TP73_all, resolution = 1)

TP73_all <- RunUMAP(TP73_all, dims = 1:nDims)

TP73_all@meta.data$seurat_clusters <-factor(TP73_all@meta.data$seurat_clusters,
levels = c("21","11","1","13","2","18","10","9","20","23","5","4","3","7","6","19","8","14",
"16","17","15","12","0","22"))
Idents(TP73_all)<-"seurat_clusters"


```

```{r, fig.width=20, fig.height=20}

FeaturePlot(TP73_all, features = c("TP73", "RELN","SLC17A6","CCNO","LHX1","LHX5","RELN","NR2F2","EBF3","CTXN1","NMS","GAD2","NMU"), label = T)

```

```{r, fig.width=15, fig.width=15}
FeaturePlot(TP73_all, features = c("PCNA","KRT5", "SULF1","CLDN5","TP73","SLC17A6","CCNO"), label = T)
```



```{r, fig.height=10, fig.width=8}
DotPlot(TP73_all, features = c("TP73","SLC17A6","NMS","NMU","TBX21","PCNA","SULF1","CLDN5","KRT5"))
```


Take clusters that are SLC17A6 and Tp73 positive 

```{r}

TP73_all_clean<-subset(TP73_all, idents = c("21","11","1","2","18","13"))

```


```{r}



TP73_all_clean<-FindVariableFeatures(TP73_all_clean, nfeatures = 1012)

Idents(TP73_all_clean)<-"orig.ident"

TP73.all.dp<-DotPlot(TP73_all_clean, features = VariableFeatures(TP73_all_clean))


#View(TP73.all.dp$data)

#FeaturePlot(TP73_all_clean, features = c("NPPC"))



TP73.all.dp.df.clean<-subset(TP73.all.dp$data, TP73.all.dp$data$pct.exp>2.5)

#View(TP73.all.dp.df.clean)

var.feat.cust<-TP73.all.dp.df.clean$features.plot

length(var.feat.cust)

TP73_all_clean <- SCTransform(TP73_all_clean, residual.features = var.feat.cust)

TP73_all_clean <- FindNeighbors(TP73_all_clean, dims = 1:20)
TP73_all_clean <- FindClusters(TP73_all_clean, resolution = 1.55)
TP73_all_clean <- RunUMAP(TP73_all_clean, dims = 1:20)

pleuro.1<-TP73_all_clean 

# you have now generated the only TP73+ neuron object
```


```{r}
#some random seed differences lead to slightly different UMAP appearence and cluster composition. For exact replication of results in paper load provided Github object 
#Loaded object also has metadata on CR identity based on integration etc

pleuro.1<-readRDS("~/Documents/Tosches_Lab/Tosches_R/PW_TP73_anotation_060625.rds")



pleuro.1@meta.data$seurat_clusters <-factor(pleuro.1@meta.data$seurat_clusters, levels = c("5","0","13","1","8","6",
"11","7","4","2","12","3","9","10"))

```

```{r}
#pleuro.1@meta.data$rename_TP73<-pleuro.1@meta.data$seurat_clusters

pleuro.1<-RenameIdents(pleuro.1, "5"="im1", "0"="im2","13"="m1.1","1"="m1.2","8"="m1.3","6"="m1.4",
             "11"="m2.1","7"="m2.2","4"="m2.3","2"="m2.4","12"="m2.5","3"="m2.6","9"="m2.7","10"="m2.8")
colors <- c("#EE00EE","red","brown4","#FFA500",
            "#FFD602","#8A7500","#00BFFF","#009ACD","#1E90FF","#1874CD","#00FF00","#00CD00","#008B00","#7CCD7C"
            )
DimPlot(pleuro.1, cols = colors, label = T )

```


This object contains various annotations used in the paper. for example the TP73_type_allen meta data reflects
which cluster the cells integrate with in the Allen TP73 data set. 
```{r}
DimPlot(pleuro.1, group.by = "TP73_type_allen", label = T)
```

