-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4_runHarmonyCellClassify.R
154 lines (86 loc) · 3.69 KB
/
4_runHarmonyCellClassify.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
##############################################
### cell type classification using SingleR ###
### reference: parturition elife ###
##############################################
## options(repos = c(CRAN = "http://cran.rstudio.com"))
## This uses updated Seurat package 3 - starts with merged counts/demux from step 2
library(Seurat)
library(Matrix)
library(tidyverse)
library(future)
library(harmony)
#################
library(SingleR)
future::plan(strategy = 'multicore', workers = 16)
options(future.globals.maxSize = 30 * 1024 ^ 3)
###########################################
## Testing sc transform
## 2and3_Diem_Output
# no soupx
#load("3_MergeDemux_Output/scFilteredSeurat.Rdata")
#sc1 <- sc
# with control samples from covid19
load("/wsu/home/groups/prbgenomics/covid19/covid19analysis_public_repo/3_MergeDemux_Output/scFilteredSeurat.Rdata")
sc_covid<-sc
sc_covid <- subset(sc_covid, subset = Condition=="Control")
[email protected]$Location <- "CAM"
[email protected]$Location [grepl("PVBP",[email protected]$EXP)] <- "PVBP"
load("./3_MergeDemux_Output/scFilteredSeurat.Rdata")
[email protected]$Condition<-sc$Labor
sc1 <- merge(sc_covid,sc, project="parturition")
# with soupx
#sc1<-read_rds("1_soupx/seuratObj-newfilter-merge.2022-03-10.rds")
sc1 <- subset(sc1, subset = nFeature_RNA > 100 & nFeature_RNA < 10000 & DIFF.LLK.BEST.NEXT > 3 & percent.mt < 25)
sc3 <- read_rds("/nfs/rprdata/scilab/novogene/Analyses/Roger_20200218/3_scTransferLabel_scLabor/ST_Integrated.scLabor.obj.rds")
# Subset a Seurat object
sc3 <- subset(sc3, subset = nFeature_RNA > 100)
# to identify the cell types of sc1, another study with known celltypes will be merged to this study and the cell types will be identified
sc <- merge(sc1,list(sc3))
dim(sc)
table(sc$Library)
## table(sc$Location)
##table(sc$sclabor.tlabel)
##table(sc$Location,sc$sclabor.tlabel)
## Harmony
DefaultAssay(sc) <- "RNA"
sc <- NormalizeData(sc, verbose=TRUE)
sc <- FindVariableFeatures(sc, selection.method = "vst", nfeatures = 3000)
sc <- ScaleData(sc, verbose = TRUE)
sc <- RunPCA(sc,pc.genes = [email protected], npcs = 100, verbose = TRUE)
##sc <- RunHarmony(sc,c("Location","percent.mt","Rep"),reduction="pca")
sc <- RunHarmony(sc,c("Library"),reduction="pca")
sc <- RunUMAP(sc,reduction = "harmony", dims = 1:30)
sc <- FindNeighbors(sc, reduction = "harmony", dims = 1:30, verbose = TRUE)
#sapply(c(0.5,0.8,1.5),function(x)
#sapply(c(0.6,"1.0"),function(x)
#sapply(c(0.8,"1.0",1.5),function(x)
sapply(c( 0.8, "1.0", 1.5,2),function(x)
{
#outFolder=paste0("./4_harmony_cellClass_elife",x,"/")
#outFolder=paste0("./4_harmony_cellClass_SoupX_elife",x,"/")
outFolder=paste0("./4_harmony_cellClass_with_covidcontrol_elife",x,"/")
system(paste0("mkdir -p ", outFolder))
###### Cluster
sc <- FindClusters(sc, verbose = TRUE,resolution=as.numeric(x))
fname=paste0(outFolder,"sc.NormIntegrated.ref.Harmony.rds")
write_rds(sc,fname)
################
he <- t(sc@[email protected][,1:30])
#unknown cell types
query.he <- he[,is.na([email protected]$FinalName)]
#known cell types
ref.he <- he[,!is.na([email protected]$FinalName)]
ref.labels <- [email protected]$FinalName[!is.na([email protected]$FinalName)]
pred.labels <- SingleR(test = query.he, ref = ref.he, labels = ref.labels)
##table(pred.labels)
table(pred.labels$pruned.labels)
sum(is.na(pred.labels$pruned.labels))
fname=paste0(outFolder,"sc.NormByLocation.ref.Harmony.singler.rds")
write_rds(pred.labels,fname)
md <- pred.labels %>% as.data.frame() %>%
rownames_to_column("BARCODES") %>%
left_join([email protected] %>% rownames_to_column("BARCODES"))
fname=paste0(outFolder,"sc.NormByLocation.ref.Harmony.singler.csv")
write_csv(md,fname)
})
## save object.