-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevelopmental_Script_For_SeuratV5.r
326 lines (286 loc) · 12.6 KB
/
Developmental_Script_For_SeuratV5.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env Rscript
library(optparse)
options(stringsAsFactors=FALSE)
##################
# OPTION PARSING
##################
option_list <- list(
make_option(opt_str = c("-i","--in_h5"),
type = "character",
default = NULL,
help = "Input filtered_feature_bc_matrix.h5 file",
metavar = "character"),
make_option(opt_str = c("-l","--in_mol"),
type = "character",
default = NULL,
help = "Input molecule_info.h5 file",
metavar = "character"),
make_option(opt_str = c("-s", "--in_sum"),
type = "character",
default = NULL,
help = "Input metrics_summary.csv file",
metavar = "character"),
make_option(opt_str = c("-k", "--in_key"),
type = "character",
default = NULL,
help = "Input SampleSheet.csv file",
metavar = "character"),
make_option(opt_str = c("-j","--in_sample"),
type = "character",
default = NULL,
help = "Sample",
metavar = "character")
)
opt_parser <- OptionParser(option_list = option_list)
args <- parse_args(opt_parser)
# Load Libraries
quiet_library <- function(...) {
suppressPackageStartupMessages(library(...))
}
quiet_library(rhdf5)
quiet_library(H5MANIPULATOR)
quiet_library(Matrix)
quiet_library(ggplot2)
quiet_library(cowplot)
quiet_library(jsonlite)
quiet_library(tidyverse)
quiet_library(googlesheets4)
quiet_library(rio)
quiet_library(Seurat)
# Arg Parse
if(is.null(args$in_h5)) {
in_h5 <- system.file("testdata/well1.h5", package = "H5MANIPULATOR")
in_mol <- system.file("testdata/sample1_molecule_info.h5", package = "H5MANIPULATOR")
in_sum <- system.file("testdata/sample1_metrics_summary.csv", package = "H5MANIPULATOR")
in_key <- system.file("reference/SampleSheet_fallback.csv", package = "H5MANIPULATOR")
in_sample <- "B000-P0C0W0"
} else {
in_h5 <- args$in_h5
in_mol <- args$in_mol
in_sum <- args$in_sum
in_key <- args$in_key
in_sample <- args$in_sample
}
if(!file.exists(in_h5)) {
stm(paste0("ERROR: Cannot find IN H5 file:", in_h5))
stop()
}
if(!file.exists(in_mol)) {
stm(paste0("ERROR: Cannot find IN Mol Info H5 file:", in_mol))
stop()
}
if(!file.exists(in_key)) {
stm(paste0("ERROR: Cannot find IN SampleSheet file:", in_key))
stop()
}
### Load inputs
#### Load scRNA-seq Dataset
stm(paste0("Loading HDF5 from ", in_h5))
h5_list <- h5dump(in_h5)
#### Load SampleSheet
if (length(grep("https",in_key)) > 0) {
ss <- read_sheet(in_key)
} else if (length(grep(".xlsx",in_key)) > 0 ){
ss <- import_list(in_key)
ss <- ss$MetaTable_expectedCell
} else {
ss <- read.csv(in_key)
}
if ("Final list" %in% colnames(ss)) {
ss <- ss %>% filter(ss$"Final list" == 1)
}
# pull library id
Sample_ss <- ss %>% filter(Sample_ID == in_sample)
library_id <- Sample_ss$Library
n_cells <- length(h5_list$matrix$barcodes)
h5_list <- set_list_path(h5_list,
"/matrix/observations/in_sample",
rep(in_sample, n_cells))
# Pull Treatment
Treatment_id <- Sample_ss$Treatment
h5_list <- set_list_path(h5_list,
"/matrix/observations/treatment",
rep(Treatment_id, n_cells))
# Tissue
if (length(Sample_ss$Tissue) > 0){
Tissue <- Sample_ss$Tissue
h5_list <- set_list_path(h5_list,
"/matrix/observations/tissue",
rep(Tissue, n_cells))
}
# Sex
if (length(Sample_ss$Sex) > 0){
Sex <- Sample_ss$Sex
h5_list <- set_list_path(h5_list,
"/matrix/observations/Sex",
rep(Sex, n_cells))
}
# pull Sequencing Batch
Sequencing_Batch_id <- Sample_ss$'Sequencing Batch'
h5_list <- set_list_path(h5_list,
"/matrix/observations/Sequencing_Batch_id",
rep(Sequencing_Batch_id, n_cells))
out_h5 <- file.path(getwd(), paste0(in_sample, ".h5"))
stm(paste0("OUT H5 file : ", out_h5))
#### Read molecule info to get read counts per cell
stm(paste0("Assembling Read Counts per Cell from ", in_mol))
bc <- sub("-1","",h5_list$matrix$barcodes)
bc_counts <- data.table(mol_idx = h5read(in_mol, "/barcode_idx"),
umi_count = h5read(in_mol, "/count"))
bc_sums <- bc_counts[, .(n_reads = sum(umi_count)), by = mol_idx]
rm(bc_counts)
mol_bc <- h5read(in_mol, "/barcodes")
bc_sums$cell_barcode <- mol_bc[bc_sums$mol_idx + 1]
rm(mol_bc)
bc_sums <- bc_sums[,.(cell_barcode, n_reads)]
n_reads <- bc_sums$n_reads[match(bc, bc_sums$cell_barcode)]
n_reads[is.na(n_reads)] <- 0
h5_list <- set_list_path(h5_list,
"/matrix/observations/n_reads",
n_reads)
### Assemble data
#### Split matrices if more than one feature type is present
h5_list <- h5_list_convert_to_dgCMatrix(h5_list, target = "matrix")
feature_types <- unique(h5_list$matrix$features$feature_type)
if(sum(feature_types != "Gene Expression") > 0) {
stm("Separating non-Gene Expression data to additional matrices")
mat <- h5_list$matrix_dgCMatrix
feature_df <- as.data.frame(h5_list$matrix$features)
h5_list$matrix_dgCMatrix <- mat[feature_df$feature_type == "Gene Expression",]
h5_list$matrix$features <- as.list(feature_df[feature_df$feature_type == "Gene Expression",])
}
#### Compute N UMIs and N Genes per cell
stm("Computing UMI and Gene Counts per Cell")
h5_list <- set_list_path(h5_list,
"/matrix/observations/n_umis",
unname(colSums(h5_list$matrix_dgCMatrix)))
h5_list <- set_list_path(h5_list,
"/matrix/observations/n_genes",
unname(colSums(h5_list$matrix_dgCMatrix > 0)))
h5_list <- h5_list_convert_from_dgCMatrix(h5_list, target = "matrix")
#### Add cell ids
stm("Adding Cell UUIDs and Names")
h5_list <- add_cell_ids(h5_list,
add_uuid = TRUE,
replace_barcode = TRUE,
retain_original_barcode = TRUE,
add_name = TRUE)
#### Add chrM gene counts
stm("Adding chrM count metadata")
so <- Read10X_h5(in_h5)
so <- CreateSeuratObject(so)
all_genes <- h5_list$matrix$features$name
mito_genes <- grep("^MT:|MT-|mt:|mt-", all_genes,ignore.case=TRUE,value=TRUE)
total_counts_per_cell <- colSums(so@assays$RNA$counts)
percent_mito <- colSums(so@assays$RNA$counts[mito_genes, ])/total_counts_per_cell *100
h5_list <- H5MANIPULATOR::set_list_path(h5_list,
"/matrix/observations/percent_mito",
percent_mito)
#### Add ribo gene counts
stm("Adding ribo count metadata")
all_genes <- h5_list$matrix$features$name
ribo_genes <- grep("^RP[LS]|Rp[LS]", all_genes,ignore.case=TRUE,value=TRUE)
total_counts_per_cell <- colSums(so@assays$RNA$counts)
percent_ribo <- colSums(so@assays$RNA$counts[ribo_genes, ])/total_counts_per_cell * 100
h5_list <- H5MANIPULATOR::set_list_path(h5_list,
"/matrix/observations/percent_ribo",
percent_ribo)
#### Add Sample Metrics
if (exists("in_sum")) {
sample_metrics <- read_tenx_metrics(in_sum)
sample_metrics <- as.list(sample_metrics)
h5_list <- set_list_path(h5_list,
"/sample",
sample_metrics)
h5_list <- set_list_path(h5_list,
"/sample/sample_id",
in_sample)
} else {
estimated_number_of_cells <- Library_ss$"Estimated Number of Cells"
h5_list <- set_list_path(h5_list,
"/sample/estimated_number_of_cells",
estimated_number_of_cells)
fraction_reads_in_cells <- Library_ss$"Fraction Reads in Cells"
h5_list <- set_list_path(h5_list,
"/sample/fraction_reads_in_cells",
fraction_reads_in_cells)
mean_reads_per_cell <- Library_ss$"Mean Reads per Cell"
h5_list <- set_list_path(h5_list,
"/sample/mean_reads_per_cell",
mean_reads_per_cell)
median_genes_per_cell <- Library_ss$"Median Genes per Cell"
h5_list <- set_list_path(h5_list,
"/sample/median_genes_per_cell",
median_genes_per_cell)
median_umi_counts_per_cell <- Library_ss$"Estimated Number of Cells"
h5_list <- set_list_path(h5_list,
"/sample/median_umi_counts_per_cell",
median_genes_per_cell)
number_of_reads <- Library_ss$"Number of Reads"
h5_list <- set_list_path(h5_list,
"/sample/number_of_reads",
number_of_reads)
q30_bases_in_barcode <- Library_ss$"Q30 Bases in Barcode"
h5_list <- set_list_path(h5_list,
"/sample/q30_bases_in_barcode",
q30_bases_in_barcode)
q30_bases_in_rna_read <- Library_ss$"Q30 Bases in RNA Read"
h5_list <- set_list_path(h5_list,
"/sample/q30_bases_in_rna_read",
q30_bases_in_rna_read)
q30_bases_in_umi <- Library_ss$"Q30 Bases in UMI"
h5_list <- set_list_path(h5_list,
"/sample/q30_bases_in_umi",
q30_bases_in_umi)
reads_mapped_antisense_to_gene <- Library_ss$"Reads Mapped Antisense to Gene"
h5_list <- set_list_path(h5_list,
"/sample/reads_mapped_antisense_to_gene",
reads_mapped_antisense_to_gene)
reads_mapped_confidently_to_exonic_regions <- Library_ss$"Reads Mapped Confidently to Exonic Regions"
h5_list <- set_list_path(h5_list,
"/sample/reads_mapped_confidently_to_exonic_regions",
reads_mapped_confidently_to_exonic_regions)
reads_mapped_confidently_to_genome <- Library_ss$"Reads Mapped Confidently to Genome"
h5_list <- set_list_path(h5_list,
"/sample/reads_mapped_confidently_to_genome",
reads_mapped_confidently_to_genome)
reads_mapped_confidently_to_intergenic_regions <- Library_ss$"Reads Mapped Confidently to Intergenic Regions"
h5_list <- set_list_path(h5_list,
"/sample/reads_mapped_confidently_to_intergenic_regions",
reads_mapped_confidently_to_intergenic_regions)
reads_mapped_confidently_to_intronic_regions <- Library_ss$"Reads Mapped Confidently to Intronic Regions"
h5_list <- set_list_path(h5_list,
"/sample/reads_mapped_confidently_to_intronic_regions",
reads_mapped_confidently_to_intronic_regions)
reads_mapped_confidently_to_transcriptome <- Library_ss$"Reads Mapped Confidently to Transcriptome"
h5_list <- set_list_path(h5_list,
"/sample/reads_mapped_confidently_to_transcriptome",
reads_mapped_confidently_to_transcriptome)
reads_mapped_to_genome <- Library_ss$"Reads Mapped Confidently to Genome"
h5_list <- set_list_path(h5_list,
"/sample/reads_mapped_to_genome",
reads_mapped_to_genome)
sample_id <- Library_ss$"Sample"
h5_list <- set_list_path(h5_list,
"/sample/sample_id",
sample_id)
sequencing_saturation <- Library_ss$"Sequencing Saturation"
h5_list <- set_list_path(h5_list,
"/sample/sequencing_saturation",
sequencing_saturation)
total_genes_detected <- Library_ss$"Total Genes Detected"
h5_list <- set_list_path(h5_list,
"/sample/total_genes_detected",
total_genes_detected)
valid_barcodes <- Library_ss$"Valid Barcodes"
h5_list <- set_list_path(h5_list,
"/sample/valid_barcodes",
valid_barcodes)
}
### Write Output
#### Write HDF5 files
stm(paste0("Writing HDF5 to ", out_h5))
write_h5_list(h5_list,
h5_file = out_h5,
overwrite = TRUE)
h5closeAll()