From 8482bc0d71b339326884332e7882dc63086d13e5 Mon Sep 17 00:00:00 2001
From: "Gervaise H. Henry" <gervaise.henry@utsouthwestern.edu>
Date: Fri, 13 Jul 2018 11:42:27 -0500
Subject: [PATCH] Fix DPrF analysis to use correct CCA code and make separate
 r.scripts for 2 and 3 patient aggregation

---
 ...apper_RUN.D.R => sc-TissueMapper_RUN.D2.R} |  37 +-
 r.scripts/sc-TissueMapper_RUN.D3.R            | 485 ++++++++++++++++++
 2 files changed, 504 insertions(+), 18 deletions(-)
 rename r.scripts/{sc-TissueMapper_RUN.D.R => sc-TissueMapper_RUN.D2.R} (95%)
 create mode 100644 r.scripts/sc-TissueMapper_RUN.D3.R

diff --git a/r.scripts/sc-TissueMapper_RUN.D.R b/r.scripts/sc-TissueMapper_RUN.D2.R
similarity index 95%
rename from r.scripts/sc-TissueMapper_RUN.D.R
rename to r.scripts/sc-TissueMapper_RUN.D2.R
index 77c9d94..012c428 100644
--- a/r.scripts/sc-TissueMapper_RUN.D.R
+++ b/r.scripts/sc-TissueMapper_RUN.D2.R
@@ -100,12 +100,12 @@ option_list=list(
   make_option("--st",action="store",default="TRUE",type='logical',help="Remove stressed cells?"),
   make_option("--stg",action="store",default="dws",type='character',help="Geneset to use for stress ID"),
   #make_option("--hpc.poststress",action="store",default=0.85,type='numeric',help="Max variance cutoff for PCs to use, post-stress"),
-  make_option("--res.poststress",action="store",default=0.2,type='numeric',help="Resolution to cluster, post-stress"),
-  make_option("--ds",action="store",default=0,type='integer',help="Number of cells to downsample"),
+  make_option("--res.poststress",action="store",default=0.1,type='numeric',help="Resolution to cluster, post-stress"),
+  make_option("--ds",action="store",default=50000,type='integer',help="Number of cells to downsample"),
   #make_option("--hpc.epi",action="store",default=0.85,type='numeric',help="Max variance cutoff for PCs to use, Epi"),
-  make_option("--res.epi",action="store",default=0.2,type='numeric',help="Resolution to cluster, Epi"),
+  make_option("--res.epi",action="store",default=0.1,type='numeric',help="Resolution to cluster, Epi"),
   #make_option("--hpc.st",action="store",default=0.85,type='numeric',help="Max variance cutoff for PCs to use, St"),
-  make_option("--res.st",action="store",default=0.2,type='numeric',help="Resolution to cluster, St")
+  make_option("--res.st",action="store",default=0.1,type='numeric',help="Resolution to cluster, St")
   )
 opt=parse_args(OptionParser(option_list=option_list))
 rm(option_list)
@@ -154,13 +154,14 @@ rm(results)
 rm(sc10x.D17)
 rm(sc10x.D27)
 
-results <- scPC(sc10x,lx=opt$lx,hx=opt$hx,ly=opt$ly,cc=opt$cc,pc=opt$pc,hpc=opt$hpc,file="pre.stress",cca=TRUE)
-sc10x <- results[[1]]
-genes.hvg <- results[[2]]
-pc.use <- results[[3]]
-rm(results)
+#results <- scPC(sc10x,lx=opt$lx,hx=opt$hx,ly=opt$ly,cc=opt$cc,pc=opt$pc,hpc=opt$hpc,file="pre.stress",cca=TRUE)
+#sc10x <- results[[1]]
+#genes.hvg <- results[[2]]
+#pc.use <- results[[3]]
+#rm(results)
 
