-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcr_partition.R
executable file
·378 lines (301 loc) · 14.2 KB
/
bcr_partition.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/opt/conda/bin/Rscript
# Julian Q. Zhou
# https://github.com/julianqz
# wrapper to partition sequences based on VJL combinations via `alakazam::groupGenes`
# - either based on heavy chains only
# - or based on both heavy and light chains
# assumes:
# - pathCSV points to a comma-separated file with the following headers
# "subj", "path_db_heavy/light" where "path_db_*" points to a .tsv file
# - note that heavy and light chains are expected to be in separate files
# If the data is single-cell heavy:light paired, but partitioning based on
# heavy chains only is desired, simply set `--heavyLight` to `FALSE` and
# light chains will be ignored.
suppressPackageStartupMessages(require(optparse))
option_list = list(
make_option("--pathCSV", action="store", default=NA,
type="character", help="Path to CSV containing subject list and paths to input files."),
make_option("--pathWork", action="store", default=NA, type="character",
help="path_work."),
make_option("--calcWithin", action="store", default=FALSE, type="logical",
help="Whether to calculate within-subject dtn."),
make_option("--calcBetween", action="store", default=FALSE, type="logical",
help="Whether to calculate bewteen-subject dtn."),
make_option("--colSubj", action="store", default=NA,
type="character", help="Column name containing subject info."),
make_option("--colSeqID", action="store", default="sequence_id",
type="character", help="Column name containing sequence ID."),
make_option("--colSeq", action="store", default="cdr3",
type="character", help="sequenceColumn."),
make_option("--colV", action="store", default="v_call",
type="character", help="vCallColumn."),
make_option("--colJ", action="store", default="j_call",
type="character", help="jCallColumn."),
make_option("--heavyLight", action="store", default=FALSE, type="logical",
help="Whether to partition using both heavy and light chains."),
make_option("--colCell", action="store", default="cell_id", type="character",
help="cellIdColumn. Ignored if --heavyLight FALSE."),
make_option("--colLocus", action="store", default="locus", type="character",
help="locusColumn. Ignored if --heavyLight FALSE.")
)
opt = parse_args(OptionParser(option_list=option_list))
subj_info = read.table(opt$pathCSV, header=T, sep=",", stringsAsFactors=F)
# check columns
# suffix for output filenames
if (opt$heavyLight) {
stopifnot( all(c("path_db_heavy", "path_db_light") %in% colnames(subj_info)) )
out_suffix = "_groupByHL"
} else {
stopifnot( "path_db_heavy" %in% colnames(subj_info) )
out_suffix = "_groupByHonly"
}
suppressPackageStartupMessages(library(alakazam))
setwd(opt$pathWork)
sinkName = paste0("computingEnv_partition_", Sys.Date(), "-",
format(Sys.time(), "%H%M%S"), '.txt')
sink(sinkName)
cat("Partition using both heavy and light:", opt$heavyLight, "\n")
cat("calcWithin:", opt$calcWithin, "\n")
cat("calcBetween:", opt$calcBetween, "\n")
sessionInfo()
sink()
#### within-subject ####
if (opt$calcWithin) {
cat("\nPerforming within-subject partitioning... \n")
for (i in 1:nrow(subj_info)) {
subj = subj_info[["subj"]][i]
if (opt$heavyLight) {
# heavy and light
# .tsv
#db_h = read.table(subj_info[["path_db_heavy"]][i],
# header=T, sep="\t", stringsAsFactors=F)
#db_l = read.table(subj_info[["path_db_light"]][i],
# header=T, sep="\t", stringsAsFactors=F)
# .RData
load(subj_info[["path_db_heavy"]][i])
db_h = db; rm(db)
load(subj_info[["path_db_light"]][i])
db_l = db; rm(db)
# columns should match
stopifnot(all.equal(colnames(db_h), colnames(db_l)))
stopifnot(all( c(opt$colCell, opt$colLocus) %in% colnames(db_h) ))
# each cell should have 1 HC and 1 LC each
cells_common = base::intersect(db_h[[opt$colCell]],
db_l[[opt$colCell]])
bool_common_h = db_h[[opt$colCell]] %in% cells_common
bool_common_l = db_l[[opt$colCell]] %in% cells_common
if (any(!bool_common_h)) {
cat("\n", subj, "- excluded", sum(!bool_common_h),
"heavy chain seqs for lacking light chain counterparts\n")
db_h = db_h[bool_common_h, ]
}
if (any(!bool_common_l)) {
cat("\n", subj, "- excluded", sum(!bool_common_l),
"light chain seqs for lacking heavy chain counterparts\n")
db_l = db_l[bool_common_l, ]
}
stopifnot(nrow(db_h)==nrow(db_l))
db = rbind(db_h, db_l)
} else {
# heavy only
# .tsv
#db = read.table(subj_info[["path_db_heavy"]][i],
# header=T, sep="\t", stringsAsFactors=F)
# .RData
load(subj_info[["path_db_heavy"]][i])
}
nrow_bf = nrow(db)
cat("\n", subj, "; nrow(db):", nrow_bf, "\n")
# use `dtn` in filename because `bcr_infer_clone_wrapper.R` expects so
fn = paste0("dtn", out_suffix, "_", subj,
".RData")
# temporary column
col_tmp_seq_len = "tmp_seq_len"
db[[col_tmp_seq_len]] = nchar(db[[opt$colSeq]])
# adds $vj_group columns
# even if `junc_len` specified, added column is named `vj_group`
if (opt$heavyLight) {
# heavy and light
db = groupGenes(data=db,
v_call=opt$colV,
j_call=opt$colJ,
junc_len=col_tmp_seq_len,
cell_id=opt$colCell,
locus=opt$colLocus,
only_heavy=F,
first=F)
# sanity check
# heavy and light chains from the same cell should have the same partition
stopifnot(all( sapply(unique(db[[opt$colCell]]),
function(s){
# wrt db
s_idx = which(db[[opt$colCell]]==s)
s_bool = length(unique(db[["vj_group"]][s_idx]))==1
return(s_bool)
}, USE.NAMES=F) ))
# all counts of vj_group should be even (heavy:light paired)
stopifnot(all( table(db[["vj_group"]]) %% 2 == 0 ))
} else {
# heavy only
db = groupGenes(data=db,
v_call=opt$colV,
j_call=opt$colJ,
junc_len=col_tmp_seq_len,
cell_id=NULL,
first=F)
}
# rename `vj_group`; remove `vj_group` after renaming
stopifnot("vj_group" %in% colnames(db))
if ("vjl_group" %in% colnames(db)) {
warning("A `vjl_group` column already exists; it will be overwritten.")
}
db[["vjl_group"]] = db[["vj_group"]]
db[["vj_group"]] = NULL
# remove temporary column
db[[col_tmp_seq_len]] = NULL
nrow_af = nrow(db)
# sanity check
# number of rows should remain the same
# unless a row contained NA in any of v_call, j_call, or junc_len
if (nrow_bf!=nrow_af) {
warning("nrow_bf (", nrow_bf, ") != nrow_af (", nrow_af, ")\n")
}
save(db, file=fn)
rm(db)
}
}
#### between-subject ####
if (opt$calcBetween) {
# skip if only 1 donor
if (nrow(subj_info)>1) {
# concat all subjects
if (opt$heavyLight) {
cols_keep = c(opt$colSeqID, opt$colSeq, opt$colV, opt$colJ,
opt$colCell, opt$colLocus)
} else {
cols_keep = c(opt$colSeqID, opt$colSeq, opt$colV, opt$colJ,
opt$colLocus)
}
for (i in 1:nrow(subj_info)) {
subj = subj_info[["subj"]][i]
if (opt$heavyLight) {
# heavy and light
# .tsv
#db_tmp_h = read.table(subj_info[["path_db_heavy"]][i],
# header=T, sep="\t", stringsAsFactors=F)
#db_tmp_l = read.table(subj_info[["path_db_light"]][i],
# header=T, sep="\t", stringsAsFactors=F)
# .RData
load(subj_info[["path_db_heavy"]][i])
db_tmp_h = db; rm(db)
load(subj_info[["path_db_light"]][i])
db_tmp_l = db; rm(db)
stopifnot(all.equal(colnames(db_tmp_h), colnames(db_tmp_l)))
stopifnot(all( c(opt$colCell, opt$colLocus) %in% colnames(db_tmp_h) ))
# each cell should have 1 HC and 1 LC each
cells_common = base::intersect(db_tmp_h[[opt$colCell]],
db_tmp_l[[opt$colCell]])
bool_common_h = db_tmp_h[[opt$colCell]] %in% cells_common
bool_common_l = db_tmp_l[[opt$colCell]] %in% cells_common
if (any(!bool_common_h)) {
cat("\n", subj, "- excluded", sum(!bool_common_h),
"heavy chain seqs for lacking light chain counterparts\n")
db_tmp_h = db_tmp_h[bool_common_h, ]
}
if (any(!bool_common_l)) {
cat("\n", subj, "- excluded", sum(!bool_common_l),
"light chain seqs for lacking heavy chain counterparts\n")
db_tmp_l = db_tmp_l[bool_common_l, ]
}
stopifnot(nrow(db_tmp_h)==nrow(db_tmp_l))
db_tmp = rbind(db_tmp_h, db_tmp_l)
} else {
# heavy only
# .tsv
#db_tmp = read.table(subj_info[["path_db_heavy"]][i],
# header=T, sep="\t", stringsAsFactors=F)
# .RData
load(subj_info[["path_db_heavy"]][i])
db_tmp = db; rm(db)
}
stopifnot(all(cols_keep %in% colnames(db_tmp)))
db_tmp = db_tmp[, cols_keep]
db_tmp[[opt$colSubj]] = subj_info[["subj"]][i]
# can't name the combined data.frame `db` within this for loop
# because of `rm(db)` when loading .RData files
if (i==1) {
# initiate db_all
db_all = db_tmp
} else {
# append new rows to db_all
db_all = rbind(db_all, db_tmp)
}
rm(db_tmp)
}
# rename
db = db_all; rm(db_all)
nrow_bf = nrow(db)
cat("\nnrow(db):", nrow_bf, "\n")
cat("\nBreakdown by subject:\n")
print(table(db[[opt$colSubj]]))
cat("\nPerforming between-subject partitioning... \n")
# use `dtn` in filename because `bcr_infer_clone_wrapper.R` expects so
fn = paste0("dtn", out_suffix, "_btwSubj",
".RData")
# temporary column
col_tmp_seq_len = "tmp_seq_len"
db[[col_tmp_seq_len]] = nchar(db[[opt$colSeq]])
# adds $vj_group columns
# even if `junc_len` specified, added column is named `vj_group`
if (opt$heavyLight) {
# heavy and light
db = groupGenes(data=db,
v_call=opt$colV,
j_call=opt$colJ,
junc_len=col_tmp_seq_len,
cell_id=opt$colCell,
locus=opt$colLocus,
only_heavy=F,
first=F)
# sanity check
# heavy and light chains from the same cell should have the same partition
stopifnot(all( sapply(unique(db[[opt$colCell]]),
function(s){
# wrt db
s_idx = which(db[[opt$colCell]]==s)
s_bool = length(unique(db[["vj_group"]][s_idx]))==1
return(s_bool)
}, USE.NAMES=F) ))
# all counts of vj_group should be even (heavy:light paired)
stopifnot(all( table(db[["vj_group"]]) %% 2 == 0 ))
} else {
# heavy only
db = groupGenes(data=db,
v_call=opt$colV,
j_call=opt$colJ,
junc_len=col_tmp_seq_len,
cell_id=NULL,
first=F)
}
# rename `vj_group`; remove `vj_group` after renaming
stopifnot("vj_group" %in% colnames(db))
if ("vjl_group" %in% colnames(db)) {
warning("A `vjl_group` column already exists; it will be overwritten.")
}
db[["vjl_group"]] = db[["vj_group"]]
db[["vj_group"]] = NULL
# remove temporary column
db[[col_tmp_seq_len]] = NULL
nrow_af = nrow(db)
# sanity check
# number of rows should remain the same
# unless a row contained NA in any of v_call, j_call, or junc_len
if (nrow_bf!=nrow_af) {
warning("nrow_bf (", nrow_bf, ") != nrow_af (", nrow_af, ")\n")
}
save(db, file=fn)
rm(db, fn)
} else {
cat("\nOnly 1 subject found. Skipping between-subject.\n")
}
}