-
Notifications
You must be signed in to change notification settings - Fork 25
/
revolver_cohort.R
312 lines (258 loc) · 9.69 KB
/
revolver_cohort.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
#' Construct a REVOLVER cohort.
#'
#' @param dataset A dataframe in the specified format (see the package vignettes).
#' @param CCF_parser A function to parse the format for the encoding of CCF
#' or binary values for each sequenced region. A possible function is available
#' inside REVOLVER; \code{\link{CCF_parser}} (the default of this parameter).
#' @param ONLY.DRIVER If true, uses only annotated driver events.
#' @param MIN.CLUSTER.SIZE Discard clusters that have less than this number of entries.
#' @param annotation Brief cohort description.
#'
#' @return An object of the S3 class \code{"rev_cohort"} that represents a \code{REVOLVER} cohort.
#'
#' @import tidyverse
#' @import tidygraph
#' @import pio
#' @import easypar
#' @import RColorBrewer
#' @import crayon
#' @import cluster
#' @import dendextend
#' @import dynamicTreeCut
#' @import igraph
#' @import ggpubr
#' @import ggrepel
#' @import clisymbols
#' @import evoverse.datasets
#'
#' @export
#' @family Cohort creation
#'
#' @examples
#' # Example cohort creation with the TRACERx data
#' data(TRACERx_data)
#'
#' # To speed up the process we use only 2 patients
#' TRACERx_data = TRACERx_data %>%
#' filter(patientID %in% c('CRUK0001', 'CRUK0002'))
#'
#' cohort = revolver_cohort(TRACERx_data, annotation = 'A toy REVOLVER dataset')
#'
#' # The S3 print/ plot for this cohort
#' print(cohort)
#' plot(cohort)
revolver_cohort = function(dataset,
CCF_parser = revolver::CCF_parser,
ONLY.DRIVER = FALSE,
MIN.CLUSTER.SIZE = 10,
annotation = 'My REVOLVER dataset')
{
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Some header print and the output object are created, before checking for input consistency
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
options = list(ONLY.DRIVER = ONLY.DRIVER, MIN.CLUSTER.SIZE = MIN.CLUSTER.SIZE)
pio::pioHdr(paste('REVOLVER ~ Cohort constructor'))
cat("\n")
cli::cli_alert_info(paste(
"Using",
ifelse(
options$ONLY.DRIVER == 0,
'only driver' %>% crayon::bold(),
'all annotated' %>% crayon::bold()
),
'mutations. '
))
cli::cli_alert_info(paste(
"Rejecting clusters with less than",
options$MIN.CLUSTER.SIZE %>% crayon::bold(),
'mutations. '
))
# The output object will be this, of class rev_cohort
obj <-
structure(
list(
patients = NULL,
variantIDs = NULL,
variantIDs.driver = NULL,
numVariants = NULL,
annotation = annotation,
dataset = NULL,
CCF = NULL,
n = NULL,
CCF_parser = CCF_parser
),
class = "rev_cohort",
call = match.call()
)
# Check input and stop on error
dataset = check_input(dataset, CCF_parser)
dataset$id = paste0('__mut_id_', 1:nrow(dataset))
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Input data is subset according to the options parameter
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
if (options$ONLY.DRIVER)
dataset = dataset %>% filter(is.driver)
# by cluster size
grp = dataset %>%
group_by(patientID, cluster) %>%
summarize(cluster_size = n(), .groups = 'keep') %>%
ungroup()
dataset = left_join(dataset, grp, by = c('patientID', 'cluster'))
if (options$MIN.CLUSTER.SIZE > 0)
{
cli::cli_h1('Filtering small clusters (starting with {.field {nrow(dataset)}} entries)')
cat('Removing\n\n')
print(dataset %>%
filter(cluster_size < options$MIN.CLUSTER.SIZE))
dataset = dataset %>%
filter(cluster_size >= options$MIN.CLUSTER.SIZE)
cli::cli_alert_success('\nAfter filtering: {.field {nrow(dataset)}} entries\n')
}
if (nrow(dataset) == 0)
stop("Your dataset is empty, aborting.")
cli::cli_h1('\nREVOLVER input data')
cat('\n')
pio::pioDisp(dataset)
obj$patients = unique(dataset$patientID)
obj$dataset = dataset
obj$variantIDs = unique(dataset$variantID)
obj$variantIDs.driver = unique(dataset %>% filter(is.driver) %>% pull(variantID))
obj$numVariants = nrow(dataset)
obj$n = list(
patients = length(obj$patients),
variants = nrow(obj$dataset),
drivers = length(obj$variantIDs.driver)
)
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Data of each patient needs to be unwrapped and stored
# This takes some times
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
cli::cli_h3('\nPreprocessing data (this may take some time)')
# cli::cli_process_start("Extracting patient data ...")
cat('\n')
obj$dataset = obj$dataset %>%
group_by(patientID) %>%
nest() %>%
ungroup() %>%
select(data) %>%
unlist(recursive = F)
names(obj$dataset) = obj$patients
# For each patient extract its explicit region CCF data
foo = function(w)
{
values = CCF_parser(w)
tv = as_tibble(as.numeric(values))
tv$sample = names(values)
tv %>%
spread(sample, value)
}
obj$dataset = lapply(obj$patients,
function(pat)
{
d = obj$dataset[[pat]] %>%
mutate(patientID = pat) %>%
select(id,
Misc,
patientID,
variantID,
is.driver,
is.clonal,
cluster,
cluster_size,
CCF)
# pio::pioStr(d$patientID[1],
# paste0(nrow(d), ' entries'),
# suffix = '\n',
# prefix = '\n')
# cli::cli_alert('{.field {d$patientID[1]}} : {.count {nrow(d)}} entries.')
cat('.')
CCF_values = d %>%
group_by(id) %>%
do(foo(.$CCF)) %>%
ungroup()
d %>%
full_join(CCF_values, by = 'id')
})
names(obj$dataset) = obj$patients
cat('\n')
# cli::cli_process_done()
cli::cli_h1('\nExtracting clones table')
cat('\n')
# pio::pioTit('Extracting clones\' table for each patient')
# Then we also create a table with the clones for each patient
obj$CCF = lapply(names(obj$dataset),
function(pat)
{
d = obj$dataset[[pat]]
# pio::pioStr(pat, paste0(nrow(d), ' entries'), suffix = '')
# cli::cli_alert('{.field {pat}} : {.count {nrow(d)}} entries.')
d = d %>%
select(-Misc, -variantID, -CCF, -id, -patientID, -cluster_size)
CCFclone_headers = d %>%
group_by(cluster) %>%
summarise(
nMuts = n(),
is.driver = any(is.driver),
is.clonal = all(is.clonal)
) %>%
arrange(desc(nMuts))
CCFclone_values = d %>%
select(-is.driver, -is.clonal) %>%
reshape2::melt(id = 'cluster') %>%
group_by(cluster, variable) %>%
summarise(CCF = median(as.numeric(value))) %>%
spread(variable, CCF) %>%
ungroup()
cli::cli_alert('{.field {pat}} : {.count {nrow(d)}} entries, {.count {nrow(CCFclone_headers)}} clone(s).')
# pio::pioStr(' --> ', paste0(nrow(CCFclone_headers), ' clones'), suffix = '\n')
CCFclone_headers %>% full_join(CCFclone_values, by = 'cluster')
})
names(obj$CCF) = names(obj$dataset)
return(obj)
}
# Check input format - fails on error
check_input = function(dataset, CCF.parser)
{
# Dataset with certain columns required
required.cols = c('Misc',
'patientID',
'variantID',
'cluster',
'is.driver',
'is.clonal',
'CCF')
# check for types
types = sapply(dataset, class)
if (!is.data.frame(dataset) |
!all(required.cols %in% colnames(dataset)) |
types['Misc'] != 'character' |
types['patientID'] != 'character' |
types['variantID'] != 'character' |
types['cluster'] != 'character' |
types['CCF'] != 'character' |
types['is.driver'] != 'logical' |
types['is.clonal'] != 'logical')
{
print(str(dataset))
stop(
'Input `dataset` should be a dataframe in with the following columns:\n
\t- Misc: any annotation (string),
\t- patientID: a unique ID to identify a patient (string)
\t- variantID: a unique ID to identify a variant across patients (string)
\t- cluster: to putative clone in this patient\'s tumour where the variant is found (string)
\t- is.driver: if the mutation is a driver to be correlated (logical)
\t- is.clonal: if the mutation is a clonal (truncal) in this patient (logical)
\t- CCF: Cancer Cell Fraction or binary annotation for this mutations across the biopsies of this patient\'s tumour (string)
For more information see the manual: https://github.com/caravagn/revolver'
)
}
if (any(dataset[, required.cols[-1]] == ''))
stop("Entries empty string ('') in the dataset are allowed only in the `Misc` field.")
if (!is.function(CCF_parser))
{
stop(
'You need to provide a function to parse CCFs, see "CCF_parser" available in this package.'
)
}
dataset[, required.cols] %>% as_tibble()
}