-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobj_create.R
156 lines (132 loc) · 5.13 KB
/
obj_create.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
150
151
152
153
154
155
156
obj_create = function(project,
type,
datapath,
savepath,
PatientID = NULL,
SampleID = NULL,
SampleType = NULL,
SampleTimepoint = NULL,
SampleMethod = NULL,
Kit = NULL) {
# only for test -----------------------------------------------------------
#datapath = '/home/zhepan/Project/MultiOmics/data/snRNA/Result'
#savepath = '/home/zhepan/Project/MultiOmics/data/snRNA/Object'
#PatientID = 'P1015'
#NeoChemoRes = 'NOTK'
#NeoRadRes = 'NOTK'
#SampleID = 'P1015S2'
#SampleTimepoint = 'S2'
#SampleMethod = 'surgery' # biopsy
#SampleDate = '20231114'
#Kit = 'sc5'
# load pkgs ---------------------------------------------------------------
suppressMessages(library(Seurat))
suppressMessages(library(dplyr))
suppressMessages(library(scDblFinder))
# load data ---------------------------------------------------------------
if (project == 'tumor') {
cellranger_path = file.path(datapath, SampleID, 'filtered_feature_bc_matrix.h5')
soupx_path = file.path(datapath, SampleID, 'soupx.rds')
decontx_path = file.path(datapath, SampleID, 'decontx.rds')
cellbender_path = file.path(
datapath,
SampleID,
'cellbender_output',
'cellbender_feature_bc_matrix_filtered.h5'
)
if (type == 'cellranger') {
count = Read10X_h5(cellranger_path)
seu = CreateSeuratObject(
count,
project = SampleID,
min.cells = 3,
min.features = 200
)
} else if (type == 'soupx') {
seu = readRDS(soupx_path)
} else if (type == 'cellbender') {
count = scCustomize::Read_CellBender_h5_Mat(cellbender_path)
seu = CreateSeuratObject(
count,
project = SampleID,
min.cells = 3,
min.features = 200
)
} else if (type == 'decontx') {
seu = readRDS(decontx_path)
}
seu$PatientID = PatientID
seu$SampleID = SampleID
seu$SampleTimepoint = SampleTimepoint
seu$SampleMethod = SampleMethod
seu$Kit = Kit
} else if (project == 'skin') {
cellranger_path = file.path(datapath, SampleID,'raw/filtered_feature_bc_matrix')
soupx_path = file.path(datapath, SampleID, 'raw/soupx.rds')
cellbender_path = file.path(datapath,
SampleID,
'raw/cellbender_filtered.h5')
decontx_path = file.path(datapath, SampleID, 'raw/decontx.rds')
if (type == 'cellranger') {
count = Read10X(cellranger_path)
seu = CreateSeuratObject(
count,
project = SampleID,
min.cells = 3,
min.features = 200
)
} else if (type == 'soupx') {
seu = readRDS(soupx_path)
} else if (type == 'cellbender') {
count = scCustomize::Read_CellBender_h5_Mat(cellbender_path)
seu = CreateSeuratObject(
count,
project = SampleID,
min.cells = 3,
min.features = 200
)
} else if (type == 'decontx') {
seu = readRDS(decontx_path)
}
seu$PatientID = PatientID
seu$SampleID = SampleID
seu$SampleType = SampleType
seu$SampleDate = SampleDate
seu$Kit = Kit
} else{
stop("please specify project")
}
# doublet detect ----------------------------------------------------------
set.seed(42)
sce = scDblFinder(as.SingleCellExperiment(seu))
seu$scDblFinder.score = sce$scDblFinder.score
seu$scDblFinder.class = sce$scDblFinder.class
# some qc info ------------------------------------------------------------
seu$percent_mt = PercentageFeatureSet(seu, pattern = '^MT-')
seu$percent_hb = PercentageFeatureSet(seu, pattern = "^HB[^(P)]")
seu$percent_rb = PercentageFeatureSet(seu, pattern = "^RP[SL]")
cyclegenes = read.delim('/home/zhepan/Reference/regev_lab_cell_cycle_genes.txt',header = F)
seu <- NormalizeData(seu)
seu = CellCycleScoring(seu, s.features = cyclegenes[1:42, 1], g2m.features = cyclegenes[43:96, 1])
#VlnPlot(seu,features = c('nCount_RNA','nFeature_RNA','percent_mt','S.Score'), group.by = 'scDblFinder.class', pt.size = 0)
# convert to h5ad ---------------------------------------------------------
if(!dir.exists(paths = file.path(savepath, SampleID,'raw'))){
dir.create(path = file.path(savepath, SampleID,'raw'),recursive = T)}
else {NULL}
if (type == 'cellranger') {
h5names = 'cellranger_doublet.h5ad'
rdsnames = 'cellranger_doublet.rds'
} else if (type == 'soupx') {
h5names = 'soupx_doublet.h5ad'
rdsnames = 'soupx_doublet.rds'
} else if (type == 'cellbender') {
h5names = 'cellbender_doublet.h5ad'
rdsnames = 'cellbender_doublet.rds'
} else if (type == 'decontx') {
h5names = 'decontx_doublet.h5ad'
rdsnames = 'decontx_doublet.rds'
}
DietSeurat(seu,counts = TRUE,data = TRUE,scale.data = FALSE)
sceasy::convertFormat(seu, from="seurat", to="anndata",outFile = file.path(savepath, SampleID, 'raw', h5names), drop_single_values = F, main_layer = 'counts')
saveRDS(seu, file = file.path(savepath, SampleID, 'raw', rdsnames))
}