-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfmedip_fragment_ratio.R
289 lines (238 loc) · 11.6 KB
/
cfmedip_fragment_ratio.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
rm(list = ls())
setwd("/Users/yong/OneDrive - UHN/Projects/TCGE/cfEpigenomics/Resource/3_fragmentomics/1_fragment_ratio")
library(dplyr)
library(ggplot2)
library(ggpubr)
##############
# ggplot theme
{
mytheme <- theme_classic(base_size=12) + theme(
axis.text.x = element_blank(),
axis.ticks.x=element_blank(),
strip.text.x = element_text(size=11),
strip.text.y = element_text(size=12),
axis.title.x = element_text(face="bold", size=17),
axis.title.y = element_text(size=15),
axis.text.y = element_text(size=15),
plot.title = element_text(size=15),
legend.position = "none",
legend.title = element_text(size=10),
legend.text = element_text(size=10),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background=element_rect(fill="white", color="white"),
panel.spacing.x=unit(0.1, "lines"))
}
## healthy mean
fr <- read.table("TCGE-CFMe-HBC/fragment_profile_GC_corrected_5mb.tsv",
comment.char = "!", header = T, check.names = F)
fr_m = data.matrix(fr[, -(1:5)])
#fr_m_z = t(scale(t(fr_m), center = TRUE, scale = TRUE)) ## z-score transform
ratio_mean_hbc = rowMeans(fr_m, na.rm = T)
fr_combo <- fr[, 1:5] ## aggregated ratio matix without scaling for all projects
###############
## per project
##############
project_id <-
s_info <- read.csv("")
fr_aggr <- data.frame() ## aggregated data.frame after scaling per project
for (cohort in project_id)
{
print(cohort)
fr <- read.table("/Users/arnavaz/Desktop/HPC/pughlab/projects/COMBAT/MEDIPIPE/aggregated/fragment_profile_GC_corrected_5mb.tsv",
comment.char = "!", header = T, check.names = F)
armlevels <- c("1p","1q","2p","2q","3p","3q","4p","4q","5p","5q","6p","6q",
"7p","7q","8p","8q", "9p", "9q","10p","10q","11p","11q","12p",
"12q","13q","14q","15q","16p","16q","17p","17q","18p","18q",
"19p", "19q","20p","20q","21q","22q")
fr$arm <- factor(fr$arm, levels=armlevels)
##
arm <- fr %>%
group_by(arm) %>%
summarize(n=n()) %>%
mutate(arm = as.character(arm))
## adjust labels for visualization
## show long arms (q) for some of chromosomes
small.arms <- setNames(c("", "12q", "", "16q",
"", "17q", "", "18q",
"", "19", "", "20",
"21", "22"),
c("12p", "12q", "16p", "16q",
"17p", "17q", "18p", "18q",
"19p", "19q", "20p", "20q",
"21q", "22q"))
arm.labels <- setNames(arm$arm, arm$arm)
arm.labels[names(small.arms)] <- small.arms
###########################################
fr_m = data.matrix(fr[, -(1:5)]) ## ratio martix
## keep cfMe only
#idx_s <- match(colnames(fr_m), s_info$sequencing_id)
fr_m <- na.omit(fr_m)
fr_m_z = t(scale(t(fr_m), center = TRUE, scale = TRUE)) ## z-score transform
## merge fr matirx
N = nrow(fr_m)
M <- ncol(fr_m)
bin = rep(fr$bin_id, M)
arm = rep(fr$arm, M)
project = rep("COMBAT", N*M)
group = rep(group_s, each = N)
sample = rep(colnames(fr_m), each = N)
ratio = as.vector(fr_m)
ratio_mean_project = rep(rowMeans(fr_m, na.rm = T), M)
ratio_mean_healthy = rep(ratio_mean_hbc, M)
## z scores
ratio_z = as.vector(fr_m_z)
## sum z scores for whole genome
ratio_z_genome = rep(colSums(fr_m_z, na.rm = TRUE), each = N)
fr_dat = data.frame(bin, arm, sample, group, project,
ratio, ratio_mean_project, ratio_mean_healthy,
ratio_z, ratio_z_genome)
fr_aggr = rbind(fr_aggr, fr_dat)
########################
## using original ratios
{
# Generate Fragmentation and Coverage plots
g <- ggplot(fr_dat, aes(x = bin, y = ratio, group = sample))
g <- g + geom_line(linewidth = 0.5, alpha = 0.5, color = "gray40")
## add mean line per project
g <- g + geom_line(aes(x = bin, y = ratio_mean_project),
size=0.75, alpha=0.5, color="black")
## add mean line baed the HBC samples
g <- g + geom_line(aes(x = bin, y = ratio_mean_healthy),
size=0.75, alpha=0.5, color="green")
g <- g + facet_grid(~arm, switch = "x",space = "free_x", scales = "free_x",
labeller = labeller(arm = arm.labels))
g <- g + labs(x = cohort, y = "Short/Long Fragment Ratio\n", color="")
g <- g + mytheme
ggsave(paste0(cohort, "_fragment_ratio_in_5Mb.png"), width = 16, height = 4)
## subgroups per each individual projects
g <- ggplot(fr_dat, aes(x = bin, y = ratio, group = sample))
g <- g + geom_line(linewidth = 0.5, alpha = 0.5, color = "gray40")
## add mean line per project
g <- g + geom_line(aes(x = bin, y = ratio_mean_project),
size=0.75, alpha=0.5, color="black")
## add mean line baed the HBC samples
g <- g + geom_line(aes(x = bin, y = ratio_mean_healthy),
size=0.75, alpha=0.5, color="green")
g <- g + facet_grid(group ~ arm, switch = "x",space = "free_x", scales = "free_x",
labeller = labeller(arm = arm.labels))
g <- g + labs(x = cohort, y = "Short/Long Fragment Ratio\n", color="")
g <- g + mytheme
ggsave(paste0(cohort, "_fragment_ratio_in_5Mb_per_group.png"), width = 15, height = 8)
}
#########################################
## using z transformed ratios per project
{
# Generate Fragmentation and Coverage plots
g <- ggplot(fr_dat, aes(x = bin, y = ratio_z, group = sample))
g <- g + geom_line(linewidth = 0.5, alpha = 0.5, color = "gray40")
g <- g + geom_hline(yintercept = 0, linetype="dashed", color = "red", linewidth = 0.5)
g <- g + facet_grid(~arm, switch = "x",space = "free_x", scales = "free_x",
labeller = labeller(arm = arm.labels))
g <- g + labs(x = cohort, y = "Short/Long Fragment Ratio Z score\n", color="")
g <- g + mytheme
ggsave(paste0(cohort, "_fragment_ratio_Z_score_in_5Mb.png"), width = 16, height = 4)
## subgroups per each individual projects
g <- ggplot(fr_dat, aes(x = bin, y = ratio_z, group = sample))
g <- g + geom_line(linewidth = 0.5, alpha = 0.5, color = "gray40")
g <- g + geom_hline(yintercept = 0, linetype="dashed", color = "red", linewidth = 0.5)
g <- g + facet_grid(group ~ arm, switch = "x",space = "free_x", scales = "free_x",
labeller = labeller(arm = arm.labels))
g <- g + labs(x = cohort, y = "Short/Long Fragment Ratio Z score\n", color="")
g <- g + mytheme
ggsave(paste0(cohort, "_fragment_ratio_Z_score_in_5Mb_per_group.png"), width = 15, height = 8)
}
}
saveRDS(fr_aggr, "Aggreated_fr_after_scaling_per_project.RDS")
saveRDS(fr_combo, "Aggreated_fr_without_scaling.RDS")
#########################################
## projects aggregated and z transformed
########################################
{
###########################################
## aggregated projects with original ratios
{
# Generate Fragmentation and Coverage plots
g <- ggplot(fr_aggr, aes(x = bin, y = ratio, group = sample))
g <- g + geom_line(linewidth = 0.5, alpha = 0.5, color = "gray40")
## add mean line per project
g <- g + geom_line(aes(x = bin, y = ratio_mean_project),
size=0.75, alpha=0.5, color="black")
## add mean line baed the HBC samples
g <- g + geom_line(aes(x = bin, y = ratio_mean_healthy),
size=0.75, alpha=0.5, color="green")
g <- g + facet_grid(project ~ arm, switch = "x",space = "free_x", scales = "free_x",
labeller = labeller(arm = arm.labels))
g <- g + labs(x = "All datasets", y = "Short/Long Fragment Ratio\n", color="")
g <- g + mytheme
ggsave("Aggregated_fragment_ratio_in_5Mb.png", width = 16, height = 20)
## healthy samples only
fr_aggr_h <- filter(fr_aggr, group == "BRCA_control" |
group == "Normal" |
group == "SCLC_Normal" |
group == "HBC")
# Generate Fragmentation and Coverage plots
g <- ggplot(fr_aggr_h, aes(x = bin, y = ratio, group = sample))
g <- g + geom_line(linewidth = 0.5, alpha = 0.5, color = "gray40")
## add mean line per project
g <- g + geom_line(aes(x = bin, y = ratio_mean_project),
size=0.75, alpha=0.5, color="black")
## add mean line baed the HBC samples
g <- g + geom_line(aes(x = bin, y = ratio_mean_healthy),
size=0.75, alpha=0.5, color="green")
g <- g + facet_grid(project ~ arm, switch = "x",space = "free_x", scales = "free_x",
labeller = labeller(arm = arm.labels))
g <- g + labs(x = "All Healthy samples", y = "Short/Long Fragment Ratio\n", color="")
g <- g + mytheme
ggsave("Aggregated_fragment_ratio_in_5Mb_Healthy.png", width = 16, height = 10)
}
#################################################
## aggregated projects ratio_z_genome per project
{
fr_aggr_p <- fr_aggr %>%
select(sample, project, group, ratio_z_genome) %>%
distinct(sample, .keep_all = TRUE)
## per cancer type
g <- ggboxplot(fr_aggr_p, x = "group", y = "ratio_z_genome", fill = "project")
g <- g + geom_hline(yintercept = 0, linetype="dashed", color = "red", linewidth = 0.5)
g <- g + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
ggsave(paste0("Aggreated_fragment_ratio_Z_Scores_per_project_group.png"), width = 16, height = 6)
}
############################################################
## using the combo fr matrix and do the Z transform globally
{
## merge fr matirx
fr_combo_m <- data.matrix(fr_combo[, -(1:5)])
fr_combo_m_z = t(scale(t(fr_combo_m), center = TRUE, scale = TRUE)) ## z-score transform
ratio_z_score = colSums(fr_combo_m_z, na.rm = TRUE)
idx_ss <- match(colnames(fr_combo_m_z), s_info$sequencing_id)
s_info_ss <- s_info[idx_ss, ]
fr_combo_z_socre <- cbind(s_info_ss, ratio_z_score)
## genome-wide score boxplot
g <- ggboxplot(fr_combo_z_socre, x = "cancer_type", y = "ratio_z_score", fill = "project_id")
g <- g + geom_hline(yintercept = 0, linetype="dashed", color = "red", linewidth = 0.5)
g <- g + labs(y = "Sum of Fragment Ratio Z socres")
g <- g + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
ggsave(paste0("Aggreated_fragment_ratio_Z_Scores_all_projects_cancer_type.png"), width = 16, height = 6)
g <- ggboxplot(fr_combo_z_socre, x = "group", y = "ratio_z_score", fill = "project_id")
g <- g + geom_hline(yintercept = 0, linetype="dashed", color = "red", linewidth = 0.5)
g <- g + labs(y = "Sum of Fragment Ratio Z socres")
g <- g + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
ggsave(paste0("Aggreated_fragment_ratio_Z_Scores_all_projects_group.png"), width = 16, height = 6)
## sex vs cancer_type
g <- ggboxplot(fr_combo_z_socre, x = "sex", y = "ratio_z_score", fill = "cancer_type")
g <- g + geom_hline(yintercept = 0, linetype="dashed", color = "red", linewidth = 0.5)
g <- g + facet_wrap(~ project_id, nrow = 2)
g <- g + labs(y = "Sum of Fragment Ratio Z socres")
g <- g + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
ggsave(paste0("Aggreated_fragment_ratio_Z_Scores_all_projects_sex_cancer_type.png"), width = 16, height = 10)
## association with ages
g <- ggscatter(fr_combo_z_socre, y = "ratio_z_score", x = "age", color = "project_id",
add = "reg.line")
g <- g + facet_wrap(. ~ project_id, nrow =2) + theme(legend.position = "none")
g <- g + stat_cor(label.y = 4000)
g <- g + stat_regline_equation(label.y = 3600)
g <- g + labs(y = "Sum of Fragment Ratio Z socres")
ggsave(paste0("Aggreated_fragment_ratio_Z_Scores_all_projects_vs_age.png"), width = 16, height = 8)
}
}