-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_bootsrap.R
More file actions
128 lines (98 loc) · 5.14 KB
/
Copy pathR_bootsrap.R
File metadata and controls
128 lines (98 loc) · 5.14 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
###############################################################################
# E2VD Bootstrap Simulation Data Generator
#
# Generates simulated DMS datasets by bootstrap resampling from real E2VD
# CSV data with minor Gaussian noise injection. Output preserves the exact
# column schema and directory layout expected by E2VD training scripts.
#
# Usage:
# source("generate_E2VD_bootstrap.R")
#
# Requirements:
# - data.table
# - E2VD repository with data/ directory populated
#
# NOTE: Simulated data is for pipeline debugging only.
# Replace with real data for any publishable analysis.
###############################################################################
rm(list = ls())
library(data.table)
set.seed(2026)
# -- Paths (edit these) -------------------------------------------------------
root_dir <- ".../data"
out_dir <- "bootstrap_E2VD"
dir.create(out_dir, recursive = TRUE, showWarnings = FALSE)
# -- Bootstrap helper ---------------------------------------------------------
# Resample rows with replacement + additive Gaussian noise on label column.
# Negative values are clipped to zero.
bootstrap_noise <- function(dt, label_col, noise = 0.03, n = NULL) {
if (is.null(n)) n <- nrow(dt)
sim <- dt[sample(.N, n, replace = TRUE)]
sim[[label_col]] <- pmax(sim[[label_col]] + rnorm(n, 0, noise), 0)
sim
}
###############################################################################
# 1. Binding affinity
###############################################################################
bind <- fread(file.path(root_dir,
"single_site_benchmark/bind/data/data_all.csv"))
bind_sim <- bootstrap_noise(bind, "label", 0.03, 4000)
# Balanced test set: 27 beneficial (label > 1) + 27 non-beneficial
benef <- which(bind_sim$label > 1)
other <- which(bind_sim$label <= 1)
test_idx <- c(sample(benef, 27), sample(other, 27))
train_idx <- setdiff(seq_len(nrow(bind_sim)), test_idx)
dir.create(file.path(out_dir, "single_site_benchmark/bind/data"),
recursive = TRUE, showWarnings = FALSE)
fwrite(bind_sim, file.path(out_dir, "single_site_benchmark/bind/data/data_all.csv"))
fwrite(bind_sim[train_idx], file.path(out_dir, "single_site_benchmark/bind/data/data_train.csv"))
fwrite(bind_sim[test_idx], file.path(out_dir, "single_site_benchmark/bind/data/data_test.csv"))
###############################################################################
# 2. Expression
###############################################################################
expr <- fread(file.path(root_dir,
"single_site_benchmark/expr/expr_4k_all.csv"))
expr_sim <- bootstrap_noise(expr, "label", 0.03, 3500)
test_idx <- sample(seq_len(nrow(expr_sim)), 100)
train_idx <- setdiff(seq_len(nrow(expr_sim)), test_idx)
dir.create(file.path(out_dir, "single_site_benchmark/expr"),
recursive = TRUE, showWarnings = FALSE)
fwrite(expr_sim, file.path(out_dir, "single_site_benchmark/expr/expr_4k_all.csv"))
fwrite(expr_sim[train_idx], file.path(out_dir, "single_site_benchmark/expr/expr_4k_train.csv"))
fwrite(expr_sim[test_idx], file.path(out_dir, "single_site_benchmark/expr/expr_4k_test.csv"))
###############################################################################
# 3. Antibody escape
###############################################################################
esc_dir <- file.path(root_dir, "single_site_benchmark/escape/split")
train_files <- list.files(esc_dir, pattern = "train", full.names = TRUE)
escape <- rbindlist(lapply(train_files, fread))
escape_sim <- bootstrap_noise(escape, "mut_escape", 0.02)
# 70 / 5 / 25 split
n <- nrow(escape_sim)
test_n <- round(n * 0.25)
val_n <- round(n * 0.05)
idx <- sample(n)
test_sim <- escape_sim[idx[seq_len(test_n)]]
val_sim <- escape_sim[idx[(test_n + 1):(test_n + val_n)]]
train_sim <- escape_sim[idx[(test_n + val_n + 1):n]]
dir.create(file.path(out_dir, "single_site_benchmark/escape/split"),
recursive = TRUE, showWarnings = FALSE)
fwrite(test_sim, file.path(out_dir, "single_site_benchmark/escape/split/test.csv"))
fwrite(val_sim, file.path(out_dir, "single_site_benchmark/escape/split/val.csv"))
# 10-fold training splits
folds <- split(seq_len(nrow(train_sim)), rep(0:9, length.out = nrow(train_sim)))
for (i in 0:9) {
fwrite(train_sim[folds[[as.character(i)]]],
file.path(out_dir,
sprintf("single_site_benchmark/escape/split/train-%d.csv", i)))
}
###############################################################################
# 4. Generalization (cross-variant)
###############################################################################
gen <- fread(file.path(root_dir,
"generalization_performance/bind/baseline-CNN/variant_data.csv"))
gen_sim <- bootstrap_noise(gen, "KD_ratio", 0.02)
dir.create(file.path(out_dir, "generalization_performance/bind/baseline-CNN"),
recursive = TRUE, showWarnings = FALSE)
fwrite(gen_sim, file.path(out_dir,
"generalization_performance/bind/baseline-CNN/variant_data.csv"))