forked from jtlovell/GENESPACE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_2genomes.R
462 lines (417 loc) · 17.8 KB
/
plot_2genomes.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
#' @title Comparative genome structure plots
#' @description
#' \code{plot_2genomes} Single function to find synteny between two genomes
#' and plot the density of genes and repeats along with syntenic links
#'
#' @param genomeIDs character vector specifying the genome IDs
#' @param faFiles character vector coercible to file paths specifying the
#' locations of the fasta assembly files
#' @param wd character string coercible to file path where the results should
#' be stored
#' @param geneGffFiles character vector coercible to file paths specifying the
#' locations of the gene gff3 annotation files
#' @param repeatGffFiles character vector coercible to file paths specifying the
#' locations of the repeatmasker (or similar) gff3 annotation files
#' @param minChrSize integer specifying the minimum chromosome size to use
#' @param verbose logical specifying whether updates should be printed to the
#' console
#' @param kmers character specifying kmers that should be classified, defualt is
#' to not plot kmer density
#' @param nCores integer specifying the number of parallel processes to run
#' @param kmerMisMatch integer specifying the number of mismatches allowed when
#' search for kmer matches
#' @param slidingwindowSize integer specifying the sliding window size
#' @param slidingwindowStep integer specifying the step between windows
#' @param plotGapSize numeric (0-1) specifying the size of gaps between the
#' largest genomes chromosomes as a fraction of the total genome size
#' @param repeatClassColumnName character specifying which column should be used
#' to grep for repeat classes
#' @param repeatGrep1 character specifying the string to grep for the first
#' repeat density
#' @param repeatGrep2 character specifying the string to grep for the second
#' repeat density
#' @param cdsGrep character specifying the string to grep for CDS in the gene
#' gff3 annotation file
#' @param transcriptGrep character specifying the string to grep for transcript
#' in the gene gff3 annotation file
#' @param plotCols character vector specifying the plot colors
#' @param plotTheme ggplot2 theme to add to the plot
#' @param overwrite logical specifying whether results should be overwritted
#' @param pdfFile character string coercible to a file path where the plot
#' should be written
#' @param returnSourceData logical, should source data be returned
#' @param forceCleanWindows logical, should clean windows output be overwritten?
#' @param ... additional arguments passed to clean_windows
#'
#' @details Coming soon
#'
#' @return A plot, written to file.
#'
#' @examples
#' \dontrun{
#' # coming soon
#' }
#'
#' @import ggplot2
#' @import data.table
#' @importFrom Biostrings readDNAStringSet
#' @export
plot_2genomes <- function(genomeIDs,
faFiles,
wd,
geneGffFiles,
repeatGffFiles,
minChrSize = 1e6,
verbose = TRUE,
kmers = NULL,
nCores = 1,
kmerMisMatch = 0,
slidingwindowSize = 1e6,
slidingwindowStep = 1e5,
plotGapSize = .1,
repeatClassColumnName = "class",
repeatGrep1 = "Gypsy",
repeatGrep2 = "Copia",
cdsGrep = "CDS",
transcriptGrep = "mRNA",
plotCols = NULL,
plotTheme = NULL,
overwrite = FALSE,
pdfFile = NULL,
returnSourceData = FALSE,
forceCleanWindows = FALSE,
...){
if(!requireNamespace("GenomicRanges", quietly = TRUE))
stop("to slide genome, install GenomicRanges from bioconductor\n")
if(!requireNamespace("BiocGenerics", quietly = TRUE))
stop("to slide genome, install BiocGenerics from bioconductor\n")
if(!requireNamespace("rtracklayer", quietly = TRUE))
stop("to slide genome, install rtracklayer from bioconductor\n")
if(!requireNamespace("gridExtra", quietly = TRUE))
stop("to slide genome, install gridExtra from CRAN\n")
requireNamespace("gridExtra", quietly = TRUE)
type <- genome <- isLarger <- start <- len <- leftGap <- end <- y <-
genome1 <- genome2 <- start1 <- chr1 <- start2 <- chr2 <- end1 <- end2 <-
x <- chr <- propWind <- id <- index <- NULL
if(is.null(plotCols))
plotCols <- c(
"#CC6828", "#F4A460", "#FFFFFF", "#0F4F8B",
"#4C86C6", "#AED8E6", "#CC2027")
if(is.null(plotTheme))
plotTheme <- theme(
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
panel.spacing = unit(0, "cm"),
axis.title.y = element_blank(),
plot.title = element_blank())
if(!is.null(pdfFile)){
if(!dir.exists(dirname(pdfFile)))
stop("path to pdf file is not valid")
}
##############################################################################
# -- 1. get synteny map
blkFile <- file.path(wd, "output", sprintf(
"%s_vs_%s.blockCoords.txt", genomeIDs[1], genomeIDs[2]))
if(verbose)
cat("Building synteny map ...\n")
if(file.exists(blkFile) && !overwrite && !forceCleanWindows){
if(verbose)
cat("\tblocks file exists and !overwrite, so not re-running clean_windows\n")
}else{
winds <- clean_windows(
faFiles = faFiles,
wd = wd,
nCores = nCores,
genomeIDs = genomeIDs,
verbose = FALSE,
overwrite = forceCleanWindows,
...)
if(verbose)
cat("\tDone!\n")
}
if(verbose)
cat("Reading in assembly fasta files ... ")
dnass1 <- readDNAStringSet(faFiles[1])
dnass2 <- readDNAStringSet(faFiles[2])
seqInfo1 <- pull_seqInfo(dnass1)
seqInfo2 <- pull_seqInfo(dnass2)
##############################################################################
# -- 2. if kmers are specified, find the positions of these
if(!is.null(kmers)){
if(verbose)
cat("Done!\nFinding kmers ... ")
if(kmerMisMatch > 0 || length(kmers) < 100){
kmers1 <- find_fewKmers(
dnass = dnass1,
kmers = kmers,
nCores = nCores,
max.mismatch = kmerMisMatch)
if(!is.null(kmers1)){
kmers1 <- with(as.data.frame(GenomicRanges::reduce(kmers1)), data.table(
chr = seqnames, start = start, end = end))
}else{
kmers1 <- data.table(chr = names(dnass1), start = 1, end = 2)
}
kmers2 <- find_fewKmers(
dnass = dnass2,
kmers = kmers,
nCores = nCores,
max.mismatch = kmerMisMatch)
if(!is.null(kmers2)){
kmers2 <- with(as.data.frame(GenomicRanges::reduce(kmers2)), data.table(
chr = seqnames, start = start, end = end))
}else{
kmers2 <- data.table(chr = names(dnass2), start = 1, end = 2)
}
}else{
kmers1 <- find_manyKmers(
dnass = dnass1,
kmers = kmers,
nCores = nCores)
if(!is.null(kmers1)){
kmers1 <- with(as.data.frame(GenomicRanges::reduce(kmers1)), data.table(
chr = seqnames, start = start, end = end))
}else{
kmers1 <- data.table(chr = names(dnass1), start = 1, end = 2)
}
kmers2 <- find_manyKmers(
dnass = dnass2,
kmers = kmers,
nCores = nCores)
if(!is.null(kmers2)){
kmers2 <- with(as.data.frame(GenomicRanges::reduce(kmers2)), data.table(
chr = seqnames, start = start, end = end))
}else{
kmers2 <- data.table(chr = names(dnass2), start = 1, end = 2)
}
}
}else{
kmers1 <- data.table(chr = names(dnass1), start = 1, end = 2)
kmers2 <- data.table(chr = names(dnass2), start = 1, end = 2)
}
if(verbose)
cat("\tDone!\nClassifying the genomes ... ")
##############################################################################
# -- 3. Classify the genome
genes1 <- rtracklayer::readGFF(geneGffFiles[1])
genes2 <- rtracklayer::readGFF(geneGffFiles[2])
repeats1 <- rtracklayer::readGFF(repeatGffFiles[1])
repeats2 <- rtracklayer::readGFF(repeatGffFiles[2])
nogrp <- sprintf("%s|%s",repeatGrep1, repeatGrep2)
rep11 <- as.data.frame(subset(repeats1, grepl(repeatGrep1, repeats1[[repeatClassColumnName]])))
rep12 <- as.data.frame(subset(repeats1, grepl(repeatGrep2, repeats1[[repeatClassColumnName]])))
rep13 <- as.data.frame(subset(repeats1, !grepl(nogrp, repeats1[[repeatClassColumnName]])))
cds1 <- as.data.frame(subset(genes1, type == cdsGrep))
gene1 <- as.data.frame(subset(genes1, type == transcriptGrep))
rep21 <- subset(repeats2, grepl(repeatGrep1, repeats2[[repeatClassColumnName]]))
rep22 <- subset(repeats2, grepl(repeatGrep2, repeats2[[repeatClassColumnName]]))
rep23 <- subset(repeats2, !grepl(nogrp, repeats2[[repeatClassColumnName]]))
cds2 <- as.data.frame(subset(genes2, type == cdsGrep))
gene2 <- as.data.frame(subset(genes2, type == transcriptGrep))
##############################################################################
# - hierarchical classification
##############################################################################
# 4. Sliding windows
if(is.null(kmers)){
beds1 <- list(
cds = with(cds1, data.table(chr = seqid, start = start, end = end)),
rep1 = with(rep11, data.table(chr = seqid, start = start, end = end)),
rep2 = with(rep12, data.table(chr = seqid, start = start, end = end)),
otherRepeat = with(rep13, data.table(chr = seqid, start = start, end = end)),
introns = with(gene1, data.table(chr = seqid, start = start, end = end)))
beds2 <- list(
cds = with(cds2, data.table(chr = seqid, start = start, end = end)),
rep1 = with(rep21, data.table(chr = seqid, start = start, end = end)),
rep2 = with(rep22, data.table(chr = seqid, start = start, end = end)),
otherRepeat = with(rep23, data.table(chr = seqid, start = start, end = end)),
introns = with(gene2, data.table(chr = seqid, start = start, end = end)))
}else{
beds1 <- list(
kmers = kmers1,
cds = with(cds1, data.table(chr = seqid, start = start, end = end)),
rep1 = with(rep11, data.table(chr = seqid, start = start, end = end)),
rep2 = with(rep12, data.table(chr = seqid, start = start, end = end)),
otherRepeat = with(rep13, data.table(chr = seqid, start = start, end = end)),
introns = with(gene1, data.table(chr = seqid, start = start, end = end)))
beds2 <- list(
kmers = kmers2,
cds = with(cds2, data.table(chr = seqid, start = start, end = end)),
rep1 = with(rep21, data.table(chr = seqid, start = start, end = end)),
rep2 = with(rep22, data.table(chr = seqid, start = start, end = end)),
otherRepeat = with(rep23, data.table(chr = seqid, start = start, end = end)),
introns = with(gene2, data.table(chr = seqid, start = start, end = end)))
}
classes1 <- classify_genome(
dnaSS = dnass1, listOfBeds = beds1, verbose = T)
classes2 <- classify_genome(
dnaSS = dnass2, listOfBeds = beds2, verbose = T)
if(is.null(kmers)){
sw1 <- slide_genome(
seqInfo = seqInfo1,
listOfGrs = classes1[c(1,5,6,2,3,4)],
windowSize = slidingwindowSize,
stepSize = slidingwindowStep)
sw2 <- slide_genome(
seqInfo = seqInfo2,
listOfGrs = classes2[c(1,5,6,2,3,4)],
windowSize = slidingwindowSize,
stepSize = slidingwindowStep)
}else{
sw1 <- slide_genome(
seqInfo = seqInfo1,
listOfGrs = classes1[c(2,6,7,3,4,5,1)],
windowSize = slidingwindowSize,
stepSize = slidingwindowStep)
sw2 <- slide_genome(
seqInfo = seqInfo2,
listOfGrs = classes2[c(2,6,7,3,4,5,1)],
windowSize = slidingwindowSize,
stepSize = slidingwindowStep)
}
sw1[,genome := genomeIDs[1]]
sw2[,genome := genomeIDs[2]]
swtp <- rbind(sw1, sw2)
##############################################################################
# 5. get linear coordinates in order
dnass1 <- dnass1[width(dnass1) >= minChrSize]
dnass2 <- dnass2[width(dnass2) >= minChrSize]
chrs1 <- names(dnass1)
chrs2 <- names(dnass2)
clens1 <- width(dnass1); names(clens1) <- chrs1
clens2 <- width(dnass2); names(clens2) <- chrs2
blks <- fread(blkFile)
len1 <- sum(clens1)
len2 <- sum(clens2)
plotGapSize <- (max(c(len1, len2)) * plotGapSize) / (length(c(chrs1, chrs1)) / 2)
size1 <- ((length(clens1) - 1) * plotGapSize) + len1
size2 <- ((length(clens2) - 1) * plotGapSize) + len2
if(size1 > size2){
gapSize1 <- plotGapSize
gapSize2 <- (size1 - len2) / (length(clens2) + 1)
smaller <- "genome2"
gpSize <- gapSize2
mpSize <- gapSize1
}else{
gapSize2 <- plotGapSize
gapSize1 <- (size2 - len1) / (length(clens1) + 1)
smaller <- "genome1"
gpSize <- gapSize1
mpSize <- gapSize2
}
pmd <- rbind(
data.table(genome = genomeIDs[1], chr = chrs1, len = clens1,
leftGap = gapSize1),
data.table(genome = genomeIDs[2], chr = chrs2, len = clens2,
leftGap = gapSize2))
pmd[,isLarger := ifelse(size1 > size2, genomeIDs[1], genomeIDs[2]) == genome]
pmd[,start := c(0, (cumsum(len + leftGap))[-.N]), by = "genome"]
pmd$start[!pmd$isLarger] <- pmd$start[!pmd$isLarger] + gpSize
pmd[,end := start + len]
pmd[,`:=`(y = match(genome, genomeIDs))]
pmd[,`:=`(y1 = y + 0.04, y2 = y - 0.04)]
lin1 <- pmd$start[pmd$genome == genomeIDs[1]]
lin2 <- pmd$start[pmd$genome == genomeIDs[2]]
names(lin1) <- pmd$chr[pmd$genome == genomeIDs[1]]
names(lin2) <- pmd$chr[pmd$genome == genomeIDs[2]]
lin <- pmd$start; names(lin) <- with(pmd, paste(genome, chr))
################################################################################
# -- Parse the synteny map
dat <- blks[,c("chr1", "chr2", "start1", "start2", "end1", "end2", "orient")]
dat[,genome1 := genomeIDs[1]]
dat[,genome2 := genomeIDs[2]]
dat[,`:=`(start1 = start1 + lin[paste(genome1, chr1)],
start2 = start2 + lin[paste(genome2, chr2)],
end1 = end1 + lin[paste(genome1, chr1)],
end2 = end2 + lin[paste(genome2, chr2)])]
dat <- subset(dat, complete.cases(dat))
braidPolygons <- rbindlist(lapply(1:nrow(dat), function(i){
x <- with(dat[i, ], calc_curvePolygon(
start1 = start1, end1 = end1, start2 = start2,
end2 = end2, y1 = 1.05, y2 = 1.95))
x[,`:=`(chr = dat$chr1[i], index = i, type = dat$orient[i])]
return(x)
}))
chrPolygons <- rbindlist(lapply(1:nrow(pmd), function(i){
z <- pmd[i,]
out <- data.table(z[,c("genome","chr")], with(z, round_rect(
xleft = start, xright = end, ybottom = y2,
ytop = y1, yrange = range(c(pmd$y1, pmd$y2)),
xrange = range(c(pmd$start, pmd$end)),
plotWidth = 18,
plotHeight = 1)))
return(out)
}))
swtp[,x := (start + end - 1)/2 + lin[paste(genome, chr)]]
swtp <- subset(swtp, complete.cases(swtp))
# 6. get data parse to make the plot
dictext <- paste(c(cdsGrep, transcriptGrep, "unannotated", repeatGrep1, repeatGrep2, "otherRepeat"),
collapse = ", ")
if(!is.null(kmers))
dictext <- sprintf("%s, kmers", dictext)
xlab1 <- sprintf(
"%s chromosomes (%s total Mb, %sMb-overlapping %sMb windows)\nColors (top to bottom): %s",
genomeIDs[1],
round(sum(clens1)/1e6, 1),
round((slidingwindowSize - slidingwindowStep)/1e6, 2),
round(slidingwindowSize/1e6, 2),
dictext)
xlab2 <- sprintf(
"%s chromosomes (%s total Mb, %sMb-overlapping %sMb windows)\nColors (top to bottom): %s",
genomeIDs[2],
round(sum(clens2)/1e6, 1),
round((slidingwindowSize - slidingwindowStep)/1e6, 2),
round(slidingwindowSize/1e6, 2),
dictext)
swtp[,genome := factor(genome, levels = rev(genomeIDs))]
p1 <- ggplot(swtp, aes(x = x, y = propWind, fill = id))+
scale_fill_manual(values = plotCols, guide = "none") +
scale_x_continuous(expand = c(0,0), name = xlab1, position = "top",
limits = c(0, max(pmd$end) + mpSize))+
scale_y_continuous(expand = c(0,0))+
plotTheme +
theme(plot.margin = unit(c(.1,0,0,.2), "lines"),
axis.title.x = element_text(family = "Helvetica", size = 6, vjust = -3))
for(i in chrs1){
p1 <- p1 + geom_area(data = subset(swtp, chr == i & genome == genomeIDs[1]))
}
p2 <- ggplot(swtp, aes(x = x, y = propWind, fill = id))+
scale_fill_manual(values = plotCols, guide = "none") +
scale_x_continuous(expand = c(0,0), name = xlab2, limits = c(0, max(pmd$end) + mpSize))+
scale_y_continuous(expand = c(0,0))+
plotTheme+
theme(plot.margin = unit(c(-.1,0,.1,.2), "lines"),
axis.title.x = element_text(family = "Helvetica", size = 6, vjust = 3))
for(i in chrs2){
p2 <- p2 + geom_area(data = subset(swtp, chr == i & genome == genomeIDs[2]))
}
p3 <- ggplot()+
geom_polygon(
data = braidPolygons,
aes(x = x, y = y, group = index, fill = type),
alpha = 1, lwd = .01, col = NA)+
geom_polygon(
data = chrPolygons,
aes(x = x, y = y, group = genome),
alpha = 1, lwd = .01, fill = "black", col = "white")+
geom_text(
data = pmd,
aes(x = (start + end)/2, y = y, label = gsub("Gm0|Gm","",chr)),
alpha = 1, col = "white", size = 2)+
scale_x_continuous(expand = c(0,0), limits = c(0, max(pmd$end) + mpSize))+
scale_y_reverse(expand = c(0,0))+
scale_fill_manual(values = c("darkred", "grey"), guide = "none")+
plotTheme+
theme(axis.title.x = element_blank(),
plot.margin = unit(c(0,0,0,.2), "lines"))
if(is.null(pdfFile))
pdfFile <- file.path(wd, sprintf("%s_vs_%s_swPlot.pdf", genomeIDs[1], genomeIDs[2]))
pdf(pdfFile, height = 3.5, width = 9)
gridExtra::grid.arrange(p1, p3, p2, nrow = 3)
dev.off()
if(returnSourceData)
return(list(braidPolygons = braidPolygons, chrPolygons = chrPolygons,
plotMetadata = pmd, slidingWindows = swtp))
}