-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest_functions.R
169 lines (141 loc) · 6.09 KB
/
manifest_functions.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
add_column_na <- function(d, col_names) {
add_cols <- col_names %>% setdiff(colnames(d))
if(length(add_cols) != 0) d[add_cols] <- NA
d
}
disperse <- function(df_a, df_b) {
if(is.null(df_a) | is.null(df_b)) { return(bind_rows(df_a, df_b)) }
if(nrow(df_b) > nrow(df_a)) { return(disperse(df_b, df_a)) }
a <- nrow(df_a)
b <- nrow(df_b)
bind_rows(df_a, df_b) %>% arrange(c(seq(a), a * seq(b)/(b + 1)))
}
get_wells <- function(n = 1, transpose = FALSE, r = 12, c = 8) {
if (n == 0) { return(NULL); }
else if (n < 1) { return( get_wells(n = 1, r = r * n, c) ) }
else {
well_rows <- if_else(rep(transpose, times = r * c),
rep(LETTERS[1:8], times = r),
rep(LETTERS[1:8], each = r))
well_columns <- if_else(rep(transpose, times = r * c),
rep(1:r, each = c),
rep(1:r, times = c))
return( c( str_c(well_rows, formatC(well_columns, width = 2, flag = "0")),
get_wells(n - 1, transpose, r, c) ) )
}
}
get_info <- function(samples, controls, plate_size, chip_size) {
lst( plate_size, chip_size,
n_samples = nrow(samples),
n_controls = length(controls),
total_plates = ceiling( n_samples / ( plate_size - n_controls ) ),
used_wells = n_samples + (total_plates * n_controls),
total_chips = ceiling( used_wells / chip_size ),
empty_wells = ( total_chips * chip_size ) - used_wells,
samples_per_plate = plate_size - n_controls,
total_controls = total_plates * n_controls)
}
format_manifest <- function(samples, by_cols, add_cols, col_vals = NULL, col_names) {
samples_w_wells <- samples %>%
group_split(Plate) %>%
map(~ mutate(., Well = get_wells(1, transpose = TRUE) %>% head(n()))) %>%
bind_rows
samples_w_wells %>%
mutate(!!! col_vals) %>%
# Rename Plate and Well for WGS template, dirty hack
# mutate("Sample Plate" = Plate, "Sample well" = Well) %>%
# mutate("Gender (M/F/U)" = Gender) %>%
add_column_na(col_names) %>%
select(union(col_names, c(all_of(by_cols), add_cols)))
}
add_controls <- function(samples, controls, info) {
if(length(controls) == 0) {
plated_samples <- samples %>%
mutate(Plate = rep(1:info$total_plates, each = info$samples_per_plate, length.out = n())) %>%
group_split(Plate) %>%
bind_rows
} else {
plated_samples <- samples %>%
mutate(Plate = rep(1:info$total_plates, each = info$samples_per_plate, length.out = n())) %>%
group_split(Plate) %>% imap(~ disperse(.x, tibble("Sample ID" = controls, Plate = .y))) %>%
bind_rows
}
plated_samples
}
simple_disperse <- function(samples, controls, seed, id_col, by_cols, empty_wells) {
set.seed(seed)
### TODO: get plate dimensions from UI
info <- get_info(samples, controls, 96, 8)
randomized_samples <- samples %>% sample_n(n()) %>%
mutate("Sample ID" = as.character(!!! syms(id_col))) %>%
group_split(!!! syms(by_cols)) %>% sample()
if (info$empty_wells > 0 & empty_wells == "Use Controls") {
empty <- tibble("Sample ID" = rep(controls, length.out = info$empty_wells))
dispersed_samples <- randomized_samples %>%
list_modify(empty = empty) %>% reduce(disperse)
}
else {
empty <- tibble("Sample ID" = rep("Empty", length.out = info$empty_wells))
dispersed_samples <- randomized_samples %>%
reduce(disperse) %>% bind_rows(empty)
}
plated_samples <- add_controls(dispersed_samples, controls, info)
plated_samples %>%
mutate(Chip = rep(1:info$total_chips, each = info$chip_size, length.out = n())) %>%
group_split(Chip) %>% map(~ sample_n(., n())) %>%
bind_rows
}
col_split <- function(.data, split_on) {
len <- length(split_on)
if(len == 0) { return(.data) }
map_depth(col_split(.data, split_on[-len]), .depth = len - 1, ~ group_split(., !! sym(split_on[len])))
}
multi_reduce <- function(.x, .f) {
if(is_tibble(.x[[1]])) { return(reduce(.x, .f)) }
map(.x, ~ multi_reduce(.x, .f)) %>% reduce(.f)
}
# Testing new dispersal mechanism
grouped_disperse <- function(samples, controls, seed, id_col, by_cols, empty_wells) {
set.seed(seed)
### TODO: get plate dimensions from UI
info <- get_info(samples, controls, 96, 8)
id_name <- "Sample ID"
# id_name for WGS, this is a quick hack for now we should do this better in the future.
# id_name <- "Subject_ID"
randomized_samples <- samples %>% sample_n(n()) %>%
mutate(!! id_name := as.character(!!! syms(id_col))) %>%
# Make this a setting... set NA to missing
# replace_na(as.list(rep("Missing", length(by_cols))) %>% set_names(by_cols)) %>%
col_split(by_cols) %>% multi_reduce(disperse)
if (info$empty_wells > 0 & empty_wells == "Use Controls") {
empty <- tibble(!! id_name := rep(controls, length.out = info$empty_wells))
dispersed_samples <- disperse(randomized_samples, empty)
} else {
empty <- tibble(!! id_name := rep("Empty", length.out = info$empty_wells))
dispersed_samples <-bind_rows(randomized_samples, empty)
}
plated_samples <- add_controls(dispersed_samples, controls, info)
plated_samples %>%
mutate(Chip = rep(1:info$total_chips, each = info$chip_size, length.out = n())) %>%
group_split(Chip) %>% map(~ sample_n(., n())) %>%
bind_rows
}
plate_randomize <- function(samples, controls, seed, id_col, by_cols, empty_wells) {
set.seed(seed)
### TODO: get plate dimensions from UI
info <- get_info(samples, controls, 96, 8)
randomized_samples <- samples %>% sample_n(n()) %>%
mutate("Sample ID" = as.character(!!! syms(id_col)))
if (info$empty_wells > 0 & empty_wells == "Use Controls") {
empty <- tibble("Sample ID" = rep(controls, length.out = info$empty_wells))
dispersed_samples <- disperse(randomized_samples, empty)
} else {
empty <- tibble("Sample ID" = rep("Empty", length.out = info$empty_wells))
dispersed_samples <-bind_rows(randomized_samples, empty)
}
plated_samples <- add_controls(dispersed_samples, controls, info)
plated_samples %>%
mutate(Chip = rep(1:info$total_chips, each = info$chip_size, length.out = n())) %>%
group_split(Chip) %>% map(~ sample_n(., n())) %>%
bind_rows
}