-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransposons_plots.R
executable file
·550 lines (460 loc) · 21.3 KB
/
transposons_plots.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
require(ggplot2)
require(plyr)
require(VennDiagram)
require(gridExtra)
require(gdata)
require(scales)
require(splitstackshape)
# Help function to create a venn plot
venn_3_samples <- function(sample1 = sample1, sample2 = sample2, sample3 = sample3, name1 = name1, name2 = name2, name3 = name3, clone_name = clone_name, save_ids = "FALSE"){
sample1 <- unique(sample1[complete.cases(sample1)])
sample2 <- unique(sample2[complete.cases(sample2)])
sample3 <- unique(sample3[complete.cases(sample3)])
inter_12_full <- unique(intersect(sample1, sample2))
inter_13_full <- unique(intersect(sample1, sample3))
inter_23_full <- unique(intersect(sample2, sample3))
inter_123_full <- unique(intersect(inter_12_full, sample3))
inter_12 <- unique(inter_12_full[!inter_12_full %in% inter_123_full])
inter_13 <- unique(inter_13_full[!inter_13_full %in% inter_123_full])
inter_23 <- unique(inter_23_full[!inter_23_full %in% inter_123_full])
unic_s1 <- unique(sample1[!sample1 %in% c(inter_12, inter_13, inter_123_full)])
unic_s2 <- unique(sample2[!sample2 %in% c(inter_12, inter_23, inter_123_full)])
unic_s3 <- unique(sample3[!sample3 %in% c(inter_13, inter_23, inter_123_full)])
# write files with the Ids of the sites in each subset of the venn plots
if(save_ids == "TRUE"){
write.table(inter_123_full,
paste(clone_name, name1, "vs", name2, "vs", name3, "intersection_transposons.txt", sep = "_"), row.names = F, col.names = F, quote = F)
write.table(inter_12,
paste(clone_name, "only", name1, "vs", name2, "intersection_transposons.txt", sep = "_"),
row.names = F, col.names = F, quote = F)
write.table(inter_13,
paste(clone_name, "only", name1, "vs", name3, "intersection_transposons.txt", sep = "_"),
row.names = F, col.names = F, quote = F)
write.table(inter_23,
paste(clone_name, "only", name2, "vs", name3, "intersection_transposons.txt", sep = "_"),
row.names = F, col.names = F, quote = F)
write.table(inter_12_full,
paste(clone_name, "all", name1, "vs", name2, "intersection_transposons.txt", sep = "_"),
row.names = F, col.names = F, quote = F)
write.table(inter_13_full,
paste(clone_name, "all", name1, "vs", name3, "intersection_transposons.txt", sep = "_"),
row.names = F, col.names = F, quote = F)
write.table(inter_23_full,
paste(clone_name, "all", name2, "vs", name3, "intersection_transposons.txt", sep = "_"),
row.names = F, col.names = F, quote = F)
write.table(unic_s1,
paste(clone_name, name1, "exclusive_transposons.txt", sep = "_"),
row.names = F, col.names = F, quote = F)
write.table(unic_s2,
paste(clone_name, name2, "exclusive_transposons.txt", sep = "_"),
row.names = F, col.names = F, quote = F)
write.table(unic_s3,
paste(clone_name, name3, "exclusive_transposons.txt", sep = "_"),
row.names = F, col.names = F, quote = F)
}else if(save_ids == "FALSE"){
}else{
print("Invalid save_ids option!")
}
grid.newpage();
venn.plot <- draw.triple.venn(
area1 = length(sample1),
area2 = length(sample2),
area3 = length(sample3),
n12 = length(inter_12_full),
n13 = length(inter_13_full),
n23 = length(inter_23_full),
n123 = length(inter_123_full),
cross.area = length(inter),
alpha = 0.5,
category = c(deparse(name1), deparse(name2), deparse(name3)),
fill = c("darkgreen", "green", "#8B4513"),
lty = "blank",
cex = 4,
cat.cex = 2.5,
cat.dist = 0.055,
ext.pos = 0,
ext.dist = -0.05,
ext.length = 0.85,
ext.line.lwd = 2,
ext.line.lty = "dashed",
scaled = T,
print.mode = c("raw", "percent"),
rotation.degree = 0)
}
transposons <- read.table(snakemake@input[[1]], sep = "\t")
transposon_in_genes <- read.table(snakemake@input[[2]], sep = "\t")
transposons <- transposons[, c(4, 10)]
transposon_in_genes <- transposon_in_genes[, c(4, 10)]
marks_in_transp <- c(unique(as.character(transposons$V4)),
unique(as.character(transposon_in_genes$V4)))
# Reads the MSD-Methylated sites
DM_marks <- read.table(snakemake@input[[3]],
header = T,
na.strings = "NA",
colClasses = "character")
# Defines the Methylated TEs of each sample
transposons_all <- data.frame()
for(i in 1:length(names(DM_marks))){
if(i == 1){
query <- DM_marks[DM_marks[, i] %in% marks_in_transp, i]
transposons_group <- transposons[transposons$V4 %in% query,][2]
colnames(transposons_group) <- paste(names(DM_marks)[i])
transposons_all <- cbind(transposons_group)
}else{
query <- DM_marks[DM_marks[,i] %in% marks_in_transp, i]
transposons_group <- transposons[transposons$V4 %in% query,][2]
colnames(transposons_group) <- paste(names(DM_marks)[i])
transposons_all <- cbindX(transposons_all, transposons_group)
}
}
# Writes a file with the TEs of each sample
write.table(transposons_all,
snakemake@output[[1]],
sep = "\t",
col.names = T,
row.names = F,
quote = F)
# Venn plots
A <- venn_3_samples(sample1 = transposons_all[,1],
sample2 = transposons_all[,2],
sample3 = transposons_all[,3],
name1 = colnames(transposons_all)[1],
name2 = colnames(transposons_all)[2],
name3 = colnames(transposons_all)[3],
clone_name = "BRASUZ1",
save_ids = "TRUE")
dev.off()
## Export as a svg file
svg(filename = snakemake@output[[2]], width = 12, height = 12, pointsize = 12)
grid.arrange(grobTree(A), ncol = 1, top = textGrob("BRASUZ1", gp = gpar(fontsize = 30, font = 8)))
dev.off()
# Select the subset of TEs of the venn plots to annotation
sample1 = transposons_all[, 1]
sample2 = transposons_all[, 2]
sample3 = transposons_all[, 3]
sample1 <- unique(as.character(sample1[complete.cases(sample1)]))
sample2 <- unique(as.character(sample2[complete.cases(sample2)]))
sample3 <- unique(as.character(sample3[complete.cases(sample3)]))
inter_12_full <- unique(intersect(sample1, sample2))
inter_13_full <- unique(intersect(sample1, sample3))
inter_23_full <- unique(intersect(sample2, sample3))
inter_123_full <- unique(intersect(inter_12_full, sample3))
inter_12 <- unique(inter_12_full[!inter_12_full %in% inter_123_full])
inter_13 <- unique(inter_13_full[!inter_13_full %in% inter_123_full])
inter_23 <- unique(inter_23_full[!inter_23_full %in% inter_123_full])
unic_s1 <- unique(sample1[!sample1 %in% c(inter_12, inter_13, inter_123_full)])
unic_s2 <- unique(sample2[!sample2 %in% c(inter_12, inter_23, inter_123_full)])
unic_s3 <- unique(sample3[!sample3 %in% c(inter_13, inter_23, inter_123_full)])
venn_subset_transp <- cbindX(as.data.frame(inter_123_full),
as.data.frame(unic_s1),
as.data.frame(unic_s2),
as.data.frame(unic_s3))
transp_genome <- read.table(snakemake@input[[4]], sep = "\t")[,4]
transposons_all <- cbindX(as.data.frame(sample1),
as.data.frame(sample2),
as.data.frame(sample3),
venn_subset_transp)
transposons_all <- cbindX(transposons_all, as.data.frame(transp_genome))
colnames(transposons_all) <- c("Adult leaves",
"Juvenile leaves",
"Xylem",
"Intersection",
"Unique in Adult",
"Unique in Juvenile",
"Unique in Xylem",
"Genome")
# Verify the TEs that are not possible to classify correctly.
TE_not_class <- read.table(snakemake@input[[4]], sep = "\t")
TE_not_class <- cSplit(indt = TE_not_class, splitCols = "V4", sep = ",", drop = F)
TE_not_class <- TE_not_class[is.na(TE_not_class$V4_02) == FALSE, ]
TE_not_class <- as.character(TE_not_class$V4)
# Defines the groups to be used in the plot
use_intersections <- snakemake@params[["use_intersections"]]
if(use_intersections == TRUE){
transp_class_all <- data.frame()
for(i in 1:length(names(transposons_all))){
transp_subset <- unique(as.character(transposons_all[, i]))
# check TEs with overlaps
transp_unclass <- unique(as.character(transp_subset[transp_subset %in% TE_not_class]))
# check if in the regions with more than one TE, the TEs are of differently classified
transp_unclass_final <- character()
for( t in 1:length(transp_unclass)){
sum_of_class <- (length(grep("DMX", transp_unclass[t])) +
length(grep("DTX", transp_unclass[t])) +
length(grep("DHX", transp_unclass[t])) +
length(grep("DXX-MtTE", transp_unclass[t])) +
length(grep("DXX_Blc", transp_unclass[t])) +
length(grep("RYX", transp_unclass[t])) +
length(grep("RtX", transp_unclass[t])) +
length(grep("RLX", transp_unclass[t])) +
length(grep("RXX-LARD", transp_unclass[t])) +
length(grep("RXX-TRtM", transp_unclass[t])) +
length(grep("RSX", transp_unclass[t])) +
length(grep("RXX_Blc", transp_unclass[t])))
if(sum_of_class > 1){
transp_unclass_final <- c(transp_unclass_final, transp_unclass[t])
}
}
unknow_TE <- length(transp_unclass_final)
transp_subset <- transp_subset[!transp_subset %in% transp_unclass_final]
#MAVERICK | DMX
maverick <- length(grep("DMX", transp_subset))
#CACTA | DTX
cacta <- length(grep("DTX", transp_subset))
#HELITRON | DHX
helitron <- length(grep("DHX", transp_subset))
#MITE | DXX-MITE
mite <- length(grep("DXX-MITE", transp_subset))
#DNA_general | DXX_Blc
dna_general <- length(grep("DXX_Blc", transp_subset))
#DIRS/VIPER | RYX
dirs_viper <- length(grep("RYX", transp_subset))
#LINE | RIX
line <- length(grep("RIX", transp_subset))
#LTR | RLX
ltr <- length(grep("RLX", transp_subset))
#LARD | RXX-LARD
lard <- length(grep("RXX-LARD", transp_subset))
#TRIM | RXX-TRIM
trim <- length(grep("RXX-TRIM", transp_subset))
#SINE | RSX
sine <- length(grep("RSX", transp_subset))
#general | RXX_Blc
rna_general <- length(grep("RXX_Blc", transp_subset))
category <- c("MAVERICK",
"CACTA",
"HELITRON",
"MITE",
"DNA_general",
"DIRS/VIPER",
"LINE",
"LTR",
"LARD",
"TRIM",
"SINE",
"RNA_general",
"Nested TEs")
len <- c(maverick,
cacta,
helitron,
mite,
dna_general,
dirs_viper,
line,ltr,
lard,
trim,
sine,
rna_general,
unknow_TE)
transp_class <- data.frame(rep(names(transposons_all)[i], length(category)), category, len)
transp_class_all <- rbind(transp_class_all ,transp_class)
}
colnames(transp_class_all) <- c("samples", "classif", "quantif")
transp_class_all <- transp_class_all[!transp_class_all$quantif == "0",]
group_plot <-character()
for(i in 1:nrow(transp_class_all)){
if(paste(transp_class_all$samples[i]) == "Genome"){
group_plot <- append(group_plot,"E. grandis")
}else{
group_plot <- append(group_plot,"Samples")
}
}
transp_class_all <- cbind(group_plot, transp_class_all)
write.table(transp_class_all, snakemake@output[[3]], quote = F, row.names = F, col.names = F, sep = "\t")
## Defines the colors of the bars.
fill <- c("#000000",
"#E69F00",
"#56B4E9",
"#009E73",
"#F0E442",
"#0072B2",
"#D55E00",
"#CC79A7",
"#2C7417",
"#CBE75F",
"#A877EA",
"#6BF4ED",
"#AC6AC9")
transp_plot <- ggplot(transp_class_all, aes(x = samples, y = quantif, fill = classif)) +
geom_bar(stat = "identity", alpha = 0.9, width = 0.5) +
scale_x_discrete(name="")+
scale_y_continuous(breaks = scales::pretty_breaks(n = 10))+
facet_wrap( ~ group_plot, ncol=2, scales="free")+
ylab("Number of transposons")+
theme_bw()+
theme(legend.text=element_text(size=14), axis.title.y=element_text(size = 20, vjust=2), axis.title.x=element_text(size=14,vjust=0), axis.text.x = element_text(size = 15), axis.text.y = element_text(size = 15), legend.title = element_blank(), strip.text.x = element_text(size = 15))+
scale_fill_manual(values=fill)
transp_plot_perc <- ggplot(transp_class_all, aes(x = samples, y = quantif, fill = classif)) +
geom_bar(position = "fill", stat = "identity", alpha = 0.9, width = 0.5) +
scale_x_discrete(name="")+
scale_y_continuous(labels = percent_format())+
facet_wrap( ~ group_plot, ncol=2, scales="free")+
ylab("Number of transposons")+
theme_bw()+
theme(legend.text=element_text(size=14), axis.title.y=element_text(size = 20, vjust=2), axis.title.x=element_text(size=14,vjust=0), axis.text.x = element_text(size = 15), axis.text.y = element_text(size = 15), legend.title = element_blank(), strip.text.x = element_text(size = 15))+
scale_fill_manual(values=fill)
# Get the ggplot grob
transp_plot_grob = ggplotGrob(transp_plot)
transp_plot_perc_grob = ggplotGrob(transp_plot_perc)
# Builds and save the bar plot.
svg(snakemake@output[[4]], width=10, height=7)
grid.newpage()
grid.draw(transp_plot_grob)
dev.off()
# Builds and save the bar plot.
svg(snakemake@output[[5]], width=10, height=7)
grid.newpage()
grid.draw(transp_plot_perc_grob)
dev.off()
}else if(use_intersections == FALSE){
transposons_all <- transposons_all[, colnames(transposons_all) %in% c("Adult leaves", "Juvenile leaves", "Xylem", "Genome") ]
transp_class_all <- data.frame()
for(i in 1:length(names(transposons_all))){
transp_subset <- unique(as.character(transposons_all[, i]))
# check TEs with overlaps
transp_unclass <- unique(as.character(transp_subset[transp_subset %in% TE_not_class]))
transp_unclass_final <- character()
for( t in 1:length(transp_unclass)){
sum_of_class <- (length(grep("DMX", transp_unclass[t])) +
length(grep("DTX", transp_unclass[t])) +
length(grep("DHX", transp_unclass[t])) +
length(grep("DXX-MtTE", transp_unclass[t])) +
length(grep("DXX_Blc", transp_unclass[t])) +
length(grep("RYX", transp_unclass[t])) +
length(grep("RtX", transp_unclass[t])) +
length(grep("RLX", transp_unclass[t])) +
length(grep("RXX-LARD", transp_unclass[t])) +
length(grep("RXX-TRtM", transp_unclass[t])) +
length(grep("RSX", transp_unclass[t])) +
length(grep("RXX_Blc", transp_unclass[t])))
if(sum_of_class > 1){
transp_unclass_final <- c(transp_unclass_final, transp_unclass[t])
}
}
unknow_TE <- length(transp_unclass_final)
transp_subset <- transp_subset[!transp_subset %in% transp_unclass_final]
#MAVERICK | DMX
maverick <- length(grep("DMX", transp_subset))
#CACTA | DTX
cacta <- length(grep("DTX", transp_subset))
#HELITRON | DHX
helitron <- length(grep("DHX", transp_subset))
#MITE | DXX-MITE
mite <- length(grep("DXX-MITE", transp_subset))
#DNA_general | DXX_Blc
dna_general <- length(grep("DXX_Blc", transp_subset))
#DIRS/VIPER | RYX
dirs_viper <- length(grep("RYX", transp_subset))
#LINE | RIX
line <- length(grep("RIX", transp_subset))
#LTR | RLX
ltr <- length(grep("RLX", transp_subset))
#LARD | RXX-LARD
lard <- length(grep("RXX-LARD", transp_subset))
#TRIM | RXX-TRIM
trim <- length(grep("RXX-TRIM", transp_subset))
#SINE | RSX
sine <- length(grep("RSX", transp_subset))
#general | RXX_Blc
rna_general <- length(grep("RXX_Blc", transp_subset))
category <- c("MAVERICK",
"CACTA",
"HELITRON",
"MITE",
"DNA_general",
"DIRS/VIPER",
"LINE",
"LTR",
"LARD",
"TRIM",
"SINE",
"RNA_general",
"Nested TEs")
len <- c(maverick,
cacta,
helitron,
mite,
dna_general,
dirs_viper,
line,ltr,
lard,
trim,
sine,
rna_general,
unknow_TE)
transp_class <- data.frame(rep(names(transposons_all)[i], length(category)), category, len)
transp_class_all <- rbind(transp_class_all ,transp_class)
}
colnames(transp_class_all) <- c("samples", "classif", "quantif")
transp_class_all <- transp_class_all[!transp_class_all$quantif == "0",]
group_plot <-character()
for(i in 1:nrow(transp_class_all)){
if(paste(transp_class_all$samples[i]) == "Genome"){
group_plot <- append(group_plot, "E. grandis")
}else{
group_plot <- append(group_plot, "Samples")
}
}
transp_class_all <- cbind(group_plot, transp_class_all)
write.table(transp_class_all, snakemake@output[[3]], quote = F, row.names = F, col.names = F, sep = "\t")
## Defines the colors of the bars.
fill <- c("#000000",
"#E69F00",
"#56B4E9",
"#009E73",
"#F0E442",
"#0072B2",
"#D55E00",
"#CC79A7",
"#2C7417",
"#CBE75F",
"#A877EA",
"#6BF4ED",
"#AC6AC9")
# labels of the grids
levels(transp_class_all$group_plot) <- c("E. grandis" = "E. grandis - all TEs", "Samples" = "Samples - Methylated TEs")
transp_plot <- ggplot(transp_class_all, aes(x = samples, y = quantif, fill = classif)) +
geom_bar(stat = "identity", alpha = 0.9, width = 0.4) +
scale_x_discrete(name="")+
scale_y_continuous(breaks = scales::pretty_breaks(n = 10))+
facet_wrap( ~ group_plot, ncol = 2, scales = "free") +
ylab("Number of transposons")+
theme_bw()+
theme(legend.text=element_text(size=14),
axis.title.y = element_text(size = 20, vjust = 2),
axis.title.x=element_text(size = 14, vjust = 0),
axis.text.x = element_text(size = 15),
axis.text.y = element_text(size = 15),
legend.title = element_blank(),
strip.text.x = element_text(size = 15))+
scale_fill_manual(values = fill)
# Get the ggplot grob
transp_plot_grob = ggplotGrob(transp_plot)
# changes the dimension of the the second box
transp_plot_grob$widths[9] = 2*transp_plot_grob$widths[9]
# Builds and save the bar plot.
svg(snakemake@output[[4]], width=10, height=7)
# Draw the plot
grid.newpage()
grid.draw(transp_plot_grob)
dev.off()
transp_plot_perc <- ggplot(transp_class_all, aes(x = samples, y = quantif, fill = classif)) +
geom_bar(position = "fill", stat = "identity", alpha = 0.9, width = 0.5) +
scale_x_discrete(name="")+
scale_y_continuous(labels = percent_format())+
facet_wrap( ~ group_plot, ncol=2, scales="free")+
ylab("Relative frequency")+
theme_bw()+
theme(legend.text=element_text(size=14), axis.title.y=element_text(size = 20, vjust=2), axis.title.x=element_text(size=14,vjust=0), axis.text.x = element_text(size = 15), axis.text.y = element_text(size = 15), legend.title = element_blank(), strip.text.x = element_text(size = 15))+
scale_fill_manual(values=fill)
# Get the ggplot grob
transp_plot_perc_grob = ggplotGrob(transp_plot_perc)
# changes the dimension of the the second box
transp_plot_perc_grob$widths[9] = 2*transp_plot_perc_grob$widths[9]
# Builds and save the bar plot.
svg(snakemake@output[[5]], width=10, height=7)
# Draw the plot
grid.newpage()
grid.draw(transp_plot_perc_grob)
dev.off()
}