-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.clone.genome.distribution.plot.R
More file actions
224 lines (204 loc) · 7.46 KB
/
create.clone.genome.distribution.plot.R
File metadata and controls
224 lines (204 loc) · 7.46 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
create.clone.genome.distribution.plot <- function(
snv.df,
genome.build = 'GRCh37',
clone.order = NULL,
clone.colours = NULL,
filename = NULL,
multi.sample = FALSE,
alpha = 0.25,
legend.x = 0.1,
legend.y = 0.55,
...
) {
# Preprocess ----------------------------------------------------------------------------------
required.cols <- c('chr', 'pos', 'clone.id');
missing.cols <- required.cols[!(required.cols %in% names(snv.df))];
if (length(missing.cols) != 0) {
stop(paste0(
'snv.df must contain the columns ',
oxford.comma.vector.concat(required.cols),
'; snv.df is missing ',
oxford.comma.vector.concat(missing.cols, paste(required.cols, collapse = ', ')), '.'));
}
if (is.null(clone.order)) {
clone.order <- sort(unique(snv.df$clone.id));
}
if (multi.sample) {
# if multi-sample is true, check for sample ids in 'ID' column
if (is.null(snv.df$ID)) {
stop('ID column must contain sample ID if multi.sample is TRUE');
}
# filename must be a directory
if (!is.null(filename) && !dir.exists(filename)) {
stop('filename must be a directory if multi.sample is TRUE');
}
} else {
if (!is.null(filename) && dir.exists(filename)) {
stop('filename must be a path (not a directory) if multi.sample is FALSE');
}
snv.df$ID <- 'all';
}
if (is.null(clone.colours)) {
clone.colours <- get.colours(clone.order, return.names = TRUE);
}
snv.df$clone.id <- factor(snv.df$clone.id, levels = clone.order);
genome.pos.df <- get.genome.pos(snv.df, genome.build);
snv.df <- genome.pos.df$snv;
# use chr.info to set x-axis label positions
chr.info <- genome.pos.df$chr.info;
chr.info$xat <- (chr.info$length / 2) + chr.info$start;
plt.list <- list();
for (s in unique(snv.df$ID)) {
# Iterate through each sample -------------------------------------------------------------
message(paste('Plotting clone distribution across the genome for sample:', s));
sample.df <- droplevels(snv.df[snv.df$ID == s, ]);
sample.df <- unique(sample.df[, c('clone.id', 'genome.pos', 'SNV.id', 'ID')]);
if (!is.null(filename) && multi.sample) {
save.plt <- file.path(filename, paste0(s, '.png'));
} else {
save.plt <- filename;
}
plt.list[[s]] <- create.clone.genome.distribution.plot.per.sample(
sample.df,
clone.colours[levels(sample.df$clone.id)],
chr.info,
save.plt = save.plt,
alpha = alpha,
legend.x = legend.x,
legend.y = legend.y,
...
);
}
return(plt.list);
}
create.clone.genome.distribution.plot.per.sample <- function(
sample.df,
clone.colours,
chr.info,
save.plt = NULL,
width = 18,
xaxis.tck = 0.5,
yaxis.tck = 0.5,
xaxis.fontface = 'bold',
yaxis.fontface = 'bold',
xlab.cex = 1.65,
ylab.cex = 1.65,
xaxis.cex = 1.5,
yaxis.cex = 1.5,
xlab.top.cex = 1.2,
legend.size = 3,
legend.title.cex = 1.2,
legend.label.cex = 1,
legend.x = 0.1,
legend.y = 0.55,
alpha = 0.25,
...
) {
# calculate densities for each cluster --------------------------------------------------------
density.list <- list();
for (k in unique(sample.df$clone.id)) {
if (sum(sample.df$clone.id == k, na.rm = TRUE) <= 1) {
warning(paste('Skipping clone', k, 'in sample', unique(sample.df$ID), 'since there is only one SNV'));
next;
}
density.list[[k]] <- calculate.density(
x = sample.df[sample.df$clone.id == k, ],
adjust = 0.05
);
}
density.df <- do.call(rbind, density.list);
# get plot legend -----------------------------------------------------------------------------
legend.label <- sapply(names(clone.colours), function(k) {
nsnv <- length(unique(sample.df[sample.df$clone.id == k, ]$SNV.id));
return(paste0(k, ' (', nsnv, ')'));
});
clone.colours <- clone.colours[levels(sample.df$clone.id)];
cluster.legend <- BoutrosLab.plotting.general::legend.grob(
list(
legend = list(
title = 'Clone (SNVs)',
labels = legend.label[names(clone.colours)],
colours = c(clone.colours),
border = 'black'
)
),
size = legend.size,
title.just = 'left',
title.cex = legend.title.cex,
label.cex = legend.label.cex
);
# create individual plot ----------------------------------------------------------------------
sample.df$colour <- clone.colours[sample.df$clone.id];
scatter.plt <- create.clone.genome.distribution.scatterplot(
scatter.df = sample.df,
nsnv = nrow(sample.df),
nclone = length(unique(sample.df$clone.id)),
chr.info = chr.info,
xlab.top.cex = xlab.top.cex,
xaxis.tck = 0,
yaxis.tck = yaxis.tck,
yaxis.fontface = yaxis.fontface,
xlab.cex = 0,
ylab.cex = ylab.cex,
xaxis.cex = 0,
yaxis.cex = yaxis.cex,
alpha = alpha
);
density.plt <- create.clone.genome.distribution.densityplot(
density.df,
clone.colours,
chr.info = chr.info,
xaxis.tck = xaxis.tck,
yaxis.tck = yaxis.tck,
xaxis.fontface = xaxis.fontface,
yaxis.fontface = yaxis.fontface,
xlab.cex = xlab.cex,
ylab.cex = ylab.cex,
xaxis.cex = xaxis.cex,
yaxis.cex = yaxis.cex
);
# create multipanel plot ----------------------------------------------------------------------
# automate plot sizing based on cumber of clones in the scatter plot
height.scatter <- 0.5 * length(unique(sample.df$clone.id));
total.height <- height.scatter + 5;
if (legend.x > 1) {
cluster.legend <- list(right = list(fun = cluster.legend));
} else {
cluster.legend <- list(inside = list(
fun = cluster.legend,
x = legend.x,
y = legend.y
));
}
return(BoutrosLab.plotting.general::create.multipanelplot(
filename = save.plt,
plot.objects = list(
scatter.plt,
density.plt
),
layout.width = 1,
layout.height = 2,
plot.objects.heights = c(height.scatter, 5) / total.height,
legend = cluster.legend,
height = total.height,
width = width,
...
));
}
get.genome.pos <- function(
snv.df,
genome.build = 'GRCh37',
chr.order = c(1:22, 'X', 'Y')
) {
if (!(genome.build %in% c('GRCh37', 'GRCh38'))) {
stop('genome.build must be either GRCh37 or GRCh38')
}
assign('chr.info', get(genome.build));
snv.df$chr <- droplevels(factor(snv.df$chr, levels = chr.order));
chr.info <- chr.info[chr.info$chr %in% levels(snv.df$chr), c('chr', 'length')];
chr.info$chr <- droplevels(factor(chr.info$chr, levels = chr.order));
chr.info$length <- as.numeric(chr.info$length);
chr.info$start <- c(0, cumsum(chr.info$length[-length(chr.info$length)]));
snv.df$genome.pos <- chr.info[match(snv.df$chr, chr.info$chr), 'start'] + as.integer(snv.df$pos);
return(list('snv' = snv.df, 'chr.info' = chr.info));
}