


.libPaths("/Users/eli/Documents/Tosches_Lab/Seurat5/Seurat5/renv/")

setwd("~/Documents/Tosches_Lab/Tosches_R/")

library(Seurat)


allen.0 <- readRDS("~/Documents/Tosches_Lab/Tosches_R/Allen_TP73_annotate_021125.rds")



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


DimPlot(allen.0)


allen.0@meta.data$TP73_type


Idents(allen.0)<-"TP73_type"



mouse_CR_vs_ETC<-FindMarkers(allen.0, ident.1 = "ms-CR", ident.2 = "ms-ETC")


View(mouse_CR_vs_ETC)

DimPlot(pleuro.0)


pleuro.0@meta.data$TP73_type_allen


Idents(pleuro.0)<-"TP73_type_allen"


pleuro_CR_vs_ETC<-FindMarkers(pleuro.0, ident.1 = "salamander CR", ident.2 = "salamander ETC")


pleuro_CR_vs_ETC$pct.dif<-pleuro_CR_vs_ETC$pct.1 - pleuro_CR_vs_ETC$pct.2

pleuro_CR_vs_ETC$genes<-rownames(pleuro_CR_vs_ETC)


######


Idents(pleuro.0)<-"seurat_clusters"
DimPlot(pleuro.0, label = T)

pleuro_13_vs_6<-FindMarkers(pleuro.0, ident.1 = "13", "6")

pleuro_13_vs_6$genes<-rownames(pleuro_13_vs_6)


View(subset(pleuro_13_vs_6, pleuro_13_vs_6$genes %in% eggnog_mouse_TF$eggnog))

View(pleuro_CR_vs_ETC)


mouse_CR_vs_ETC$pct.dif<-mouse_CR_vs_ETC$pct.1 - mouse_CR_vs_ETC$pct.2

mouse_CR_vs_ETC$genes<-rownames(mouse_CR_vs_ETC)



mouse_ETC_table<-subset(mouse_CR_vs_ETC, mouse_CR_vs_ETC$pct.dif< (-.25) & mouse_CR_vs_ETC$avg_log2FC< (-2) & mouse_CR_vs_ETC$pct.1<.35 & mouse_CR_vs_ETC$p_val_adj<.05)         

pleuro_ETC_table<-subset(pleuro_CR_vs_ETC, pleuro_CR_vs_ETC$pct.dif< (-.25) & pleuro_CR_vs_ETC$avg_log2FC< (-2) & pleuro_CR_vs_ETC$pct.1<.35 & pleuro_CR_vs_ETC$p_val_adj <.05)         





mouse_CR_table<-subset(mouse_CR_vs_ETC, mouse_CR_vs_ETC$pct.dif > .25 & mouse_CR_vs_ETC$avg_log2FC>2 & mouse_CR_vs_ETC$pct.2<.35  & mouse_CR_vs_ETC$p_val_adj<.05)


pleuro_CR_table<-subset(pleuro_CR_vs_ETC, pleuro_CR_vs_ETC$pct.dif > .25 & pleuro_CR_vs_ETC$avg_log2FC>2 & pleuro_CR_vs_ETC$pct.2<.35  & pleuro_CR_vs_ETC$p_val_adj<.05)




library(readr)


Mouse_TF <- read_table("Mus_musculus_TF.txt")




mouse_CR_table_TF<-subset(mouse_CR_table, mouse_CR_table$genes%in%Mouse_TF$Symbol)

pleuro_TF<-toupper((Mouse_TF$Symbol))


pleuro_CR_table_TF<-subset(pleuro_CR_table, pleuro_CR_table$genes%in%pleuro_TF )


intersect(toupper(mouse_CR_table_TF$genes), pleuro_CR_table_TF$genes)





mouse_ETC_table_TF<-subset(mouse_ETC_table, mouse_ETC_table$genes%in%Mouse_TF$Symbol)

pleuro_TF<-toupper((Mouse_TF$Symbol))


pleuro_ETC_table_TF<-subset(pleuro_ETC_table, pleuro_ETC_table$genes%in%pleuro_TF )


intersect(toupper(mouse_ETC_table_TF$genes), pleuro_ETC_table_TF$genes)


mouse_ETC_table_TF

pleuro_ETC_table_TF


VlnPlot(pleuro.0, idents = c("salamander CR", "salamander ETC"), features = c("NFIA","NFIB","NFIX","ZIC1","ZIC2"))


VlnPlot(allen.0, idents = c("ms-CR", "ms-ETC"), features = c("Nfia","Nfib","Nfix","Zic1","Zic2"))



VlnPlot(chick.x, idents = c("0", "4"), features = c("NFIA","NFIB","NFIX","ZIC1","ZIC2"), group.by = "orig.ident")

### make Chick Idents based on integration with mouse allen TP73


integrated_v4<-readRDS("seurat5_Chick_Allen_tp73_int_eggnog_MAT_V2R_v8000_if5000_SCT_OG_SVG_exact_d50_080825_clr0.1.rds")

DimPlot(integrated_v4)

Chicken_mouse_CR<-WhichCells(integrated_v4, idents = "0")

chick.x@meta.data$integration_TP73<-ifelse(rownames(chick.x@meta.data) %in% Chicken_mouse_CR, "Chicken_CR", "NA" )


Idents(chick.x)<-"integration_TP73"
  
VlnPlot(chick.x, idents = c("Chicken_CR"), features = c("NFIA","NFIB","NFIX","ZIC1","ZIC2"))

### Problem for zebrafish NFI genes p much gone from data set (no expression)



int_CR_cells<-WhichCells(integrated_v4, idents = "0")

int_ETC_cells<-WhichCells(integrated_v4, idents = "1")


