-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.Rhistory
More file actions
512 lines (512 loc) · 22.4 KB
/
.Rhistory
File metadata and controls
512 lines (512 loc) · 22.4 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
DIR_BOX_BASE <- file.path("/Users", "mattdufort", "Library", "CloudStorage", "Box-Box")
# DR3 / DQ2 = presence of DRB1*03:01 and DQB1*02:01 (this lines up perfectly with the KQ1 and TN18 dr3Result values)
REGEX_DR3_DQ2 <- "^0301\\$[0-9]{4}\\$0201"
## DR4 / DQ8 = presence of DRB1*04:xx and DQB1*03:02 (this lines up perfectly with the KQ1 and TN18 dr4Result values)
# using only DQB1*03:02 is not perfect because there are occasional haplotypes that have it but not DR4 (e.g. 0801$0301$0302 has DR4 = ABSENT in TN18)
# if we don't include the DRB1*04:xx, then
REGEX_DR4_DQ8 <- "^04[0-9]{2}\\$[0-9]{4}\\$0302"
## Function to generate 4 combinations per row (unless haplotypes are identical)
# this function is for generating potential haplotypes from unphased HLA DRB1/DQA1/DQB1 data, to match to a known set of genotypes
generateHlaHaplotypes <- function(row, id_col) {
combos <- list(
c(row["hlaDrb1_1"], row["hlaDqa1_1"], row["hlaDqb1_1"], row["hlaDrb1_2"], row["hlaDqa1_2"], row["hlaDqb1_2"]),
c(row["hlaDrb1_1"], row["hlaDqa1_1"], row["hlaDqb1_2"], row["hlaDrb1_2"], row["hlaDqa1_2"], row["hlaDqb1_1"]),
c(row["hlaDrb1_1"], row["hlaDqa1_2"], row["hlaDqb1_1"], row["hlaDrb1_2"], row["hlaDqa1_1"], row["hlaDqb1_2"]),
c(row["hlaDrb1_1"], row["hlaDqa1_2"], row["hlaDqb1_2"], row["hlaDrb1_2"], row["hlaDqa1_1"], row["hlaDqb1_1"])
)
# Create a data frame with concatenated columns for each combination
data.frame(
id = row[[id_col]],
hlaDrb1Dqa1Dqb1Alpha = sapply(combos, function(x) paste(x[1], x[2], x[3], sep = "$")),
hlaDrb1Dqa1Dqb1Beta = sapply(combos, function(x) paste(x[4], x[5], x[6], sep = "$"))
) %>%
# keep unique combinations
distinct()
}
## Function to extract DQA1/DQB1 haplotypes (with format 0000$0000$0000) from DRB1/DQA1/DQB1 haplotypes (with format 0000$0000$0000)
extractHlaDqa1Dqb1 <- function(hlaDrb1Dqa1Dqb1) {
str_extract(hlaDrb1Dqa1Dqb1, "[0-9]{3}[0-9X]\\$[0-9]{4}$")
}
## Function to determine DR3/DR4 genotype from [DRB1]$[DQA1]$[DQB1] alpha/beta alleles
getGenotypeHlaDr3Dr4 <- function(dataHla, colnamessHla = c("hlaDrb1Dqa1Dqb1Alpha", "hlaDrb1Dqa1Dqb1Beta")) {
## this function counts the DR3 and DR4 alleles in HLA data, and adds columns for counts of those haplotypes and the DR3/DR4 genotype
# it assumes that each row contains both haplotypes, and that data are in the format "[DRB1]$[DQA1]$[DQB1]" where DRB1/DQA1/DQB1 are 4-digit alleles
REGEX_DR3_DQ2 <- "^0301\\$[0-9]{4}\\$0201"
REGEX_DR4_DQ8 <- "^04[0-9]{2}\\$[0-9]{4}\\$0302"
dataHla <-
dataHla %>%
rowwise() %>%
mutate(
countHlaDr3 = sum(str_detect(!!rlang::sym(colnamessHla[1]), REGEX_DR3_DQ2), str_detect(!!rlang::sym(colnamessHla[2]), REGEX_DR3_DQ2)),
countHlaDr4 = sum(str_detect(!!rlang::sym(colnamessHla[1]), REGEX_DR4_DQ8), str_detect(!!rlang::sym(colnamessHla[2]), REGEX_DR4_DQ8)),
genotypeHlaDr3Dr4 =
case_when(
countHlaDr3 == 2 ~ "DR3/DR3",
(countHlaDr3 == 1) & (countHlaDr4 == 0) ~ "DR3/x",
(countHlaDr3 == 1) & (countHlaDr4 == 1) ~ "DR3/DR4",
countHlaDr4 == 2 ~ "DR4/DR4",
(countHlaDr3 == 0) & (countHlaDr4 == 1) ~ "DR4/x",
countHlaDr3 + countHlaDr4 == 0 ~ "x/x",
TRUE ~ "unknown") %>%
factor(levels = c("DR3/DR3", "DR3/x", "DR3/DR4", "DR4/DR4", "DR4/x", "x/x", "unknown"))) %>%
ungroup()
dataHla
}
## Function to match HLA DQA1/DQB1 alleles to a set of alleles containing "X" as wildcard
matchHlaAlleleWithWildcard <- function(allele, patterns, wildcard = "X") {
# replace $ for easier matching with wildcards
allele <- str_replace(allele, "\\$", "_")
patterns <- str_replace(patterns, "\\$", "_")
if (length(allele) > 1)
stop("This function operates on one allele at a time; for application to multiple values, please use sapply()")
for (pat in patterns) {
regex <- gsub(wildcard, "[0-9]", pat) # e.g. A*03:0X -> A*03:[0-9]{2}
if (grepl(paste0("^", regex, "$"), allele)) {
return(str_replace(pat, "_", "$"))
}
}
return(NA)
}
## Function to calculate HLA-specific GRS2 risk based on DQA1/DQB1 haplotypes
calcGrs2HlaOnly <- function(dataHla, scoresGenotype, scoresHaplotype, colnamesHla = c("hlaDrb1Dqa1Dqb1Alpha", "hlaDrb1Dqa1Dqb1Beta")) {
## this function calculates the HLA-DQA1/DQB1 component of the T1D GRS2, using the two haplotypes either in interaction or independently
# it assumes that each row contains both haplotypes, and that data are in the format "[DRB1]$[DQA1]$[DQB1]" where DRB1/DQA1/DQB1 are 4-digit alleles
# Extract DQA1/DQB1 genotypes from DRB1/DQA1/DQB1 genotypes
dataHla$hlaDqa1Dqb1Alpha <- extractHlaDqa1Dqb1(dataHlaTn19[[colnamesHla[1]]])
dataHla$hlaDqa1Dqb1Beta <- extractHlaDqa1Dqb1(dataHlaTn19[[colnamesHla[2]]])
# Extract alleles from score tables, and match the actual data to those alleles (yields NA if not found, which is fine)
allelesHlaAllInScores <-
unique(c(scoresGenotype$hlaDqa1Dqb1Alpha, scoresGenotype$hlaDqa1Dqb1Beta, scoresHaplotype$hlaDqa1Dqb1))
dataHla$hlaDqa1Dqb1Alpha <-
sapply(dataHla$hlaDqa1Dqb1Alpha, FUN = matchHlaAlleleWithWildcard, patterns = allelesHlaAllInScores)
dataHla$hlaDqa1Dqb1Beta <-
sapply(dataHla$hlaDqa1Dqb1Beta, FUN = matchHlaAlleleWithWildcard, patterns = allelesHlaAllInScores)
# Ensure genotype score alleles are sorted alphabetically to standardize
scoresGenotype$key <- apply(scoresGenotype[, c("hlaDqa1Dqb1Alpha", "hlaDqa1Dqb1Beta")], 1, function(x) paste(sort(x), collapse = "|"))
# Create a named vector for quick haplotype lookup
scoresVecHaplo <- setNames(scoresHaplotype$score, scoresHaplotype$hlaDqa1Dqb1)
# Result vector
scoresResult <- numeric(nrow(dataHla))
for (i in seq_len(nrow(dataHla))) {
a1 <- dataHla$hlaDqa1Dqb1Alpha[i]
a2 <- dataHla$hlaDqa1Dqb1Beta[i]
key <- paste(sort(c(a1, a2)), collapse = "|")
# Try to match genotype
matchIndex <- match(key, scoresGenotype$key)
if (!is.na(matchIndex)) {
scoresResult[i] <- scoresGenotype$score[matchIndex]
} else {
# Fallback to haplotype sum
score1 <- scoresVecHaplo[a1]
score2 <- scoresVecHaplo[a2]
scoresResult[i] <- sum(c(score1, score2), na.rm = TRUE)
}
}
return(scoresResult)
}
## load HLA genotype / haplotype scores from files
dataScoresHlaGenotypes <-
readxl::read_xlsx(
file.path(DIR_BOX_BASE, "Projects", "T1D_Tscm_disease_variation", "data", "GRS2 SNPs_Axiom array.xlsx"),
sheet = "scoresHlaGenotype") %>%
standardizeDimnames() %>%
mutate(hlaDqa1Dqb1Alpha = paste(dqa1_1, dqb1_1, sep = "$") %>% str_remove_all("\\:"),
hlaDqa1Dqb1Beta = paste(dqa1_2, dqb1_2, sep = "$") %>% str_remove_all("\\:")) %>%
dplyr::rename(score = beta)
dataScoresHlaHaplotypes <-
readxl::read_xlsx(
file.path(DIR_BOX_BASE, "Projects", "T1D_Tscm_disease_variation", "data", "GRS2 SNPs_Axiom array.xlsx"),
sheet = "scoresHlaHaplotype") %>%
standardizeDimnames() %>%
mutate(hlaDqa1Dqb1 = paste(dqa1, dqb1, sep = "$") %>% str_remove_all("\\:")) %>%
dplyr::rename(score = beta)
## save all of these objects for later use
save(list = setdiff(ls(), "DIR_BOX_BASE"),
file = file.path(DIR_BOX_BASE, "code", "functionsDataForGrs2HlaScores.RData"))
## save all of these objects for later use
save(list = setdiff(ls(), "DIR_BOX_BASE"),
file = file.path(DIR_BOX_BASE, "code", "Projects", "T1D_Tscm_disease_variation", "functionsDataForGrs2HlaScores.RData"))
## save all of these objects for later use
save(list = setdiff(ls(), "DIR_BOX_BASE"),
file = file.path(DIR_BOX_BASE, "Projects", "T1D_Tscm_disease_variation", "code", "functionsDataForGrs2HlaScores.RData"))
rm(list = ls())
#### GRS2 / HLA functions
### This is a collection of functions for determining GRS2 scores and HLA alleles.
## It is bundled here to make it easier to use across datasets, without building a proper R package
library(miscHelpers)
load_packages_with_install("tidyverse")
DIR_BOX_BASE <- file.path("/Users", "mattdufort", "Library", "CloudStorage", "Box-Box")
## Function to generate 4 combinations per row (unless haplotypes are identical)
# this function is for generating potential haplotypes from unphased HLA DRB1/DQA1/DQB1 data, to match to a known set of genotypes
generateHlaHaplotypes <- function(row, id_col) {
combos <- list(
c(row["hlaDrb1_1"], row["hlaDqa1_1"], row["hlaDqb1_1"], row["hlaDrb1_2"], row["hlaDqa1_2"], row["hlaDqb1_2"]),
c(row["hlaDrb1_1"], row["hlaDqa1_1"], row["hlaDqb1_2"], row["hlaDrb1_2"], row["hlaDqa1_2"], row["hlaDqb1_1"]),
c(row["hlaDrb1_1"], row["hlaDqa1_2"], row["hlaDqb1_1"], row["hlaDrb1_2"], row["hlaDqa1_1"], row["hlaDqb1_2"]),
c(row["hlaDrb1_1"], row["hlaDqa1_2"], row["hlaDqb1_2"], row["hlaDrb1_2"], row["hlaDqa1_1"], row["hlaDqb1_1"])
)
# Create a data frame with concatenated columns for each combination
data.frame(
id = row[[id_col]],
hlaDrb1Dqa1Dqb1Alpha = sapply(combos, function(x) paste(x[1], x[2], x[3], sep = "$")),
hlaDrb1Dqa1Dqb1Beta = sapply(combos, function(x) paste(x[4], x[5], x[6], sep = "$"))
) %>%
# keep unique combinations
distinct()
}
## Function to extract DQA1/DQB1 haplotypes (with format 0000$0000$0000) from DRB1/DQA1/DQB1 haplotypes (with format 0000$0000$0000)
extractHlaDqa1Dqb1 <- function(hlaDrb1Dqa1Dqb1) {
str_extract(hlaDrb1Dqa1Dqb1, "[0-9]{3}[0-9X]\\$[0-9]{4}$")
}
# # DR3 / DQ2 = presence of DRB1*03:01 and DQB1*02:01 (this lines up perfectly with the KQ1 and TN18 dr3Result values)
# REGEX_DR3_DQ2 <- "^0301\\$[0-9]{4}\\$0201"
#
# ## DR4 / DQ8 = presence of DRB1*04:xx and DQB1*03:02 (this lines up perfectly with the KQ1 and TN18 dr4Result values)
# # using only DQB1*03:02 is not perfect because there are occasional haplotypes that have it but not DR4 (e.g. 0801$0301$0302 has DR4 = ABSENT in TN18)
# # if we don't include the DRB1*04:xx, then
# REGEX_DR4_DQ8 <- "^04[0-9]{2}\\$[0-9]{4}\\$0302"
## Function to determine DR3/DR4 genotype from [DRB1]$[DQA1]$[DQB1] alpha/beta alleles
getGenotypeHlaDr3Dr4 <- function(dataHla, colnamessHla = c("hlaDrb1Dqa1Dqb1Alpha", "hlaDrb1Dqa1Dqb1Beta")) {
## this function counts the DR3 and DR4 alleles in HLA data, and adds columns for counts of those haplotypes and the DR3/DR4 genotype
# it assumes that each row contains both haplotypes, and that data are in the format "[DRB1]$[DQA1]$[DQB1]" where DRB1/DQA1/DQB1 are 4-digit alleles
REGEX_DR3_DQ2 <- "^0301\\$[0-9]{4}\\$0201"
REGEX_DR4_DQ8 <- "^04[0-9]{2}\\$[0-9]{4}\\$0302"
dataHla <-
dataHla %>%
rowwise() %>%
mutate(
countHlaDr3 = sum(str_detect(!!rlang::sym(colnamessHla[1]), REGEX_DR3_DQ2), str_detect(!!rlang::sym(colnamessHla[2]), REGEX_DR3_DQ2)),
countHlaDr4 = sum(str_detect(!!rlang::sym(colnamessHla[1]), REGEX_DR4_DQ8), str_detect(!!rlang::sym(colnamessHla[2]), REGEX_DR4_DQ8)),
genotypeHlaDr3Dr4 =
case_when(
countHlaDr3 == 2 ~ "DR3/DR3",
(countHlaDr3 == 1) & (countHlaDr4 == 0) ~ "DR3/x",
(countHlaDr3 == 1) & (countHlaDr4 == 1) ~ "DR3/DR4",
countHlaDr4 == 2 ~ "DR4/DR4",
(countHlaDr3 == 0) & (countHlaDr4 == 1) ~ "DR4/x",
countHlaDr3 + countHlaDr4 == 0 ~ "x/x",
TRUE ~ "unknown") %>%
factor(levels = c("DR3/DR3", "DR3/x", "DR3/DR4", "DR4/DR4", "DR4/x", "x/x", "unknown"))) %>%
ungroup()
dataHla
}
## Function to match HLA DQA1/DQB1 alleles to a set of alleles containing "X" as wildcard
matchHlaAlleleWithWildcard <- function(allele, patterns, wildcard = "X") {
# replace $ for easier matching with wildcards
allele <- str_replace(allele, "\\$", "_")
patterns <- str_replace(patterns, "\\$", "_")
if (length(allele) > 1)
stop("This function operates on one allele at a time; for application to multiple values, please use sapply()")
for (pat in patterns) {
regex <- gsub(wildcard, "[0-9]", pat) # e.g. A*03:0X -> A*03:[0-9]{2}
if (grepl(paste0("^", regex, "$"), allele)) {
return(str_replace(pat, "_", "$"))
}
}
return(NA)
}
## Function to calculate HLA-specific GRS2 risk based on DQA1/DQB1 haplotypes
calcGrs2HlaOnly <- function(dataHla, scoresGenotype, scoresHaplotype, colnamesHla = c("hlaDrb1Dqa1Dqb1Alpha", "hlaDrb1Dqa1Dqb1Beta")) {
## this function calculates the HLA-DQA1/DQB1 component of the T1D GRS2, using the two haplotypes either in interaction or independently
# it assumes that each row contains both haplotypes, and that data are in the format "[DRB1]$[DQA1]$[DQB1]" where DRB1/DQA1/DQB1 are 4-digit alleles
# Extract DQA1/DQB1 genotypes from DRB1/DQA1/DQB1 genotypes
dataHla$hlaDqa1Dqb1Alpha <- extractHlaDqa1Dqb1(dataHlaTn19[[colnamesHla[1]]])
dataHla$hlaDqa1Dqb1Beta <- extractHlaDqa1Dqb1(dataHlaTn19[[colnamesHla[2]]])
# Extract alleles from score tables, and match the actual data to those alleles (yields NA if not found, which is fine)
allelesHlaAllInScores <-
unique(c(scoresGenotype$hlaDqa1Dqb1Alpha, scoresGenotype$hlaDqa1Dqb1Beta, scoresHaplotype$hlaDqa1Dqb1))
dataHla$hlaDqa1Dqb1Alpha <-
sapply(dataHla$hlaDqa1Dqb1Alpha, FUN = matchHlaAlleleWithWildcard, patterns = allelesHlaAllInScores)
dataHla$hlaDqa1Dqb1Beta <-
sapply(dataHla$hlaDqa1Dqb1Beta, FUN = matchHlaAlleleWithWildcard, patterns = allelesHlaAllInScores)
# Ensure genotype score alleles are sorted alphabetically to standardize
scoresGenotype$key <- apply(scoresGenotype[, c("hlaDqa1Dqb1Alpha", "hlaDqa1Dqb1Beta")], 1, function(x) paste(sort(x), collapse = "|"))
# Create a named vector for quick haplotype lookup
scoresVecHaplo <- setNames(scoresHaplotype$score, scoresHaplotype$hlaDqa1Dqb1)
# Result vector
scoresResult <- numeric(nrow(dataHla))
for (i in seq_len(nrow(dataHla))) {
a1 <- dataHla$hlaDqa1Dqb1Alpha[i]
a2 <- dataHla$hlaDqa1Dqb1Beta[i]
key <- paste(sort(c(a1, a2)), collapse = "|")
# Try to match genotype
matchIndex <- match(key, scoresGenotype$key)
if (!is.na(matchIndex)) {
scoresResult[i] <- scoresGenotype$score[matchIndex]
} else {
# Fallback to haplotype sum
score1 <- scoresVecHaplo[a1]
score2 <- scoresVecHaplo[a2]
scoresResult[i] <- sum(c(score1, score2), na.rm = TRUE)
}
}
return(scoresResult)
}
## load HLA genotype / haplotype scores from files
dataScoresHlaGenotypes <-
readxl::read_xlsx(
file.path(DIR_BOX_BASE, "Projects", "T1D_Tscm_disease_variation", "data", "GRS2 SNPs_Axiom array.xlsx"),
sheet = "scoresHlaGenotype") %>%
standardizeDimnames() %>%
mutate(hlaDqa1Dqb1Alpha = paste(dqa1_1, dqb1_1, sep = "$") %>% str_remove_all("\\:"),
hlaDqa1Dqb1Beta = paste(dqa1_2, dqb1_2, sep = "$") %>% str_remove_all("\\:")) %>%
dplyr::rename(score = beta)
dataScoresHlaHaplotypes <-
readxl::read_xlsx(
file.path(DIR_BOX_BASE, "Projects", "T1D_Tscm_disease_variation", "data", "GRS2 SNPs_Axiom array.xlsx"),
sheet = "scoresHlaHaplotype") %>%
standardizeDimnames() %>%
mutate(hlaDqa1Dqb1 = paste(dqa1, dqb1, sep = "$") %>% str_remove_all("\\:")) %>%
dplyr::rename(score = beta)
## save all of these objects for later use
save(list = setdiff(ls(), "DIR_BOX_BASE"),
file = file.path(DIR_BOX_BASE, "Projects", "T1D_Tscm_disease_variation", "code", "functionsDataForGrs2HlaScores.RData"))
?aggregate
tabulate_shared_TCR_chains <-
function(tcr_chain_matches,
tcr1_col = "tcr1", tcr2_col = "tcr2") {
checkmate::assert(
checkmate::check_data_frame(tcr_chain_matches)
)
if (nrow(tcr_chain_matches) == 0) {
tcr_chains_shared <-
cbind(
tcr_chain_matches[, c(tcr1_col, tcr2_col)],
"num_shared_chains" = integer())
} else {
# convert numeric column identifiers to column names
for (i in c("tcr1_col", "tcr2_col"))
if (is.numeric(get(i))) assign(i, colnames(tcrs)[get(i)])
# extract all the matches
tcr_chains_shared <-
summarise(
group_by(tcr_chain_matches, !!rlang::sym(tcr1_col), !!rlang::sym(tcr2_col)),
num_shared_chains = n())
# make the order of the cells consistent, and drop the duplicates
for (i in 1:nrow(tcr_chains_shared)) {
tcr_chains_shared[i, c(tcr1_col, tcr2_col)] <-
tcr_chains_shared[i, c(tcr1_col, tcr2_col)] %>%
unlist() %>%
sort()
}
tcr_chains_shared <- distinct(tcr_chains_shared)
}
tcr_chains_shared
}
col
??summarise
?summarise
?distinct
?group_by
?summarise
rowwise
?dplyr::rowwise
?sort
tabulate_shared_TCR_chains <-
function(tcr_chain_matches,
tcr1_col = "tcr1", tcr2_col = "tcr2") {
checkmate::assert(
checkmate::check_data_frame(tcr_chain_matches)
)
if (nrow(tcr_chain_matches) == 0) {
tcr_chains_shared <-
cbind(
tcr_chain_matches[, c(tcr1_col, tcr2_col)],
"num_shared_chains" = integer())
} else {
# convert numeric column identifiers to column names
for (currTcrColName in c("tcr1_col", "tcr2_col"))
if (is.numeric(get(currTcrColName))) assign(currTcrColName, colnames(tcrs)[get(currTcrColName)])
# extract all the matches
tcr_chains_shared <-
dplyr::summarise(
dplyr::group_by(tcr_chain_matches, !!rlang::sym(tcr1_col), !!rlang::sym(tcr2_col)),
num_shared_chains = n())
tcr_chains_shared <- as.data.frame(tcr_chains_shared)
# # make the order of the cells consistent, and drop the duplicates
# for (currRow in seq_len(nrow(tcr_chains_shared))) {
# tcr_chains_shared[currRow, c(tcr1_col, tcr2_col)] <-
# tcr_chains_shared[currRow, c(tcr1_col, tcr2_col)] %>%
# unlist() %>%
# sort()
# }
# tcr_chains_shared <- distinct(tcr_chains_shared)
# drop duplicates where tcr1_col == tcr2_col, alternate approach
tcr_chains_shared <-
mutate(rowwise(tcr_chains_shared), tcrs_combined = paste(sort(c(!!rlang::sym(tcr1_col), !!rlang::sym(tcr2_col)))))
tcr_chains_shared <-
tcr_chains_shared[!duplicated(tcr_chains_shared$tcrs_combined),]
tcr_chains_shared$tcrs_combined <- NULL
}
tcr_chains_shared
}
?paste
tabulate_shared_TCR_chains <-
function(tcr_chain_matches,
tcr1_col = "tcr1", tcr2_col = "tcr2") {
checkmate::assert(
checkmate::check_data_frame(tcr_chain_matches)
)
if (nrow(tcr_chain_matches) == 0) {
tcr_chains_shared <-
cbind(
tcr_chain_matches[, c(tcr1_col, tcr2_col)],
"num_shared_chains" = integer())
} else {
# convert numeric column identifiers to column names
for (currTcrColName in c("tcr1_col", "tcr2_col"))
if (is.numeric(get(currTcrColName))) assign(currTcrColName, colnames(tcrs)[get(currTcrColName)])
# extract all the matches
tcr_chains_shared <-
dplyr::summarise(
dplyr::group_by(tcr_chain_matches, !!rlang::sym(tcr1_col), !!rlang::sym(tcr2_col)),
num_shared_chains = n())
tcr_chains_shared <- as.data.frame(tcr_chains_shared)
# # make the order of the cells consistent, and drop the duplicates
# for (currRow in seq_len(nrow(tcr_chains_shared))) {
# tcr_chains_shared[currRow, c(tcr1_col, tcr2_col)] <-
# tcr_chains_shared[currRow, c(tcr1_col, tcr2_col)] %>%
# unlist() %>%
# sort()
# }
# tcr_chains_shared <- distinct(tcr_chains_shared)
# drop duplicates where tcr1_col == tcr2_col, alternate approach
tcr_chains_shared <-
mutate(rowwise(tcr_chains_shared),
tcrs_combined = paste(sort(c(!!rlang::sym(tcr1_col), !!rlang::sym(tcr2_col))), collapse = ""))
tcr_chains_shared <- as.data.frame(tcr_chains_shared)
tcr_chains_shared <-
tcr_chains_shared[!duplicated(tcr_chains_shared$tcrs_combined),]
tcr_chains_shared$tcrs_combined <- NULL
}
tcr_chains_shared
}
tabulate_shared_TCR_chains <-
function(tcr_chain_matches,
tcr1_col = "tcr1", tcr2_col = "tcr2") {
checkmate::assert(
checkmate::check_data_frame(tcr_chain_matches)
)
if (nrow(tcr_chain_matches) == 0) {
tcr_chains_shared <-
cbind(
tcr_chain_matches[, c(tcr1_col, tcr2_col)],
"num_shared_chains" = integer())
} else {
# convert numeric column identifiers to column names
for (currTcrColName in c("tcr1_col", "tcr2_col"))
if (is.numeric(get(currTcrColName))) assign(currTcrColName, colnames(tcrs)[get(currTcrColName)])
# extract all the matches
tcr_chains_shared <-
dplyr::summarise(
dplyr::group_by(tcr_chain_matches, !!rlang::sym(tcr1_col), !!rlang::sym(tcr2_col)),
num_shared_chains = n())
tcr_chains_shared <- as.data.frame(tcr_chains_shared)
# # make the order of the cells consistent, and drop the duplicates
# for (currRow in seq_len(nrow(tcr_chains_shared))) {
# tcr_chains_shared[currRow, c(tcr1_col, tcr2_col)] <-
# tcr_chains_shared[currRow, c(tcr1_col, tcr2_col)] %>%
# unlist() %>%
# sort()
# }
# tcr_chains_shared <- distinct(tcr_chains_shared)
# drop duplicates where tcr1_col == tcr2_col, alternate approach
tcr_chains_shared <-
mutate(rowwise(tcr_chains_shared),
tcrs_combined = paste(sort(c(!!rlang::sym(tcr1_col), !!rlang::sym(tcr2_col))), collapse = ""))
tcr_chains_shared <- as.data.frame(ungroup(tcr_chains_shared))
tcr_chains_shared <-
tcr_chains_shared[!duplicated(tcr_chains_shared$tcrs_combined),]
tcr_chains_shared$tcrs_combined <- NULL
}
tcr_chains_shared
}
rm(list = ls())
# setwd("~/Box Sync/Tools/R_scripts/R_packages")
library(devtools)
library(roxygen2)
packagesDirectory <-
file.path("~", "Library", "CloudStorage", "Box-Box",
"Tools", "R_scripts", "R_packages")
# packageName <- "miscHelpers"
# packageName <- "comboVisTools"
# packageName <- "corrMatrix"
# packageName <- "countSubsetNorm"
# packageName <- "geneSetTools"
# packageName <- "limmaTools"
# packageName <- "MASTtools"
# packageName <- "RNAseQC"
# packageName <- "WGCNAtools"
# packageName <- "R10Xtools"
packageName <- "TCRtools"
wd.tmp <- getwd()
setwd(file.path(packagesDirectory, packageName))
# usethis::create_package(packageName) # initial package creation
devtools::document()
credentials::set_github_pat()
devtools::install(file.path("..", packageName))
setwd(wd.tmp)
rm(wd.tmp)
# check package contents
devtools::check(file.path(packagesDirectory, packageName))
wd.tmp <- getwd()
setwd(file.path(packagesDirectory, packageName))
# usethis::create_package(packageName) # initial package creation
devtools::document()
credentials::set_github_pat()
devtools::install(file.path("..", packageName))
setwd(wd.tmp)
rm(wd.tmp)
# check package contents
devtools::check(file.path(packagesDirectory, packageName))
# setwd("~/Box Sync/Tools/R_scripts/R_packages")
library(devtools)
library(roxygen2)
packagesDirectory <-
file.path("~", "Library", "CloudStorage", "Box-Box",
"Tools", "R_scripts", "R_packages")
packageName <- "TCRtools"
wd.tmp <- getwd()
setwd(file.path(packagesDirectory, packageName))
# usethis::create_package(packageName) # initial package creation
devtools::document()
credentials::set_github_pat()
devtools::install(file.path("..", packageName))
setwd(wd.tmp)
rm(wd.tmp)
# check package contents
devtools::check(file.path(packagesDirectory, packageName))
# setwd("~/Box Sync/Tools/R_scripts/R_packages")
library(devtools)
library(roxygen2)
packagesDirectory <-
file.path("~", "Library", "CloudStorage", "Box-Box",
"Tools", "R_scripts", "R_packages")
packageName <- "TCRtools"
wd.tmp <- getwd()
setwd(file.path(packagesDirectory, packageName))
# usethis::create_package(packageName) # initial package creation
devtools::document()
credentials::set_github_pat()
devtools::install(file.path("..", packageName))
setwd(wd.tmp)
rm(wd.tmp)
# check package contents
devtools::check(file.path(packagesDirectory, packageName))
# check package contents
devtools::check(file.path(packagesDirectory, packageName))
credentials::set_github_pat()
devtools::install_github(paste("BenaroyaResearch", packageName, sep="/"))
# setwd("~/Box Sync/Tools/R_scripts/R_packages")
library(devtools)
library(roxygen2)
packagesDirectory <-
file.path("~", "Library", "CloudStorage", "Box-Box",
"Tools", "R_scripts", "R_packages")
packageName <- "miscHelpers"
wd.tmp <- getwd()
setwd(file.path(packagesDirectory, packageName))
# usethis::create_package(packageName) # initial package creation
devtools::document()
credentials::set_github_pat()
devtools::install(file.path("..", packageName))