-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 23.3 KB
/
.Rhistory
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
}
n_int <- ncol(Z_int)
p_two <- numeric(n_int)
s_two <- numeric(n_int)
for (i in 1:n_int) {
dat <- if (is.null(random_effect)) {
data.frame(Y = Z_int[, i], factor1 = factor1, factor2 = factor2, factor3 = factor3)
} else {
data.frame(Y = Z_int[, i], factor1= factor1, factor2 = factor2, factor3 = factor3, random_effect = random_effect)
}
# Two-way test: compare lm_int_alt2way vs. lm_full
res_two <- run_feature_anova(dat, formulas$lm_int_alt2way, formulas$lm_full, func, test_func)
p_two[i] <- res_two[1]
s_two[i] <- res_two[2]
}
p_two_adj <- if (is.null(adj_method)) p_two else p.adjust(p_two, method = adj_method)
pval_matrix[idx_int, 6] <- p_two_adj
stat_matrix[idx_int, 6] <- s_two
class_df[idx_int, "Int"] <- ifelse(pval_matrix[idx_int, 6] < threeways, "threeways", "twoways")
# Further two-way tests on features classified as "twoways"
idx_twoways <- which(class_df$Int == "twoways")
if (length(idx_twoways) > 0) {
if (length(idx_twoways) > 1) {
Z_twoways <- Z[, idx_twoways]
} else {
Z_twoways <- data.frame(Z[, idx_twoways], check.names = FALSE)
colnames(Z_twoways) <- colnames(Z)[idx_twoways]
rownames(Z_twoways) <- rownames(Z)
}
n_twoways <- ncol(Z_twoways)
p_f1f2 <- numeric(n_twoways); s_f1f2 <- numeric(n_twoways)
p_f1f3 <- numeric(n_twoways); s_f1f3 <- numeric(n_twoways)
p_f2f3 <- numeric(n_twoways); s_f2f3 <- numeric(n_twoways)
for (i in 1:n_twoways) {
dat <- if (is.null(random_effect)) {
data.frame(Y = Z_twoways[, i], factor1 = factor1, factor2 = factor2, factor3 = factor3)
} else {
data.frame(Y = Z_twoways[, i], factor1= factor1, factor2 = factor2, factor3 = factor3, random_effect = random_effect)
}
# Test F1F2: compare lm_int_altf1f2 vs. lm_int_alt2way
res_f1f2 <- run_feature_anova(dat, formulas$lm_int_altf1f2, formulas$lm_int_alt2way, func, test_func)
p_f1f2[i] <- res_f1f2[1]; s_f1f2[i] <- res_f1f2[2]
# Test F1F3: compare lm_int_altf1f3 vs. lm_int_alt2way
res_f1f3 <- run_feature_anova(dat, formulas$lm_int_altf1f3, formulas$lm_int_alt2way, func, test_func)
p_f1f3[i] <- res_f1f3[1]; s_f1f3[i] <- res_f1f3[2]
# Test F2F3: compare lm_int_altf2f3 vs. lm_int_alt2way
res_f2f3 <- run_feature_anova(dat, formulas$lm_int_altf2f3, formulas$lm_int_alt2way, func, test_func)
p_f2f3[i] <- res_f2f3[1]; s_f2f3[i] <- res_f2f3[2]
}
p_f1f2_adj <- if (is.null(adj_method)) p_f1f2 else p.adjust(p_f1f2, method = adj_method)
p_f1f3_adj <- if (is.null(adj_method)) p_f1f3 else p.adjust(p_f1f3, method = adj_method)
p_f2f3_adj <- if (is.null(adj_method)) p_f2f3 else p.adjust(p_f2f3, method = adj_method)
pval_matrix[idx_twoways, 7] <- p_f1f2_adj
pval_matrix[idx_twoways, 8] <- p_f2f3_adj
pval_matrix[idx_twoways, 9] <- p_f1f3_adj
stat_matrix[idx_twoways, 7] <- s_f1f2
stat_matrix[idx_twoways, 8] <- s_f2f3
stat_matrix[idx_twoways, 9] <- s_f1f3
cls_twoways <- rep("twowaycombinations", length(idx_twoways))
cls_twoways[pval_matrix[idx_twoways, 7] <= F1F2 & pval_matrix[idx_twoways, 8] > F2F3 & pval_matrix[idx_twoways, 9] > F1F3] <- "F1F2"
cls_twoways[pval_matrix[idx_twoways, 7] > F1F2 & pval_matrix[idx_twoways, 8] <= F2F3 & pval_matrix[idx_twoways, 9] > F1F3] <- "F2F3"
cls_twoways[pval_matrix[idx_twoways, 7] > F1F2 & pval_matrix[idx_twoways, 8] > F2F3 & pval_matrix[idx_twoways, 9] <= F1F3] <- "F1F3"
class_df[idx_twoways, "twoways"] <- cls_twoways
}
}
# Set column names and return results
colnames(pval_matrix) <- colnames(stat_matrix) <- c("Sig0", "Intornotint", "F1", "F2", "F3",
"twowaysorthreeways", "F1F2", "F2F3", "F1F3")
rownames(pval_matrix) <- rownames(stat_matrix) <- class_df$varname
return(list(pval_matrix = pval_matrix, stat_matrix = stat_matrix, class_df = class_df))
}
oc3=iDAS_3F(Z = log2(data.frame(t(all.meanExprs[rowSums(all.meanExprs>0)>1,]),check.names = F)+1),
factor1 = all.timepoint,
factor2 = all.pcellstats,
factor3 = all.pcelltype,
random_effect = all.pid, test_func = "lmer",
Sig_cutoff = 0.02,Int = 0.01,F1 = 0.01,F2=0.02,F3=0.01,adj_method = "BH")
library(iDAS)
detach("package:iDAS", unload = TRUE)
# Helper: Build model formulas for three-factor analysis
build_formulas_3F <- function(factor_tmp, test_func, random_effect) {
if (test_func == "lm" && is.null(random_effect)) {
lm_full <- paste("Y ~ (", factor_tmp$factor1_tmp, ")*(", factor_tmp$factor2_tmp, ")*(", factor_tmp$factor3_tmp, ")", sep = "")
lm_int_alt2way <- paste("Y ~ (", factor_tmp$factor2_tmp, "):(", factor_tmp$factor3_tmp, ") + (",
factor_tmp$factor1_tmp, "):(", factor_tmp$factor3_tmp, ") + (",
factor_tmp$factor1_tmp, "):(", factor_tmp$factor2_tmp, ") + (",
factor_tmp$factor1_tmp, ") + (", factor_tmp$factor2_tmp, ") + (",
factor_tmp$factor3_tmp, ")", sep = "")
lm_int_altf1f2 <- paste("Y~(", factor_tmp$factor2_tmp, "):(", factor_tmp$factor3_tmp, ")+(",
factor_tmp$factor1_tmp, "):(", factor_tmp$factor3_tmp, ")+(",
factor_tmp$factor1_tmp, ")+(", factor_tmp$factor2_tmp, ")+(",
factor_tmp$factor3_tmp,")",
sep = "")
lm_int_altf1f3 <- paste("Y~(", factor_tmp$factor2_tmp, "):(", factor_tmp$factor3_tmp, ")+(",
factor_tmp$factor1_tmp, "):(", factor_tmp$factor2_tmp, ")+(",
factor_tmp$factor1_tmp, ")+(", factor_tmp$factor2_tmp, ")+(",
factor_tmp$factor3_tmp,")",
sep = "")
lm_int_altf2f3 <- paste("Y~(", factor_tmp$factor1_tmp, "):(", factor_tmp$factor3_tmp, ")+(",
factor_tmp$factor1_tmp, "):(", factor_tmp$factor2_tmp, ")+(",
factor_tmp$factor1_tmp, ")+(", factor_tmp$factor2_tmp, ")+(",
factor_tmp$factor3_tmp,")",
sep = "")
lm_int_null <- paste("Y ~ (", factor_tmp$factor1_tmp, ") + (", factor_tmp$factor2_tmp, ") + (",
factor_tmp$factor3_tmp, ")", sep = "")
lm_f1 <- paste("Y ~ (", factor_tmp$factor1_tmp, ")", sep = "")
lm_f2 <- paste("Y ~ (", factor_tmp$factor2_tmp, ")", sep = "")
lm_f3 <- paste("Y ~ (", factor_tmp$factor3_tmp, ")", sep = "")
} else {
lm_full <- paste("Y ~ (", factor_tmp$factor1_tmp, ")*(", factor_tmp$factor2_tmp, ")*(", factor_tmp$factor3_tmp,
") + (1|", factor_tmp$random_effect_tmp, ")", sep = "")
lm_int_alt2way <- paste("Y ~ (", factor_tmp$factor2_tmp, "):(", factor_tmp$factor3_tmp, ") + (",
factor_tmp$factor1_tmp, "):(", factor_tmp$factor3_tmp, ") + (",
factor_tmp$factor1_tmp, "):(", factor_tmp$factor2_tmp, ") + (",
factor_tmp$factor1_tmp, ") + (", factor_tmp$factor2_tmp, ") + (",
factor_tmp$factor3_tmp, ") + (1|", factor_tmp$random_effect_tmp, ")", sep = "")
lm_int_altf1f2 <- paste("Y~(", factor_tmp$factor2_tmp, "):(", factor_tmp$factor3_tmp, ")+(",
factor_tmp$factor1_tmp, "):(", factor_tmp$factor3_tmp, ")+(",
factor_tmp$factor1_tmp, ")+(", factor_tmp$factor2_tmp, ")+(",
factor_tmp$factor3_tmp,")+(1|",factor_tmp$random_effect_tmp,")",sep = "")
lm_int_altf1f3 <- paste("Y~(", factor_tmp$factor2_tmp, "):(", factor_tmp$factor3_tmp, ")+(",
factor_tmp$factor1_tmp, "):(", factor_tmp$factor2_tmp, ")+(",
factor_tmp$factor1_tmp, ")+(", factor_tmp$factor2_tmp, ")+(",
factor_tmp$factor3_tmp,")+(1|",factor_tmp$random_effect_tmp,")",sep = "")
lm_int_altf2f3 <- paste("Y~(", factor_tmp$factor1_tmp, "):(", factor_tmp$factor3_tmp, ")+(",
factor_tmp$factor1_tmp, "):(", factor_tmp$factor2_tmp, ")+(",
factor_tmp$factor1_tmp, ")+(", factor_tmp$factor2_tmp, ")+(",
factor_tmp$factor3_tmp,")+(1|",factor_tmp$random_effect_tmp,")",sep = "")
lm_int_null <- paste("Y ~ (", factor_tmp$factor1_tmp, ") + (", factor_tmp$factor2_tmp, ") + (",
factor_tmp$factor3_tmp, ") + (1|", factor_tmp$random_effect_tmp, ")", sep = "")
lm_f1 <- paste("Y ~ (", factor_tmp$factor1_tmp, ") + (1|", factor_tmp$random_effect_tmp, ")", sep = "")
lm_f2 <- paste("Y ~ (", factor_tmp$factor2_tmp, ") + (1|", factor_tmp$random_effect_tmp, ")", sep = "")
lm_f3 <- paste("Y ~ (", factor_tmp$factor3_tmp, ") + (1|", factor_tmp$random_effect_tmp, ")", sep = "")
}
return(list(lm_full = lm_full,
lm_int_alt2way = lm_int_alt2way,
lm_int_altf1f2 = lm_int_altf1f2,
lm_int_altf1f3 = lm_int_altf1f3,
lm_int_altf2f3 = lm_int_altf2f3,
lm_int_null = lm_int_null,
lm_f1 = lm_f1,
lm_f2 = lm_f2,
lm_f3 = lm_f3))
}
# Helper: Run an ANOVA test for a single feature
run_feature_anova <- function(dat, calc0, calc1, func, test_func) {
if (test_func == "lm") {
M0 <- func(formula(calc0), data = dat)
M1 <- func(formula(calc1), data = dat)
res <- anova(M0, M1, test = "F")
p <- res[2, ncol(res)]
stat <- res[2, ncol(res) - 1]
} else {
M0 <- func(formula(calc0), data = dat)
M1 <- func(formula(calc1), data = dat)
res <- anova(M0, M1, test = "F", refit = FALSE)
p <- res[2, ncol(res)]
stat <- res[2, ncol(res) - 2]
}
return(c(p, stat))
}
# Main function: iDAS_3F
iDAS_3F <- function(Z, factor1, factor2, factor3, random_effect = NULL, test_func = "lm",
Sig_cutoff = 0.02, Sig = 0.05, Int = 0.01,
F1 = 0.01, F2 = 0.01, F3 = 0.01,
F1F2 = 0.01, F1F3 = 0.01, F2F3 = 0.01,
threeways = 0.01,
adj_method = "BH", factor1_name = NULL, factor2_name = NULL,
factor3_name = NULL, random_effect_name = NULL) {
# Initialize matrices for p-values and test statistics (9 columns)
pval_matrix <- stat_matrix <- matrix(NA, nrow = ncol(Z), ncol = 9)
# Format factors (assumes check_factor_name3 is defined externally)
factor_tmp <- check_factor_name(factor1_name=factor1_name, factor2_name=factor2_name,
factor3_name=factor3_name, random_effect_name=random_effect_name,
factor1=factor1, factor2=factor2, factor3=factor3, random_effect=random_effect)
# Build all necessary formulas using helper function
formulas <- build_formulas_3F(factor_tmp, test_func, random_effect)
# Select base model formula and model fitting function
if (test_func == "lm" && is.null(random_effect)) {
calc0_overall <- "Y ~ 1"
func <- lm
} else {
calc0_overall <- "Y ~ 1 + (1|random_effect)"
func <- lmer
}
calc1_overall <- formulas$lm_full
nfeat <- ncol(Z)
p_sig <- numeric(nfeat)
s_sig <- numeric(nfeat)
# Loop over features: Overall (full) model test
for (i in 1:nfeat) {
dat <- if (is.null(random_effect)) {
data.frame(Y = Z[, i], factor1 = factor1, factor2 = factor2, factor3 = factor3)
} else {
data.frame(Y = Z[, i], factor1= factor1, factor2 = factor2, factor3 = factor3, random_effect = random_effect)
}
res <- run_feature_anova(dat, calc0_overall, calc1_overall, func, test_func)
p_sig[i] <- res[1]
s_sig[i] <- res[2]
}
p_sig_adj <- if (is.null(adj_method)) p_sig else p.adjust(p_sig, method = adj_method)
pval_matrix[, 1] <- p_sig_adj
stat_matrix[, 1] <- s_sig
# Classify overall significance
if (!is.null(Sig_cutoff)) {
cutoff <- sort(p_sig_adj)[ceiling(length(p_sig_adj) * Sig_cutoff)]
overall_class <- ifelse(p_sig_adj <= cutoff, "sig", "non-sig")
} else {
overall_class <- ifelse(p_sig_adj <= Sig, "sig", "non-sig")
}
class_df <- data.frame(Sig0 = overall_class, varname = colnames(Z))
idx_sig <- which(class_df$Sig0 == "sig")
if (length(idx_sig) == 0) {
warning("No significant features found.")
colnames(pval_matrix) <- colnames(stat_matrix) <- c("Sig0", "Intornotint", "F1", "F2", "F3",
"twowaysorthreeways", "F1F2", "F2F3", "F1F3")
rownames(pval_matrix) <- rownames(stat_matrix) <- class_df$varname
return(list(pval_matrix = pval_matrix, stat_matrix = stat_matrix, class_df = class_df))
}
# Subset Z to significant features
if (length(idx_sig) > 1) {
Z_sig <- Z[, idx_sig]
} else {
Z_sig <- data.frame(Z[, idx_sig], check.names = FALSE)
colnames(Z_sig) <- colnames(Z)[idx_sig]
rownames(Z_sig) <- rownames(Z)
}
# Interaction test on significant features: compare lm_int_null vs. lm_full
n_sig <- ncol(Z_sig)
p_int <- numeric(n_sig)
s_int <- numeric(n_sig)
for (i in 1:n_sig) {
dat <- if (is.null(random_effect)) {
data.frame(Y = Z_sig[, i], factor1 = factor1, factor2 = factor2, factor3 = factor3)
} else {
data.frame(Y = Z_sig[, i],factor1= factor1, factor2 = factor2, factor3 = factor3, random_effect = random_effect)
}
res <- run_feature_anova(dat, formulas$lm_int_null, formulas$lm_full, func, test_func)
p_int[i] <- res[1]
s_int[i] <- res[2]
}
p_int_adj <- if (is.null(adj_method)) p_int else p.adjust(p_int, method = adj_method)
pval_matrix[idx_sig, 2] <- p_int_adj
stat_matrix[idx_sig, 2] <- s_int
class_df[idx_sig, "Sig1"] <- ifelse(pval_matrix[idx_sig, 2] > Int, "notInt", "Int")
# Split significant features into interaction and non-interaction groups
idx_int <- which(class_df$Sig1 == "Int")
idx_notint <- which(class_df$Sig1 == "notInt")
## Main Effects Tests on Non-interaction Features
if (length(idx_notint) > 0) {
if (length(idx_notint) > 1) {
Z_notint <- Z[, idx_notint]
} else {
Z_notint <- data.frame(Z[, idx_notint], check.names = FALSE)
colnames(Z_notint) <- colnames(Z)[idx_notint]
rownames(Z_notint) <- rownames(Z)
}
n_notint <- ncol(Z_notint)
p_f1 <- numeric(n_notint); s_f1 <- numeric(n_notint)
p_f2 <- numeric(n_notint); s_f2 <- numeric(n_notint)
p_f3 <- numeric(n_notint); s_f3 <- numeric(n_notint)
for (i in 1:n_notint) {
dat <- if (is.null(random_effect)) {
data.frame(Y = Z_notint[, i], factor1 = factor1, factor2 = factor2, factor3 = factor3)
} else {
data.frame(Y = Z_notint[, i], factor1= factor1, factor2 = factor2, factor3 = factor3, random_effect = random_effect)
}
# Main effect test for factor1: compare lm_f1 vs. lm_int_null
res1 <- run_feature_anova(dat, formulas$lm_f1, formulas$lm_int_null, func, test_func)
p_f1[i] <- res1[1]; s_f1[i] <- res1[2]
# Main effect test for factor2: compare lm_f2 vs. lm_int_null
res2 <- run_feature_anova(dat, formulas$lm_f2, formulas$lm_int_null, func, test_func)
p_f2[i] <- res2[1]; s_f2[i] <- res2[2]
# Main effect test for factor3: compare lm_f3 vs. lm_int_null
res3 <- run_feature_anova(dat, formulas$lm_f3, formulas$lm_int_null, func, test_func)
p_f3[i] <- res3[1]; s_f3[i] <- res3[2]
}
p_f1_adj <- if (is.null(adj_method)) p_f1 else p.adjust(p_f1, method = adj_method)
p_f2_adj <- if (is.null(adj_method)) p_f2 else p.adjust(p_f2, method = adj_method)
p_f3_adj <- if (is.null(adj_method)) p_f3 else p.adjust(p_f3, method = adj_method)
pval_matrix[idx_notint, 3] <- p_f1_adj
pval_matrix[idx_notint, 4] <- p_f2_adj
pval_matrix[idx_notint, 5] <- p_f3_adj
stat_matrix[idx_notint, 3] <- s_f1
stat_matrix[idx_notint, 4] <- s_f2
stat_matrix[idx_notint, 5] <- s_f3
# Classify non-interaction features based on main effects:
cls_notint <- rep("Add", length(idx_notint))
cls_notint[pval_matrix[idx_notint, 3] > F1 & pval_matrix[idx_notint, 4] <= F2 & pval_matrix[idx_notint, 5] <= F3] <- "F1"
cls_notint[pval_matrix[idx_notint, 3] <= F1 & pval_matrix[idx_notint, 4] > F2 & pval_matrix[idx_notint, 5] <= F3] <- "F2"
cls_notint[pval_matrix[idx_notint, 3] <= F1 & pval_matrix[idx_notint, 4] <= F2 & pval_matrix[idx_notint, 5] > F3] <- "F3"
class_df[idx_notint, "notInt"] <- cls_notint
}
## Two-way Tests on Interaction Features
if (length(idx_int) > 0) {
if (length(idx_int) > 1) {
Z_int <- Z[, idx_int]
} else {
Z_int <- data.frame(Z[, idx_int], check.names = FALSE)
colnames(Z_int) <- colnames(Z)[idx_int]
rownames(Z_int) <- rownames(Z)
}
n_int <- ncol(Z_int)
p_two <- numeric(n_int)
s_two <- numeric(n_int)
for (i in 1:n_int) {
dat <- if (is.null(random_effect)) {
data.frame(Y = Z_int[, i], factor1 = factor1, factor2 = factor2, factor3 = factor3)
} else {
data.frame(Y = Z_int[, i], factor1= factor1, factor2 = factor2, factor3 = factor3, random_effect = random_effect)
}
# Two-way test: compare lm_int_alt2way vs. lm_full
res_two <- run_feature_anova(dat, formulas$lm_int_alt2way, formulas$lm_full, func, test_func)
p_two[i] <- res_two[1]
s_two[i] <- res_two[2]
}
p_two_adj <- if (is.null(adj_method)) p_two else p.adjust(p_two, method = adj_method)
pval_matrix[idx_int, 6] <- p_two_adj
stat_matrix[idx_int, 6] <- s_two
class_df[idx_int, "Int"] <- ifelse(pval_matrix[idx_int, 6] < threeways, "threeways", "twoways")
# Further two-way tests on features classified as "twoways"
idx_twoways <- which(class_df$Int == "twoways")
if (length(idx_twoways) > 0) {
if (length(idx_twoways) > 1) {
Z_twoways <- Z[, idx_twoways]
} else {
Z_twoways <- data.frame(Z[, idx_twoways], check.names = FALSE)
colnames(Z_twoways) <- colnames(Z)[idx_twoways]
rownames(Z_twoways) <- rownames(Z)
}
n_twoways <- ncol(Z_twoways)
p_f1f2 <- numeric(n_twoways); s_f1f2 <- numeric(n_twoways)
p_f1f3 <- numeric(n_twoways); s_f1f3 <- numeric(n_twoways)
p_f2f3 <- numeric(n_twoways); s_f2f3 <- numeric(n_twoways)
for (i in 1:n_twoways) {
dat <- if (is.null(random_effect)) {
data.frame(Y = Z_twoways[, i], factor1 = factor1, factor2 = factor2, factor3 = factor3)
} else {
data.frame(Y = Z_twoways[, i], factor1= factor1, factor2 = factor2, factor3 = factor3, random_effect = random_effect)
}
# Test F1F2: compare lm_int_altf1f2 vs. lm_int_alt2way
res_f1f2 <- run_feature_anova(dat, formulas$lm_int_altf1f2, formulas$lm_int_alt2way, func, test_func)
p_f1f2[i] <- res_f1f2[1]; s_f1f2[i] <- res_f1f2[2]
# Test F1F3: compare lm_int_altf1f3 vs. lm_int_alt2way
res_f1f3 <- run_feature_anova(dat, formulas$lm_int_altf1f3, formulas$lm_int_alt2way, func, test_func)
p_f1f3[i] <- res_f1f3[1]; s_f1f3[i] <- res_f1f3[2]
# Test F2F3: compare lm_int_altf2f3 vs. lm_int_alt2way
res_f2f3 <- run_feature_anova(dat, formulas$lm_int_altf2f3, formulas$lm_int_alt2way, func, test_func)
p_f2f3[i] <- res_f2f3[1]; s_f2f3[i] <- res_f2f3[2]
}
p_f1f2_adj <- if (is.null(adj_method)) p_f1f2 else p.adjust(p_f1f2, method = adj_method)
p_f1f3_adj <- if (is.null(adj_method)) p_f1f3 else p.adjust(p_f1f3, method = adj_method)
p_f2f3_adj <- if (is.null(adj_method)) p_f2f3 else p.adjust(p_f2f3, method = adj_method)
pval_matrix[idx_twoways, 7] <- p_f1f2_adj
pval_matrix[idx_twoways, 8] <- p_f2f3_adj
pval_matrix[idx_twoways, 9] <- p_f1f3_adj
stat_matrix[idx_twoways, 7] <- s_f1f2
stat_matrix[idx_twoways, 8] <- s_f2f3
stat_matrix[idx_twoways, 9] <- s_f1f3
cls_twoways <- rep("twowaycombinations", length(idx_twoways))
cls_twoways[pval_matrix[idx_twoways, 7] <= F1F2 & pval_matrix[idx_twoways, 8] > F2F3 & pval_matrix[idx_twoways, 9] > F1F3] <- "F1F2"
cls_twoways[pval_matrix[idx_twoways, 7] > F1F2 & pval_matrix[idx_twoways, 8] <= F2F3 & pval_matrix[idx_twoways, 9] > F1F3] <- "F2F3"
cls_twoways[pval_matrix[idx_twoways, 7] > F1F2 & pval_matrix[idx_twoways, 8] > F2F3 & pval_matrix[idx_twoways, 9] <= F1F3] <- "F1F3"
class_df[idx_twoways, "twoways"] <- cls_twoways
}
}
# Set column names and return results
colnames(pval_matrix) <- colnames(stat_matrix) <- c("Sig0", "Intornotint", "F1", "F2", "F3",
"twowaysorthreeways", "F1F2", "F2F3", "F1F3")
rownames(pval_matrix) <- rownames(stat_matrix) <- class_df$varname
return(list(pval_matrix = pval_matrix, stat_matrix = stat_matrix, class_df = class_df))
}
oc3=iDAS_3F(Z = log2(data.frame(t(all.meanExprs[rowSums(all.meanExprs>0)>1,]),check.names = F)+1),
factor1 = all.timepoint,
factor2 = all.pcellstats,
factor3 = all.pcelltype,
random_effect = all.pid, test_func = "lmer",
Sig_cutoff = 0.02,Int = 0.01,F1 = 0.01,F2=0.02,F3=0.01,adj_method = "BH")
table(oc3$cls_df$Sig0)
table(oc3$cls_df$Sig1)
table(oc3$cls_df$notInt)
table(oc3$cls_df$Int)
table(oc3$cls_df$twoways)
table(oc3$class_df$Sig0)
table(oc3$class_df$Sig1)
table(oc3$class_df$notInt)
table(oc3$class_df$Int)
table(oc3$class_df$twoways)
table(oc2$cls_df$Sig0)
table(oc2$cls_df$Sig1)
table(oc2$cls_df$notInt)
table(oc2$cls_df$Int)
table(oc2$cls_df$twoways)
setwd("/dskh/nobackup/lijiay/Usyd-scPB/20240621-cdce_2-LY/src")
gc()
library(scater)
library(Seurat)
library(SingleCellExperiment)
library(eNODAL)
library(tidyr)
library(viridis)
library(miloR)
library(scran)
library(dplyr)
library(patchwork)
source("./20-runTEST_function.R")
source("./16-runHT_bonferroni.R")
library(ggplot2)
library(ggrepel)
library(patchwork)
library(BayesPrism)
library(nestedcv)
source("./twoway_function_p.R")
source("./twoway_function_permu.R")
source("100-iDAS.R")
sc=readRDS("../out/Malignant_melanoma_seuratobject_BT.rds")
mat=as.matrix(t(sc@assays$SCT$counts))
sc.dat.filtered <- cleanup.genes(input=mat,
input.type="count.matrix",
species="hs",
gene.group=c("other_Rb","chrM","chrX","chrY","Rb","Mrp","act","hb","MALAT1") , #"Rb","Mrp","other_Rb",,"MALAT1","chrX","chrY"
exp.cells=5)
sc.dat.filtered.pc <- select.gene.type (sc.dat.filtered,
gene.type = "protein_coding")
protein_coding_gene_list=colnames(sc.dat.filtered.pc)
scemilo <- SingleCellExperiment(assays=list(counts=sc@assays$SCT@counts[protein_coding_gene_list,]),
# logcounts= sce_sub@assays$SCT@data),
reducedDims=SimpleList(PCA=sc@reductions$pca@cell.embeddings,
UMAP=sc@reductions$umap@cell.embeddings),
colData=sc@meta.data)
hn_milo <- Milo(scemilo)
hn_milo <- buildGraph(hn_milo, k = 10, d = 30,reduced.dim = "PCA")
set.seed(4321)
hn_milo <- makeNhoods(hn_milo, prop = 0.1, k = 10, d=30, refined = TRUE,refinement_scheme="graph")
hn_milo <- countCells(hn_milo, meta.data = colData(hn_milo), samples="patient_ID")
hn_design=data.frame(Condition=colData(hn_milo)$response)#,batch=sim$batch)
hn_design[,"patient_ID"]= colData(hn_milo)$patient_ID
# hn_design$batch <- as.factor(hn_design$batch)
hn_design <- distinct(hn_design)
rownames(hn_design) <- hn_design$patient_ID
da_results <- testNhoods(hn_milo, design = ~ Condition,
design.df = hn_design[!is.na(hn_design$Condition),],
fdr.weighting="graph-overlap")
da_results %>%
arrange(- SpatialFDR) %>%
head()
sum(da_results$FDR<0.1)
hn_milo <- buildNhoodGraph(hn_milo)
plotNhoodGraphDA(hn_milo, da_results, layout="UMAP",alpha=0.2)
da_results <- groupNhoods(hn_milo, da_results,compute.new =T,da.fdr =0.1)# max.lfc.delta = 2,
plotDAbeeswarm(da_results, "NhoodGroup",alpha = 0.2)
da_results <- annotateNhoods(hn_milo, da_results, coldata_col = "patient_ID")
da_results <- annotateNhoods(hn_milo, da_results, coldata_col = "celltype")
da_results <- annotateNhoods(hn_milo, da_results, coldata_col = "response")
x=hn_milo
# da.res=age.da_results
da.res=da_results[da_results$FDR<=0.2&abs(da_results$logFC)>=5,]#
nhs.da.gr <- da.res$NhoodGroup
names(nhs.da.gr) <- da.res$Nhood
nhood.gr <- unique(nhs.da.gr)
# nhs <- nhoods(x)
nhs <- nhoods(x)[,as.integer(row.names(da.res))]
fake.meta <- data.frame(CellID = colnames(x)[rowSums(nhs) >
0], Nhood.Group = rep(NA, sum(rowSums(nhs) > 0)))
rownames(fake.meta) <- fake.meta$CellID
for (i in seq_along(nhood.gr)) {
nhood.x <- which(nhs.da.gr == nhood.gr[i])
nhs <- nhs[rowSums(nhs) > 0, ]
nhood.gr.cells <- rowSums(nhs[, nhood.x, drop = FALSE]) >
0
fake.meta[nhood.gr.cells, "Nhood.Group"] <- ifelse(is.na(fake.meta[nhood.gr.cells,
"Nhood.Group"]), nhood.gr[i], NA)
}
fake.meta <- fake.meta[!is.na(fake.meta$Nhood.Group), ]
milo.subset=hn_milo[,fake.meta$CellID]
#
# saveRDS(hn_milo,"../out/Malignant_melanoma_miloR_BT.rds")
#
sccount=milo.subset@assays@data$counts
sc$sel="unseletected"
sc$sel[colnames(sc)%in%colnames(sccount)]="selected"
umap=data.frame(sc@reductions$umap@cell.embeddings)
umap[,"selected"]=sc$sel
umap[,"response"]=sc$response
umap[,"celltype"]=sc$celltype
umap.df=pivot_longer(
umap,
cols = -c(UMAP_1, UMAP_2), # Select all columns except 'celltype' and 'scID' for transformation
names_to = "cate", # This will be the name of the new column containing the original column names (genes)
values_to = "value" # This will be the name of the new column containing the values of gene expression
)
combinations=table(colData(milo.subset)$patient_ID,colData(milo.subset)$celltype)|> as.data.frame()
colnames(combinations)=c("patientid","celltypeid","cellcount")
tmp_patient <- colData(milo.subset)$patient_ID
tmp_celltype <- colData(milo.subset)$celltype
tmp_cellstats<- colData(milo.subset)$response
meanExprs <- apply(combinations[combinations$cellcount>1,],
1,function(cb) {
Matrix::rowMeans(sccount[, tmp_patient == unlist(cb[1])&tmp_celltype == unlist(cb[2]), drop = FALSE])
}
)
colnames(meanExprs)=apply(combinations[combinations$cellcount>1,],1,function(x)paste(x[1],x[2],sep = ":"))
pcelltype=combinations[combinations$cellcount>1,][,2]
ptid=combinations[combinations$cellcount>1,][,1]
meta_data=colData(milo.subset)
prdf=unique(meta_data[,c("response","patient_ID")])
row.names(prdf)=prdf$patient_ID
pcell_stats=prdf[ptid,]$response
oc2=iDAS_2F_permu(Z= log2(data.frame(t(meanExprs[rowSums(meanExprs>0)>1,]),check.names = F)+1),
f1=pcelltype,f2=pcell_stats,random=NULL,test_func="lm",
Sig_cutoff=0.02, Sig = 0.1,Int= 0.01, F1 = 0.01,F2 = 0.01,
adj_method="BH",f1name=NULL,f2name=NULL,randomname=NULL)