PD.0@meta.data$TP73_type_fine<-ifelse(rownames(PD.0@meta.data) %in% int_CR_cells, "ZF_CR",
                                      ifelse(rownames(PD.0@meta.data)%in% int_ETC_cells, "ZF_ETC",
                                             "ZF_PV")) 


Idents(PD.0)<-"TP73_type_fine"



ZF_ETC_cells<-WhichCells(PD.0, idents = "ZF_ETC")

PD.x@meta.data$integration_TP73 <-ifelse(rownames(PD.x@meta.data) %in% ZF_ETC_cells, "ZF_ETC", "Other")

Idents(PD.x)<-"integration_TP73"

a<-VlnPlot(allen.0, idents = c("ms-CR", "ms-ETC"), features = c("Nfia","Nfib","Nfix","Zic1","Zic2"), stack = T, flip =T)+theme(legend.position = "none")+theme(axis.text.x=element_blank())

b<-VlnPlot(pleuro.0, idents = c("salamander CR", "salamander ETC"), features = c("NFIA","NFIB","NFIX","ZIC1","ZIC2"), stack = T, flip = T)+theme(legend.position = "none")+theme(axis.text.x=element_blank())
c<-VlnPlot(chick.x, idents = c("Chicken_CR"), features = c("NFIA","NFIB","NFIX","ZIC1","ZIC2"), stack = T, flip = T)+theme(legend.position = "none")+theme(axis.text.x=element_blank())
d<-VlnPlot(PD.x, ,features = c("nfia","lhx1a","nfixb","zic1","zic2a","lhx1a"), stack = T, flip = T )+theme(legend.position = "none")+theme(axis.text.x=element_blank())

plot_grid(a,b,c,d, nrow = 1)



PD.x@meta.data$integration_TP73 <-factor(PD.x@meta.data$integration_TP73, levels = c("ZF_ETC","Other"))
Idents(PD.x)<-"integration_TP73"

F

pdf(paste0("Species_CR_ETC_TFS.pdf"), width=10, height=10)

plot_grid(a,c,b,d, nrow = 1)
dev.off()




e<-VlnPlot(allen.0, idents = c("ms-CR", "ms-ETC"), features = c("Nfia","Nfib","Nfix","Zic1","Zic2"), stack = T, flip =T, cols = c("pink2", "#EEB422"), fill.by = "ident")+theme(legend.position = "none")+theme(axis.text.x=element_blank())+ theme(axis.title = element_blank())

f<-VlnPlot(pleuro.0, idents = c("salamander CR", "salamander ETC"), features = c("NFIA","NFIB","NFIX","ZIC1","ZIC2"), stack = T, flip = T, cols = c("pink2", "#EEB422"), fill.by = "ident")+theme(legend.position = "none")+theme(axis.text.x=element_blank())+ theme(axis.title = element_blank())
g<-VlnPlot(chick.x, idents = c("Chicken_CR"), features = c("NFIA","NFIB","NFIX","ZIC1","ZIC2"), stack = T, flip = T, cols = c("pink2"), fill.by = "ident")+theme(legend.position = "none")+theme(axis.text.x=element_blank())+ theme(axis.title = element_blank())
h<-VlnPlot(PD.x, ,features = c("nfia","lhx1a","nfixb","zic1","zic2a"), stack = T, flip = T , cols = c( "#EEB422","blue"), fill.by = "ident")+theme(legend.position = "none")+theme(axis.text.x=element_blank())+ theme(axis.title = element_blank())

plot_grid(e,g,f,h, nrow = 1)


pdf(paste0("Species_CR_TFS.pdf"), width=15, height=6)

plot_grid(e,g,f,h, nrow = 1)
dev.off()


plot_grid(a,c,b,d,e,g,f,h, nrow = 1, axis = "t" )

eggnog_mouse <- read.table("mus_eggnog_pruned.txt" , header=T)

library(readr)


Mouse_TF <- read_table("Mus_musculus_TF.txt")



View(Mouse_TF)


eggnog_mouse_TF<-subset(eggnog_mouse, eggnog_mouse$mouse %in% Mouse_TF$Symbol)


eggnog_mouse_TF_ETC<-subset(eggnog_mouse_TF,  eggnog_mouse_TF$mouse %in% mouse_ETC_table$genes )




eggnong_pleuro_TF_ETC<-subset(eggnog_mouse_TF,  eggnog_mouse_TF$eggnog %in% pleuro_ETC_table$genes )


intersect(eggnog_mouse_TF_ETC$mouse, eggnong_pleuro_TF_ETC$mouse)



View(subset(pleuro_CR_vs_ETC, pleuro_CR_vs_ETC$genes%in% eggnog_mouse_TF$eggnog))




eggnog_mouse_TF_CR<-subset(eggnog_mouse_TF,  eggnog_mouse_TF$mouse %in% mouse_CR_table$genes )




eggnong_pleuro_TF_CR<-subset(eggnog_mouse_TF,  eggnog_mouse_TF$eggnog %in% pleuro_CR_table$genes )


intersect(eggnog_mouse_TF_CR$mouse, eggnong_pleuro_TF_CR$mouse)



library(dittoSeq)
dittoBarPlot(
  object = allen.0,
  var = "region_of_interest_acronym",
  group.by = "seurat_clusters")

DimPlot(allen.0, group.by = "region_of_interest_acronym", label = T)



VlnPlot(PD.x, features = c("nCount_RNA","nFeature_RNA"), group.by = "orig.ident")

VlnPlot(pleuro.0, features = c("nCount_RNA","nFeature_RNA"), group.by = "orig.ident")
FeaturePlot(allen.0, features = c("Ebf3"))