-sc10x <- scCluster(sc10x,pc.use=pc.use,res.use=opt$res.prestress,folder="pre.stress")
+pc.use <- 25
+sc10x <- scCluster(sc10x,pc.use=pc.use,res.use=opt$res.prestress,folder="pre.stress",red="cca.aligned")
 
 if (opt$st==TRUE){
   results <- scStress(sc10x,stg=opt$stg,res.use=opt$res.prestress,pc.use=pc.use)
@@ -169,13 +170,13 @@ if (opt$st==TRUE){
   sc10x.Stress <- results[[3]]
   rm(results)
 
-  results <- scPC(sc10x,lx=opt$lx,hx=opt$hx,ly=opt$ly,cc=opt$cc,pc=opt$pc,hpc=opt$hpc,file="post.stress")
-  sc10x <- results[[1]]
-  genes.hvg.poststress <- results[[2]]
-  pc.use.poststress <- results[[3]]
-  rm(results)
+  #results <- scPC(sc10x,lx=opt$lx,hx=opt$hx,ly=opt$ly,cc=opt$cc,pc=opt$pc,hpc=opt$hpc,file="post.stress")
+  #sc10x <- results[[1]]
+  #genes.hvg.poststress <- results[[2]]
+  #pc.use.poststress <- results[[3]]
+  #rm(results)
   
-  sc10x <- scCluster(sc10x,pc.use=pc.use.poststress,res.use=opt$res.poststress,folder="post.stress")
+  sc10x <- scCluster(sc10x,pc.use=pc.use,res.use=opt$res.poststress,folder="post.stress",red="cca.aligned")
 }
 
 gene.set1 <- read_delim("./genesets/genes.deg.St.csv",",",escape_double=FALSE,trim_ws=TRUE,col_names=TRUE)
@@ -203,7 +204,7 @@ if (any(levels(sc10x@ident)=="Unknown")){
   sc10x.St <- scSubset(sc10x,i="Lin",g="St")
 }
 
-sc10x.Epi <- scCluster(sc10x.Epi,pc.use=pc.use.poststress,res.use=opt$res.epi,folder="epi")
+sc10x.Epi <- scCluster(sc10x.Epi,pc.use=pc.use,res.use=opt$res.epi,folder="epi",red="cca.aligned")
 
 gene.set1 <- read_delim("./genesets/genes.deg.BE.csv",",",escape_double=FALSE,trim_ws=TRUE,col_names=TRUE)
 gene.set1 <- gene.set1[1]
@@ -252,7 +253,7 @@ rm(gene.set1)
 results.cor.Epi.lgea <- scQuSAGEsm(sc10x.Epi,gs=gene.set,ds=opt$ds,nm="Epi.dws.sc",folder="lgea")
 rm(gene.set)
 
-sc10x.St <- scCluster(sc10x.St,pc.use=pc.use.poststress,res.use=opt$res.epi,folder="st")
+sc10x.St <- scCluster(sc10x.St,pc.use=pc.use,res.use=opt$res.epi,folder="st",red="cca.aligned")
 
 gene.set1 <- read_delim("./genesets/genes.deg.Fib.csv",",",escape_double=FALSE,trim_ws=TRUE,col_names=TRUE)
 gene.set1 <- gene.set1[1]
diff --git a/r.scripts/sc-TissueMapper_RUN.D3.R b/r.scripts/sc-TissueMapper_RUN.D3.R
new file mode 100644
index 0000000..40beb6b
--- /dev/null
+++ b/r.scripts/sc-TissueMapper_RUN.D3.R
@@ -0,0 +1,485 @@
+gc()
+library(methods)
+library(optparse)
+library(Seurat)
+library(readr)
+library(fBasics)
+library(pastecs)
+library(qusage)
+library(RColorBrewer)
+
+source("../r.scripts/sc-TissueMapper.R")
+
+#Create folder structure
+setwd("../")
+if (!dir.exists("./analysis")){
+  dir.create("./analysis")
+}
+if (!dir.exists("./analysis/qc")){
+  dir.create("./analysis/qc")
+}
+if (!dir.exists("./analysis/qc/cc")){
+  dir.create("./analysis/qc/cc")
+}
+if (!dir.exists("./analysis/tSNE")){
+  dir.create("./analysis/tSNE")
+}
+if (!dir.exists("./analysis/tSNE/pre.stress")){
+  dir.create("./analysis/tSNE/pre.stress")
+}
+if (!dir.exists("./analysis/pca")){
+  dir.create("./analysis/pca")
+}
+if (!dir.exists("./analysis/pca/stress")){
+  dir.create("./analysis/pca/stress")
+}
+if (!dir.exists("./analysis/violin")){
+  dir.create("./analysis/violin")
+}
+if (!dir.exists("./analysis/violin/stress")){
+  dir.create("./analysis/violin/stress")
+}
+if (!dir.exists("./analysis/table")){
+  dir.create("./analysis/table")
+}
+if (!dir.exists("./analysis/tSNE/post.stress")){
+  dir.create("./analysis/tSNE/post.stress")
+}
+if (!dir.exists("./analysis/cor")){
+  dir.create("./analysis/cor")
+}
+if (!dir.exists("./analysis/tSNE/lin")){
+  dir.create("./analysis/tSNE/lin")
+}
+if (!dir.exists("./analysis/tSNE/epi")){
+  dir.create("./analysis/tSNE/epi")
+}
+if (!dir.exists("./analysis/tSNE/st")){
+  dir.create("./analysis/tSNE/st")
+}
+if (!dir.exists("./analysis/tSNE/merge")){
+  dir.create("./analysis/tSNE/merge")
+}
+if (!dir.exists("./analysis/pca/ne")){
+  dir.create("./analysis/pca/ne")
+}
+if (!dir.exists("./analysis/tSNE/ne")){
+  dir.create("./analysis/tSNE/ne")
+}
+if (!dir.exists("./analysis/violin/ne")){
+  dir.create("./analysis/violin/ne")
+}
+if (!dir.exists("./analysis/tSNE/FINAL")){
+  dir.create("./analysis/tSNE/FINAL")
+}
+if (!dir.exists("./analysis/deg")){
+  dir.create("./analysis/deg")
+}
+if (!dir.exists("./analysis/cca")){
+  dir.create("./analysis/cca")
+}
+if (!dir.exists("./analysis/diy")){
+  dir.create("./analysis/diy")
+}
+
+#Retrieve command-line options
+option_list=list(
+  make_option("--p",action="store",default="DPrF",type='character',help="Project Name"),
+  make_option("--g",action="store",default="ALL",type='character',help="Group To analyze"),
+  make_option("--lg",action="store",default=500,type='integer',help="Threshold for cells with minimum genes"),
+  make_option("--hg",action="store",default=2500,type='integer',help="Threshold for cells with maximum genes"),
+  make_option("--lm",action="store",default=0,type='numeric',help="Threshold for cells with minimum %mito genes"),
+  make_option("--hm",action="store",default=0.1,type='numeric',help="Threshold for cells with maximum %mito genes"),
+  make_option("--lx",action="store",default=0.0125,type='numeric',help="x low threshold for hvg selection"),
+  make_option("--hx",action="store",default=3.5,type='numeric',help="x high threshold for hvg selection"),
+  make_option("--ly",action="store",default=0.5,type='numeric',help="y low threshold for hvg selection"),
+  make_option("--cc",action="store",default=TRUE,type='logical',help="Scale cell cycle?"),
+  make_option("--pc",action="store",default=25,type='integer',help="Number of PCs to cacluate"),
+  make_option("--hpc",action="store",default=0.85,type='numeric',help="Max variance cutoff for PCs to use, pre-stress"),
+  make_option("--res.prestress",action="store",default=1,type='numeric',help="Resolution to cluster, pre-stress"),
+  make_option("--st",action="store",default="TRUE",type='logical',help="Remove stressed cells?"),
+  make_option("--stg",action="store",default="dws",type='character',help="Geneset to use for stress ID"),
+  #make_option("--hpc.poststress",action="store",default=0.85,type='numeric',help="Max variance cutoff for PCs to use, post-stress"),
+  make_option("--res.poststress",action="store",default=0.1,type='numeric',help="Resolution to cluster, post-stress"),
+  make_option("--ds",action="store",default=50000,type='integer',help="Number of cells to downsample"),
+  #make_option("--hpc.epi",action="store",default=0.85,type='numeric',help="Max variance cutoff for PCs to use, Epi"),
+  make_option("--res.epi",action="store",default=0.1,type='numeric',help="Resolution to cluster, Epi"),
+  #make_option("--hpc.st",action="store",default=0.85,type='numeric',help="Max variance cutoff for PCs to use, St"),
+  make_option("--res.st",action="store",default=0.1,type='numeric',help="Resolution to cluster, St")
+  )
+opt=parse_args(OptionParser(option_list=option_list))
+rm(option_list)
+if (opt$lm==0){opt$lm=-Inf}
+
+sc10x.D17 <- scLoad("D17PrF",sub=TRUE)
+sc10x.D27 <- scLoad("D27PrF",sub=TRUE)
+sc10x.D35 <- scLoad("D35PrF",sub=TRUE)
+
+if (opt$cc==TRUE){
+  results <- scCellCycle(sc10x.D17)
+  sc10x.D17 <- results[[1]]
+  genes.s <- results[[2]]
+  genes.g2m <- results[[3]]
+  rm(results)
+  results <- scCellCycle(sc10x.D27)
+  sc10x.D27 <- results[[1]]
+  genes.s <- results[[2]]
+  genes.g2m <- results[[3]]
+  rm(results)
+  results <- scCellCycle(sc10x.D35)
+  sc10x.D35 <- results[[1]]
+  genes.s <- results[[2]]
+  genes.g2m <- results[[3]]
+  rm(results)
+} else {
+  genes.s=""
+  genes.g2m=""
+}
+
+results <- scQC(sc10x.D17,lg=opt$lg,hg=opt$hg,lm=opt$lm,hm=opt$hm)
+sc10x.D17 <- results[[1]]
+counts.cell.raw.D17 <- results[[2]]
+counts.gene.raw.D17 <- results[[3]]
+counts.cell.filtered.D17 <- results[[4]]
+counts.gene.filtered.D17 <- results[[5]]
+rm(results)
+
+results <- scQC(sc10x.D27,lg=opt$lg,hg=opt$hg,lm=opt$lm,hm=opt$hm)
+sc10x.D27 <- results[[1]]
+counts.cell.raw.D27 <- results[[2]]
+counts.gene.raw.D27 <- results[[3]]
+counts.cell.filtered.D27 <- results[[4]]
+counts.gene.filtered.D27 <- results[[5]]
+rm(results)
+
+results <- scQC(sc10x.D35,lg=opt$lg,hg=opt$hg,lm=opt$lm,hm=opt$hm)
+sc10x.D35 <- results[[1]]
+counts.cell.raw.D35 <- results[[2]]
+counts.gene.raw.D35 <- results[[3]]
+counts.cell.filtered.D35 <- results[[4]]
+counts.gene.filtered.D35 <- results[[5]]
+rm(results)
+
+results <- sc3CCA(sc10x.D17,sc10x.D27,sc10x.D35,nm.1="D17",nm.2="D27",nm.3="D35",cc=opt$cc)
+sc10x <- results[[1]]
+genes.hvg.cca <- results[[2]]
+rm(results)
+
+rm(sc10x.D17)
+rm(sc10x.D27)
+rm(sc10x.D35)
+
+#results <- scPC(sc10x,lx=opt$lx,hx=opt$hx,ly=opt$ly,cc=opt$cc,pc=opt$pc,hpc=opt$hpc,file="pre.stress",cca=TRUE)
+#sc10x <- results[[1]]
+#genes.hvg <- results[[2]]
+#pc.use <- results[[3]]
+#rm(results)
+
+pc.use <- 25
+sc10x <- scCluster(sc10x,pc.use=pc.use,res.use=opt$res.prestress,folder="pre.stress",red="cca.aligned")
+
+if (opt$st==TRUE){
+  results <- scStress(sc10x,stg=opt$stg,res.use=opt$res.prestress,pc.use=pc.use)
+  sc10x <- results[[1]]
+  counts.cell.filtered.stress <- results[[2]]
+  sc10x.Stress <- results[[3]]
+  rm(results)
+
+  #results <- scPC(sc10x,lx=opt$lx,hx=opt$hx,ly=opt$ly,cc=opt$cc,pc=opt$pc,hpc=opt$hpc,file="post.stress")
+  #sc10x <- results[[1]]
+  #genes.hvg.poststress <- results[[2]]
+  #pc.use.poststress <- results[[3]]
+  #rm(results)
+  
+  sc10x <- scCluster(sc10x,pc.use=pc.use,res.use=opt$res.poststress,folder="post.stress",red="cca.aligned")
+}
+
+gene.set1 <- read_delim("./genesets/genes.deg.St.csv",",",escape_double=FALSE,trim_ws=TRUE,col_names=TRUE)
+gene.set1 <- gene.set1[1]
+gene.set1 <- as.list(gene.set1)
+names(gene.set1) <- "St"
+gene.set <- c(gene.set1)
+gene.set1 <- read_delim("./genesets/genes.deg.Epi.csv",",",escape_double=FALSE,trim_ws=TRUE,col_names=TRUE)
+gene.set1 <- gene.set1[1]
+gene.set1 <- as.list(gene.set1)
+names(gene.set1) <- "Epi"
+gene.set <- c(gene.set,gene.set1)
+rm(gene.set1)
+results <- scQuSAGE(sc10x,gs=gene.set,res.use=opt$res.poststress,ds=opt$ds,nm="Lin",folder="lin")
+sc10x <- results[[1]]
+results.cor.Lin <- results[[2]]
+results.clust.Lin.id <- results[[3]]
+rm(results)
+rm(gene.set)
+
+sc10x.Epi <- scSubset(sc10x,i="Lin",g="Epi")
+if (any(levels(sc10x@ident)=="Unknown")){
+  sc10x.St <- scSubset(sc10x,i="Lin",g=c("St","Unknown"))
+} else {
+  sc10x.St <- scSubset(sc10x,i="Lin",g="St")
+}
+
+sc10x.Epi <- scCluster(sc10x.Epi,pc.use=pc.use,res.use=opt$res.epi,folder="epi",red="cca.aligned")
+
+gene.set1 <- read_delim("./genesets/genes.deg.BE.csv",",",escape_double=FALSE,trim_ws=TRUE,col_names=TRUE)
+gene.set1 <- gene.set1[1]
+gene.set1 <- as.list(gene.set1)
+names(gene.set1) <- "BE"
+gene.set <- c(gene.set1)
+gene.set1 <- read_delim("./genesets/genes.deg.LE.csv",",",escape_double=FALSE,trim_ws=TRUE,col_names=TRUE)
+gene.set1 <- gene.set1[1]
+gene.set1 <- as.list(gene.set1)
+names(gene.set1) <- "LE"
+gene.set <- c(gene.set,gene.set1)
+gene.set1 <- read_delim("./genesets/genes.deg.OE1.csv",",",escape_double=FALSE,trim_ws=TRUE,col_names=TRUE)
+gene.set1 <- gene.set1[1]
+gene.set1 <- as.list(gene.set1)
+names(gene.set1) <- "OE_SCGB"
+gene.set <- c(gene.set,gene.set1)
+gene.set1 <- read_delim("./genesets/genes.deg.OE2.csv",",",escape_double=FALSE,trim_ws=TRUE,col_names=TRUE)
+gene.set1 <- gene.set1[1]
+gene.set1 <- as.list(gene.set1)
+names(gene.set1) <- "OE_KRT13"
+gene.set <- c(gene.set,gene.set1)
+rm(gene.set1)
+results <- scQuSAGE(sc10x.Epi,gs=gene.set,res.use=opt$res.epi,ds=opt$ds,nm="Epi.dws.sc",folder="epi")
+sc10x.Epi <- results[[1]]
+results.cor.Epi.dws.sc <- results[[2]]
+results.clust.Epi.dws.sc.id <- results[[3]]
+rm(results)
+rm(gene.set)
+
+gene.set1 <- read_csv("./genesets/Basal cells-signature-genes.csv")
+gene.set1 <- gene.set1[,2]
+gene.set1 <- as.list(gene.set1)
+names(gene.set1) <- "BC"
+gene.set <- c(gene.set1)
+gene.set1 <- read_csv("./genesets/Normal AT2 cells-signature-genes.csv")
+gene.set1 <- gene.set1[,2]
+gene.set1 <- as.list(gene.set1)
+names(gene.set1) <- "AT2"
+gene.set <- c(gene.set,gene.set1)
+gene.set1 <- read_csv("./genesets/Club_Goblet cells-signature-genes.csv")
+gene.set1 <- gene.set1[,2]
+gene.set1 <- as.list(gene.set1)
+names(gene.set1) <- "CGC"
+gene.set<- c(gene.set,gene.set1)
+rm(gene.set1)
+results.cor.Epi.lgea <- scQuSAGEsm(sc10x.Epi,gs=gene.set,ds=opt$ds,nm="Epi.dws.sc",folder="lgea")
+rm(gene.set)
+
+sc10x.St <- scCluster(sc10x.St,pc.use=pc.use,res.use=opt$res.epi,folder="st",red="cca.aligned")
+
+gene.set1 <- read_delim("./genesets/genes.deg.Fib.csv",",",escape_double=FALSE,trim_ws=TRUE,col_names=TRUE)
+gene.set1 <- gene.set1[1]
+gene.set1 <- as.list(gene.set1)
+names(gene.set1) <- "Fib"
+gene.set <- c(gene.set1)
+gene.set1 <- read_delim("./genesets/genes.deg.SM.csv",",",escape_double=FALSE,trim_ws=TRUE,col_names=TRUE)
+gene.set1 <- gene.set1[1]
+gene.set1 <- as.list(gene.set1)
+names(gene.set1) <- "SM"
+gene.set <- c(gene.set,gene.set1)
+gene.set1 <- read_delim("./genesets/genes.deg.Endo.csv",",",escape_double=FALSE,trim_ws=TRUE,col_names=TRUE)
+gene.set1 <- gene.set1[1]
+gene.set1 <- as.list(gene.set1)
+names(gene.set1) <- "Endo"
+gene.set <- c(gene.set,gene.set1)
+gene.set1 <- read_delim("./genesets/genes.deg.Leu.csv",",",escape_double=FALSE,trim_ws=TRUE,col_names=TRUE)
+gene.set1 <- gene.set1[1]
+gene.set1 <- as.list(gene.set1)
+names(gene.set1) <- "Leu"
+gene.set <- c(gene.set,gene.set1)
+rm(gene.set1)
+results <- scQuSAGE(sc10x.St,gs=gene.set,res.use=opt$res.st,ds=opt$ds,nm="St.dws.sc",folder="st")
+sc10x.St <- results[[1]]
+results.cor.St.dws.sc <- results[[2]]
+results.clust.St.dws.sc.id <- results[[3]]
+rm(results)
+rm(gene.set)
+
+sc10x.Epi.NE <- scNE(sc10x.Epi,neg="dws")
+
+sc10x.Epi <- scMergeSubClust(sc10x.Epi,i="Epi.dws.sc",g=c("BE","LE","OE_SCGB","OE_KRT13"),nm="Merge")
+
+sc10x.St <- scMergeSubClust(sc10x.St,i="St.dws.sc",g=c("Endo","SM","Fib","Leu"),nm="Merge")
+
+sc10x <- scMerge(sc10x,sc10x.Epi,sc10x.St,i.1="Merge",i.2="Merge",nm="Merge_Epi.dws.sc_St.dws.sc")
+
+sc10x <- scMerge(sc10x,sc10x,sc10x.Epi.NE,i.1="Merge_Epi.dws.sc_St.dws.sc",i.2="NE",nm="Merge_Epi.dws.sc_St.dws.sc_NE")
+
+sc10x <- SetAllIdent(object=sc10x,id="Merge_Epi.dws.sc_St.dws.sc")
+sc10x@ident <- factor(sc10x@ident,levels=c("BE","LE","OE_SCGB","OE_KRT13","Fib","SM","Endo","Leu"))
+postscript("./analysis/tSNE/FINAL/tSNE_FINAL.eps")
+plot <- TSNEPlot(object=sc10x,pt.size=2.5,do.return=TRUE,vector.friendly=FALSE)
+plot <- plot+theme(axis.text.x=element_text(size=20),axis.text.y=element_text(size=20),axis.title.x=element_text(size=20),axis.title.y=element_text(size=20),legend.text=element_text(size=20))
+plot <- plot+guides(colour=guide_legend(override.aes=list(size=10)))
+plot(plot)
+dev.off()
+
+scTables(sc10x,i.1="samples",i.2="Merge_Epi.dws.sc_St.dws.sc")
+scTables(sc10x,i.1="samples",i.2="Merge_Epi.dws.sc_St.dws.sc_NE")
+scTables(sc10x,i.1="Merge_Epi.dws.sc_St.dws.sc_NE",i.2="Merge_Epi.dws.sc_St.dws.sc")
+
+genes.deg.Stress <- scDEG(sc10x.Stress,i="Stress",g.1="Stress",g.2="ALL",pct=0.5,t=5)
+
+genes.deg.Epi <- scDEG(sc10x,i="Lin",g.1="Epi",g.2="St",t=2)
+genes.deg.St <- scDEG(sc10x,i="Lin",g.1="St",g.2="Epi",t=2)
+
+genes.deg.BE <- scDEG(sc10x.Epi.NE,i="Epi.dws.sc",g.1="BE",g.2=c("LE","OE_SCGB","OE_KRT13"),pct=0.25,t=2)
+genes.deg.LE <- scDEG(sc10x.Epi.NE,i="Epi.dws.sc",g.1="LE",g.2=c("BE","LE","OE_SCGB"),pct=0.25,t=2)
+genes.deg.OE_SCGB <- scDEG(sc10x.Epi.NE,i="Epi.dws.sc",g.1="OE_SCGB",g.2=c("BE","LE","OE_KRT13"),pct=0.25,t=2)
+genes.deg.OE_KRT13 <- scDEG(sc10x.Epi.NE,i="Epi.dws.sc",g.1="OE_KRT13",g.2=c("BE","LE","OE_SCGB"),pct=0.25,t=2)
+
+genes.deg.NE <- scDEG(sc10x.Epi.NE,i="NE",g.1="NE",g.2="ALL",pct=0.25,t=2)
+
+genes.deg.Fib <- scDEG(sc10x.St,i="St.dws.sc",g.1="Fib",g.2=c("SM","Endo","Leu"),pct=0.25,t=2)
+genes.deg.SM <- scDEG(sc10x.St,i="St.dws.sc",g.1="SM",g.2=c("Fib","Endo","Leu"),pct=0.25,t=2)
+genes.deg.Endo <- scDEG(sc10x.St,i="St.dws.sc",g.1="Endo",g.2=c("Fib","SM","Leu"),pct=0.25,t=2)
+genes.deg.Leu <- scDEG(sc10x.St,i="St.dws.sc",g.1="Leu",g.2=c("Fib","SM","Endo"),pct=0.25,t=2)
+
+genes.deg.BE.unique <- setdiff(rownames(genes.deg.BE),Reduce(union,list(rownames(genes.deg.St),rownames(genes.deg.LE),rownames(genes.deg.OE_SCGB),rownames(genes.deg.OE_KRT13),rownames(genes.deg.NE),rownames(genes.deg.Fib),rownames(genes.deg.SM),rownames(genes.deg.Endo),rownames(genes.deg.Leu))))
+genes.deg.LE.unique <- setdiff(rownames(genes.deg.LE),Reduce(union,list(rownames(genes.deg.St),rownames(genes.deg.BE),rownames(genes.deg.OE_SCGB),rownames(genes.deg.OE_KRT13),rownames(genes.deg.NE),rownames(genes.deg.Fib),rownames(genes.deg.SM),rownames(genes.deg.Endo),rownames(genes.deg.Leu))))
+genes.deg.OE_SCGB.unique <- setdiff(rownames(genes.deg.OE_SCGB),Reduce(union,list(rownames(genes.deg.St),rownames(genes.deg.BE),rownames(genes.deg.LE),rownames(genes.deg.OE_KRT13),rownames(genes.deg.NE),rownames(genes.deg.Fib),rownames(genes.deg.SM),rownames(genes.deg.Endo),rownames(genes.deg.Leu))))
+genes.deg.OE_KRT13.unique <- setdiff(rownames(genes.deg.OE_KRT13),Reduce(union,list(rownames(genes.deg.St),rownames(genes.deg.BE),rownames(genes.deg.LE),rownames(genes.deg.OE_SCGB),rownames(genes.deg.NE),rownames(genes.deg.Fib),rownames(genes.deg.SM),rownames(genes.deg.Endo),rownames(genes.deg.Leu))))
+genes.deg.NE.unique <- setdiff(rownames(genes.deg.NE),Reduce(union,list(rownames(genes.deg.St),rownames(genes.deg.BE),rownames(genes.deg.LE),rownames(genes.deg.OE_SCGB),rownames(genes.deg.OE_KRT13),rownames(genes.deg.Fib),rownames(genes.deg.SM),rownames(genes.deg.Endo),rownames(genes.deg.Leu))))
+genes.deg.Fib.unique <- setdiff(rownames(genes.deg.Fib),Reduce(union,list(rownames(genes.deg.Epi),rownames(genes.deg.BE),rownames(genes.deg.LE),rownames(genes.deg.OE_SCGB),rownames(genes.deg.OE_KRT13),rownames(genes.deg.NE),rownames(genes.deg.SM),rownames(genes.deg.Endo),rownames(genes.deg.Leu))))
+genes.deg.SM.unique <- setdiff(rownames(genes.deg.SM),Reduce(union,list(rownames(genes.deg.Epi),rownames(genes.deg.BE),rownames(genes.deg.LE),rownames(genes.deg.OE_SCGB),rownames(genes.deg.OE_KRT13),rownames(genes.deg.NE),rownames(genes.deg.Fib),rownames(genes.deg.Endo),rownames(genes.deg.Leu))))
+genes.deg.Endo.unique <- setdiff(rownames(genes.deg.Endo),Reduce(union,list(rownames(genes.deg.Epi),rownames(genes.deg.BE),rownames(genes.deg.LE),rownames(genes.deg.OE_SCGB),rownames(genes.deg.OE_KRT13),rownames(genes.deg.NE),rownames(genes.deg.Fib),rownames(genes.deg.SM),rownames(genes.deg.Leu))))
+genes.deg.Leu.unique <- setdiff(rownames(genes.deg.Leu),Reduce(union,list(rownames(genes.deg.Epi),rownames(genes.deg.BE),rownames(genes.deg.LE),rownames(genes.deg.OE_SCGB),rownames(genes.deg.OE_KRT13),rownames(genes.deg.NE),rownames(genes.deg.Fib),rownames(genes.deg.SM),rownames(genes.deg.Endo))))
+
+genes.deg.5 <- c(genes.deg.BE.unique[1:5],genes.deg.LE.unique[1:5],genes.deg.OE_SCGB.unique[1:5],genes.deg.OE_KRT13.unique[1:5],genes.deg.NE.unique[1:5],genes.deg.Fib.unique[1:5],genes.deg.SM.unique[1:5],genes.deg.Endo.unique[1:5],genes.deg.Leu.unique[1:5])
+genes.deg.5 <- rev(genes.deg.5)
+genes.deg.10 <- c(genes.deg.BE.unique[1:10],genes.deg.LE.unique[1:10],genes.deg.OE_SCGB.unique[1:10],genes.deg.OE_KRT13.unique[1:10],genes.deg.NE.unique[1:10],genes.deg.Fib.unique[1:10],genes.deg.SM.unique[1:10],genes.deg.Endo.unique[1:10],genes.deg.Leu.unique[1:10])
+genes.deg.10 <- rev(genes.deg.10)
+
+
+sc10x <- SetAllIdent(object=sc10x,id="Merge_Epi.dws.sc_St.dws.sc_NE")
+sc10x <- SetAllIdent(object=sc10x,id="Merge_Epi.dws.sc_St.dws.sc_NE")
+sc10x@ident <- factor(sc10x@ident,levels=c("BE","LE","OE_SCGB","OE_KRT13","NE","Fib","SM","Endo","Leu"))
+postscript("./analysis/deg/Dot.DEG.eps",paper="special",width=10,height=5,horizontal=FALSE)
+DotPlot(sc10x,genes.deg.5,x.lab.rot=TRUE,plot.legend=TRUE,dot.scale=4)
+dev.off()
+postscript("./analysis/deg/Heatmap.DEG.eps",paper="special",width=10,height=5,horizontal=FALSE)
+plot(DoHeatmap(sc10x,genes.use=genes.deg.10,slim.col.label=TRUE,group.label.rot=TRUE,group.spacing=0.25,cex.row=2.5))
+dev.off()
+
+for (i in ls(pattern="^genes.deg*unique")){
+  postscript(paste0("./analysis/deg/Violin.",i,".eps"),paper="special",width=10,height=5,horizontal=FALSE)
+  plot(VlnPlot(sc10x,features.plot=get(i)[1:10],nCol=5,point.size.use=0.1,size.title.use=15,x.lab.rot=TRUE))
+  dev.off()
+  postscript(paste0("./analysis/deg/Ridge.",i,".eps"),paper="special",width=10,height=5,horizontal=FALSE)
+  plot(RidgePlot(sc10x,features.plot=get(i)[1:10],nCol=5,size.x.use=10,size.title.use=15))
+  dev.off()
+  postscript(paste0("./analysis/deg/Heatmap.",i,".eps"),paper="special",width=10,height=5,horizontal=FALSE)
+  plot(DoHeatmap(sc10x,genes.use=get(i),slim.col.label=TRUE,group.label.rot=TRUE,group.spacing=0.25,cex.row=2.5))
+  dev.off()
+}
+
+sc10x.Stress <- SetAllIdent(object=sc10x.Stress,id="Stress")
+postscript("./analysis/deg/Violin.Stress.eps",paper="special",width=10,height=5,horizontal=FALSE)
+plot(VlnPlot(sc10x.Stress,features.plot=rownames(genes.deg.Stress)[1:10],nCol=5,point.size.use=0.1,size.title.use=15,x.lab.rot=TRUE))
+dev.off()
+postscript("./analysis/deg/Ridge.Stress.eps",paper="special",width=10,height=5,horizontal=FALSE)
+plot(RidgePlot(sc10x.Stress,features.plot=rownames(genes.deg.Stress)[1:10],nCol=5,size.x.use=10,size.title.use=15))
+dev.off()
+postscript("./analysis/deg/Heatmap.Stress.eps",paper="special",width=10,height=5,horizontal=FALSE)
+plot(DoHeatmap(sc10x.Stress,genes.use=rownames(genes.deg.Stress),slim.col.label=TRUE,group.label.rot=TRUE,group.spacing=0.25,cex.row=2.5))
+dev.off()
+
+for (i in ls(pattern="^genes.deg")){
+  write.table(get(i),file=paste0("./analysis/deg/",i,".csv"),row.names=TRUE,col.names=NA,append=FALSE,sep=",")
+}
+
+sctSNECustCol(sc10x,i="Lin",bl="Epi",rd="St",file="Aggr")
+sctSNECustCol(sc10x,i="Merge_Epi.dws.sc_St.dws.sc",bl=c("BE","LE","OE_SCGB","OE_KRT13"),rd=c("Fib","SM","Endo","Leu"),file="Aggr")
+sctSNECustCol(sc10x.Epi,i="Epi.dws.sc",bl=c("BE","LE","OE_SCGB","OE_KRT13"),rd="",file="Aggr")
+sctSNECustCol(sc10x.St,i="St.dws.sc",bl="",rd=c("Fib","SM","Endo","Leu"),file="Aggr")
+
+sctSNEbwCol(sc10x,i="res0.2",file="ALL",files="Aggr")
+sctSNEbwCol(sc10x.Epi,i="res0.2",file="Epi",files="Aggr")
+sctSNEbwCol(sc10x.St,i="res0.2",file="St",files="Aggr")
+sctSNEbwCol(sc10x,i="Merge_Epi.dws.sc_St.dws.sc",file="ALL",files="Aggr")
+sctSNEbwCol(sc10x.Epi,i="Epi.dws.sc",file="Epi",files="Aggr")
+sctSNEbwCol(sc10x.St,i="St.dws.sc",file="St",files="Aggr")
+
+for (g in c("Epi","St")){
+  sctSNEHighlight(sc10x,i="Lin",g=g,file="Aggr")
+}
+for (g in c("BE","LE","OE_SCGB","OE_KRT13")){
+  sctSNEHighlight(sc10x,i="Merge_Epi.dws.sc_St.dws.sc",g=g,file="Aggr")
+  sctSNEHighlight(sc10x.Epi,i="Epi.dws.sc",g=g,file="Aggr")
+}
+sctSNEHighlight(sc10x.Epi.NE,i="NE",g="NE",file="Aggr")
+for (g in c("Fib","SM","Endo","Leu")){
+  sctSNEHighlight(sc10x,i="Merge_Epi.dws.sc_St.dws.sc",g=g,file="Aggr")
+  sctSNEHighlight(sc10x.St,i="St.dws.sc",g=g,file="Aggr")
+}
+for (g in c("D17PrPzF_BE","D17PrPzF_LE","D17PrPzF_OE","D17PrPzF_FMSt","D17PrPzF_Via","D17PrTzF_Via","D27PrTzF_BE","D27PrTzF_LE","D27PrTzF_OE","D27PrTzF_Edn","D27PrTzF_StPDPNp","D27PrTzF_StPDPNn","D27PrPzF_Via","D27PrTzF_Via")){
+  sctSNEHighlight(sc10x,i="samples",g=g,file="Aggr")
+}
+rm(g)
+
+scCustHeatmap(sc10x.Epi,i="Epi.dws.sub",gs=c(genes.deg.BE.unique,genes.deg.LE.unique,genes.deg.OE_SCGB.unique,genes.deg.OE_KRT13.unique),g=c("BE","LE","OE_SCGB","OE_KRT13"))
+scCustHeatmap(sc10x.St,i="St.go",gs=c(genes.deg.Fib.unique,genes.deg.SM.unique,genes.deg.Endo.unique,genes.deg.Leu.unique),g=c("Fib","SM","Endo","Leu"))
+
+
+sc10x.D27 <- scSubset(sc10x,i="samples",g=c("D27PrTzF_BE","D27PrTzF_LE","D27PrTzF_OE","D27PrTzF_Edn","D27PrTzF_StPDPNp","D27PrTzF_StPDPNn","D27PrPzF_Via","D27PrTzF_Via"))
+sc10x.D27.Epi <- scSubset(sc10x.Epi,i="samples",g=c("D27PrTzF_BE","D27PrTzF_LE","D27PrTzF_OE","D27PrTzF_Edn","D27PrTzF_StPDPNp","D27PrTzF_StPDPNn","D27PrPzF_Via","D27PrTzF_Via"))
+sc10x.D27.Epi.NE <- scSubset(sc10x.Epi.NE,i="samples",g=c("D27PrTzF_BE","D27PrTzF_LE","D27PrTzF_OE","D27PrTzF_Edn","D27PrTzF_StPDPNp","D27PrTzF_StPDPNn","D27PrPzF_Via","D27PrTzF_Via"))
+sc10x.D27.St <- scSubset(sc10x.St,i="samples",g=c("D27PrTzF_BE","D27PrTzF_LE","D27PrTzF_OE","D27PrTzF_Edn","D27PrTzF_StPDPNp","D27PrTzF_StPDPNn","D27PrPzF_Via","D27PrTzF_Via"))
+
+sc10x.D27 <- RunTSNE(object=sc10x.D27,dims.use=1:pc.use.poststress,do.fast=TRUE)
+sc10x.D27.Epi <- RunTSNE(object=sc10x.D27.Epi,dims.use=1:pc.use.poststress,do.fast=TRUE)
+sc10x.D27.Epi.NE <- RunTSNE(object=sc10x.D27.Epi.NE,dims.use=1:pc.use.poststress,do.fast=TRUE)
+sc10x.D27.St <- RunTSNE(object=sc10x.D27.St,dims.use=1:pc.use.poststress,do.fast=TRUE)
+
+sctSNECustCol(sc10x.D27,i="Lin",bl="Epi",rd="St",file="D27")
+sctSNECustCol(sc10x.D27,i="Merge_Epi.dws.sc_St.dws.sc",bl=c("BE","LE","OE_SCGB","OE_KRT13"),rd=c("Fib","SM","Endo","Leu"),file="D27")
+sctSNECustCol(sc10x.D27.Epi,i="Epi.dws.sc",bl=c("BE","LE","OE_SCGB","OE_KRT13"),rd="",file="D27.Epi")
+sctSNECustCol(sc10x.D27.St,i="St.dws.sc",bl="",rd=c("Fib","SM","Endo","Leu"),file="D27.St")
+
+sctSNEbwCol(sc10x.D27,i="res0.2",file="ALL",files="D27")
+sctSNEbwCol(sc10x.D27.Epi,i="res0.2",file="Epi",files="D27")
+sctSNEbwCol(sc10x.D27.St,i="res0.2",file="St",files="D27")
+sctSNEbwCol(sc10x.D27,i="Merge_Epi.dws.sc_St.dws.sc",file="ALL",files="D27")
+sctSNEbwCol(sc10x.D27.Epi,i="Epi.dws.sc",file="Epi",files="D27")
+sctSNEbwCol(sc10x.D27.St,i="St.dws.sc",file="St",files="D27")
+
+for (g in c("Epi","St")){
+  sctSNEHighlight(sc10x.D27,i="Lin",g=g,file="D27")
+}
+for (g in c("BE","LE","OE_SCGB","OE_KRT13")){
+  sctSNEHighlight(sc10x.D27,i="Merge_Epi.dws.sc_St.dws.sc",g=g,file="D27")
+  sctSNEHighlight(sc10x.D27.Epi,i="Epi.dws.sc",g=g,file="D27")
+}
+sctSNEHighlight(sc10x.D27.Epi.NE,i="NE",g="NE",file="D27")
+for (g in c("Fib","SM","Endo","Leu")){
+  sctSNEHighlight(sc10x.D27,i="Merge_Epi.dws.sc_St.dws.sc",g=g,file="D27")
+  sctSNEHighlight(sc10x.D27.St,i="St.dws.sc",g=g,file="D27")
+}
+for (g in c("D27PrTzF_BE","D27PrTzF_LE","D27PrTzF_OE","D27PrTzF_Edn","D27PrTzF_StPDPNp","D27PrTzF_StPDPNn","D27PrPzF_Via","D27PrTzF_Via")){
+  sctSNEHighlight(sc10x.D27,i="samples",g=g,file="D27")
+}
+rm(g)
+
+sc10x.D27 <- SetAllIdent(object=sc10x.D27,id="samples")
+cell <- names(sc10x.D27@ident[grep("^D27PrTzF_StPDPN",as.character(sc10x.D27@ident))])
+sc10x.D27 <- SetIdent(object=sc10x.D27,cells.use=cell,ident.use="D27PrTzF_FMSt")
+rm(cell)
+sctSNEHighlight(sc10x.D27,i="current",g="D27PrTzF_FMSt",file="D27")
+
+
+save(list=ls(pattern="^sc10x.D27"),file="./analysis/sc10x.D27.Rda")
+rm(list=ls(pattern="^sc10x.D27"))
+save(list=ls(pattern="sc10x.Stress"),file="./analysis/sc10x.Stress.Rda")
+rm(list=ls(pattern="sc10x.Stress"))
+save(list=ls(pattern="^genes.deg"),file="./analysis/DEG.Rda")
+rm(list=ls(pattern="^genes.deg"))
+save(list=ls(pattern="sc10x.Epi"),file="./analysis/sc10x.Epi.Rda")
+rm(list=ls(pattern="^sc10x.Epi"))
+save(list=ls(pattern="sc10x.St"),file="./analysis/sc10x.St.Rda")
+rm(list=ls(pattern="sc10x.St"))
+save(list=ls(pattern="^sc10x"),file="./analysis/sc10x.Rda")
+rm(list=ls(pattern="^sc10x"))
+save.image(file="./analysis/Data.RData")
-- 
GitLab