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

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

library(Seurat)



mouse.0   <- readRDS("FMH_Neuron_mouse_develop_clean_2000-_1CLR_072624_annotate_V2.rds")



DimPlot(mouse.0)


mouse.0@meta.data$r_anno



Idents(mouse.0)<-"r_anno"


Lamanno_CR_markers<-FindMarkers(mouse.0, "Mouse Cajal-Retzius")


View(Lamanno_CR_markers)

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


Lamanno_CR_markers$gene.name<-rownames(Lamanno_CR_markers)



saveRDS(Lamanno_CR_markers, file = "Lamanno_CR_markers.rds")


Lamanno_CR_markers<-readRDS("Lamanno_CR_markers.rds")

Lamanno_CR_markers_lfc_2_pctd_0.2<-subset(Lamanno_CR_markers, Lamanno_CR_markers$avg_log2FC>2 & Lamanno_CR_markers$pct.dif>.2)



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


eggnong_mouse_CR_up<-subset(eggnog_mouse, eggnog_mouse$mouse%in%Lamanno_CR_markers_lfc_2_pctd_0.2$gene.name)


eggnong_mouse_CR_up_VF<-subset(eggnong_mouse_CR_up, eggnong_mouse_CR_up$eggnog %in% VariableFeatures(pleuro.0))


CR_module_genes<-eggnong_mouse_CR_up_VF$eggnog


AddModuleScore(pleuro.0, features = CR_module_genes, name = "LM_CR_MOD")

################





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

Lamanno_CR_markers<-readRDS("Lamanno_CR_markers.rds")
DimPlot(pleuro.0)


pleuro.0@meta.data$tr_anno



DimPlot(pleuro.0, group.by = "tr_anno", label = T)


FeaturePlot(pleuro.0, features = "NFIX")



Lamanno_CR_markers_lfc_2_pctd_0.2<-subset(Lamanno_CR_markers, Lamanno_CR_markers$avg_log2FC>2 & Lamanno_CR_markers$pct.dif>.2)






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


eggnong_mouse_CR_up<-subset(eggnog_mouse, eggnog_mouse$mouse%in%Lamanno_CR_markers_lfc_2_pctd_0.2$gene.name)


eggnong_mouse_CR_up_F<-subset(eggnong_mouse_CR_up, eggnong_mouse_CR_up$eggnog %in% Features(pleuro.0))


CR_module_genes<-eggnong_mouse_CR_up_F$eggnog


pleuro.0<-AddModuleScore(pleuro.0, features = list(CR_module_genes), name = "LM_CR_MOD2_")

Idents(pleuro.0)<-"tr_anno"

pleuro.0<-subset(pleuro.0, idents = c("Pleuro Other Diencephalon", "Pleuro Other Telencephalon"), invert = T)

FeaturePlot(pleuro.0, features ="LM_CR_MOD2_1")


VlnPlot(pleuro.0, features ="LM_CR_MOD2_1", group.by = "tr_anno",  pt.size = 0)


p<-DotPlot(pleuro.0, features = "LM_CR_MOD2_1")

LM_CR_MOD_2_1<-p$data$avg.exp



p_df<-data.frame(LM_CR_MOD_2_1, row.names = p$data$id)



paletteLength <- 100
myColor <- colorRampPalette(c("darkblue", "white","darkred"))(paletteLength)
# length(breaks) == length(paletteLength) + 1
# use floor and ceiling to deal with even/odd length pallettelengths
myBreaks <- c(seq(-max(p_df$LM_CR_MOD_2_1), 0, length.out=ceiling(paletteLength/2) + 1), 
              seq(max(p_df$LM_CR_MOD_2_1)/paletteLength, max(p_df$LM_CR_MOD_2_1), length.out=floor(paletteLength/2)))



pdf("Lamanno_module_score_HM_110225.pdf", width = 5, height = 5)
pheatmap(p_df, cluster_rows = F, cluster_cols = F,  color = myColor, breaks = myBreaks, cellwidth = 25, cellheight = 25 )
dev.off()




View(p$data)

library(SeuratExtend)


list(c("PW_TP73_Neuron","Pleuro GABAergic Diencephalon"),c("PW_TP73_Neuron","Pleuro GABAergic Telencephalon"),c("PW_TP73_Neuron","Pleuro Glutamatergic Diencephalon"), c("PW_TP73_Neuron","Pleuro Glutamatergic Telencephalon"),c("PW_TP73_Neuron","Pleuro Other Diencephalon"),
     c("PW_TP73_Neuron","Pleuro Other Telencephalon"))


VlnPlot2(pleuro.0, features = "LM_CR_MOD2_1", group.by = "tr_anno", 
         stat.method = "t.test", 
         #comparisons = list(c("PW_TP73_Neuron","Pleuro GABAergic Diencephalon"),
                        #    c("PW_TP73_Neuron","Pleuro GABAergic Telencephalon"),
                         #   c("PW_TP73_Neuron","Pleuro Glutamatergic Diencephalon"),
                          #  c("PW_TP73_Neuron","Pleuro Glutamatergic Telencephalon"),
                           # c("PW_TP73_Neuron","Pleuro Other Diencephalon"),
                                                                                                                   
                            # c("PW_TP73_Neuron","Pleuro Other Telencephalon")), 
         p.adjust.method = "bonferroni",
         label = "p.adj",
         hide.ns = TRUE)



VlnPlot2(pleuro.0, features = "LM_CR_MOD2_1", group.by = "tr_anno", pt=FALSE, show.mean=TRUE, box=TRUE)
 

    







ZF_eggnog<-readRDS("ZF_eggnog_fix_020525.rds")



ZF_pandley <- readRDS("~/Documents/Tosches_Lab/Tosches_R/ZF_SOs/ZF_pandley_anno_MAT_ident_dev_021015.rds")

Idents(ZF_pandley)<-"pandley_anno"

ZF_pandley@meta.data$tr_anno_fix<-ifelse(rownames(ZF_pandley@meta.data) %in% WhichCells(ZF_pandley, idents="Pallium_09"), "ZF_Diencephalon", as.character(ZF_pandley@meta.data$tr_anno) )


DimPlot(ZF_pandley, group.by = "tr_anno_fix")

options(future.globals.maxSize = 60000 * 1024^2)

ZF_pandley <- SCTransform(ZF_pandley, variable.features.n = 3000, conserve.memory = T , vars.to.regress = "Stage")
ZF_pandley <- RunPCA(ZF_pandley, features = VariableFeatures(object = ZF_pandley))
ZF_pandley <- FindNeighbors(ZF_pandley, dims = 1:35)
ZF_pandley <- FindClusters(ZF_pandley, resolution = 1)
ZF_pandley <- RunUMAP(ZF_pandley, dims = 1:35)





DimPlot(ZF_pandley, group.by = "Stage")




Lamanno_CR_markers_lfc_2_pctd_0.2<-subset(Lamanno_CR_markers, Lamanno_CR_markers$avg_log2FC>2 & Lamanno_CR_markers$pct.dif>.2)






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


eggnong_mouse_CR_up<-subset(eggnog_mouse, eggnog_mouse$mouse%in%Lamanno_CR_markers_lfc_2_pctd_0.2$gene.name)




eggnong_ZF_CR_up<-subset(ZF_eggnog, ZF_eggnog$eggnog %in% eggnong_mouse_CR_up$eggnog)


eggnong_ZF_CR_up_F<-subset(eggnong_ZF_CR_up, eggnong_ZF_CR_up$ZF %in% Features(ZF_pandley))


CR_module_genes_ZF<-eggnong_ZF_CR_up_F$ZF


ZF_pandley<-AddModuleScore(ZF_pandley, features = list(CR_module_genes_ZF), name = "LM_CR_MOD2_")



Idents(ZF_pandley)<-"tr_anno_fix"


p<-DotPlot(ZF_pandley, features = "LM_CR_MOD2_1")



LM_CR_MOD_2_1<-p$data$avg.exp



p_df<-data.frame(LM_CR_MOD_2_1, row.names = p$data$id)

custom_order<-c("ZF_OB","ZF_Pallium","ZF_Subpallium","ZF_Diencephalon")

p_df_value<-p_df[match(custom_order, rownames(p_df)),]

p_df2<-data.frame(p_df_value, row.names = custom_order)






paletteLength <- 100
myColor <- colorRampPalette(c("darkblue", "white","darkred"))(paletteLength)
# length(breaks) == length(paletteLength) + 1
# use floor and ceiling to deal with even/odd length pallettelengths
myBreaks <- c(seq(-max(p_df$LM_CR_MOD_2_1), 0, length.out=ceiling(paletteLength/2) + 1), 
              seq(max(p_df$LM_CR_MOD_2_1)/paletteLength, max(p_df$LM_CR_MOD_2_1), length.out=floor(paletteLength/2)))


library(pheatmap)

pdf("TP73_module_score_ZF_HM_021226.pdf", width = 5, height = 5)
pheatmap(p_df2, cluster_rows = F, cluster_cols = F,  color = myColor, breaks = myBreaks, cellwidth = 25, cellheight = 25 )
dev.off()



#pheatmap(p_df2, cluster_rows = F, cluster_cols = F,  color = colorRampPalette(c("white", "steelblue"))(100) )



library(ggplot2)
FeaturePlot(ZF_pandley, features = "LM_CR_MOD2_1")+scale_color_viridis_c()


VlnPlot(ZF_pandley, features ="LM_CR_MOD2_1", group.by = "tr_anno",  pt.size = 0)






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

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

library(Seurat)

ZF_pandley <- readRDS("~/Documents/Tosches_Lab/Tosches_R/ZF_SOs/ZF_pandley_anno_MAT_ident_dev_021015.rds")

Idents(ZF_pandley)<-"pandley_anno"

ZF_pandley@meta.data$tr_anno_fix<-ifelse(rownames(ZF_pandley@meta.data) %in% WhichCells(ZF_pandley, idents="Pallium_09"), "ZF_Diencephalon", as.character(ZF_pandley@meta.data$tr_anno) )


DimPlot(ZF_pandley, group.by = "tr_anno_fix")

options(future.globals.maxSize = 60000 * 1024^2)

ZF_pandley <- SCTransform(ZF_pandley, variable.features.n = 3000, conserve.memory = T , vars.to.regress = "Stage")
ZF_pandley <- RunPCA(ZF_pandley, features = VariableFeatures(object = ZF_pandley))
ZF_pandley <- FindNeighbors(ZF_pandley, dims = 1:35)
ZF_pandley <- FindClusters(ZF_pandley, resolution = 1)
ZF_pandley <- RunUMAP(ZF_pandley, dims = 1:35)



PD.0<-ZF_pandley

rm(ZF_pandley)

PD.0@meta.data$tr_anno_fix<-factor(PD.0@meta.data$tr_anno_fix, levels = c("ZF_OB","ZF_Pallium","ZF_Subpallium","ZF_Diencephalon"))


#Idents(pleuro.0)<-"tr_anno_fix"
Idents(PD.0)<-"tr_anno_fix"


DimPlot(PD.0, group.by = "tr_anno_fix")


PD.de <- FindAllMarkers(PD.0, max.cells.per.ident = 600, logfc.threshold = .75)
PD.de <- PD.de[PD.de$p_val_adj<0.01,]
PD.de.genes <- unique(PD.de$gene)





DefaultAssay(PD.0)<-"RNA"

PD.0<- NormalizeData(
  PD.0,
  assay = "RNA",
  normalization.method = "LogNormalize",
  scale.factor = 10000,
  margin = 1,
  verbose = TRUE)


ExpressionTablePD<-AverageExpression(PD.0, group.by = "tr_anno_fix", assays = "RNA")

rownames(ExpressionTablePD$RNA)

colnames(ExpressionTablePD$RNA)


mouse.0 <- readRDS("FB_Neuron_mouse_develop_clean_4000-_1CLR_022026_annotate_V3_fix_022026.rds")






mouse.0@meta.data$tr_anno<-factor(mouse.0@meta.data$tr_anno, levels = c("Mouse Cajal-Retzius","Mouse Glutamatergic Telencephalon", 
                                                                        "Mouse GABAergic Telencephalon",
                                                                        "Mouse Glutamatergic Diencephalon","Mouse GABAergic Diencephalon"))


mouse.0 <- SCTransform(mouse.0, variable.features.n = 4000, vars.to.regress =c("percent.mt", "CellCycle", "Age"), conserve.memory = T )
mouse.0 <- RunPCA(mouse.0, features = VariableFeatures(object = mouse.0))
mouse.0 <- FindNeighbors(mouse.0, dims = 1:35)
mouse.0 <- FindClusters(mouse.0, resolution = 1)
mouse.0 <- RunUMAP(mouse.0, dims = 1:35)




Idents(mouse.0)<-"tr_anno"

DimPlot(mouse.0)

table(mouse.0@meta.data$tr_anno)

#mouse.0<-subset(mouse.0, idents = "Mouse Other Telencephalon", invert=T )

mouse.de <- FindAllMarkers(mouse.0, max.cells.per.ident = 600, logfc.threshold = .75)
mouse.de <- mouse.de[mouse.de$p_val_adj<0.01,]
mouse.de.genes <- unique(mouse.de$gene)





DefaultAssay(mouse.0)<-"RNA"

mouse.0<- NormalizeData(
  mouse.0,
  assay = "RNA",
  normalization.method = "LogNormalize",
  scale.factor = 10000,
  margin = 1,
  verbose = TRUE)


ExpressionTableMouse<-AverageExpression(mouse.0, group.by = "tr_anno", assays = "RNA")




CorrComparePlot(ExpressionTableSpecies1 = data.frame(ExpressionTablePD$RNA),   DEgenesSpecies1 = PD.de.genes,
                ExpressionTableSpecies2 = data.frame(ExpressionTableMouse$RNA) ,
                DEgenesSpecies2 = mouse.de.genes,
                Species1 = "ZF", Species2 = "mouse",
                filename = "Pandley_Lamanno_int_tr_030726", Height=12, Width=12)                             





#######




#Chicken




